Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


            if (classAsStream == null) return false;
            ClassParser classParser = new ClassParser(classAsStream, className);
            m_javaClass = classParser.parse();

            m_constantPoolGen = new ConstantPoolGen(m_javaClass.getConstantPool());
            m_classGen = new ClassGen(m_javaClass);
        }
        catch (Exception e) {
            throw new WrappedRuntimeException(e);
        }
        return true;
View Full Code Here


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

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

        // filter caller classes
        if (classFilter(classMetaData, cg)) {
            return;
        }
        final Method[] methods = cg.getMethods();

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

        final Set callerSideJoinPoints = new HashSet();

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

        for (int i = 0; i < methods.length; i++) {

            // filter caller methods
            if (methodFilterCaller(methods[i])) {
                continue;
            }

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

            final InstructionList il = mg.getInstructionList();
            if (il == null) {
                continue;
            }
            InstructionHandle ih = il.getStart();
            isMethodChanged = false;
            // search for all InvokeInstruction instructions and
            // inserts the call side pointcuts
            while (ih != null) {
                final Instruction ins = ih.getInstruction();

                if (ins instanceof INVOKESPECIAL ||
                        ins instanceof INVOKESTATIC ||
                        ins instanceof INVOKEVIRTUAL) {

                    final InvokeInstruction invokeInstruction = (InvokeInstruction)ins;

                    // get the callee method name, signature and class name
                    final String calleeMethodName = invokeInstruction.getName(cpg);
                    final String calleeClassName = invokeInstruction.getClassName(cpg);
                    final String calleeMethodSignature = invokeInstruction.getSignature(cpg);

                    // filter callee classes
                    if (!m_definition.inIncludePackage(calleeClassName)) {
                        ih = ih.getNext();
                        continue;
                    }
                    // filter callee methods
                    if (methodFilterCallee(calleeMethodName)) {
                        ih = ih.getNext();
                        continue;
                    }

                    // create the class meta-data
                    ClassMetaData calleeSideClassMetaData;
                    try {
                        JavaClass javaClass = context.getRepository().loadClass(calleeClassName);
                        calleeSideClassMetaData = BcelMetaDataMaker.createClassMetaData(javaClass);
                    }
                    catch (ClassNotFoundException e) {
                        throw new WrappedRuntimeException(e);
                    }

                    // create the method meta-data
                    MethodMetaData calleeSideMethodMetaData = BcelMetaDataMaker.createMethodMetaData(
                            invokeInstruction, cpg
                    );

                    // is this a caller side method pointcut?
                    if (m_definition.isPickedOutByCallPointcut(calleeSideClassMetaData, calleeSideMethodMetaData)) {

                        // get the caller method name and signature
                        Method method = mg.getMethod();
                        String callerMethodName = method.getName();
                        String callerMethodSignature = method.getSignature();

                        final Type joinPointType = TransformationUtil.CALLER_SIDE_JOIN_POINT_TYPE;

                        // take care of identification of overloaded methods
                        // by inserting a sequence number
                        if (methodSequences.containsKey(calleeMethodName)) {
                            int sequence = ((Integer)methodSequences.get(calleeMethodName)).intValue();

                            methodSequences.remove(calleeMethodName);
                            sequence++;
                            methodSequences.put(calleeMethodName, new Integer(sequence));
                        }
                        else {
                            methodSequences.put(calleeMethodName, new Integer(1));
                        }
                        int methodSequence = ((Integer)methodSequences.get(calleeMethodName)).intValue();

                        isClassAdvised = true;
                        isMethodChanged = true;

                        insertPreAdvice(
                                il, ih, cg,
                                calleeMethodName,
                                methodSequence,
                                factory,
                                joinPointType
                        );

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

                        StringBuffer key = new StringBuffer();
                        key.append(className);
                        key.append(TransformationUtil.DELIMITER);
                        key.append(calleeMethodName);
                        key.append(TransformationUtil.DELIMITER);
                        key.append(methodSequence);

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

                            addStaticJoinPointField(
                                    cpg, cg, calleeMethodName,
                                    methodSequence, joinPointType
                            );

                            if (hasClInitMethod) {
                                methods[clinitIndex] = createStaticJoinPointField(
                                        cpg, cg,
                                        methods[clinitIndex],
                                        callerMethodName,
                                        calleeClassName,
                                        calleeMethodName,
                                        methodSequence,
                                        callerMethodSignature,
                                        calleeMethodSignature,
                                        factory,
                                        joinPointType,
                                        m_definition.getUuid()
                                );
                            }
                            else if (clInitMethod == null) {
                                clInitMethod = createClInitMethodWithStaticJoinPointField(
                                        cpg, cg,
                                        callerMethodName,
                                        calleeClassName,
                                        calleeMethodName,
                                        methodSequence,
                                        callerMethodSignature,
                                        calleeMethodSignature,
                                        factory,
                                        joinPointType,
                                        m_definition.getUuid()
                                );
                            }
                            else {
                                clInitMethod = createStaticJoinPointField(
                                        cpg, cg,
                                        clInitMethod,
                                        callerMethodName,
                                        calleeClassName,
                                        calleeMethodName,
                                        methodSequence,
                                        callerMethodSignature,
                                        calleeMethodSignature,
                                        factory,
                                        joinPointType,
                                        m_definition.getUuid()
                                );
                            }
                        }
                    }
                }
                ih = ih.getNext();
            }

            if (isMethodChanged) {
                mg.setMaxStack();
                methods[i] = mg.getMethod();
            }
        }

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

                newMethods.add(clInitMethod);
            }
            else {
                addStaticClassField(cpg, cg);
                methods[clinitIndex] = createStaticClassField(
                        cpg, cg,
                        methods[clinitIndex],
                        factory
                );
            }
        //}//TODO CHECK THIS
        // 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);
        }
        }//TODO CHECK THIS
    }
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(cg, classMetaData, definition)) {
                return;
            }
            //todo: what is this cache for ? not compliant for 0.10
            //if (m_transformed.contains(cg.getClassName())) {
            //    return;
            //}
            //m_transformed.add(cg.getClassName());

            ConstantPoolGen cpg = cg.getConstantPool();
            InstructionFactory factory = new InstructionFactory(cg);

            if (definition.isAttribDef()) {
                org.codehaus.aspectwerkz.attribdef.transform.IntroductionTransformer.addMethodIntroductions(
                        definition, context, classMetaData, cg, cpg, factory, this
View Full Code Here

     *
     * @param context the transformation context
     * @param klass the class weaved
     */
    public void transformInterface(final Context context, final Klass klass) {
        final ClassGen cg = klass.getClassGen();
        if (classFilter(cg)) {
            return;
        }
        if (!TransformationUtil.isSerializable(context, cg)) {
            return;
View Full Code Here

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

        m_definition.loadAspects(context.getLoader());

        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());
        context.markAsAdvised();

        addMetaDataEnhancableInterface(cg, cpg);
        addMapField(cg);
        addMetaDataGetterMethod(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);
        context.markAsAdvised();
    }
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 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++) {
                MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);
                if (methodFilter(definition, classMetaData, methodMetaData, methods[i])) {
                    continue;
                }
                methodLookupList.add(methods[i]);
            }

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

            final Map methodSequences = new HashMap();
            final List proxyMethods = new ArrayList();
            boolean isClassAdvised = false;
            for (int i = 0; i < methods.length; i++) {
                MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);
                // filter the methods
                if (methodFilter(definition, classMetaData, methodMetaData, methods[i]) ||
                        methods[i].isStatic()) {
                    continue;
                }

                isClassAdvised = true;
                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
                final String controllerClassName = definition.getJoinPointController(
                        classMetaData,
                        methodMetaData
                );

                // advise all the constructors
                for (Iterator it2 = initIndexes.iterator(); it2.hasNext();) {
                    final int initIndex = ((Integer)it2.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(),
                        definition.getUuid(),
                        controllerClassName
                ));

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

                mg.setMaxStack();
            }

            if (isClassAdvised) {
                context.markAsAdvised();

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

                // add the proxy methods
                for (Iterator it2 = proxyMethods.iterator(); it2.hasNext();) {
                    cg.addMethod((Method)it2.next());
                }
            }
        }
    }
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();

            ClassMetaData classMetaData = BcelMetaDataMaker.createClassMetaData(context.getJavaClass(cg));

            if (classFilter(cg, classMetaData, definition)) {
                return;
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();

            addReadObjectMethod(cg, cpg, factory);
        }
    }
View Full Code Here

    return jc;
  }

  private void haikuReleaseLock(Method method) {
    if (theClassGen==null) {
      theClassGen = new ClassGen(jc);
      theCPool = theClassGen.getConstantPool();
    }

    Verbose.println("  PREPROCESSING haikuReleaseLock: "+jc.getClassName()+"."+method.getName()+method.getSignature());
    MethodGen genMethod = new MethodGen(method, theClassGen.getClassName(), theCPool);
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.