Package org.objectweb.asm

Examples of org.objectweb.asm.CodeVisitor


            logger.log(BasicLevel.DEBUG, "Add the method '" + methodName
                    + "' in the class '" + className + "'");
        }
        logger.log(BasicLevel.DEBUG, "Add to the class " + className
                + " the method " + methodName);
        CodeVisitor _cv = this.cv.visitMethod(
                Constants.ACC_PUBLIC,
                methodName,
                "()" + USER_ID_DESC,
                null,
                null);

        // GENERATE: Titi t = new Titi();
        // GENERATE: Titi is the class of the user identifier
        logger.log(BasicLevel.DEBUG, "add code: " + uicn + " t = new " + uicn + "();");
        _cv.visitTypeInsn(Constants.NEW, uicn);
        _cv.visitInsn(Constants.DUP);
        _cv.visitMethodInsn(Constants.INVOKESPECIAL, uicn, "<init>", "()V");
        _cv.visitVarInsn(Constants.ASTORE, 1);

        //t.f1 = this.f1
        boolean hasLongOrDouble = false;
        SpeedoClass current = speedoClass;
        while(current != null) {
          for(Iterator it = current.fields.values().iterator(); it.hasNext();) {
              SpeedoField sp = (SpeedoField) it.next();
              if (sp.primaryKey) {
                  hasLongOrDouble |= sp.type.charAt(0) == 'D'
                        || sp.type.charAt(0) == 'J';
                  logger.log(BasicLevel.DEBUG, "add code: this." + sp.name + " = t." + sp.name);
                  _cv.visitVarInsn(Constants.ALOAD, 1); //Can be a long|double value
                  _cv.visitVarInsn(Constants.ALOAD, 0);
                  _cv.visitFieldInsn(Constants.GETFIELD, className, sp.name, sp.type);
                  _cv.visitFieldInsn(Constants.PUTFIELD, uicn, sp.name, sp.type);
              }
          }
      SpeedoClass tmp = current.getSuper();
      current = (tmp == current ? null : tmp);
    }

        // GENERATE: t.speedoSetPersistentClassName(this.getPType().getJormName());
        logger.log(BasicLevel.DEBUG, "add code: t.speedoSetPersistentClassName(this.getPType().getJormName());");
        _cv.visitVarInsn(Constants.ALOAD, 1);
        _cv.visitVarInsn(Constants.ALOAD, 0);
        _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
                className, "getPType", "()" + PTYPE_DESC);
        _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
                PTYPE_CLASS_NAME, "getJormName", "()Ljava/lang/String;");
        _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
                uicn, "speedoSetPersistentClassName", "(Ljava/lang/String;)V");
        logger.log(BasicLevel.DEBUG, "add code: return t;");
        _cv.visitVarInsn(Constants.ALOAD, 1);
        _cv.visitInsn(Constants.ARETURN);
        _cv.visitMaxs((hasLongOrDouble ? 3 : 2), 2);
    }
View Full Code Here


    public CodeVisitor visitMethod(final int access,
                                   final String name,
                                   final String desc,
                                   final String[] exceptions,
                                   final Attribute attrs) {
        CodeVisitor mv = this.cv.visitMethod(access, name, desc, exceptions, attrs);
        return new PersistenceAwareCodeModifier(mv);
    }
