Example usage for org.objectweb.asm AnnotationVisitor visitEnd

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

Introduction

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

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the annotation.

Usage

From source file:ataspectj.UnweavableTest.java

License:Open Source License

ISome getJit() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object",
            new String[] { "ataspectj/UnweavableTest$ISome" });
    AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true);
    av.visitEnd();

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*w  w  w . j a  v a 2s .co  m*/
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (ISome) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:blue.lapis.pore.event.PoreListenerGenerator.java

License:Open Source License

private static byte[] generate(String name, Class<?> pore, Class<?> sponge, EventPriority priority,
        Order order) {//  w w w. ja v  a  2  s . c o m
    name = name.replace('.', '/');
    String poreName = Type.getInternalName(pore);
    String spongeSignature = "(L" + Type.getInternalName(sponge) + ";)V";

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av;

    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "on" + sponge.getSimpleName(), spongeSignature, null, null);
        {
            av = mv.visitAnnotation(ANNOTATION_DESCRIPTOR, true);
            av.visitEnum("order", ORDER_DESCRIPTOR, order.name());
            av.visitEnd();
        }
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "get", GET_CACHE, false);
        mv.visitTypeInsn(CHECKCAST, poreName);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(NEW, poreName);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, poreName, "<init>", spongeSignature, false);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "set", SET_CACHE, false);
        mv.visitLabel(l0);
        mv.visitFrame(F_APPEND, 1, new Object[] { poreName }, 0, null);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(GETSTATIC, PRIORITY_CLASS, priority.name(), PRIORITY_DESCRIPTOR);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "call", CALL_EVENT, false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:blue.origami.asm.OAnno.java

License:Apache License

void asm(Object cv) {
    if (annoMap != null) {
        for (Class<?> c : annoMap.keySet()) {
            AnnotationVisitor av = visitAnnotation(cv, c);
            Map<String, Object> values = annoMap.get(c);
            if (values != null) {
                for (String key : values.keySet()) {
                    av.visit(key, values.get(key));
                }/*ww  w.ja v  a  2 s.  co m*/
            }
            av.visitEnd();
        }
    }
}

From source file:blue.origami.asm.OAnno.java

License:Apache License

public static void asmAnnotation(Object v, Class<?> c) {
    AnnotationVisitor av = visitAnnotation(v, c);
    av.visitEnd();
}

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void generateAnn(MethodVisitor v, PrimInfo pi, int i) {
    AnnotationVisitor a = v.visitAnnotation(Type.getDescriptor(JellyCode.class), true);
    if (i == 0)//w  ww.j  a v a  2s  . co  m
        a.visit("arity", pi.arity);
    a.visitEnd();
}

From source file:ch.raffael.contracts.processor.pmap.ParameterMapAnnotationProcessor.java

License:Apache License

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Building parameter maps");
    for (Element elem : roundEnv.getRootElements()) {
        if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) {
            // FIXME: enums & enum constants
            try {
                TypeElement typeElem = (TypeElement) elem;
                processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
                        "Processing: " + ((TypeElement) elem).getQualifiedName());
                String className = typeElem.getQualifiedName().toString();
                if (className.endsWith(CONTRACTS_CLASS_SUFFIX)) {
                    continue;
                }/*from w  ww  .jav  a  2s .c  o m*/
                String ctrClassName = className + CONTRACTS_CLASS_SUFFIX;
                JavaFileObject contractsFile = processingEnv.getFiler().createClassFile(ctrClassName, elem);
                ClassWriter classWriter = new ClassWriter(0);
                classWriter.visit(V1_7, ACC_PUBLIC + ACC_SUPER, toInternalName(ctrClassName), null, null, null);
                MethodVisitor ctor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "()V", null, null);
                ctor.visitCode();
                ctor.visitLabel(new Label());
                ctor.visitEnd();
                //classWriter.visitOuterClass(toInternalName(className), null, null);
                AnnotationVisitor parameterMap = classWriter
                        .visitAnnotation(Type.getDescriptor(ParameterMap.class), false);
                writeType(parameterMap, typeElem, null);
                parameterMap.visitEnd();
                classWriter.visitEnd();
                try (OutputStream output = contractsFile.openOutputStream()) {
                    output.write(classWriter.toByteArray());
                }
            } catch (IOException e) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getLocalizedMessage());
            }
        }
    }
    return false;
}

From source file:ch.raffael.contracts.processor.pmap.ParameterMapAnnotationProcessor.java

License:Apache License

