Example usage for org.objectweb.asm ClassVisitor visitEnd

List of usage examples for org.objectweb.asm ClassVisitor visitEnd

Introduction

In this page you can find the example usage for org.objectweb.asm ClassVisitor visitEnd.

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the class.

Usage

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

private void generateCodec(ClassVisitor cv, Schema schema, int suffix, boolean javaClassCodec) {
    final String genClassInternalName = GENERATED_CLASS_INAME + suffix;
    cv.visit(V1_7, ACC_PUBLIC + ACC_FINAL, genClassInternalName, null, baseclassIName, null);

    generateConstructorAndFields(schema, cv, genClassInternalName, javaClassCodec);

    generateReadStaticGroup(schema, cv, genClassInternalName, javaClassCodec);
    generateReadStaticGroupForTypeAndCreate(schema, cv, genClassInternalName, javaClassCodec);
    generateReadStaticGroupForType(schema, cv, genClassInternalName, javaClassCodec);

    generateWriteStaticGroup(schema, cv, genClassInternalName, javaClassCodec);
    generateWriteStaticGroupForTypeWithId(schema, cv, genClassInternalName, javaClassCodec);
    generateWriteStaticGroupForType(schema, cv, genClassInternalName, javaClassCodec);

    cv.visitEnd();
}

From source file:com.facebook.buck.jvm.java.abi.ClassVisitorDriverFromElement.java

License:Apache License

public void driveVisitor(Element fullElement, ClassVisitor visitor) {
    fullElement.accept(new ElementVisitorAdapter(), visitor);
    visitor.visitEnd();
}

From source file:com.facebook.presto.byteCode.ClassDefinition.java

License:Apache License

public void visit(ClassVisitor visitor) {
    // Generic signature if super class or any interface is generic
    String signature = null;/* w  w  w.j a  v a2s.c o  m*/
    if (superClass.isGeneric() || any(interfaces, ParameterizedType.isGenericType())) {
        signature = genericClassSignature(superClass, interfaces);
    }

    String[] interfaces = new String[this.interfaces.size()];
    for (int i = 0; i < interfaces.length; i++) {
        interfaces[i] = this.interfaces.get(i).getClassName();
    }
    visitor.visit(V1_7, toAccessModifier(access) | ACC_SUPER, type.getClassName(), signature,
            superClass.getClassName(), interfaces);

    // visit source
    if (source != null) {
        visitor.visitSource(source, debug);
    }

    // visit annotations
    for (AnnotationDefinition annotation : annotations) {
        annotation.visitClassAnnotation(visitor);
    }

    // visit fields
    for (FieldDefinition field : fields) {
        field.visit(visitor);
    }

    // visit clinit method
    classInitializer.visit(visitor, true);

    // visit methods
    for (MethodDefinition method : methods) {
        method.visit(visitor);
    }

    // done
    visitor.visitEnd();
}

From source file:com.github.antag99.retinazer.weaver.MapperGenerator.java

License:Open Source License