View Full Code Here

            SpeedoField sp = (SpeedoField) it.next();
            if (sp.primaryKey) {
                String methodName = "pnGet" + upperFL(sp.name);
                logger.log(BasicLevel.DEBUG, "Add to the class " + className
                        + " the method " + methodName);
                CodeVisitor _cv = this.cv.visitMethod(
                        Constants.ACC_PUBLIC,
                        methodName,
                        "(Ljava/lang/Object;)" + sp.type,
                        null,
                        null);
                _cv.visitVarInsn(Constants.ALOAD, 0);
                _cv.visitFieldInsn(Constants.GETFIELD, className, sp.name, sp.type);
                Type returnType = Type.getType(sp.type);
                _cv.visitInsn(returnType.getOpcode(Constants.IRETURN));
                _cv.visitMaxs(
                        (sp.type.equals("J") || sp.type.equals("D")) ? 2 : 1,
                        2);
            }
        }
        // declare the field speedoPersistentClassName
        //int access, String name, String desc, Object value
        logger.log(BasicLevel.DEBUG, "Add to the class " + className
                + " the field " + ADDED_FIELD_NAME);
        cv.visitField(Constants.ACC_PRIVATE, ADDED_FIELD_NAME,
                ADDED_FIELD_DESC, null, null);

        //Add the method speedoGetPersistentClassName
        String methodName = "speedoGetPersistentClassName";
        logger.log(BasicLevel.DEBUG, "Add to the class " + className
                + " the method " + methodName);
        CodeVisitor mv = this.cv.visitMethod(
                Constants.ACC_PUBLIC,
                methodName,
                "()Ljava/lang/String;",
                null,
                null);
        mv.visitVarInsn(Constants.ALOAD, 0);
        mv.visitFieldInsn(Constants.GETFIELD,
                className, ADDED_FIELD_NAME, ADDED_FIELD_DESC);
        Type returnType = Type.getType(ADDED_FIELD_DESC);
        mv.visitInsn(returnType.getOpcode(Constants.IRETURN));
        mv.visitMaxs(1, 2);

        //Add the method speedoSetPersistentClassName
        methodName = "speedoSetPersistentClassName";
        logger.log(BasicLevel.DEBUG, "Add to the class " + className
                + " the method " + methodName);
        mv = this.cv.visitMethod(
                Constants.ACC_PUBLIC,
                methodName,
                "(Ljava/lang/String;)V",
                null,
                null);
        mv.visitVarInsn(Constants.ALOAD, 0);
        mv.visitVarInsn(Constants.ALOAD, 1);
        mv.visitFieldInsn(Constants.PUTFIELD,
                className, ADDED_FIELD_NAME, ADDED_FIELD_DESC);
        mv.visitInsn(Constants.RETURN);
        mv.visitMaxs(2, 2);
    }
View Full Code Here

    public CodeVisitor visitMethod(final int access,
                                   final String name,
                                   final String desc,
                                   final String[] exceptions,
                                   final Attribute attrs) {
        CodeVisitor c = cv.visitMethod(access, name, desc, exceptions, attrs);
        if (name.equals("<init>")) {
            c.visitVarInsn(Constants.ALOAD, 0);
            c.visitLdcInsn(speedoClass.getFQName()); //load a constant
            c.visitFieldInsn(Constants.PUTFIELD, className,
                ADDED_FIELD_NAME, ADDED_FIELD_DESC);
        }
        return c;
    }
View Full Code Here

        }
    }

    private void generateStaticFieldGetter(Field f, Type ft) {
        //public final static ${f.memoryType} ${f.getter}(${baseClassName} instance) {
        CodeVisitor mv;
        mv = cv.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
                f.getGetter(),
                "(" + getJVMType(classToWrite) + ")" + ft.getDescriptor(), null, null);
        //return instance.${f.getter}();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite,
                f.getGetter(), "()" +  ft.getDescriptor());
        mv.visitInsn(ft.getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
    }
View Full Code Here

        mv.visitInsn(ft.getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
    }
    private void generateStaticFieldSetter(Field f, Type ft) {
        //public final static void ${f.setter}(${baseClassName} instance, ${f.memoryType} val) {
        CodeVisitor mv;
        mv = cv.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, f.getSetter(),
                "(" + getJVMType(classToWrite) + ft.getDescriptor() + ")V", null, null);
        //instance.${f.setter}(val);
        mv.visitVarInsn(ALOAD, 0); //Class instance
        mv.visitVarInsn(ft.getOpcode(ILOAD), 1); //field
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, f.getSetter(),
                "(" + ft.getDescriptor() + ")V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
