Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


    public Class generate(Class clazz) {
        String className = clazz.getName();
        Random rand = new Random(System.currentTimeMillis());
        String clonerClassname = "Cloner" + (Math.abs(rand.nextInt() % 3256));

        ClassGen cg = new ClassGen(clonerClassname,
                                   "java.lang.Object",
                                   clonerClassname + ".java",
                                   Constants.ACC_PUBLIC | Constants.ACC_SUPER,
                                   new String[] { "io.s4.util.Cloner" });
        ConstantPoolGen cp = cg.getConstantPool();
        InstructionFactory instFactory = new InstructionFactory(cg, cp);

        InstructionList il = new InstructionList();

        // build constructor method for new class
        MethodGen constructor = new MethodGen(Constants.ACC_PUBLIC,
                                              Type.VOID,
                                              Type.NO_ARGS,
                                              new String[] {},
                                              "<init>",
                                              clonerClassname,
                                              il,
                                              cp);
        il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
        il.append(instFactory.createInvoke("java.lang.Object",
                                           "<init>",
                                           Type.VOID,
                                           Type.NO_ARGS,
                                           Constants.INVOKESPECIAL));

        il.append(InstructionFactory.createReturn(Type.VOID));
        constructor.setMaxStack();
        constructor.setMaxLocals();
        cg.addMethod(constructor.getMethod());
        il.dispose();

        // build clone method
        il = new InstructionList();
        MethodGen method = new MethodGen(Constants.ACC_PUBLIC,
                                         Type.OBJECT,
                                         new Type[] { Type.OBJECT },
                                         new String[] { "arg0" },
                                         "clone",
                                         clonerClassname,
                                         il,
                                         cp);

        il.append(InstructionConstants.ACONST_NULL);
        il.append(InstructionFactory.createStore(Type.OBJECT, 2));
        il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
        il.append(new INSTANCEOF(cp.addClass(new ObjectType(className))));
        BranchInstruction ifeq_6 = InstructionFactory.createBranchInstruction(Constants.IFEQ,
                                                                              null);
        il.append(ifeq_6);
        il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
        il.append(instFactory.createCheckCast(new ObjectType(className)));
        il.append(InstructionFactory.createStore(Type.OBJECT, 2));
        InstructionHandle ih_14 = il.append(InstructionFactory.createLoad(Type.OBJECT,
                                                                          2));
        il.append(new INSTANCEOF(cp.addClass(new ObjectType("java.lang.Cloneable"))));
        BranchInstruction ifne_18 = InstructionFactory.createBranchInstruction(Constants.IFNE,
                                                                               null);
        il.append(ifne_18);
        il.append(instFactory.createFieldAccess("java.lang.System",
                                                "out",
                                                new ObjectType("java.io.PrintStream"),
                                                Constants.GETSTATIC));
        il.append(new PUSH(cp, "Not cloneable!"));
        il.append(instFactory.createInvoke("java.io.PrintStream",
                                           "println",
                                           Type.VOID,
                                           new Type[] { Type.STRING },
                                           Constants.INVOKEVIRTUAL));
        il.append(InstructionConstants.ACONST_NULL);
        il.append(InstructionFactory.createReturn(Type.OBJECT));
        InstructionHandle ih_31 = il.append(InstructionFactory.createLoad(Type.OBJECT,
                                                                          2));
        il.append(instFactory.createInvoke(className,
                                           "clone",
                                           Type.OBJECT,
                                           Type.NO_ARGS,
                                           Constants.INVOKEVIRTUAL));
        il.append(InstructionFactory.createReturn(Type.OBJECT));
        ifeq_6.setTarget(ih_14);
        ifne_18.setTarget(ih_31);
        method.setMaxStack();
        method.setMaxLocals();
        cg.addMethod(method.getMethod());
        il.dispose();

        JavaClass jc = cg.getJavaClass();
        ClonerClassLoader cl = new ClonerClassLoader();

        return cl.loadClassFromBytes(clonerClassname, jc.getBytes());
    }
