Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ClassGen


        createClasses();
    }

    private void createClasses ()
    {
        _baseClassGen = new ClassGen(_javaClass.copy());
        _derivedClassGen = new ClassGen(_javaClass.copy());

        if (_baseClassGen.isFinal()) {
            _baseClassGen.isFinal(false);
            _derivedClassGen.isFinal(false);
            try {
View Full Code Here


     **/

    public ClassFabricator(String className, String parentClassName, int accessFlags)
    {
        _classGen =
            new ClassGen(
                className,
                parentClassName,
                "<synthetic>",
                accessFlags | Constants.ACC_SUPER,
                null);
View Full Code Here

     *
     * @return byte code
     */
    private static byte[] createSubClassByteCode(final String className)
    {
        ClassGen classGen = new ClassGen(className,
                AbstractLDAPSSLSocketFactory.class.getName(),
                "<generated>",
                ACC_PUBLIC | ACC_SUPER,
                null);
        ConstantPoolGen constantPoolGen = classGen.getConstantPool();
        InstructionFactory factory = new InstructionFactory(classGen);

        createSslContextStaticField(classGen, constantPoolGen);
        createGetDefaultStaticMethod(classGen, constantPoolGen, factory);

        classGen.addEmptyConstructor(Constants.ACC_PROTECTED);

        JavaClass javaClass = classGen.getJavaClass();
        ByteArrayOutputStream out = null;
        try
        {
            out = new ByteArrayOutputStream();
            javaClass.dump(out);
View Full Code Here

private InstructionFactory _factory;
  private ConstantPoolGen    _cp;
  private ClassGen           _cg;

  public ComplexObjectCreator(String objectType) {
    _cg = new ClassGen(objectType, "java.lang.Object", "ComplexObject.java", ACC_PUBLIC | ACC_SUPER, new String[] { "com.betfair.cougar.api.Result" });

    _cp = _cg.getConstantPool();
    _factory = new InstructionFactory(_cg, _cp);
    this.objectType = objectType;
  }
View Full Code Here

    }

    private static void nullBCELAdapt(final byte[] b) throws IOException {
        JavaClass jc = new ClassParser(new ByteArrayInputStream(b),
                "class-name").parse();
        ClassGen cg = new ClassGen(jc);
        ConstantPoolGen cp = cg.getConstantPool();
        Method[] ms = cg.getMethods();
        for (int k = 0; k < ms.length; ++k) {
            MethodGen mg = new MethodGen(ms[k], cg.getClassName(), cp);
            boolean lv = ms[k].getLocalVariableTable() == null;
            boolean ln = ms[k].getLineNumberTable() == null;
            if (lv) {
                mg.removeLocalVariables();
            }
            if (ln) {
                mg.removeLineNumbers();
            }
            mg.stripAttributes(skipDebug);
            InstructionList il = mg.getInstructionList();
            if (il != null) {
                InstructionHandle ih = il.getStart();
                while (ih != null) {
                    ih = ih.getNext();
                }
                if (compute) {
                    mg.setMaxStack();
                    mg.setMaxLocals();
                }
                if (computeFrames) {
                    ModifiedPass3bVerifier verif;
                    verif = new ModifiedPass3bVerifier(jc, k);
                    verif.do_verify();
                }
            }
            cg.replaceMethod(ms[k], mg.getMethod());
        }
        cg.getJavaClass().getBytes();
    }
View Full Code Here

        Class generatedClass;
        synchronized ( Type.class )
        {
            // Create BCEL class generator
            m_classGenerator =
                new ClassGen(
                    wrapperClassName,
                    WRAPPER_SUPERCLASS_NAME,
                    null,
                    Constants.ACC_FINAL
                |Constants.ACC_PUBLIC
View Full Code Here

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

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

        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

     *
     * @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());

        final ConstantPoolGen cpg = cg.getConstantPool();
        final InstructionFactory factory = new InstructionFactory(cg);
        addIntroductions(context, cg, cpg, factory);
    }
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

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.