<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Apache的BCEL庫,文檔很少,例子也很簡單。動態(tài)構(gòu)建類的工作,要求的只是并不是熟練使用BCEL類庫本身,而是要對java的class結(jié)構(gòu)了解。我對java的pcode也不熟悉,但是我曾經(jīng)做過大量的.NET的反編譯工作,兩者類似,所以我用BCEL也不覺得困難。

    我提供一個例子,這里例子是使用BCEL創(chuàng)建類的實例,而不是使用反射。

    如下:

    IFactory.java

    public interface IFactory {
        
    public Object newInstance();    
    }

    FileClassLoader.java
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    public class FileClassLoader extends ClassLoader {
        
    public FileClassLoader() {
            super();
        }


        
    public FileClassLoader(ClassLoader parent) {
            super(parent);
        }


        
    public Class getClass(String className, String filePath) {
            FileInputStream fileInputStream 
    = null;
            
    byte[] data = null;
            
    try {
                fileInputStream 
    = new FileInputStream(new File(filePath));

                ByteArrayOutputStream byteArrayOutputStream 
    = new ByteArrayOutputStream();
                
    int ch = 0;
                
    while ((ch = fileInputStream.read()) != -1{
                    byteArrayOutputStream.write(ch);
                }

                data 
    = byteArrayOutputStream.toByteArray();
                
    return defineClass(className, data, 0, data.length);
            }
     catch (IOException e) {
                e.printStackTrace();
                
    throw new Error(e.getMessage(), e);
            }

        }

    }

    buildFactory方法:
    public static IFactory buildFactory(String procductClassName)
            throws Exception 
    {
        InstructionList il 
    = new InstructionList();
        String className 
    = "HelloWorld";
        ClassGen class_gen 
    = new ClassGen(className, "java.lang.Object",
                
    "", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
        ConstantPoolGen cons_pool 
    = class_gen.getConstantPool();
        class_gen.addInterface(IFactory.
    class.getName());

        InstructionFactory il_factory 
    = new InstructionFactory(class_gen);

        
    // 創(chuàng)建構(gòu)造函數(shù)
        {
            String methodName 
    = "";

            Type returnType 
    = Type.VOID;
            Type[] arg_Types 
    = new Type[] {};
            String[] arg_names 
    = new String[] {};
            MethodGen method_gen 
    = new MethodGen(Constants.ACC_PUBLIC,
                    returnType, arg_Types, arg_names, methodName, className,
                    il, cons_pool);

            
    // super();
            il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
            il.append(il_factory.createInvoke(
    "java.lang.Object""",
                    Type.VOID, 
    new Type[0], Constants.INVOKESPECIAL));
            il.append(InstructionFactory.createReturn(Type.VOID));

            method_gen.setMaxStack();
            class_gen.addMethod(method_gen.getMethod());
            il.dispose(); 
    // Reuse instruction handles of list
        }


        
    {
            String methodName 
    = "newInstance";
            Type returnType 
    = Type.OBJECT;
            Type[] arg_Types 
    = new Type[] {};
            String[] arg_names 
    = new String[] {};
            MethodGen method_gen 
    = new MethodGen(Constants.ACC_PUBLIC,
                    returnType, arg_Types, arg_names, methodName, className,
                    il, cons_pool);
            il.append(il_factory.createNew(procductClassName));
            il.append(InstructionConstants.DUP);
            il.append(il_factory.createInvoke(procductClassName, 
    "",
                    Type.VOID, 
    new Type[0], Constants.INVOKESPECIAL));
            il.append(InstructionFactory.createReturn(Type.OBJECT));
            method_gen.setMaxStack();

            class_gen.addMethod(method_gen.getMethod());
            il.dispose(); 
    // Reuse instruction handles of list
        }


        
    // 保存到文件中
        JavaClass clazz = class_gen.getJavaClass();
        String path 
    = "e:\\temp\\" + className + ".class";
        class_gen.getJavaClass().dump(path);

        
    // 使用ClassLoader裝載class
        FileClassLoader classLoader = new FileClassLoader();
        Class factoryClass 
    = classLoader.getClass(className, path);
        Object newInst 
    = factoryClass.newInstance();

        
    return (IFactory) newInst;
    }

    測試用例:
    String className = "java.lang.Object";
    IFactory factory 
    = buildFactory(className);
    Object inst 
    = factory.newInstance();
    posted on 2005-04-14 23:59 溫少的日志 閱讀(8647) 評論(7)  編輯  收藏
    Comments

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
     
    主站蜘蛛池模板: 精品亚洲成a人在线观看| 最近免费中文字幕mv在线电影| 亚洲美女中文字幕| 免费在线黄色网址| 国产免费久久精品99re丫y| 成年免费a级毛片免费看无码| 亚洲乱色伦图片区小说| 亚洲理论片在线中文字幕| 国产成人无码综合亚洲日韩| 全亚洲最新黄色特级网站 | 亚洲AV成人一区二区三区AV| 亚洲成AⅤ人影院在线观看| 午夜私人影院免费体验区| 1000部拍拍拍18勿入免费凤凰福利| 任你躁在线精品免费| 成人精品综合免费视频| 亚洲AV无码片一区二区三区| 97se亚洲国产综合自在线| 亚洲精品中文字幕麻豆| 久久丫精品国产亚洲av| 亚洲AV无码一区二区乱子伦 | 妇女自拍偷自拍亚洲精品| 亚洲永久网址在线观看| 亚洲精品亚洲人成在线播放| 亚洲国产精品无码久久久| 亚洲美免无码中文字幕在线| 亚洲福利在线观看| 久久久亚洲精品国产| 久久久久亚洲av无码尤物| 亚洲av无码不卡| 水蜜桃亚洲一二三四在线| 五月天网站亚洲小说| 亚洲综合激情视频| 亚洲婷婷在线视频| 亚洲粉嫩美白在线| 亚洲av无码一区二区三区在线播放 | 亚洲免费网站在线观看| 亚洲乱码在线播放| 亚洲AV日韩综合一区尤物| 亚洲另类自拍丝袜第五页| 国产偷国产偷亚洲高清在线|