Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


            if (fieldName.equals(f[i].getName()))
            {
                return classGen;
            }
        }
        ClassGen scg = classGen;
       
        if( scg.getSuperclassName() == null )
        {
            return null;
        }
        JavaClass jc = Repository.getRepository().findClass(scg.getSuperclassName());
        while( jc != null )
        {
            scg = new ClassGen(jc);
            f = scg.getFields();
            for (int i = 0; i < f.length; i++)
            {
                if (fieldName.equals(f[i].getName()))
                {
                    return scg;
                }
            }         
            if( scg.getSuperclassName() == null )
            {
                return scg;
            }
            jc = Repository.getRepository().findClass(scg.getSuperclassName());
        }

       
        return null;
    }    
View Full Code Here


        Field f[] = classGen.getFields();
        for (int i = 0; i < f.length; i++)
        {
            if (fieldName.equals(f[i].getName()))
            {
                return new ClassGen(classGen);
            }
        }
        ClassGen scg = new ClassGen(classGen);
       
        if( scg.getSuperclassName() == null )
        {
            return null;
        }
        JavaClass jc = Repository.getRepository().findClass(scg.getSuperclassName());
        while( jc != null )
        {
            scg = new ClassGen(jc);
            f = scg.getFields();
            for (int i = 0; i < f.length; i++)
            {
                if (fieldName.equals(f[i].getName()))
                {
                    return scg;
                }
            }         
            if( scg.getSuperclassName() == null )
            {
                return scg;
            }
            jc = Repository.getRepository().findClass(scg.getSuperclassName());
        }

       
        return null;
    }    
View Full Code Here

        if (Repository.getRepository() == null || !(Repository.getRepository() instanceof JPOXRepository))
        {
            Repository.setRepository(new JPOXRepository(new JDOClassLoaderResolver()));
        }
        Repository.addClass(enhanceClass);
        classGen = new ClassGen(this.enhanceClass);

        // Copy fields after we have the classGen set
        copyMembersFromInterface(imd);
    }
View Full Code Here

            String msg = LOCALISER_ENH.msg("Enhancer.ClassNotFound", fullName, "");
            JPOXLogger.ENHANCER.error(msg);
            throw new JDOFatalException(msg);
        }

        classGen = new ClassGen(this.enhanceClass);
    }
View Full Code Here

        return c;
    }

    private byte[] transform(JavaClass javaclazz) throws ClassNotFoundException {
        // make all methods of java class continuable
        ClassGen clazz = new ClassGen(javaclazz);
        ConstantPoolGen cp = clazz.getConstantPool();
        // obsolete, but neccesary to execute the InvokeContext
        InstConstraintVisitor icv = new InstConstraintVisitor();
        icv.setConstantPoolGen(cp);
        // vistor to build the frame information
        ExecutionVisitor ev = new ExecutionVisitor();
        ev.setConstantPoolGen(cp);

        Method[] methods = clazz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            MethodGen method = new MethodGen(methods[i], clazz.getClassName(), cp);

            currentMethodStatic = methods[i].isStatic();
            if (isValid(method)) {
                // analyse the code of the method to create the frame
                // information about every instruction
                ControlFlowGraph cfg = new ControlFlowGraph(method);
                analyse(clazz, method, cfg, icv, ev);
                // add intercepting code
                rewrite(method, cfg);
                // make last optional check for consistency
                clazz.replaceMethod(methods[i], method.getMethod());
            }
        }
        clazz.addInterface(CONTINUATIONCAPABLE_CLASS);
        return clazz.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

        Class cls = null;

        try {
            _javaClass = new ClassParser(is, className).parse();

            _derivedClassGen = new ClassGen(_javaClass.copy());

            String oldClassName = _derivedClassGen.getClassName();
            newName = getDerivedName();

                //set the class name and the name for the member field references all at once.
View Full Code Here

    {
        String strippedClassName = strippedClassName(field);
        String newClassName = StringUtil.strcat(
            newClassNamePrefix(strippedClassName), "_field_", field.getName());
        String javaFileName = StringUtil.strcat(strippedClassName, ".java");
        ClassGen classGen = new ClassGen(newClassName,
            CompiledAccessor.class.getName(),
            javaFileName,
            Constants.ACC_PUBLIC | Constants.ACC_FINAL | Constants.ACC_SUPER,
            EmptyStringArray);
        classGen.addEmptyConstructor(Constants.ACC_PUBLIC);

        ConstantPoolGen constantPoolGen = classGen.getConstantPool();
        InstructionFactory instructionFactory = new InstructionFactory(classGen,
                                                                       constantPoolGen);

        org.apache.bcel.classfile.Method getterMethod =
            constructGetterMethod(newClassName, constantPoolGen,
                                  instructionFactory, field);
        classGen.addMethod(getterMethod);

        org.apache.bcel.classfile.Method setterMethod =
            constructSetterMethod(newClassName, constantPoolGen,
                                  instructionFactory, field);
        classGen.addMethod(setterMethod);

        return generateClassAndGetInstance(classGen, newClassName,
                                           field.getDeclaringClass().getProtectionDomain());
    }
View Full Code Here

        String strippedClassName = strippedClassName(method);
        String newClassName =
            StringUtil.strcat(newClassNamePrefix(strippedClassName), "_",
                              methodType(isSetter), "Method_", method.getName());
        String javaFileName = StringUtil.strcat(strippedClassName, ".java");
        ClassGen classGen = new ClassGen(newClassName,
            CompiledAccessor.class.getName(),
            javaFileName,
            Constants.ACC_PUBLIC | Constants.ACC_FINAL | Constants.ACC_SUPER,
            EmptyStringArray);
        classGen.addEmptyConstructor(Constants.ACC_PUBLIC);

        ConstantPoolGen constantPoolGen = classGen.getConstantPool();
        InstructionFactory instructionFactory = new InstructionFactory(classGen,
                                                                       constantPoolGen);

        org.apache.bcel.classfile.Method accessorMethod = null;
        if (isSetter) {
            accessorMethod =
                constructSetterMethod(newClassName, constantPoolGen,
                                      instructionFactory, method);
        }
        else {
            accessorMethod =
                constructGetterMethod(newClassName, constantPoolGen,
                                      instructionFactory, method);
        }
        classGen.addMethod(accessorMethod);

        return generateClassAndGetInstance(classGen, newClassName,
                                           method.getDeclaringClass().getProtectionDomain());
    }
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.