Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


                ("Internal error - non-null array required");
        }
        m_interfaceNames = impls;
        m_file = file;
        m_isWritable = true;
        m_genClass = new ClassGen(name, sclas.getName(), "",
            access | SYNTHETIC_ACCESS_FLAG, impls);
        m_genPool = m_genClass.getConstantPool();
        int index = m_genPool.addUtf8("Synthetic");
        m_genClass.addAttribute(new Synthetic(index, 0, EMPTY_BYTES,
            m_genPool.getConstantPool()));
View Full Code Here


     *
     * @param intf fully qualified interface name
     * @return <code>true</code> if added, <code>false</code> if already present
     */
    public boolean addInterface(String intf) {
        ClassGen gen = getClassGen();
        String[] intfs = gen.getInterfaceNames();
        for (int i = 0; i < intfs.length; i++) {
            if (intf.equals(intfs[i])) {
                return false;
            }
        }
        gen.addInterface(intf);
        m_isModified = true;
        m_instanceOfs = null;
        return true;
    }
View Full Code Here

     * @return generator for class
     */
    private ClassGen getClassGen() {
        if (m_genClass == null) {
            if (m_isWritable) {
                m_genClass = new ClassGen(m_curClass);
                m_genPool = m_genClass.getConstantPool();
                m_instBuilder = new InstructionBuilder(m_genClass, m_genPool);
                m_isHashCurrent = false;
            } else {
                throw new IllegalStateException("Internal error - cannot modify class " + getName() + " loaded from " + m_path);
View Full Code Here

     *
     * @param name field name
     * @return <code>true</code> if field was present, <code>false</code> if not
     */
    public boolean deleteField(String name) {
        ClassGen cg = getClassGen();
        Field field = cg.containsField(name);
        if (field == null) {
            return false;
        } else {
            cg.removeField(field);
            m_isModified = true;
            m_isHashCurrent = false;
            return true;
        }
    }
View Full Code Here

     * @param name method name
     * @param sig method signature
     * @return <code>true</code> if method was present, <code>false</code> if not
     */
    public boolean deleteMethod(String name, String sig) {
        ClassGen cg = getClassGen();
        Method method = cg.containsMethod(name, sig);
        if (method == null) {
            return false;
        } else {
            cg.removeMethod(method);
            m_isModified = true;
            m_isHashCurrent = false;
            return true;
        }
    }
View Full Code Here

     * @return the optimized JavaClass
     */
    public static JavaClass speedup(JavaClass source) {
        try {
            logger.info("Entry speedup(JavaClass)");
            ClassGen cg = new ClassGen(source);
            Method[] methods = cg.getMethods();
            ConstantPoolGen cp = cg.getConstantPool();

            Transform trans = getTransformationForJavaClass(source, cp);
            if (trans == null) {
                logger.warn(
                    "No transformer found for class " + source.getClassName());
                return source;
            }
            for (int i = 0; i < methods.length; i++) {
                if (!(methods[i].isAbstract() || methods[i].isNative())) {
                    MethodGen mg =
                        new MethodGen(methods[i], source.getClassName(), cp);
                    Method stripped = trans.speedup(methods[i], mg, cp);
                    if (stripped != null)
                        cg.replaceMethod(methods[i], stripped);
                }
            }
            trans.done();
            JavaClass target = cg.getJavaClass();
            target.setFileName(source.getFileName());
            return target;
        } catch (Exception e) {
            e.printStackTrace();
        }
View Full Code Here

    }

    static org.apache.bcel.generic.Type printStreamT = org.apache.bcel.generic.Type.getType("Ljava/io/PrintStream;");

    static byte[] bcelHelloWorld() {
        ClassGen cg = new ClassGen("HelloWorld",
                "java/lang/Object",
                "HelloWorld.java",
                Constants.ACC_PUBLIC,
                null);

        cg.addEmptyConstructor(Constants.ACC_PUBLIC);

        ConstantPoolGen cp = cg.getConstantPool();
        org.apache.bcel.generic.InstructionList il = new org.apache.bcel.generic.InstructionList();
        org.apache.bcel.generic.InstructionFactory factory = new org.apache.bcel.generic.InstructionFactory(cg);

        MethodGen mg = new MethodGen(Constants.ACC_STATIC
                | Constants.ACC_PUBLIC,
                org.apache.bcel.generic.Type.VOID,
                new org.apache.bcel.generic.Type[] { new ArrayType(org.apache.bcel.generic.Type.STRING,
                        1) },
                null,
                "main",
                "HelloWorld",
                il,
                cp);
        il.append(factory.createGetStatic("java/lang/System",
                "out",
                printStreamT));
        il.append(new PUSH(cp, "Hello world!"));
        il.append(factory.createInvoke("java.io.PrintStream",
                "println",
                org.apache.bcel.generic.Type.VOID,
                new org.apache.bcel.generic.Type[] { org.apache.bcel.generic.Type.STRING },
                Constants.INVOKESPECIAL));

        mg.setMaxStack();
        cg.addMethod(mg.getMethod());

        return cg.getJavaClass().getBytes();
    }
View Full Code Here

    }

    static void nullBCELAdapt(final byte[] b) throws IOException {
        JavaClass jc = new ClassParser(new ByteArrayInputStream(b),
                "class-name").parse();
        ClassGen cg = new ClassGen(jc);
        ConstantPoolGen cp = cg.getConstantPool();
        Method[] ms = cg.getMethods();
        for (int k = 0; k < ms.length; ++k) {
            MethodGen mg = new MethodGen(ms[k], cg.getClassName(), cp);
            boolean lv = ms[k].getLocalVariableTable() == null;
            boolean ln = ms[k].getLineNumberTable() == null;
            if (lv) {
                mg.removeLocalVariables();
            }
            if (ln) {
                mg.removeLineNumbers();
            }
            mg.stripAttributes(skipDebug);
            InstructionList il = mg.getInstructionList();
            if (il != null) {
                InstructionHandle ih = il.getStart();
                while (ih != null) {
                    ih = ih.getNext();
                }
                if (compute) {
                    mg.setMaxStack();
                    mg.setMaxLocals();
                }
                if (computeFrames) {
                    ModifiedPass3bVerifier verif;
                    verif = new ModifiedPass3bVerifier(jc, k);
                    verif.do_verify();
                }
            }
            cg.replaceMethod(ms[k], mg.getMethod());
        }
        cg.getJavaClass().getBytes();
    }
View Full Code Here

        String[] interfaceNames = new String[interfaces.length + 1];
        for (int i = 0; i < interfaces.length; i++)
            interfaceNames[i] = interfaces[i].getName();
        interfaceNames[interfaces.length] = Stub.class.getName();

        ClassGen newStubClass = new ClassGen(className, superClassName,
                "generated", // file name
                Constants.ACC_PUBLIC | Constants.ACC_FINAL, interfaceNames);

        ConstantPoolGen cp = newStubClass.getConstantPool();

        if (handlerMethodRef == null)
            throw new IllegalArgumentException("handler method is null");

        //
        // Check that the handler method is valid
        //
        Class[] paramTypes = handlerMethodRef.getParameterTypes();
        if (paramTypes.length != 3) {
            throw new IllegalArgumentException(
                    "handler method must have three arguments");
        }

        if (!paramTypes[0].isAssignableFrom(superClass)) {
            throw new IllegalArgumentException(
                    "Handler's 1st argument must be super-type for "
                            + superClass);
        }

        // the type of data fields
        Type typeOfDataFields = translate(paramTypes[1]);

        if (Object[].class != paramTypes[2]) {
            throw new IllegalArgumentException(
                    "Handler's 3rd argument must be Object[]");
        }

        //
        // Construct field for the handler reference
        //
        Class handlerClass = handlerMethodRef.getDeclaringClass();
        FieldGen handlerFieldGen = new FieldGen(Constants.ACC_PRIVATE
                | Constants.ACC_FINAL, translate(handlerClass), Util
                .handlerFieldName(), cp);
        newStubClass.addField(handlerFieldGen.getField());

        //
        // Construct the method that gets the stub handler.
        //
        generateHandlerGetter(newStubClass, handlerFieldGen);

        //
        // construct the field that holds the initializer
        //
        FieldGen initializerFieldGen = new FieldGen(Constants.ACC_PRIVATE
                | Constants.ACC_STATIC, translate(StubInitializer.class), Util
                .initializerFieldName(), cp);
        newStubClass.addField(initializerFieldGen.getField());

        //
        // Emit constructor
        //
        emitInitializerConstructor(newStubClass, handlerFieldGen,
                initializerFieldGen);

        //
        // Construct data fields
        //
        FieldGen[] dataFieldGens = new FieldGen[methods.length];
        for (int i = 0; i < methods.length; i++) {
            MethodRef method = methods[i];

            dataFieldGens[i] = new FieldGen(Constants.ACC_PRIVATE
                    | Constants.ACC_STATIC, typeOfDataFields, Util
                    .methodFieldName(i), cp);

            newStubClass.addField(dataFieldGens[i].getField());
        }

        //
        // Construct method stubs
        //
        for (int i = 0; i < methods.length; i++) {
            generate(newStubClass, methods[i], dataFieldGens[i],
                    handlerFieldGen, handlerMethodRef);
        }

        //
        // Construct super-method trampolines
        //
        for (int i = 0; i < superMethodRefs.length; i++) {
            generateSuperMethod(newStubClass, superMethodRefs[i]);
        }

        JavaClass javaClass = newStubClass.getJavaClass();
        byte[] classData = javaClass.getBytes();

        try {
            if (Boolean.getBoolean("org.apache.yoko.rmi.util.stub.debug")) {
                java.io.File out = new java.io.File(className + ".class");
View Full Code Here

        String[] interfaceNames = new String[interfaces.length + 1];
        for (int i = 0; i < interfaces.length; i++)
            interfaceNames[i] = interfaces[i].getName();
        interfaceNames[interfaces.length] = Stub.class.getName();

        ClassGen newStubClass = new ClassGen(className, superClassName,
                "generated", // file name
                Constants.ACC_PUBLIC | Constants.ACC_FINAL, interfaceNames);

        ConstantPoolGen cp = newStubClass.getConstantPool();

        if (handlerMethodRef == null)
            throw new IllegalArgumentException("handler method is null");

        //
        // Check that the handler method is valid
        //
        Class[] paramTypes = handlerMethodRef.getParameterTypes();
        if (paramTypes.length != 3) {
            throw new IllegalArgumentException(
                    "handler method must have three arguments");
        }

        if (!paramTypes[0].isAssignableFrom(superClass)) {
            throw new IllegalArgumentException(
                    "Handler's 1st argument must be super-type for "
                            + superClass);
        }

        // the type of data fields
        Type typeOfDataFields = translate(paramTypes[1]);

        if (Object[].class != paramTypes[2]) {
            throw new IllegalArgumentException(
                    "Handler's 3rd argument must be Object[]");
        }

        //
        // Construct field for the handler reference
        //
        Class handlerClass = handlerMethodRef.getDeclaringClass();
        FieldGen handlerFieldGen = new FieldGen(Constants.ACC_PRIVATE
                | Constants.ACC_FINAL, translate(handlerClass), Util
                .handlerFieldName(), cp);
        newStubClass.addField(handlerFieldGen.getField());

        //
        // Construct the method that gets the stub handler.
        //
        generateHandlerGetter(newStubClass, handlerFieldGen);

        //
        // Emit constructor
        //
        emitOneArgConstructor(newStubClass, handlerFieldGen);

        //
        // Construct data fields
        //
        FieldGen[] dataFieldGens = new FieldGen[methods.length];
        for (int i = 0; i < methods.length; i++) {
            MethodRef method = methods[i];

            dataFieldGens[i] = new FieldGen(Constants.ACC_PRIVATE
                    | Constants.ACC_STATIC, typeOfDataFields, Util
                    .methodFieldName(i), cp);

            newStubClass.addField(dataFieldGens[i].getField());
        }

        //
        // Construct method stubs
        //
        for (int i = 0; i < methods.length; i++) {
            generate(newStubClass, methods[i], dataFieldGens[i],
                    handlerFieldGen, handlerMethodRef);
        }

        JavaClass javaClass = newStubClass.getJavaClass();
        byte[] classData = javaClass.getBytes();

        try {
            if (Boolean.getBoolean("org.apache.yoko.rmi.util.stub.debug")) {
                java.io.File out = new java.io.File(className + ".class");
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.ClassGen

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.