View Full Code Here


    fullQualifiedClassName= packageName.equals("") ? clazzName : packageName + "." + clazzName;
    String baseClazz= clazz.getAttributeValue("extends");
    // TODO Read interfaces
    String[] interfaces= new String[] {};
    short accessFlags= getAccessFlags(clazz);
    classGen= new ClassGen(fullQualifiedClassName, baseClazz, fileName, accessFlags, interfaces);
    constantPoolGen= classGen.getConstantPool();
    factory= new InstructionFactory(classGen, constantPoolGen);
  }
View Full Code Here

        break;
      }
    }
   
    // create class generator
    cg = new ClassGen(classname, SUPER_NAME_N[superclassType], filename,
        Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
    cp = cg.getConstantPool(); // cg creates constant pool

    // main instruction lists
    factory = new InstructionFactory(cg);
View Full Code Here


    public byte[] patchCL(byte[] b) {
        try {

            final ClassGen cg = new ClassGen(new ClassParser(new ByteArrayInputStream(b), "<generated>").parse());
            final String className = cg.getClassName();
            final Method[] methods = cg.getMethods();
            final ConstantPoolGen cpg = cg.getConstantPool();
            final InstructionFactory factory = new InstructionFactory(cg);

            // for all methods, look for caller side "this.define0" calls
            for (int i = 0; i < methods.length; i++) {
                final MethodGen mg = new MethodGen(methods[i], className, cpg);
                final InstructionList il = mg.getInstructionList();
                if (il == null) continue;
                InstructionHandle ih = il.getStart();
    String methodName = methods[i].getName();

                while (ih != null) {
                    final Instruction ins = ih.getInstruction();
                    if (ins instanceof INVOKESPECIAL
                            || ins instanceof INVOKESTATIC
                            || ins instanceof INVOKEVIRTUAL) {
                        final InvokeInstruction invokeInst = (InvokeInstruction)ins;
                        final String callerSideMethodClassName = invokeInst.getClassName(cpg);
                        final String callerSideMethodName = invokeInst.getMethodName(cpg);

                        if ("java.lang.ClassLoader".equals(callerSideMethodClassName)
                                && "defineClass0".equals(callerSideMethodName)) {

                            //assert compliant JRE
                            Type args[] = invokeInst.getArgumentTypes(cpg);
                            assertSupported(args);

                            // store former method args in local vars
                            InstructionHandle ihc = null;
                            if (args.length > 5) {
                                // IBM like JRE with extra args
                                ihc = il.append(ih.getPrev(), factory.createStore(args[args.length - 1], 2100 + args.length - 1));
                                for (int index = args.length - 2; index >= 5; index--) {
                                    ihc = il.append(ihc, InstructionFactory.createStore(args[index], 2100 + index));
                                }
                                ihc = il.append(ihc, factory.createStore(Type.OBJECT, 2016));//protection domain
                            }
                            else {
                                // SUN regular JRE
                                ihc = il.append(ih.getPrev(), factory.createStore(Type.OBJECT, 2016));//protection domain
                            }

                            ihc = il.append(ihc, factory.createStore(Type.INT, 2015));//length
                            ihc = il.append(ihc, factory.createStore(Type.INT, 2014));//index
                            ihc = il.append(ihc, factory.createStore(Type.OBJECT, 2013));//bytes
                            ihc = il.append(ihc, factory.createStore(Type.OBJECT, 2012));//name

                            // prepare method call stack
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 2012));
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 2013));
                            ihc = il.append(ihc, factory.createInvoke(
                                    "com.lambda.Debugger.InstrumentorForCL",
                                    "debugify",
                                    new ArrayType(Type.BYTE, 1),
                                    new Type[]{
                                        Type.STRING,
                                        new ArrayType(Type.BYTE, 1),
            },
                                    Constants.INVOKESTATIC));
                            ihc = il.append(ihc, factory.createStore(Type.OBJECT, 3018));//result bytes

                            // rebuild former method call stack
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 2012));//name
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 3018));//bytes
                            ihc = il.append(ihc, new PUSH(cpg, 0));
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 3018));//bytes
                            ihc = il.append(ihc, InstructionConstants.ARRAYLENGTH);//.length
                            ihc = il.append(ihc, factory.createLoad(Type.OBJECT, 2016));//protection domain

                            // extra args for IBM like JRE
                            if (args.length > 5) {
                                for (int index = 5; index < args.length; index++) {
                                    ihc = il.append(ihc, factory.createLoad(args[index], 2100 + index));
                                }
                            }
                        }
                    }
                    ih = ih.getNext();
                }
                mg.setInstructionList(il);
                mg.setMaxLocals();
                mg.setMaxStack();
                methods[i] = mg.getMethod();
            }
            cg.setMethods(methods);
            return cg.getJavaClass().getBytes();
        }
        catch (Exception e) {
            System.err.println("failed to patch ClassLoader:");
            e.printStackTrace();
            return b;
View Full Code Here

        try {
            log(klass);

            // build the ClassGen
            ClassParser parser = new ClassParser(new ByteArrayInputStream(abyte), "<generated>");//@todo is this needed _"+klass+">");
            ClassGen cg = new ClassGen(parser.parse());

            // instrument
            if ( ! cg.isInterface() && ! Arrays.asList(cg.getInterfaceNames()).contains("java.util.EventListener") )
                cg.addInterface("java.util.EventListener");

            try {
                cg.getJavaClass().dump("_dump/"+klass.replace('.', '/')+".class");
            } catch (Exception e) {
                System.err.println("failed to dump " + klass);
                e.printStackTrace();
            }


            return cg.getJavaClass().getBytes();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
View Full Code Here

        for (Iterator it = m_definitions.iterator(); it.hasNext();) {
            AspectWerkzDefinition definition = (AspectWerkzDefinition)it.next();

            definition.loadAspects(context.getLoader());

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

            if (classFilter(definition, 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++) {
                Method method = methods[i];

                MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);
                if (methodFilter(definition, classMetaData, methodMetaData, method)) {
                    continue;
                }
                methodLookupList.add(methods[i]);

                // TODO: does not work, needs to be thought through more
                // if advised swap add the prefixed one as well to enable second-round instrumentation
//                String originalPrefixedName =
//                        TransformationUtil.ORIGINAL_METHOD_PREFIX +
//                        methods[i].getName();
//                Method[] declaredMethods = cg.getMethods();
//                for (int j = 0; j < declaredMethods.length; j++) {
//                    Method declaredMethod = declaredMethods[j];
//                    if (declaredMethod.getName().startsWith(originalPrefixedName)) {
//                        methodLookupList.add(declaredMethod);
//                    }
//                }
            }

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

            final Map methodSequences = new HashMap();
            final List newMethods = new ArrayList();
            Method clInitMethod = null;
            boolean isClassAdvised = false;

            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];
                MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(method);

                if (methodFilter(definition, classMetaData, methodMetaData, method)
                        || !method.isStatic()) {
                    continue;
                }

                isClassAdvised = true;

                // TODO: does not work, needs to be thought through more
                // if advised swap the method to the prefixed one
//                String originalPrefixedName =
//                        TransformationUtil.ORIGINAL_METHOD_PREFIX +
//                        methods[i].getName();
//                Method[] declaredMethods = cg.getMethods();
//                for (int j = 0; j < declaredMethods.length; j++) {
//                    Method declaredMethod = declaredMethods[j];
//                    if (declaredMethod.getName().startsWith(originalPrefixedName)) {
//                        method = declaredMethod;
//                    }
//                }

                final MethodGen mg = new MethodGen(method, cg.getClassName(), cpg);

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

                final int methodLookupId = methodLookupList.indexOf(method);
                final int methodSequence = ((Integer)methodSequences.get(method.getName())).intValue();

                addStaticJoinPointField(cpg, cg, mg, methodSequence);

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

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

                // create a proxy method for the original method
                newMethods.add(createProxyMethod(
                        cpg, cg,
                        methodMetaData.getName(),
                        mg, factory,
                        methodLookupId,
                        methodSequence,
                        method.getAccessFlags(),
                        definition.getUuid(),
                        controllerClassName
                ));

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

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

            if (isClassAdvised) {
                context.markAsAdvised();

                // 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 it2 = newMethods.iterator(); it2.hasNext();) {
                    Method method = (Method)it2.next();
                    cg.addMethod(method);
                }
            }
        }
    }
