Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.MethodGen


        {
            // Skip synthetic method
            return;
        }

        MethodGen methodGen = new MethodGen(m, className, constantPoolGen);
        InstructionList il = methodGen.getInstructionList();
        if ((il == null) || (il.size() == 0))
        {
            return;
        }
        InstructionFactory factory = new InstructionFactory(newClass);
        boolean isCloneMethod = ("clone".equals(m.getName()) && ((m.getArgumentTypes() == null) || (m.getArgumentTypes().length == 0)));
        boolean change = false;
        InstructionHandle ih = il.getStart();
        while (ih != null)
        {
            Instruction i = ih.getInstruction();

            if ((i instanceof GETFIELD) || (i instanceof PUTFIELD))
            {
                Field f;
                FieldInstruction field = (FieldInstruction) i;
                Constant c = m.getConstantPool().getConstant(field.getIndex());
                ConstantFieldref fieldRef = (ConstantFieldref) c;

                ConstantClass cclass = (ConstantClass) m.getConstantPool().getConstant(fieldRef.getClassIndex());
                ConstantUtf8 utfClassName = (ConstantUtf8) m.getConstantPool().getConstant(cclass.getNameIndex());
                String utfClassNameString = StringUtils.replaceAll(utfClassName.getBytes().toString(), "/", ".");
                JavaClass fieldJavaClass = null;
                try
                {
                    fieldJavaClass = Repository.lookupClass(utfClassNameString);
                }
                catch (Throwable ex)
                {
                    // catch Throwable, so it is compatible with latest BCEL changes in methods signature.
                    // It nows raises ClassNotFoundException. In order to be able to compible this code
                    // with bcel-5.1 or bcel-5.1+, we catch as throwable
                    JPOXLogger.ENHANCER.error(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString, ex));
                    throw new JDOFatalException(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString, ex));
                }
                if (fieldJavaClass == null)
                {
                    throw new JDOFatalException(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString,
                        new NullPointerException()));
                }

                f = BCELUtils.getFieldByName(field.getName(constantPoolGen), fieldJavaClass);
                if (f == null)
                {
                    String message = LOCALISER.msg("Enhancer.FieldIsNull", className, m.getName(), field.getName(constantPoolGen));
                    JPOXLogger.ENHANCER.error(message);
                    throw new NullPointerException(message);
                }

                ClassGen cg = BCELUtils.getClassByFieldByName(field.getName(constantPoolGen), fieldJavaClass);
                BCELFieldPropertyMetaData fieldConfig = null;
                BCELClassMetaData jdoConfigClass = ((BCELClassMetaData) cmd);

                // the accessing class is not this
                if (!cg.getClassName().equals(newClass.getClassName()))
                {
                    jdoConfigClass = (BCELClassMetaData) cmd.getPackageMetaData().getFileMetaData().getMetaDataManager().getMetaDataForClass(
                        cg.getClassName(), clr);
                }
                if( jdoConfigClass != null )
                {
                    AbstractMemberMetaData apmd = jdoConfigClass.findField(f);

                    if (apmd == null)
                    {
                        //check if a property(getter,setter) exists with this field name
                        if( jdoConfigClass.findProperty(f)==null )
                        {
                            //no fields netiher properties in the class, so something is wrong
                            String message = LOCALISER.msg("Enhancer.FieldConfigIsNullError", className + "." + f.getName());
                            JPOXLogger.ENHANCER.fatal(message);
                            throw new RuntimeException(message);
                        }
                    }
                    if( apmd != null && apmd.getPersistenceModifier() != FieldPersistenceModifier.NONE )
                    {
                        // do nothing
                    }
                    if (!isFieldAccessInPersistenceCapableClass(ih, m.getConstantPool()))
                    {
                        // do nothing
                    }
                    else if (fieldConfig != null && fieldConfig.getJdoFieldFlag() == 0)
                    {
                        // do nothing
                    }
                    else if (f.isStatic() || f.isFinal())
                    {
                        // do nothing
                    }
                    else if (BCELUtils.isSynthetic(f))
                    {
                        // do nothing
                    }
                    else
                    {
                        if (isDebugEnabled)
                        {
                            JPOXLogger.ENHANCER.debug(LOCALISER.msg("Enhancer.EnhanceOriginalMethod",
                                className + "." + m.getName(), f.getName()));
                        }
                        if( apmd != null && !apmd.isProperty() && apmd.getPersistenceModifier() != FieldPersistenceModifier.NONE)
                        {
                            //properties do not use jdoXXX methods
                            if (i instanceof GETFIELD)
                            {
                                ih.setInstruction(factory.createInvoke(cg.getClassName(), "jdo" + BCELUtils.getGetterName(f),
                                    field.getType(constantPoolGen), new Type[]{new ObjectType(cg.getClassName())}, Constants.INVOKESTATIC));
                            }
                            else
                            {
                                ih.setInstruction(factory.createInvoke(cg.getClassName(), "jdo" + BCELUtils.getSetterName(f),
                                    Type.VOID, new Type[]{new ObjectType(cg.getClassName()), field.getType(constantPoolGen)},
                                    Constants.INVOKESTATIC));
                            }
                            change = true;
                        }
                    }
                }
            }
            else if (isCloneMethod && (i instanceof INVOKESPECIAL))
            {
                // If this is the clone method and is the root persistence capable, call jdoSuperClone
                INVOKESPECIAL is = (INVOKESPECIAL) i;
                if ((cmd.getPersistenceCapableSuperclass() == null) && ("clone".equals(is.getMethodName(constantPoolGen))) &&
                    ("()Ljava/lang/Object;".equals(is.getSignature(constantPoolGen))))
                {
                    ih.setInstruction(factory.createInvoke(className, ClassEnhancer.MN_JdoSuperClone, Type.OBJECT, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
                    if (isDebugEnabled)
                    {
                        JPOXLogger.ENHANCER.debug(LOCALISER.msg("Enhancer.EnhanceOriginalMethod", className + "." + m.getName(), "super.clone()"));
                    }
                    change = true;
                }
            }

            ih = ih.getNext();
        }
        if (change)
        {
            methodGen.setMaxLocals();
            methodGen.setMaxStack();
            newClass.replaceMethod(m, methodGen.getMethod());
        }
    }
