Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


     *  
   * @param method
   */
  private void synchronizedMethod(Method method) {
    if (theClassGen==null) {
      theClassGen = new ClassGen(jc);
      theCPool = theClassGen.getConstantPool();
    }

    Verbose.println("  PREPROCESSING synchronized: "+jc.getClassName()+"."+method.getName()+method.getSignature());
   
View Full Code Here


     * @throws IOException
     * @throws ClassFormatException
     */
    public static ClassGen fromByte(final byte[] bytecode) throws IOException, ClassFormatException {
        ClassParser parser = new ClassParser(new ByteArrayInputStream(bytecode), "<generated>");
        return new ClassGen(parser.parse());
    }
View Full Code Here

     * @param klass the unextendable class set
     */
    public void transformInterface(final Context context, final Klass klass) {
        if (ADD_UUID == null) return; // do not do any transformations

        final ClassGen cg = klass.getClassGen();
        final ConstantPoolGen cpg = cg.getConstantPool();
        final InstructionFactory factory = new InstructionFactory(cg);

        if (classFilter(cg)) {
            return;
        }
        if (m_hasBeenTransformed.contains(cg.getClassName())) {
            return;
        }

        // mark the class as transformed
        m_hasBeenTransformed.add(cg.getClassName());

        addIdentifiableInterface(cg, cpg);
        addUuidField(cg);
        addUuidGetterMethod(cg, cpg, factory);
    }
View Full Code Here

     * @param klass the class.
     */
    public void transformCode(final Context context, final Klass klass) {
        if (ADD_UUID == null) return; // do not do any transformations

        final ClassGen cg = klass.getClassGen();
        if (classFilter(cg)) {
            return;
        }
        if (cg.containsField(TransformationUtil.UUID_FIELD) == null) {
            return;
        }

        final InstructionFactory factory = new InstructionFactory(cg);
        final ConstantPoolGen cpg = cg.getConstantPool();
        final Method[] methods = cg.getMethods();

        // get the indexes for the <init> methods
        List initIndexes = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("<init>")) {
                initIndexes.add(new Integer(i));
            }
        }

        // advise all the constructors
        for (Iterator it = initIndexes.iterator(); it.hasNext();) {
            final int initIndex = ((Integer)it.next()).intValue();

            methods[initIndex] = createUuidField(
                    cpg, cg,
                    methods[initIndex],
                    factory).getMethod();
        }

        // update the old methods
        cg.setMethods(methods);
    }
View Full Code Here

     * @param context the transformation context
     * @param klass the class set.
     */
    public void transformCode(final Context context, final Klass klass) {

        final ClassGen cg = klass.getClassGen();
        ClassMetaData classMetaData = BcelMetaDataMaker.
                createClassMetaData(context.getJavaClass(cg));

        if (classFilter(classMetaData, cg)) {
            return;
        }

        final InstructionFactory factory = new InstructionFactory(cg);
        final ConstantPoolGen cpg = cg.getConstantPool();
        final Method[] methods = cg.getMethods();

        // get the index for the <clinit> method (if there is one)
        boolean noClinitMethod = true;
        int indexClinit = -1;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("<clinit>")) {
                indexClinit = i;
                noClinitMethod = false;
                break;
            }
        }

        // build and sort the method lookup list
        final List methodLookupList = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);
            if (methodFilter(classMetaData, methodMetaData, methods[i]) == null) {
                continue;
            }
            methodLookupList.add(methods[i]);
        }
        Collections.sort(methodLookupList, BCELMethodComparator.getInstance());

        final Map methodSequences = new HashMap();
        final List newMethods = new ArrayList();
        Method clInitMethod = null;
        for (int i = 0; i < methods.length; i++) {

            MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);

            String uuid = methodFilter(classMetaData, methodMetaData, methods[i]);
            if (!methods[i].isStatic() || uuid == null) {
                continue;
            }

            final MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cpg);

            // take care of identification of overloaded methods by
            // inserting a sequence number
            if (methodSequences.containsKey(methods[i].getName())) {
                int sequence = ((Integer)methodSequences.get(methods[i].getName())).intValue();
                methodSequences.remove(methods[i].getName());
                sequence++;
                methodSequences.put(methods[i].getName(), new Integer(sequence));
            }
            else {
                methodSequences.put(methods[i].getName(), new Integer(1));
            }

            final int methodLookupId = methodLookupList.indexOf(methods[i]);
            final int methodSequence =
                    ((Integer)methodSequences.get(methods[i].getName())).intValue();

            addStaticJoinPointField(cpg, cg, mg, methodSequence);

            // get the join point controller
            final String controllerClassName =
                    m_definition.getJoinPointController(classMetaData, methodMetaData);

            if (noClinitMethod) {
                // no <clinit> method exists
                if (clInitMethod == null) {
                    clInitMethod = createClInitMethodWithStaticJoinPointField(
                            cpg, cg,
                            methods[i],
                            factory,
                            methodSequence
                    );
                }
                else {
                    clInitMethod = createStaticJoinPointField(
                            cpg, cg, clInitMethod,
                            methods[i],
                            factory,
                            methodSequence
                    );
                }
            }
            else {
                // we have a <clinit> method
                methods[indexClinit] = createStaticJoinPointField(
                        cpg, cg, methods[indexClinit],
                        methods[i],
                        factory,
                        methodSequence
                );
            }

            // create a proxy method for the original method
            newMethods.add(createProxyMethod(
                    cpg, cg, mg,
                    factory,
                    methodLookupId,
                    methodSequence,
                    methods[i].getAccessFlags(),
                    uuid,
                    controllerClassName
            ));

            // add a prefix to the original method
            methods[i] = addPrefixToMethod(mg, methods[i], methodSequence);

            mg.setMaxLocals();
            mg.setMaxStack();
        }

        // if we have transformed methods, create the static class field
        if (noClinitMethod && clInitMethod != null) {
            addStaticClassField(cpg, cg);
            clInitMethod = createStaticClassField(cpg, cg, clInitMethod, factory);

            newMethods.add(clInitMethod);
        }
        else if (newMethods.size() != 0) {
            addStaticClassField(cpg, cg);
            methods[indexClinit] = createStaticClassField(cpg, cg, methods[indexClinit], factory);
        }

        // update the old methods
        cg.setMethods(methods);

        // add the new methods
        for (Iterator it = newMethods.iterator(); it.hasNext();) {
            Method method = (Method)it.next();
            cg.addMethod(method);
        }
    }
