Example usage for org.objectweb.asm Opcodes GETFIELD

List of usage examples for org.objectweb.asm Opcodes GETFIELD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes GETFIELD.

Prototype

int GETFIELD

To view the source code for org.objectweb.asm Opcodes GETFIELD.

Click Source Link

Usage

From source file:org.apache.drill.exec.compile.bytecode.ValueHolderIden.java

License:Apache License

public void transferToLocal(DirectSorter adder, int localVariable) {
    for (int i = 0; i < types.length; i++) {
        Type t = types[i];//  w  w w. j  ava2 s  . co  m
        if (i + 1 < types.length) {
            adder.visitInsn(dup(t)); // don't dup for last value.
        }
        adder.visitFieldInsn(Opcodes.GETFIELD, type.getInternalName(), names[i], t.getDescriptor());
        adder.directVarInsn(t.getOpcode(Opcodes.ISTORE), localVariable + i);
    }
}

From source file:org.apache.felix.scrplugin.helper.ClassModifier.java

License:Apache License

private static void createMethod(final ClassWriter cw, final String className, final String referenceName,
        final String fieldName, final String typeName, final boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase()
            + referenceName.substring(1);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V",
            null, null);//from  w  w  w . j  a v a 2  s. c  o  m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
}

From source file:org.apache.felix.scrplugin.tags.qdox.QDoxJavaClassDescription.java

License:Apache License

protected void createMethod(ClassWriter cw, String propertyName, String typeName, boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + propertyName.substring(0, 1).toUpperCase()
            + propertyName.substring(1);
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V", null,
            null);/*from   w ww. j  av a 2s  .  c o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    // add to qdox
    final JavaParameter param = new JavaParameter(new Type(typeName), "param");
    final JavaParameter[] params = new JavaParameter[] { param };
    final com.thoughtworks.qdox.model.JavaMethod meth = new com.thoughtworks.qdox.model.JavaMethod();
    meth.setName(methodName);
    for (int i = 0; i < params.length; i++) {
        meth.addParameter(params[i]);
    }
    meth.setModifiers(new String[] { "protected" });
    this.javaClass.addMethod(meth);
}

From source file:org.apache.flink.api.java.ClosureCleaner.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {
    return new MethodVisitor(Opcodes.ASM5) {

        @Override/* ww  w  .jav a 2s .  c  om*/
        public void visitFieldInsn(int op, String owner, String name, String desc) {
            if (op == Opcodes.GETFIELD && name.equals(this0Name)) {
                isThis0Accessed = true;
            }
        }
    };
}

From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java

License:Apache License

public void testVisitFieldInsnWithPrimitive() {
    visitor.visitFieldInsn(Opcodes.GETFIELD, "a/b/c", "x", "I");

    assertClasses("a.b.c");
}

From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java

License:Apache License

public void testVisitFieldInsnWithObject() {
    visitor.visitFieldInsn(Opcodes.GETFIELD, "a/b/c", "x", "Lx/y/z;");

    assertClasses("a.b.c", "x.y.z");
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodCheck(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PRIVATE, Enhancer.METHOD_ENHANCED_CHECK,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();// w w  w .  j av  a 2 s.co  m

    final Label lCheckInternal = new Label();
    final Label lCheckInitialized = new Label();
    final Label lReturn = new Label();
    final Label lFind = new Label();
    final Label lInitialized = new Label();
    final Label lChanged = new Label();
    final Label lOut = new Label();

    // if (!this.__enhanced__$$__internal) { return }
    mv.visitLabel(lCheckInternal);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INTERNAL,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFEQ, lCheckInitialized);
    mv.visitInsn(Opcodes.RETURN);

    // if (!this.__enhanced__$$__initialized) {
    mv.visitLabel(lCheckInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFNE, lChanged);

    //     if (this.__enhanced_$$__session == null)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lFind);

    //         throw new PersistenceException("No session to initialize the instance");
    mv.visitTypeInsn(Opcodes.NEW, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION);
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("No session to initialize the instance");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION,
            Enhancer.CONSTRUCTOR_INIT, Enhancer.makeDescription(Void.TYPE, String.class));
    mv.visitInsn(Opcodes.ATHROW);

    //       this.__enhanced_$$__session.getEntityManager().find(this.__enhanced_$$__type, this.__enhanced__$$__id);
    mv.visitLabel(lFind);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_SESSION, Enhancer.METHOD_GET_ENTITY_MANAGER,
            Enhancer.makeDescription(EntityManagerImpl.class));
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_ENTITY_MANAGER, Enhancer.METHOD_FIND,
            Enhancer.makeDescription(Object.class, Class.class, Object.class));
    mv.visitInsn(Opcodes.POP);

    //   this.__enhanced__$$__initialized = true;
    mv.visitLabel(lInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);

    // if (this.__enhanced_$$__session != null)
    mv.visitLabel(lChanged);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNULL, lReturn);

    //     this.__enhanced__$$__managedInstance.changed();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_MANAGED_INSTANCE, Enhancer.METHOD_CHANGED,
            Enhancer.makeDescription(Void.TYPE));

    // return;
    mv.visitLabel(lReturn);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(lOut);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, lCheckInternal, lOut, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodGetManagedInstance(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, Enhancer.METHOD_ENHANCED_GET_MANAGED_INSTANCE,
            Enhancer.makeDescription(ManagedInstance.class), null, null);
    mv.visitCode();/* w ww  .  j a va 2s.c  o  m*/

    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE);
    mv.visitInsn(Opcodes.ARETURN);

    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodIsInitialized(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, Enhancer.METHOD_ENHANCED_IS_INITIALIZED,
            Enhancer.makeDescription(Boolean.TYPE), null, null);
    mv.visitCode();/*from  ww  w.  jav  a  2 s .  c  o m*/

    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitInsn(Opcodes.IRETURN);

    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.boretti.drools.integration.drools5.DroolsClassVisitor.java

License:Open Source License

@Override
public void visitEnd() {
    FieldVisitor fv = null;/* ww  w  . jav  a  2s .co  m*/
    if (isNeedChangeForBoth()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor(), null,
                null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
    }
    if (isNeedChangeForField()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor(), null, null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_PRIVATE, DROOLS_METHOD_RUN, "()V", null, null);
        AnnotationVisitor av = mv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
        AnnotationVisitor value = av.visitArray("value");
        value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
        value.visitEnd();
        av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
        av.visitEnd();
        mv.visitCode();
        Label start = new Label();
        mv.visitLabel(start);
        Label doIt = new Label();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitJumpInsn(Opcodes.IFEQ, doIt);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(doIt);
        mv.visitFrame(Opcodes.F_SAME, 1, new Object[] { Type.getObjectType(me).getInternalName() }, 0,
                new Object[] {});
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor());
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(RuleBase.class).getInternalName(),
                "newStatelessSession", "()Lorg/drools/StatelessSession;");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(FIELD_NAME_LOGGER);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(Object.class).getInternalName(), "getClass",
                "()Ljava/lang/Class;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(org.apache.log4j.Logger.class).getInternalName(),
                "getLogger", "(Ljava/lang/Class;)Lorg/apache/log4j/Logger;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "setGlobal", "(Ljava/lang/String;Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "execute", "(Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitInsn(Opcodes.RETURN);
        Label end = new Label();
        mv.visitLabel(end);
        mv.visitLocalVariable("this", Type.getObjectType(me).getDescriptor(), null, start, end, 0);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}