Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.Method


            if ( im.name.equals( "<clinit>" ) )
            {
                continue;
            }

            final Method generatedMethod =
                createMethodWrapper( im );

            gmList.add( generatedMethod );
        }
View Full Code Here


     *            extract
     * @return   The extracted {@link org.apache.bcel.classfile.Method Method}
     */
    private Method extractMethod( final MethodGen mg )
    {
        final Method m = mg.getMethod();
        m_instructionList.dispose();
        return m;
    }
View Full Code Here

                            serializedAttribute.length,
                            serializedAttribute,
                            m_constantPoolGen.getConstantPool()
                    );
                    methodGen.addAttribute(attr);
                    Method newMethod = methodGen.getMethod();
                    m_classGen.replaceMethod(classfileMethod[i], newMethod);
                }
            }
        }
    }
View Full Code Here

        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;

        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();
            // 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.inTransformationScope(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.isCallerSideMethod(
                            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));
                        }
                        final int methodSequence =
                                ((Integer)methodSequences.get(calleeMethodName)).intValue();

                        isClassAdvised = 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();
            }

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

        if (isClassAdvised) {
            // 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
                );
            }
        }
        // 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

            // static intializer in different lists
            List constructorList = new ArrayList();
            List regularMethodList = new ArrayList();
            if (methods != null) {
                for (int i = 0; i < methods.length; i++) {
                    Method method = methods[i];
                    if (method.getName().equals("<clinit>")) {
                        // handle static intiailization.
                        out.writeUTF("<clinit>");
                        out.writeInt(Constants.ACC_STATIC);
                        out.writeUTF("()V");
                    }
                    else if (method.getName().equals("<init>")) {
                        constructorList.add(method);
                    }
                    else {
                        regularMethodList.add(method);
                    }
                }
            }

            // handle constructors.
            Object[] constructors = constructorList.toArray();
            Arrays.sort(constructors, new Comparator() {
                public int compare(Object o1, Object o2) {
                    try {
                        Method c1 = (Method)o1;
                        Method c2 = (Method)o2;
                        return c1.getSignature().compareTo(c2.getSignature());
                    }
                    catch (Exception e) {
                        throw new WrappedRuntimeException(e);
                    }
                }
            });
            for (int i = 0; i < constructors.length; i++) {
                Method constructor = (Method)constructors[i];
                int mods = constructor.getModifiers();
                if ((mods & Constants.ACC_PRIVATE) == 0) {
                    out.writeUTF("<init>");
                    out.writeInt(mods);
                    out.writeUTF(constructor.getSignature().replace('/', '.'));
                }
            }

            // handle regular methods.
            Object[] regularMethods = regularMethodList.toArray();
            Arrays.sort(regularMethods, new Comparator() {
                public int compare(Object o1, Object o2) {
                    try {
                        Method m1 = (Method)o1;
                        Method m2 = (Method)o2;
                        int value = m1.getName().compareTo(m2.getName());
                        if (value == 0) {
                            value = m1.getSignature().compareTo(m2.getSignature());
                        }
                        return value;
                    }
                    catch (Exception e) {
                        throw new WrappedRuntimeException(e);
                    }
                }
            });
            for (int i = 0; i < regularMethods.length; i++) {
                Method method = (Method)regularMethods[i];
                int mods = method.getModifiers();
                if ((mods & Constants.ACC_PRIVATE) == 0) {
                    out.writeUTF(method.getName());
                    out.writeInt(mods);
                    out.writeUTF(method.getSignature().replace('/', '.'));
                }
            }

            // calculate hash.
            out.flush();
View Full Code Here

        // methods
        List methodList = new ArrayList();
        Method[] methods = javaClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            methodList.add(createMethodMetaData(method));
        }
        classMetaData.setMethods(methodList);

        // fields
View Full Code Here

        }
        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

        // 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

                        className.equals(superClassName)) {
                    JavaClass superClass = context.getSuperClass(cg);
                    Method[] methods = superClass.getMethods();
                    MethodMetaData methodMetaData = null;
                    for (int i = 0; i < methods.length; i++) {
                        Method method = methods[i];
                        if (method.getSignature().equals(signature) && method.getName().equals(methodName)) {
                            methodMetaData = BcelMetaDataMaker.createMethodMetaData(method);
                            break;
                        }
                    }
                    if (methodMetaData == null) {
View Full Code Here

     * Searches the given class for the native methods.
     *
     * @param clss - a searched class.
     */
    private void searchForMethods(JavaClass clss) {
        Method method[] = clss.getMethods();
        for (int i = 0; i < method.length; i++) {
            Method m = method[i];
            if (m.isNative()) {
                boolean overloaded = false;
                // Check if the current method is overloaded.
                for (int j = 0; j < method.length; j++) {
                    Method mj = method[j];
                    if (mj != m
                            && mj.isNative()
                            && mj.getName().equals(m.getName())) {
                        overloaded = true;
                    }
                }
                methods.addElement(new ClazzMethod(this, m, overloaded));
            }
View Full Code Here

TOP

Related Classes of org.apache.bcel.classfile.Method

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.