@Override
public void accept(ClassVisitor visitor) {
    visitor.visit(V1_7, ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC, metadata.getMapperName(),
            "Lcom/github/antag99/retinazer/PackedMapper<L" + metadata.getMapperName() + ";>;",
            "com/github/antag99/retinazer/PackedMapper", null);
    for (ComponentProperty property : metadata.properties) {
        FieldVisitor propertyField = visitor.visitField(ACC_PRIVATE + ACC_FINAL, property.getMetadataName(),
                property.getMetadataDesc(), null, null);
        if (propertyField != null)
            propertyField.visitEnd();/*from   www.  ja v  a 2 s.  c om*/
    }

    FieldVisitor flyweightField = visitor.visitField(ACC_PRIVATE + ACC_FINAL, "flyweight",
            "L" + metadata.internalName + ";", null, null);
    if (flyweightField != null)
        flyweightField.visitEnd();

    // @off: Formatter mangles the elegant method syntax

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "<init>",
            "(Lcom/github/antag99/retinazer/Engine;I)V", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitVarInsn(ALOAD, 1);
            visitLdcInsn(Type.getObjectType(metadata.internalName));
            visitVarInsn(ILOAD, 2);
            visitMethodInsn(Opcodes.INVOKESPECIAL, "com/github/antag99/retinazer/PackedMapper", "<init>",
                    "(Lcom/github/antag99/retinazer/Engine;Ljava/lang/Class;I)V", false);

            for (ComponentProperty property : metadata.properties) {
                visitVarInsn(ALOAD, 0);
                visitTypeInsn(NEW, WeaverConstants.getMetadataName(property.type));
                visitInsn(Opcodes.DUP);
                visitLdcInsn(property.name);
                if (property.type.getSort() == Type.OBJECT) {
                    visitLdcInsn(property.type);
                    visitMethodInsn(INVOKESPECIAL, WeaverConstants.getMetadataName(property.type), "<init>",
                            "(Ljava/lang/String;Ljava/lang/Class;)V", false);
                } else {
                    visitMethodInsn(INVOKESPECIAL, WeaverConstants.getMetadataName(property.type), "<init>",
                            "(Ljava/lang/String;)V", false);
                }
                visitFieldInsn(PUTFIELD, metadata.getMapperName(), property.getMetadataName(),
                        property.getMetadataDesc());
            }

            visitVarInsn(ALOAD, 0);
            visitInsn(DUP);
            visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "createFlyweight",
                    "()L" + metadata.internalName + ";", false);
            visitFieldInsn(PUTFIELD, metadata.getMapperName(), "flyweight", "L" + metadata.internalName + ";");
            visitInsn(RETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "createFlyweight",
            "()L" + metadata.internalName + ";", null, null)) {
        {
            visitCode();
            visitTypeInsn(NEW, metadata.internalName);
            visitInsn(DUP);
            visitMethodInsn(INVOKESPECIAL, metadata.internalName, "<init>", "()V", false);

            for (ComponentProperty property : metadata.properties) {
                visitInsn(DUP);
                visitVarInsn(ALOAD, 0);
                visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(),
                        property.getMetadataDesc());
                visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getMetadataName(property.type), "getBag",
                        "()" + property.getBagDesc(), false);
                visitFieldInsn(PUTFIELD, metadata.internalName, property.getBagName(), property.getBagDesc());
            }

            visitInsn(ARETURN);

            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5,
            visitor.visitMethod(ACC_PUBLIC, "create", "(I)L" + metadata.internalName + ";", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitVarInsn(ILOAD, 1);
            visitMethodInsn(INVOKEVIRTUAL, "com/github/antag99/retinazer/Mapper", "checkCreate", "(I)V", false);
            visitVarInsn(ALOAD, 0);
            visitVarInsn(ILOAD, 1);
            visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "get",
                    "(I)L" + metadata.internalName + ";", false);
            visitInsn(DUP);
            visitMethodInsn(INVOKEVIRTUAL, metadata.internalName, WeaverConstants.RESET_METHOD_NAME,
                    WeaverConstants.RESET_METHOD_DESC, false);
            visitInsn(ARETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC + ACC_SYNTHETIC + ACC_BRIDGE, "create",
            "(I)Lcom/github/antag99/retinazer/Component;", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitVarInsn(ILOAD, 1);
            visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "create",
                    "(I)L" + metadata.internalName + ";", false);
            visitInsn(ARETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5,
            visitor.visitMethod(ACC_PUBLIC, "get", "(I)L" + metadata.internalName + ";", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitFieldInsn(GETFIELD, metadata.getMapperName(), "flyweight", "L" + metadata.internalName + ";");
            visitInsn(DUP);
            visitVarInsn(ILOAD, 1);
            visitFieldInsn(PUTFIELD, metadata.internalName, WeaverConstants.INDEX_FIELD_NAME,
                    WeaverConstants.INDEX_FIELD_DESC);
            visitInsn(ARETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "get",
            "(I)Lcom/github/antag99/retinazer/Component;", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitVarInsn(ILOAD, 1);
            visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "get",
                    "(I)L" + metadata.internalName + ";", false);
            visitInsn(ARETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "getProperties",
            "()[Lcom/github/antag99/retinazer/util/Property;", null, null)) {
        {
            visitCode();
            visitLdcInsn(metadata.properties.size());
            visitTypeInsn(ANEWARRAY, "com/github/antag99/retinazer/util/Property");

            List<ComponentProperty> properties = metadata.properties;
            for (int i = 0, n = properties.size(); i < n; i++) {
                ComponentProperty property = properties.get(i);
                visitInsn(DUP);
                visitLdcInsn(i);
                visitVarInsn(ALOAD, 0);
                visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(),
                        property.getMetadataDesc());
                visitInsn(AASTORE);
            }

            visitInsn(ARETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "applyComponentChanges", "()V", null, null)) {
        {
            visitCode();
            visitVarInsn(ALOAD, 0);
            visitFieldInsn(GETFIELD, "com/github/antag99/retinazer/Mapper", "componentsMask",
                    "Lcom/github/antag99/retinazer/util/Mask;");
            visitVarInsn(ASTORE, 1);
            visitVarInsn(ALOAD, 0);
            visitFieldInsn(GETFIELD, "com/github/antag99/retinazer/Mapper", "removeMask",
                    "Lcom/github/antag99/retinazer/util/Mask;");
            visitVarInsn(ASTORE, 2);
            for (ComponentProperty property : metadata.properties) {
                visitVarInsn(ALOAD, 0);
                visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(),
                        property.getMetadataDesc());
                visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getMetadataName(property.type), "getBag",
                        "()" + property.getBagDesc(), false);
                visitVarInsn(ALOAD, 2);
                visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getBagName(property.type), "clear",
                        "(Lcom/github/antag99/retinazer/util/Mask;)V", false);
            }
            visitVarInsn(ALOAD, 1);
            visitVarInsn(ALOAD, 2);
            visitMethodInsn(INVOKEVIRTUAL, "com/github/antag99/retinazer/util/Mask", "andNot",
                    "(Lcom/github/antag99/retinazer/util/Mask;)V", false);
            visitInsn(RETURN);
            visitMaxs(0, 0);
            visitEnd();
        }
    };

    visitor.visitEnd();
}