View Full Code Here

                "(" + ft.getDescriptor() + ")V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
    private void generateFieldGetter(Field f, Type ft, String ftd, int nbField) {
        CodeVisitor mv;
        mv = cv.visitMethod(ACC_PUBLIC, f.getGetter(), "()" + ftd, null, null);
        if (f.isReference) {
            // if ( (speedoReferenceState != null)
            generateGetRefState(mv, false);
            Label l1 = new Label();
            mv.visitJumpInsn(IFNULL, l1);
            // && (speedoReferenceState.getDetachedStatus() != DetachedLifeCycle.DETACHED_NONE)
            generateGetRefState(mv, false);
            mv.visitMethodInsn(INVOKEVIRTUAL, xfieldsAncestorJCN, "getDetachedStatus", "()B");
            Util.visitIntConstant(mv, DetachedLifeCycle.DETACHED_NONE);
            mv.visitInsn(I2B);
            mv.visitJumpInsn(IF_ICMPEQ, l1);
            // && !(($classNameFields) speedoReferenceState).${f.name}Loaded
            generateGetRefState(mv, true);
            mv.visitFieldInsn(GETFIELD, xfieldsJCN, f.getName() + "Loaded", "Z");
            mv.visitJumpInsn(IFNE, l1);
            {
                mv.visitTypeInsn(NEW, personality.getDetachedFieldAccessExceptionClassNameSlash());
                mv.visitInsn(DUP);
                mv.visitLdcInsn("Field " + f.getName() + " cannot be accessed: not loaded when the object has been detached");
                mv.visitMethodInsn(INVOKESPECIAL, personality.getDetachedFieldAccessExceptionClassNameSlash(), "<init>", "(Ljava/lang/String;)V");
                mv.visitInsn(ATHROW);
            }
            mv.visitLabel(l1);
        }
       
        //StateItf sa = this.speedoReadIntention(new long[] { ... });
        mv.visitVarInsn(ALOAD, 0);
        generateFieldIdAsLongArray(f, nbField, mv);
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, "speedoReadIntention",
                "([J)" + JT_STATE);
        mv.visitTypeInsn(CHECKCAST, xfieldsJCN);
        mv.visitVarInsn(ASTORE, 1);

        //return sa.f1;
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(GETFIELD, xfieldsJCN, f.getName(), ftd);
        mv.visitInsn(ft.getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
    }