View Full Code Here


        if (apmd == null || !apmd.isProperty() || apmd.getPersistenceModifier() == FieldPersistenceModifier.NONE)
        {
            return;
        }

        MethodGen methodGen = new MethodGen(m, className, constantPoolGen);
        methodGen.setName(newMethodName);
        methodGen.setMaxLocals();
        methodGen.setMaxStack();
        newClass.addMethod(methodGen.getMethod());
    }
View Full Code Here

    public void initialise()
    {
        factory = new InstructionFactory(classGen);
        il = new InstructionList();
        this.methodGen =
            new MethodGen(access, (Type)returnType, (Type[])argTypes, argNames, methodName, className, il,
                classGen.getConstantPool());
    }
View Full Code Here

     * Create a default constructor, assuming that there is no persistent superclass.
     */
    protected void createDefaultConstructor()
    {
        InstructionList il = new InstructionList();
        MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[]{}, "<init>",
            this.fullClassName, il, constantPool);

        il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
        il.append(factory.createInvoke(fullSuperclassName, "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
        il.append(InstructionFactory.createReturn(Type.VOID));
        method.setMaxStack();
        method.setMaxLocals();
        classGen.addMethod(method.getMethod());
        il.dispose();
    }
View Full Code Here

        boolean isBoolean = mmd.getTypeName().equals("boolean");
        String getterName = ClassUtils.getJavaBeanGetterName(mmd.getName(), isBoolean);
        String typeName = mmd.getTypeName();
        Type objectType = BCELUtils.getBCELTypeFromJavaType(typeName);
        InstructionList il = new InstructionList();
        MethodGen method = new MethodGen(ACC_PUBLIC, objectType, Type.NO_ARGS, new String[]{},
            getterName, this.fullClassName, il, constantPool);

        il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
        il.append(factory.createFieldAccess(this.fullClassName, mmd.getName(), objectType, Constants.GETFIELD));
        il.append(InstructionFactory.createReturn(objectType));
        method.setMaxStack();
        method.setMaxLocals();
        classGen.addMethod(method.getMethod());
        il.dispose();
    }