From source file:com.google.code.jconts.instrument.gen.ComputationClassGenerator.java

License:Apache License

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();

    // extends Object implements Computation<ValueType>
    SignatureWriter sign = new SignatureWriter();
    SignatureVisitor supsign = sign.visitSuperclass();
    supsign.visitClassType(OBJECT_NAME);
    supsign.visitEnd();/*from   w w  w. ja v a  2 s  .  c  om*/
    SignatureVisitor iface = sign.visitInterface();
    iface.visitClassType(COMPUTATION_NAME);
    SignatureVisitor argsign = iface.visitTypeArgument('=');
    new SignatureReader(info.valueSignature).acceptType(argsign);
    argsign.visitEnd();

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL, info.computationClassName, sign.toString(), OBJECT_NAME,
            new String[] { COMPUTATION_NAME });

    cv.visitSource(info.ownerSource, null);
    cv.visitInnerClass(info.stateClassName, info.owner, info.stateSimpleName, 0);
    cv.visitInnerClass(info.computationClassName, info.owner, info.computationSimpleName, 0);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "state", stateDesc, null, null);

    generateConstructor(cv);
    generateExecute(cv);

    cv.visitEnd();
}

From source file:com.google.code.jconts.instrument.gen.ContinuationClassGenerator.java

License:Apache License

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL, info.continuationClassName, signature, OBJECT_NAME,
            new String[] { CONTINUATION_NAME });

    cv.visitSource(info.ownerSource, null);
    cv.visitInnerClass(info.stateClassName, info.owner, info.stateSimpleName, 0);
    cv.visitInnerClass(info.continuationClassName, info.owner, info.continuationSimpleName, 0);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "state", stateDesc, null, null);

    cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "index", "I", null, null);

    generateConstructor(cv);//from   w  w w .ja  v a  2 s . c o m
    generateExecute(cv, true);
    generateExecute(cv, false);

    cv.visitEnd();
}

From source file:com.google.code.jconts.instrument.gen.StateClassGenerator.java

License:Apache License