View Full Code Here

        mv.visitInsn(ft.getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
    }

    private void generateFieldSetter(Field f, Type ft, String ftd, int nbField) {
        CodeVisitor mv;
        int nextLocalVarIdx = 1 + ft.getSize();
        mv = cv.visitMethod(ACC_PUBLIC, f.getSetter(), "(" + ftd + ")V", null, null);

        //if (!speedoIsActive()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, ISACTIVE_FIELD_NAME, "()Z");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        {
            //if(speedoReferenceState.getDetachedStatus() != DetachedLifeCycle.DETACHED_NONE) {
            generateGetRefState(mv, false);
            mv.visitMethodInsn(INVOKEVIRTUAL, xfieldsAncestorJCN, "getDetachedStatus", "()B");
            Util.visitIntConstant(mv, DetachedLifeCycle.DETACHED_NONE);
            mv.visitInsn(I2B);
            Label l3 = new Label();
            mv.visitJumpInsn(IF_ICMPEQ, l3);
            {
                //if (!(($classNameFields) speedoReferenceState).${f.name}Loaded ) {
                generateGetRefState(mv, true);
                mv.visitFieldInsn(GETFIELD, xfieldsJCN, f.getName() + "Loaded", "Z");
                Label l5 = new Label();
                mv.visitJumpInsn(IFNE, l5);
                {
                    //throw new DetachedFieldAccessException("Field $f.name cannot be accessed: not loaded when the object has been detached");
                    mv.visitTypeInsn(NEW, personality.getDetachedFieldAccessExceptionClassNameSlash());
                    mv.visitInsn(DUP);
                    mv.visitLdcInsn("Field " + f.getName()
                            + " cannot be accessed: not loaded when the object has been detached");
                    mv.visitMethodInsn(INVOKESPECIAL, personality.getDetachedFieldAccessExceptionClassNameSlash(), "<init>", "(Ljava/lang/String;)V");
                    mv.visitInsn(ATHROW);
                }
                mv.visitLabel(l5);
                //mark the detached copy as dirty
                //speedoReferenceState.setDetachedStatus(DetachedLifeCycle.DETACHED_DIRTY);
                generateGetRefState(mv, false);
                Util.visitIntConstant(mv, DetachedLifeCycle.DETACHED_DIRTY);
                mv.visitInsn(I2B);
                mv.visitMethodInsn(INVOKEVIRTUAL, xfieldsAncestorJCN, "setDetachedStatus", "(B)V");
            }
            mv.visitLabel(l3);
            //(($classNameFields) speedoReferenceState).${f.name} = val;
            generateGetRefState(mv, true);
            mv.visitVarInsn(ft.getOpcode(ILOAD), 1);
            mv.visitFieldInsn(PUTFIELD, xfieldsJCN, f.getName(), ftd);

            //return
            mv.visitInsn(RETURN);
        }

        mv.visitLabel(l1);
        //The po is activated
        //Logger logger = ((org.objectweb.jorm.util.api.Loggable) getPClassMapping()).getLogger();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, "getPClassMapping",
                "()Lorg/objectweb/jorm/api/PClassMapping;");
        mv.visitTypeInsn(CHECKCAST, "org/objectweb/jorm/util/api/Loggable");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/objectweb/jorm/util/api/Loggable",
                "getLogger", "()Lorg/objectweb/util/monolog/api/Logger;");
        final int loggerIdx = nextLocalVarIdx;
        nextLocalVarIdx++;
        mv.visitVarInsn(ASTORE, loggerIdx);

        //$classNameFields state = ($classNameFields) speedoWriteIntention(${f.jormFielIdDecl});
        mv.visitVarInsn(ALOAD, 0);
        generateFieldIdAsLongArray(f, nbField, mv);
        mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, "speedoWriteIntention",
                "([J)" + JT_STATE);
        mv.visitTypeInsn(CHECKCAST, xfieldsJCN);
        final int stateIdx = nextLocalVarIdx;
        nextLocalVarIdx++;
        mv.visitVarInsn(ASTORE, stateIdx);

        if (f.getIsReference()) {
            generateReferenceSetter(f, ft, nbField, loggerIdx, stateIdx, nextLocalVarIdx, mv);
        } else {
            mv.visitVarInsn(ALOAD, stateIdx);
            mv.visitVarInsn(ft.getOpcode(ILOAD), 1);
            mv.visitFieldInsn(PUTFIELD, xfieldsJCN, f.getName(), ftd);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
View Full Code Here

                    "(Ljava/lang/Object;)V");
        }
        return nextLocalVarIdx;
    }
    private void generateCoherenceFieldSetter(Field f, Type ft, String ftd, int nbField) {
        CodeVisitor mv;
        mv = cv.visitMethod(ACC_PUBLIC, f.getCoherentSetter(), "(" + ftd + ")V", null, null);
        if (f.getIs11Relation()) {
            generateCoherenceFieldSetter11(f, ft, ftd, nbField, mv);
        } else if (f.getIsM1Relation()) {
            generateCoherenceFieldSetterM1(f, ft, ftd, nbField, mv);
        } else if (f.getIsxMRelation()) {
            //The coherency management is done on the GenClass implementation
            // by the setElements method (see the normal setter)
           
            //${f.setter}(val);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ft.getOpcode(ILOAD), 1);
            mv.visitMethodInsn(INVOKEVIRTUAL, classToWrite, f.getSetter(),
                    "(" + ftd + ")V");
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
View Full Code Here

            mv.visitLabel(labelValIsNull);
        }
        mv.visitLabel(l2);
    }
    private void generateSpeedoElementAddedMethod() {
        CodeVisitor mv;
        mv = cv.visitMethod(ACC_PUBLIC, "speedoElementAdded",
                "(Ljava/lang/Object;I)V", null, null);
        //${classNameFields}.speedoElementAdded(elem, gcid, this);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, xfieldsJCN, "speedoElementAdded",
                "(Ljava/lang/Object;I" + JT_PO + ")V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
View Full Code Here

TOP

Related Classes of org.objectweb.asm.CodeVisitor

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.