View Full Code Here

        for (Iterator it = m_definitions.iterator(); it.hasNext();) {
            AspectWerkzDefinition definition = (AspectWerkzDefinition)it.next();

            definition.loadAspects(context.getLoader());

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

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

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

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

        if (ADD_UUID == null) return; // do not do any transformations

        for (Iterator it = m_definitions.iterator(); it.hasNext();) {
            AspectWerkzDefinition definition = (AspectWerkzDefinition)it.next();

            final ClassGen cg = klass.getClassGen();
            if (classFilter(cg, definition)) {
                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 it2 = initIndexes.iterator(); it2.hasNext();) {
                final int initIndex = ((Integer)it2.next()).intValue();

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

            // update the old methods
            cg.setMethods(methods);
            context.markAsAdvised();
        }
    }
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

        for (Iterator it = m_definitions.iterator(); it.hasNext();) {
            AspectWerkzDefinition definition = (AspectWerkzDefinition)it.next();

            definition.loadAspects(context.getLoader());

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

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

            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));
                }
            }

            final ConstantPoolGen cpg = cg.getConstantPool();
            final String className = cg.getClassName();
            final InstructionFactory factory = new InstructionFactory(cg);

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

            boolean isClassAdvised = false;
            boolean isMethodAdvised = false;
            for (int i = 0; i < methods.length; i++) {

                // filter methods
                if (methodFilter(methods[i])) {
                    continue;
                }
                isMethodAdvised = false;

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

                // do not modify anything within the hidden system methods
                if (mg.getMethod().getName().startsWith(TransformationUtil.ASPECTWERKZ_PREFIX)) {
                    continue;
                }

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

                // get the current field instruction
                FieldInstruction currentGetFieldIns = null;

                // search for all GETFIELD and PUTFIELD instructions and
                // inserts the pre and post advices
                // handle GETFIELD followed by INVOKEINTERFACE on Collection like carrefully
                while (ih != null) {
                    Instruction ins = ih.getInstruction();

                    // handle the java.util.Collection classes
                    if (ins instanceof GETFIELD) {
                        FieldInstruction checkMe = (FieldInstruction)ins;
                        // if the field is an added join point field => skip it
                        // needed if a field of type collection is both setField
                        // and getField advised
                        if (!checkMe.getFieldName(cpg).startsWith(TransformationUtil.JOIN_POINT_PREFIX)) {
                            currentGetFieldIns = checkMe;
                            Instruction next = ih.getNext().getInstruction();
                            if (next instanceof INVOKEINTERFACE) {
                                // handle the INVOKEINTERFACE instruction
                                final InvokeInstruction invokeIns = (InvokeInstruction)next;

                                // do we have a collection?
                                if (invokeIns.getClassName(cpg).equals("java.util.Collection") ||
                                        invokeIns.getClassName(cpg).equals("java.util.Enumeration") ||
                                        invokeIns.getClassName(cpg).equals("java.util.Iterator") ||
                                        invokeIns.getClassName(cpg).equals("java.util.List") ||
                                        invokeIns.getClassName(cpg).equals("java.util.Map") ||
                                        invokeIns.getClassName(cpg).equals("java.util.Set") ||
                                        invokeIns.getClassName(cpg).equals("java.util.SortedMap") ||
                                        invokeIns.getClassName(cpg).equals("java.util.SortedSet")) {

                                    String methodName = invokeIns.getName(cpg);

                                    // is the collection modified (not only accessed or single PUTFIELD instr)?
                                    if (methodName.equals("add") ||
                                            methodName.equals("addAll") ||
                                            methodName.equals("set") ||
                                            methodName.equals("remove") ||
                                            methodName.equals("removeAll") ||
                                            methodName.equals("retainAll") ||
                                            methodName.equals("clear") ||
                                            methodName.equals("put") ||
                                            methodName.equals("putAll")) {

                                        final String fieldName = currentGetFieldIns.getName(cpg);
                                        final String signature = currentGetFieldIns.getFieldType(cpg).
                                                toString() + " " + fieldName;
                                        final Type joinPointType = TransformationUtil.MEMBER_FIELD_SET_JOIN_POINT_TYPE;

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

                                        String uuid = setFieldFilter(definition, classMetaData, fieldMetaData);

                                        // do we have to set a joinpoint ?
                                        if (uuid != null) {
                                            final String fieldClassName = currentGetFieldIns.getClassName(cpg);

                                            if (fieldClassName.equals(cg.getClassName())) {
                                                isMethodAdvised = true;

                                                insertPreAdvice(
                                                        il, ih, cg, fieldName,
                                                        factory, joinPointType);

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

                                                // store the join point field data
                                                JoinPointFieldData data = new JoinPointFieldData(
                                                        fieldName, signature, joinPointType, uuid);
                                                if (!setFieldJoinPoints.contains(data)) {
                                                    setFieldJoinPoints.add(data);
                                                }

                                                // add one step more to the InstructionList (GETFIELD(current) INVOKEINTERFACE with jp)
                                                ih = ih.getNext();
                                                ins = ih.getInstruction();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // handle the getField instructions
                    // (if we have set a getfield for collection modification jp the ins has been altered)
                    if (ins instanceof GETFIELD) {
                        final FieldInstruction gfIns = (FieldInstruction)ins;

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

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

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

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

                                isMethodAdvised = true;

                                insertPreAdvice(
                                        il, ih, cg, fieldName,
                                        factory, joinPointType);

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

                                // store the join point field data
                                JoinPointFieldData data = new JoinPointFieldData(
                                        fieldName, signature, joinPointType, uuid);

                                if (!getFieldJoinPoints.contains(data)) {
                                    getFieldJoinPoints.add(data);
                                }
                            }
                        }
                    }
                    // handle the setField instructions
                    else if (ins instanceof PUTFIELD) {
                        final FieldInstruction pfIns = (FieldInstruction)ins;

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

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

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

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

                                isMethodAdvised = true;

                                insertPreAdvice(
                                        il, ih, cg, fieldName,
                                        factory, joinPointType);

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

                                // store the join point field data
                                JoinPointFieldData data = new JoinPointFieldData(
                                        fieldName, signature, joinPointType, uuid);

                                if (!setFieldJoinPoints.contains(data)) {
                                    setFieldJoinPoints.add(data);
                                }
                            }
                        }
                    }
                    ih = ih.getNext();
                }

                if (isMethodAdvised) {
                    mg.setMaxStack();
                    methods[i] = mg.getMethod();
                    isClassAdvised = true;
                }
            }
            // create the set field join point member fields
            for (Iterator it2 = setFieldJoinPoints.iterator(); it2.hasNext();) {
                JoinPointFieldData data = (JoinPointFieldData)it2.next();
                addJoinPointMemberField(cpg, cg, data.getName(), data.getType());

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

                    methods[initIndex] = createJoinPointMemberField(
                            cpg, cg, methods[initIndex], factory,
                            data.getName(), data.getSignature(),
                            data.getType(), data.getUuid());

                }
            }
            // create the get field join point member fields
            for (Iterator it2 = getFieldJoinPoints.iterator(); it2.hasNext();) {
                JoinPointFieldData data = (JoinPointFieldData)it2.next();
                addJoinPointMemberField(cpg, cg, data.getName(), data.getType());

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

                    methods[initIndex] = createJoinPointMemberField(
                            cpg, cg, methods[initIndex], factory,
                            data.getName(), data.getSignature(),
                            data.getType(), data.getUuid());

                }
            }

            if (isClassAdvised) {
                context.markAsAdvised();
                cg.setMethods(methods);
            }
        }
    }
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.