public void accept(TransformationContext context) {
    ClassVisitor cv = context.writer();

    final String name = info.stateClassName;

    cv.visit(Opcodes.V1_6, Opcodes.ACC_FINAL /*| Opcodes.ACC_SYNTHETIC*/, name, null, OBJECT_NAME, null);

    cv.visitSource(info.ownerSource, null);
    cv.visitOuterClass(info.owner, null, null);

    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, CONTINUATION_FIELD, CONTINUATION_DESC,
            'L' + CONTINUATION_NAME + '<' + info.valueSignature + ">;", null);

    // Local variables state
    List<String> names = info.tracker.getFieldNames();
    List<Type> types = info.tracker.getFieldTypes();
    for (int i = 0; i < names.size(); ++i) {
        cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, names.get(i), types.get(i).getDescriptor(), null, null);
    }//from   w ww.  j av  a  2s.  c  o m

    // Return value variable
    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, "result", OBJECT_DESC, null, null);
    cv.visitField(0/*Opcodes.ACC_SYNTHETIC*/, "exception", THROWABLE_DESC, null, null);

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, DEFAULT_CTOR_DESC, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);
    mv.visitLineNumber(0, start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', null, start, end, 0);

    mv.visitMaxs(1, 1);
    mv.visitEnd();

    cv.visitEnd();
}

From source file:com.google.gwt.dev.shell.rewrite.HostedModeClassRewriter.java

License:Apache License

public byte[] writeJsoIntf(final String className, byte classBytes[]) {
    String desc = toDescriptor(className);
    assert (jsoIntfDescs.contains(desc));
    assert (jsoSuperDescs.containsKey(desc));
    List<String> superDescs = jsoSuperDescs.get(desc);
    assert (superDescs != null);
    assert (superDescs.size() > 0);

    // The ASM model is to chain a bunch of visitors together.
    ClassWriter writer = new ClassWriter(0);
    final ClassVisitor v = writer;

    // v = new CheckClassAdapter(v);
    // v = new TraceClassVisitor(v, new PrintWriter(System.out));

    String[] interfaces;/*from w  w w . j  a va  2  s . com*/
    // TODO(bov): something better than linear?
    if (superDescs.contains("java/lang/Object")) {
        interfaces = null;
    } else {
        interfaces = superDescs.toArray(new String[superDescs.size()]);
    }
    v.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_INTERFACE, desc, null, "java/lang/Object",
            interfaces);
    if (classBytes != null) {
        // Java7 enforces innerclass/outerclass consistency. In order to fix this, copy from original
        ClassVisitor cv = new EmptyVisitor() {
            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
                // copy inner class table from original JSO to synthetic interface
                v.visitInnerClass(name, outerName, innerName, access);
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
                // copy outer class table from original JSO to synthetic interface
                v.visitOuterClass(owner, name, desc);
            }
        };
        new ClassReader(classBytes).accept(cv, 0);
    }
    v.visitEnd();
    return writer.toByteArray();
}

From source file:com.googlecode.ddom.weaver.asm.ClassVisitorTee.java

License:Apache License

public void visitEnd() {
    for (ClassVisitor visitor : visitors) {
        visitor.visitEnd();
    }
}

From source file:com.googlecode.ddom.weaver.ext.ModelExtensionClass.java

License:Apache License

public void accept(ClassVisitor classVisitor) {
    ImplementationInfo implementationInfo = info.getImplementation().get(ImplementationInfo.class);
    String name = Util.classNameToInternalName(info.getClassName());
    String superName = Util.classNameToInternalName(info.getSuperClassName());
    classVisitor.visit(Opcodes.V1_5,//from   w w w  . j  a  v a  2 s .co  m
            info.isAbstract() ? Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT : Opcodes.ACC_PUBLIC, name, null,
            superName, new String[] {
                    Util.classNameToInternalName(info.getExtensionInterface().getClassInfo().getName()) });
    for (ConstructorInfo constructor : implementationInfo.getConstructors()) {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructor.getDescriptor(),
                constructor.getSignature(), constructor.getExceptions());
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            Type[] argumentTypes = constructor.getArgumentTypes();
            for (int i = 0; i < argumentTypes.length; i++) {
                mv.visitVarInsn(argumentTypes[i].getOpcode(Opcodes.ILOAD), i + 1);
            }
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructor.getDescriptor());
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            for (int i = 0; i < argumentTypes.length; i++) {
                mv.visitLocalVariable("arg" + i, argumentTypes[i].getDescriptor(), null, l0, l1, i + 1);
            }
            mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1);
            mv.visitEnd();
        }
    }
    classVisitor.visitEnd();
}