View Full Code Here

     * @param klass the class
     */
    public void transformInterface(final Context context, final Klass klass) {
        if (ADD_METADATA == null) return; // do not do any transformations

        final ClassGen cg = klass.getClassGen();
        final ConstantPoolGen cpg = cg.getConstantPool();
        final InstructionFactory factory = new InstructionFactory(cg);

        if (classFilter(cg)) {
            return;
        }
        if (m_hasBeenTransformed.contains(cg.getClassName())) {
            return;
        }

        // mark the class as transformed
        m_hasBeenTransformed.add(cg.getClassName());

        addMetaDataEnhancableInterface(cg, cpg);
        addMapField(cg);
        addMetaDataGetterMethod(cg, cpg, factory);
        addMetaDataSetterMethod(cg, cpg, factory);
View Full Code Here

     * @param klass the class
     */
    public void transformCode(final Context context, final Klass klass) {
        if (ADD_METADATA == null) return; // do not do any transformations

        final ClassGen cg = klass.getClassGen();
        if (classFilter(cg)) {
            return;
        }
        if (cg.containsField(TransformationUtil.META_DATA_FIELD) == null) {
            return;
        }

        final InstructionFactory factory = new InstructionFactory(cg);
        final ConstantPoolGen cpg = cg.getConstantPool();
        final Method[] methods = cg.getMethods();

        // get the indexes for the <init> methods
        List initIndexes = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("<init>")) {
                initIndexes.add(new Integer(i));
            }
        }

        // advise all the constructors
        for (Iterator it = initIndexes.iterator(); it.hasNext();) {
            final int initIndex = ((Integer)it.next()).intValue();

            methods[initIndex] = createMetaDataField(
                    cpg, cg,
                    methods[initIndex],
                    factory).getMethod();
        }

        // update the old methods
        cg.setMethods(methods);
    }
