Example usage for org.objectweb.asm AnnotationVisitor visit

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

Introduction

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

Prototype

public void visit(final String name, final Object value) 

Source Link

Document

Visits a primitive value of the annotation.

Usage

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));
                }/*  w  ww. ja v  a2  s.c  o m*/
            }
            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  ava  2s.  co  m
        a.visit("arity", pi.arity);
    a.visitEnd();
}

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;/*www.jav a2  s .c  om*/
        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   www  .ja  v a 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();//  www  . java 2 s  .  c  om

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

    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();//from   w w w. ja va2s. c om

    newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;");
    newAnnotation.visit("innerEntry", "innerValue");
    newAnnotation.visitEnd();
    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));
}

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

License:Apache License

@Test
public void testDiffListOnAnnotationNodesWithIntArrayValue() throws Exception {
    AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;");
    AnnotationVisitor test = original.visitArray("test");
    test.visit("stub", 1);
    test.visit("stub", 2);
    test.visit("stub", 3);
    test.visitEnd();/*ww w.ja va  2  s  . c  o  m*/

    AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;");
    test = updated.visitArray("test");
    test.visit("stub", 1);
    test.visit("stub", 2);
    test.visit("stub", 3);
    test.visitEnd();

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

From source file:com.facebook.buck.java.abi.AnnotationValueMirror.java

License:Apache License

public void accept(@Nullable String name, AnnotationVisitor visitor) {
    if (primitiveValue != null) {
        visitor.visit(name, primitiveValue);
    } else if (enumValue != null) {
        visitor.visitEnum(name, enumValue.desc, enumValue.value);
    } else if (annotationValue != null) {
        annotationValue.appendTo(visitor, name);
    } else if (arrayValue != null) {
        AnnotationVisitor arrayVisitor = visitor.visitArray(name);

        for (AnnotationValueMirror element : arrayValue) {
            element.accept(null, arrayVisitor);
        }/*  w  w w. jav a2s  .co  m*/

        arrayVisitor.visitEnd();
    }
}

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

License:Apache License

private void visit(AnnotationVisitor visitor, String name, Object value) {
    if (value instanceof AnnotationDefinition) {
        AnnotationDefinition annotation = (AnnotationDefinition) value;
        AnnotationVisitor annotationVisitor = visitor.visitAnnotation(name, annotation.type.getType());
        annotation.visit(annotationVisitor);
    } else if (value instanceof Enum) {
        Enum<?> enumConstant = (Enum<?>) value;
        visitor.visitEnum(name, type(enumConstant.getDeclaringClass()).getClassName(), enumConstant.name());
    } else if (value instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) value;
        visitor.visit(name, Type.getType(parameterizedType.getType()));
    } else if (value instanceof Class) {
        Class<?> clazz = (Class<?>) value;
        visitor.visit(name, Type.getType(clazz));
    } else if (value instanceof List) {
        AnnotationVisitor arrayVisitor = visitor.visitArray(name);
        for (Object element : (List<?>) value) {
            visit(arrayVisitor, null, element);
        }/*from  w ww .  j a  v a  2s .c om*/
        arrayVisitor.visitEnd();
    } else {
        visitor.visit(name, value);
    }
}

From source file:com.google.code.nanorm.internal.introspect.asm.MapperBuilder.java

License:Apache License

/**
 * Visit annotation property (String, primitive value, class reference or
 * other annotation)//from w w  w . jav  a 2 s .  c  om
 * @param name annotation name
 * @param obj value
 * @param visitor annotation visitar
 */
private static void visitAnnotationProperty(String name, Object obj, AnnotationVisitor visitor) {
    final Class<?> objClass = obj.getClass();
    if (objClass.isArray()) {
        // Array
        final AnnotationVisitor arrayVisitor = visitor.visitArray(name);
        for (Object o : (Object[]) obj) {
            visitAnnotationProperty(name, o, arrayVisitor);
        }
        arrayVisitor.visitEnd();
    } else if (objClass.isEnum()) {
        // Enumeration
        visitor.visitEnum(name, Type.getDescriptor(obj.getClass()), obj.toString());
    } else if (Annotation.class.isAssignableFrom(obj.getClass())) {
        // Nested annotation
        Annotation nested = (Annotation) obj;
        AnnotationVisitor nestedVisitor = visitor.visitAnnotation(name,
                Type.getDescriptor(nested.annotationType()));
        visitAnnotation(nestedVisitor, nested);
    } else if (obj instanceof Class<?>) {
        // Class reference
        visitor.visit(name, Type.getDescriptor((Class<?>) obj));
    } else {
        // String or primitive type
        visitor.visit(name, obj);
    }
}