private void writeType(AnnotationVisitor annotation, TypeElement element, AnnotationVisitor innerClassArray) {
    AnnotationVisitor methodsArray = annotation.visitArray("methods");
    for (Element elem : element.getEnclosedElements()) {
        String methodName;//from   w ww  . j ava2  s .c  o m
        if (elem.getKind() == ElementKind.CONSTRUCTOR) {
            methodName = CTOR_NAME;
        } else if (elem.getKind() == ElementKind.METHOD) {
            methodName = elem.getSimpleName().toString();
        } else {
            continue;
        }
        AnnotationVisitor methodAnnotation = methodsArray.visitAnnotation(null,
                Type.getDescriptor(ParameterMap.Method.class));
        methodAnnotation.visit("descriptor", getMethodDescriptor((ExecutableElement) elem));
        AnnotationVisitor paramNames = methodAnnotation.visitArray("parameterNames");
        for (VariableElement param : ((ExecutableElement) elem).getParameters()) {
            paramNames.visit(null, param.getSimpleName().toString());
        }
        paramNames.visitEnd();
        methodAnnotation.visitEnd();
    }
    methodsArray.visitEnd();
    boolean endInnerClassArray = false;
    if (innerClassArray == null) {
        innerClassArray = annotation.visitArray("innerClasses");
        endInnerClassArray = true;
    }
    for (Element innerElem : element.getEnclosedElements()) {
        if (isType(innerElem)) {
            AnnotationVisitor innerClass = innerClassArray.visitAnnotation(null,
                    Type.getDescriptor(ParameterMap.InnerClass.class));
            innerClass.visit("name", toInternalName(
                    processingEnv.getElementUtils().getBinaryName((TypeElement) innerElem).toString()));
            writeType(innerClass, (TypeElement) innerElem, innerClassArray);
            innerClass.visitEnd();
        }
    }
    if (endInnerClassArray) {
        innerClassArray.visitEnd();
    }
}

From source file:co.cask.cdap.internal.app.runtime.service.http.HttpHandlerGenerator.java

License:Apache License

/**
 * Inspects the given type and copy/rewrite handler methods from it into the newly generated class.
 *
 * @param delegateType The user handler type
 * @param inspectType The type that needs to be inspected. It's either the delegateType or one of its parents
 *//*from  w ww  . j ava  2  s  .com*/
private void inspectHandler(final TypeToken<?> delegateType, final TypeToken<?> inspectType,
        final String pathPrefix, final Type classType, final ClassWriter classWriter,
        final List<Class<?>> preservedClasses) throws IOException {
    Class<?> rawType = inspectType.getRawType();

    // Visit the delegate class, copy and rewrite handler method, with method body just do delegation
    try (InputStream sourceBytes = rawType.getClassLoader()
            .getResourceAsStream(Type.getInternalName(rawType) + ".class")) {
        ClassReader classReader = new ClassReader(sourceBytes);
        classReader.accept(new ClassVisitor(Opcodes.ASM5) {

            // Only need to visit @Path at the class level if we are inspecting the user handler class
            private final boolean inspectDelegate = delegateType.equals(inspectType);
            private boolean visitedPath = !inspectDelegate;

            @Override
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                super.visit(version, access, name, signature, superName, interfaces);
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Copy the class annotation if it is @Path. Only do it for one time
                Type type = Type.getType(desc);
                if (inspectDelegate && type.equals(Type.getType(Path.class))) {
                    visitedPath = true;
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(desc, visible);
                    return new AnnotationVisitor(Opcodes.ASM5, annotationVisitor) {
                        @Override
                        public void visit(String name, Object value) {
                            // "value" is the key for the Path annotation string.
                            if (name.equals("value")) {
                                super.visit(name, pathPrefix + value.toString());
                            } else {
                                super.visit(name, value);
                            }
                        }
                    };

                } else {
                    return super.visitAnnotation(desc, visible);
                }
            }

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                // Create a class-level annotation with the prefix, if the user has not specified any class-level
                // annotation.
                if (!visitedPath) {
                    String pathDesc = Type.getType(Path.class).getDescriptor();
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(pathDesc, true);
                    annotationVisitor.visit("value", pathPrefix);
                    annotationVisitor.visitEnd();
                    visitedPath = true;
                }

                // Copy the method if it is public and annotated with one of the HTTP request method
                MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                if (!Modifier.isPublic(access)) {
                    return mv;
                }
                return new HandlerMethodVisitor(delegateType, mv, desc, signature, access, name, exceptions,
                        classType, classWriter, preservedClasses);
            }
        }, ClassReader.SKIP_DEBUG);
    }
}

From source file:com.android.build.gradle.internal.incremental.InstantRunVerifierTest.java

License:Apache License

@Test
public void testDiffListOnAnnotationNodesWithAnnotationNodeValue() throws Exception {
    AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;");
    AnnotationVisitor newAnnotation = original.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();

    AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;");
    newAnnotation = updated.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();/*from   w  ww.ja  v a 2s. c om*/

    assertEquals(InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList(Lists.newArrayList(original),
            Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR));
}

From source file:com.android.build.gradle.internal.incremental.InstantRunVerifierTest.java

License:Apache License

@Test
public void testDiffListOnAnnotationNodesWithAnnotationArrayValue() throws Exception {
    AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;");

    AnnotationVisitor arrayVisitor = original.visitArray("test");
    AnnotationVisitor newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();

    newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();//w w w  . j a  v a2 s . c o m
    arrayVisitor.visitEnd();

    AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;");
    arrayVisitor = updated.visitArray("test");
    newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();

    newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();
    arrayVisitor.visitEnd();

    assertEquals(InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList(Lists.newArrayList(original),
            Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR));
}