View Full Code Here

     * @param context the transformation context
     * @param klass the class set.
     */
    public void transformCode(final Context context, final Klass klass) {

        final ClassGen cg = klass.getClassGen();
        ClassMetaData classMetaData = BcelMetaDataMaker.createClassMetaData(context.getJavaClass(cg));

        if (classFilter(classMetaData, cg)) {
            return;
        }

        final InstructionFactory factory = new InstructionFactory(cg);
        final ConstantPoolGen cpg = cg.getConstantPool();
        final Method[] methods = cg.getMethods();

        // get the indexes for the <init> methods
        List initIndexes = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("<init>")) {
                initIndexes.add(new Integer(i));
            }
        }

        // build and sort the method lookup list
        final List methodLookupList = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            if (methodFilter(classMetaData, methods[i]) == null) {
                continue;
            }
            methodLookupList.add(methods[i]);
        }

        Collections.sort(methodLookupList, BCELMethodComparator.getInstance());

        final Map methodSequences = new HashMap();
        final List proxyMethods = new ArrayList();
        for (int i = 0; i < methods.length; i++) {

            // filter the methods
            String uuid = methodFilter(classMetaData, methods[i]);
            if (methods[i].isStatic() || uuid == null) {
                continue;
            }

            final MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cpg);

            // take care of identification of overloaded methods by inserting a sequence number
            if (methodSequences.containsKey(methods[i].getName())) {
                int sequence = ((Integer)methodSequences.get(methods[i].getName())).intValue();
                methodSequences.remove(methods[i].getName());
                sequence++;
                methodSequences.put(methods[i].getName(), new Integer(sequence));
            }
            else {
                methodSequences.put(methods[i].getName(), new Integer(1));
            }

            final int methodLookupId = methodLookupList.indexOf(methods[i]);
            final int methodSequence = ((Integer)methodSequences.
                    get(methods[i].getName())).intValue();

            //handleCallToOverriddenSuperClassMethod(mg, cg, cpg, factory, methodSequence, context);

            addJoinPointField(cpg, cg, mg, methodSequence);

            // get the join point controller
            MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);

            final String controllerClassName =
                    m_definition.getJoinPointController(classMetaData, methodMetaData);

            // advise all the constructors
            for (Iterator it = initIndexes.iterator(); it.hasNext();) {
                final int initIndex = ((Integer)it.next()).intValue();

                methods[initIndex] = createJoinPointField(
                        cpg, cg,
                        methods[initIndex],
                        methods[i],
                        factory,
                        methodSequence
                ).getMethod();
            }

            proxyMethods.add(createProxyMethod(
                    cpg, cg, mg,
                    factory,
                    methodLookupId,
                    methodSequence,
                    methods[i].getAccessFlags(),
                    uuid,
                    controllerClassName
            ));

            methods[i] = addPrefixToMethod(mg, methods[i], methodSequence);

            mg.setMaxStack();
        }

        // update the old methods
        cg.setMethods(methods);

        // add the proxy methods
        for (Iterator it = proxyMethods.iterator(); it.hasNext();) {
            Method method = (Method)it.next();
            cg.addMethod(method);
        }
    }