View Full Code Here

    {
        String setterName = ClassUtils.getJavaBeanSetterName(mmd.getName());
        String typeName = mmd.getTypeName();
        Type objectType = BCELUtils.getBCELTypeFromJavaType(typeName);
        InstructionList il = new InstructionList();
        MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[]{objectType}, new String[]{"arg0"},
            setterName, this.fullClassName, il, constantPool);

        il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
        il.append(InstructionFactory.createLoad(objectType, 1));
        il.append(factory.createFieldAccess(this.fullClassName, mmd.getName(), objectType, Constants.PUTFIELD));
        il.append(InstructionFactory.createReturn(Type.VOID));
        method.setMaxStack();
        method.setMaxLocals();
        classGen.addMethod(method.getMethod());
        il.dispose();
    }
View Full Code Here

        BCELFieldPropertyMetaData fieldConfig,
        BCELClassEnhancer enhancer)
    {
        this.method = m;
        this.fieldConfig = fieldConfig;
        this.methodGen = new MethodGen(m, className, constantPoolGen);
        this.methodGen.setInstructionList(new InstructionList());
        this.methodGen.removeLocalVariables();
        this.methodGen.removeLineNumbers();
        this.methodGen.removeExceptions();
        this.il = methodGen.getInstructionList();
View Full Code Here

        BCELFieldPropertyMetaData fieldConfig,
        BCELClassEnhancer enhancer)
    {
        this.method = m;
        this.fieldConfig = fieldConfig;
        this.methodGen = new MethodGen(m, className, constantPoolGen);
        this.methodGen.setInstructionList(new InstructionList());
        this.methodGen.removeLocalVariables();
        this.methodGen.removeLineNumbers();
        this.methodGen.removeExceptions();
        this.il = methodGen.getInstructionList();
View Full Code Here

        ExecutionVisitor ev = new ExecutionVisitor();
        ev.setConstantPoolGen(cp);

        Method[] methods = clazz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            MethodGen method = new MethodGen(methods[i], clazz.getClassName(), cp);

            currentMethodStatic = methods[i].isStatic();
            if (isValid(method)) {
                // analyse the code of the method to create the frame
                // information about every instruction
                ControlFlowGraph cfg = new ControlFlowGraph(method);
                analyse(clazz, method, cfg, icv, ev);
                // add intercepting code
                rewrite(method, cfg);
                // make last optional check for consistency
                clazz.replaceMethod(methods[i], method.getMethod());
            }
        }
        clazz.addInterface(CONTINUATIONCAPABLE_CLASS);
        return clazz.getJavaClass().getBytes();
    }
View Full Code Here

            FieldGen handlerField, FieldGen initializerField) {
        String stubClassName = stubClass.getClassName();
        ConstantPoolGen cp = stubClass.getConstantPool();
        InstructionList il = new InstructionList();

        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
                Type.NO_ARGS, null, "<init>", stubClassName, il, cp);

        InstructionFactory fac = new InstructionFactory(stubClass, cp);

        // call super-constructor
        il.append(InstructionFactory.createThis());
        il.append(fac.createInvoke(stubClass.getSuperclassName(), "<init>",
                Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));

        // push "this"
        il.append(InstructionFactory.createThis());

        // get static initializer
        il.append(fac.createGetStatic(stubClassName,
                initializerField.getName(), initializerField.getType()));

        emitInvoke(il, fac, getStubHandlerRef);

        // checkCast
        il.append(fac.createCast(Type.OBJECT, handlerField.getType()));

        // put handlerField
        il.append(new PUTFIELD(cp.addFieldref(stubClassName, handlerField
                .getName(), handlerField.getSignature())));

        // return
        il.append(InstructionConstants.RETURN);

        // compute stack and locals...
        mg.setMaxStack();
        mg.setMaxLocals();

        stubClass.addMethod(mg.getMethod());
    }
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.MethodGen

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.