Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.InstructionFactory$MethodObject


    // construct the method generator
        this.constPoolGen = constPoolGen;
       
    mGen = new MethodGen( m, javaClassName, constPoolGen );
    this.factory = new InstructionFactory( constPoolGen );
    this.iList = mGen.getInstructionList();
  }
View Full Code Here


    this.methodIdx = methodIdx;
    this.mGen = mGen;
    this.joinpointPattern = joinpointPattern;
    this.targets = targets;
    this.constPoolGen = constPoolGen;
    this.factory = new InstructionFactory( constPoolGen );
    this.iList = mGen.getInstructionList();
  }
View Full Code Here

               0, 1, fieldNameBuffer.substring(0,1).toUpperCase() );
       fieldNameBuffer.insert(0,"get");
      
       // build the getters instructions
       InstructionList iList = new InstructionList();
       InstructionFactory factory =
           new InstructionFactory(cGen);
      
       MethodGen mGen =
           new MethodGen(
               Constants.ACC_PRIVATE,
               interfaceType,
               Type.NO_ARGS,
               new String[] {},
               fieldNameBuffer.toString(),
               cGen.getClassName(),
               iList,
               constPoolGen );
      
       // a local variable
       iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );
       // load the member variable
       iList.append(
           factory.createFieldAccess(
               cGen.getClassName(),
               fieldName,
               interfaceType,
               Constants.GETFIELD  ) );
      
       // test for non null of the member variable          
       BranchInstruction bi =
           InstructionFactory.createBranchInstruction(
                   Constants.IFNONNULL, null );
      
       iList.append( bi );
      
       // create the delegate
       iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );
      
       iList.append( factory.createNew( implClass.getName() ) );
       iList.append( InstructionConstants.DUP );
       iList.append(
           factory.createInvoke(
               implClass.getName(),
               Constants.CONSTRUCTOR_NAME,
               Type.VOID, Type.NO_ARGS,
               Constants.INVOKESPECIAL ) );
      
       // use a factory if the implementation class is a factory
       if( MixInImplementationFactory.class.isAssignableFrom( implClass ) ) {

           iList.append(
               factory.createInvoke(
                       implClass.getName(),
                       "getInstance",
                       Type.OBJECT,
                       Type.NO_ARGS, Constants.INVOKEVIRTUAL ) );
          
           iList.append(
               factory.createCheckCast
                   new ObjectType( interfaceClass.getName() ) ) );
          
       }
       // store the created delgate object
       iList.append(
           factory.createFieldAccess(
               cGen.getClassName(),
               fieldName,
               interfaceType,
               Constants.PUTFIELD) );
      
       // load the delegate and return it
       InstructionHandle loadInstruction =
           iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );
      
       bi.setTarget( loadInstruction );
      
       iList.append(
               factory.createFieldAccess(
                   cGen.getClassName(),
                   fieldName,
                   interfaceType,
                   Constants.GETFIELD) );
      
View Full Code Here

       }
      
       // create instructions for delgation
       InstructionList iList = new InstructionList();
      
       InstructionFactory factory =
           new InstructionFactory(cGen);
      
       MethodGen mGen =
           new MethodGen(
               Constants.ACC_PUBLIC,
               Type.getReturnType( sig ),
               Type.getArgumentTypes( sig ),
               argNames,m.getName(),
               cGen.getClassName(),
               iList,
               constPoolGen );
      
       // get the delegate
       iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );

       iList.append(
           factory.createInvoke(
               cGen.getClassName(),
               getterMethodName,
               Type.getType( interfaceClass ),
               Type.NO_ARGS,
               Constants.INVOKESPECIAL ) );
      
       // load the argument on to the stack
       int argIdx =1;
       for( Type t : Type.getArgumentTypes( sig ) ) {
          
           iList.append( InstructionFactory.createLoad( t, argIdx++ ) );
          
           if( Reflection.isCat2Type( t ) ) {
               argIdx++;
           }
          
       }

       // do the delegation
       iList.append(
           factory.createInvoke(
               interfaceClass.getName(),
               m.getName(),
               Type.getReturnType( sig ),
               Type.getArgumentTypes( sig ),
               Constants.INVOKEINTERFACE ) );