View Full Code Here

     * @param context the transformation context
     * @param klass the class set.
     */
    public void transformCode(final Context context, final Klass klass) {

        final ClassGen cg = klass.getClassGen();
        ClassMetaData classMetaData = BcelMetaDataMaker.
                createClassMetaData(context.getJavaClass(cg));

        if (classFilter(classMetaData, cg)) {
            return;
        }

        Method[] methods = cg.getMethods();

        // get the index for the <clinit> method (if there is one)
        boolean noClInitMethod = true;
        int clinitIndex = -1;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("<clinit>")) {
                clinitIndex = i;
                noClInitMethod = false;
                break;
            }
        }
        ConstantPoolGen cpg = cg.getConstantPool();
        String className = cg.getClassName();
        InstructionFactory factory = new InstructionFactory(cg);

        final Set setFieldJoinPoints = new HashSet();
        final Set getFieldJoinPoints = new HashSet();

        Method clInitMethod = null;
        List newMethods = new ArrayList();
        boolean isClassAdvised = false;
        for (int i = 0; i < methods.length; i++) {

            if (methodFilter(methods[i])) {
                continue;
            }

            MethodGen mg = new MethodGen(methods[i], className, cpg);

            InstructionList il = mg.getInstructionList();
            InstructionHandle ih = il.getStart();

            // search for all GETFIELD and GETSTATIC instructions and
            // inserts the pre and post advices
            while (ih != null) {
                Instruction ins = ih.getInstruction();

                if (ins instanceof GETFIELD || ins instanceof GETSTATIC) {
                    FieldInstruction gfIns = (FieldInstruction)ins;

                    String fieldName = gfIns.getName(cpg);
                    String signature = gfIns.getFieldType(cpg).toString() + " " + fieldName;
                    Type joinPointType = TransformationUtil.STATIC_FIELD_GET_JOIN_POINT_TYPE;
                    String joinPointClass = TransformationUtil.STATIC_FIELD_GET_JOIN_POINT_CLASS;

                    FieldMetaData fieldMetaData =
                            BcelMetaDataMaker.createFieldMetaData(gfIns, cpg);

                    String uuid = getFieldFilter(classMetaData, fieldMetaData);
                    if (uuid != null) {

                        String fieldClassName = gfIns.getClassName(cpg);
                        if (fieldClassName.equals(cg.getClassName())) {
                            isClassAdvised = true;

                            // in static context
                            if (mg.isStatic()) {
                                insertPreAdvice(il, ih, cg, fieldName, factory, joinPointType);
                                insertPostAdvice(il, ih.getNext(), cg, fieldName, factory, joinPointType);

                                // skip the creation of the join point if we
                                // already have one
                                if (!getFieldJoinPoints.contains(fieldName)) {
                                    getFieldJoinPoints.add(fieldName);

                                    addStaticJoinPointField(cpg, cg, fieldName, joinPointType);

                                    if (noClInitMethod) {
                                        clInitMethod = createClInitMethodWithStaticJoinPointField(
                                                cpg, cg, fieldName, signature, factory,
                                                joinPointType, joinPointClass, uuid);
                                    }
                                    else {
                                        methods[clinitIndex] = createStaticJoinPointField(
                                                cpg, cg, methods[clinitIndex], fieldName, signature,
                                                factory, joinPointType, joinPointClass, uuid);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (ins instanceof PUTFIELD || ins instanceof PUTSTATIC) {
                    FieldInstruction pfIns = (FieldInstruction)ins;

                    String fieldName = pfIns.getName(cpg);
                    String signature = pfIns.getFieldType(cpg).toString() + " " + fieldName;
                    Type joinPointType = TransformationUtil.STATIC_FIELD_SET_JOIN_POINT_TYPE;
                    String joinPointClass = TransformationUtil.STATIC_FIELD_SET_JOIN_POINT_CLASS;

                    FieldMetaData fieldMetaData =
                            BcelMetaDataMaker.createFieldMetaData(pfIns, cpg);

                    String uuid = setFieldFilter(classMetaData, fieldMetaData);
                    if (uuid != null) {

                        String fieldClassName = pfIns.getClassName(cpg);
                        if (fieldClassName.equals(cg.getClassName())) {

                            // in static context
                            if (mg.isStatic()) {
                                isClassAdvised = true;

                                insertPreAdvice(il, ih, cg, fieldName, factory, joinPointType);
                                insertPostAdvice(il, ih.getNext(), cg, fieldName, factory, joinPointType);

                                // skip the creation of the join point if we
                                // already have one
                                if (!setFieldJoinPoints.contains(fieldName)) {
                                    setFieldJoinPoints.add(fieldName);

                                    addStaticJoinPointField(
                                            cpg, cg,
                                            fieldName,
                                            joinPointType);

                                    if (noClInitMethod) {
                                        clInitMethod = createClInitMethodWithStaticJoinPointField(
                                                cpg, cg, fieldName, signature, factory,
                                                joinPointType, joinPointClass, uuid);
                                    }
                                    else {
                                        methods[clinitIndex] = createStaticJoinPointField(
                                                cpg, cg, methods[clinitIndex], fieldName, signature,
                                                factory, joinPointType, joinPointClass, uuid);
                                    }
                                }
                            }
                        }
                    }
                }
                ih = ih.getNext();
            }

            if (isClassAdvised) {
                mg.setMaxStack();
                methods[i] = mg.getMethod();
            }
        }
        if (isClassAdvised) {
            // if we have transformed methods, create the static class field

            if (noClInitMethod && clInitMethod != null) {
                addStaticClassField(cpg, cg);
                clInitMethod = createStaticClassField(cpg, cg, clInitMethod, factory);
                newMethods.add(clInitMethod);
            }
            else if (!noClInitMethod) {
                addStaticClassField(cpg, cg);
                methods[clinitIndex] =
                        createStaticClassField(cpg, cg, methods[clinitIndex], factory);
            }
        }
        // update the old methods
        cg.setMethods(methods);

        // add the new methods
        for (Iterator it = newMethods.iterator(); it.hasNext();) {
            Method method = (Method)it.next();
            cg.addMethod(method);
        }
    }
View Full Code Here

     *
     * @param context the transformation context
     * @param klass the class
     */
    public void transformInterface(final Context context, final Klass klass) {
        final ClassGen cg = klass.getClassGen();
        if (classFilter(cg)) {
            return;
        }
        if (m_transformed.contains(cg.getClassName())) {
            return;
        }
        m_transformed.add(cg.getClassName());

        ConstantPoolGen cpg = cg.getConstantPool();
        int[] interfaces = cg.getInterfaces();

        for (Iterator it2 = m_definition.getIntroductionNames(
                cg.getClassName()).iterator(); it2.hasNext();) {

            String introductionName = (String)it2.next();
            String interfaceName = m_definition.getIntroductionInterfaceName(introductionName);

            boolean addInterface = true;

            for (int l = 0; l < interfaces.length; l++) {
                ConstantClass cc = (ConstantClass)cpg.getConstant(interfaces[l]);
                ConstantUtf8 cu = (ConstantUtf8)cpg.getConstant(cc.getNameIndex());

                if (implementsInterface(cu, interfaceName)) {
                    addInterface = false;
                    break;
                }
            }
            if (addInterface) {
                if (interfaceName == null || interfaceName.equals("")) {
                    throw new DefinitionException("trying to weave null interface to " + cg.getClassName() + ": definition file is not consistentadd");
                }
                TransformationUtil.addInterfaceToClass(cg, interfaceName);
            }
        }
    }
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.