View Full Code Here

    }

    protected void enhanceStaticInitializers()
    {
        InstructionList il = null;
        InstructionFactory factory = new InstructionFactory(newClass);
        Method clinit = null;
        InstructionList ilOriginal = null;
        MethodGen methodGen = null;

        MethodGen methodGenOriginal = null;
        {
            Method methods[] = newClass.getMethods();
            for (int i = 0; i < methods.length; i++)
            {
                if (methods[i].getName().equals(Constants.STATIC_INITIALIZER_NAME))
                {
                    clinit = methods[i];
                    methodGenOriginal = new MethodGen(clinit, className, constantPoolGen);
                    ilOriginal = methodGenOriginal.getInstructionList();
                    break;
                }
            }
        }

        il = new InstructionList();
        methodGen = new MethodGen(Constants.ACC_STATIC, Type.VOID, Type.NO_ARGS, null, Constants.STATIC_INITIALIZER_NAME,
            className, il, constantPoolGen);

        if (ilOriginal != null)
        {
            // remove return instruction and add to top of static initialization block
            il.append(ilOriginal);

            InstructionHandle h[] = il.getInstructionHandles();
            if ("return".equalsIgnoreCase(h[h.length - 1].getInstruction().getName()))
            {
                try
                {
                    il.delete(h[h.length - 1]);
                }
                catch (TargetLostException e)
                {
                    InstructionHandle[] targets = e.getTargets();
                    for (int i2 = 0; i2 < targets.length; i2++)
                    {
                        InstructionTargeter[] targeters = targets[i2].getTargeters();
                        for (int j = 0; j < targeters.length; j++)
                        {
                            targeters[j].updateTarget(targets[i2], h[j]);
                        }
                    }
                }
            }

        }

        if (addSerialVersionUID != 0)
        {
            int svUidIndex = constantPoolGen.addLong(addSerialVersionUID);
            il.append(new LDC2_W(svUidIndex));
            il.append(factory.createPutStatic(className, FN_serialVersionUID, Type.LONG));
        }
        // field init MN_jdoFieldNamesInit
        il.append(factory.createInvoke(className, MN_FieldNamesInitMethod, new ArrayType(Type.STRING, 1), Type.NO_ARGS,
            Constants.INVOKESTATIC));
        il.append(factory.createPutStatic(className, FN_FieldNames, new ArrayType(Type.STRING, 1)));

        // field init MN_jdoFieldTypesInit
        il.append(factory.createInvoke(className, MN_FieldTypesInitMethod, new ArrayType(OT_CLASS, 1), Type.NO_ARGS, Constants.INVOKESTATIC));
        il.append(factory.createPutStatic(className, FN_FieldTypes, new ArrayType(OT_CLASS, 1)));

        // field init MN_FieldFlagsInitMethod
        il.append(factory.createInvoke(className, MN_FieldFlagsInitMethod, new ArrayType(Type.BYTE, 1), Type.NO_ARGS,
            Constants.INVOKESTATIC));
        il.append(factory.createPutStatic(className, FN_FieldFlags, new ArrayType(Type.BYTE, 1)));

        // field init MN_JdoFieldCounInitMethod
        il.append(factory.createInvoke(className, MN_JdoGetInheritedFieldCount, Type.INT, Type.NO_ARGS, Constants.INVOKESTATIC));
        il.append(factory.createPutStatic(className, FN_JdoInheritedFieldCount, Type.INT));

        // ___jdo$PersistenceCapableSuperclass =
        // ___jdo$PersistenceCapableSuperclassInit();
        il.append(factory.createInvoke(className, MN_JdoPersistenceCapableSuperclassInit, OT_CLASS, Type.NO_ARGS, Constants.INVOKESTATIC));
        il.append(factory.createPutStatic(className, FN_PersistenceCapableSuperclass, OT_CLASS));

        // class init
        /*
         * JDOImplHelper.registerClass( ___jdo$loadClass("fullclassname"),
         * ___jdo$fieldNames, ___jdo$fieldTypes, ___jdo$FieldFlags,
         * ___jdo$PersistenceCapableSuperclass, new ClassConstrutor());
         */
        il.append(new LDC(constantPoolGen.addString(className)));
        il.append(factory.createInvoke(className, MN_jdoLoadClass, OT_CLASS, new Type[]{Type.STRING}, Constants.INVOKESTATIC));
        il.append(factory.createGetStatic(className, FN_FieldNames, new ArrayType(Type.STRING, 1)));
        il.append(factory.createGetStatic(className, FN_FieldTypes, new ArrayType(OT_CLASS, 1)));
        il.append(factory.createGetStatic(className, FN_FieldFlags, new ArrayType(Type.BYTE, 1)));
        il.append(factory.createGetStatic(className, FN_PersistenceCapableSuperclass, OT_CLASS));

        if (((BCELClassMetaData) cmd).getClassGen().isAbstract())
        {
            // null
            il.append(InstructionConstants.ACONST_NULL);
        }
        else
        {
            // new ClassConstrutor()
            il.append(factory.createNew(new ObjectType(className)));
            il.append(InstructionConstants.DUP);
            il.append(factory.createInvoke(className, Constants.CONSTRUCTOR_NAME, Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
        }
        il.append(factory.createInvoke(CN_JDOImplHelper, "registerClass", Type.VOID, new Type[]{OT_CLASS, new ArrayType(Type.STRING, 1),
                new ArrayType(OT_CLASS, 1), new ArrayType(Type.BYTE, 1), OT_CLASS, OT_PersistenceCapable}, Constants.INVOKESTATIC));

        staticInitializerAppend(factory, il);
        il.append(InstructionConstants.RETURN);

View Full Code Here

        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;
View Full Code Here

    /**
     * Method to initialise the class method.
     */
    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

        // Start the class
        this.classGen = new ClassGen(fullClassName, fullSuperclassName, className + ".java", ACC_PUBLIC | ACC_SUPER,
            (String[])interfaces.toArray(new String[interfaces.size()]));
        this.constantPool = classGen.getConstantPool();
        this.factory = new InstructionFactory(classGen, constantPool);

        // Create fields, default ctor, and methods
        createPropertyFields();
        createDefaultConstructor();
        createPropertyMethods();
View Full Code Here

        this.methodGen.setInstructionList(new InstructionList());
        this.methodGen.removeLocalVariables();
        this.methodGen.removeLineNumbers();
        this.methodGen.removeExceptions();
        this.il = methodGen.getInstructionList();
        this.factory = new InstructionFactory(newClass);
        this.className = className;
        this.cmd = (BCELClassMetaData)enhancer.getClassMetaData();
        this.classGen = newClass;
        this.fieldConfig = fieldConfig;
        this.constantPoolGen = constantPoolGen;
View Full Code Here

        this.methodGen.setInstructionList(new InstructionList());
        this.methodGen.removeLocalVariables();
        this.methodGen.removeLineNumbers();
        this.methodGen.removeExceptions();
        this.il = methodGen.getInstructionList();
        this.factory = new InstructionFactory(newClass);
        this.className = className;
        this.cmd = (BCELClassMetaData)enhancer.getClassMetaData();
        this.classGen = newClass;
        this.fieldConfig = fieldConfig;
        this.constantPoolGen = constantPoolGen;
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.InstructionFactory$MethodObject

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.