Example usage for org.objectweb.asm.tree MethodNode visitAnnotation

List of usage examples for org.objectweb.asm.tree MethodNode visitAnnotation

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitAnnotation.

Prototype

@Override
    public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) 

Source Link

Usage

From source file:org.apache.commons.weaver.privilizer.BlueprintingVisitor.java

License:Apache License

private String importMethod(final Pair<Type, Method> key) {
    if (importedMethods.containsKey(key)) {
        return importedMethods.get(key);
    }//from  w  w w  . ja v  a2 s .c o  m
    final String result = new StringBuilder(key.getLeft().getInternalName().replace('/', '_')).append("$$")
            .append(key.getRight().getName()).toString();
    importedMethods.put(key, result);
    privilizer().env.debug("importing %s#%s as %s", key.getLeft().getClassName(), key.getRight(), result);
    final int access = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC;

    final MethodNode source = typeInfo(key.getLeft()).methods.get(key.getRight());

    final String[] exceptions = source.exceptions.toArray(ArrayUtils.EMPTY_STRING_ARRAY);

    // non-public fields accessed
    final Set<FieldAccess> fieldAccesses = new LinkedHashSet<>();

    source.accept(new MethodVisitor(Privilizer.ASM_VERSION) {
        @Override
        public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
            final FieldAccess fieldAccess = fieldAccess(Type.getObjectType(owner), name);

            super.visitFieldInsn(opcode, owner, name, desc);
            if (!Modifier.isPublic(fieldAccess.access)) {
                fieldAccesses.add(fieldAccess);
            }
        }
    });

    final MethodNode withAccessibleAdvice = new MethodNode(access, result, source.desc, source.signature,
            exceptions);

    // spider own methods:
    MethodVisitor mv = new NestedMethodInvocationHandler(withAccessibleAdvice, key); //NOPMD

    if (!fieldAccesses.isEmpty()) {
        mv = new AccessibleAdvisor(mv, access, result, source.desc, new ArrayList<>(fieldAccesses));
    }
    source.accept(mv);

    // private can only be called by other privileged methods, so no need to mark as privileged
    if (!Modifier.isPrivate(source.access)) {
        withAccessibleAdvice.visitAnnotation(Type.getType(Privileged.class).getDescriptor(), false).visitEnd();
    }
    withAccessibleAdvice.accept(this.cv);

    return result;
}

From source file:org.jacoco.core.internal.analysis.filter.AnnotationGeneratedFilterTest.java

License:Open Source License

@Test
public void should_filter_methods_annotated_with_runtime_visible_org_groovy_transform_Generated() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lgroovy/transform/Generated;", true);

    m.visitInsn(Opcodes.ICONST_0);/*from w ww  .j a  va  2s  .co  m*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter(m, context, output);

    assertMethodIgnored(m);
}

From source file:org.jacoco.core.internal.analysis.filter.AnnotationGeneratedFilterTest.java

License:Open Source License

@Test
public void should_filter_methods_annotated_with_runtime_invisible_lombok_Generated() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Llombok/Generated;", false);

    m.visitInsn(Opcodes.ICONST_0);/*  ww w  . ja  v a2s  .c  o m*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter(m, context, output);

    assertMethodIgnored(m);
}

From source file:org.jacoco.core.internal.analysis.filter.AnnotationGeneratedFilterTest.java

License:Open Source License

@Test
public void should_not_filter_when_other_annotations() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("LOtherAnnotation;", true);

    m.visitInsn(Opcodes.ICONST_0);/*from   ww w  .  j  a va  2  s. c  o  m*/
    m.visitInsn(Opcodes.IRETURN);

    context.classAnnotations.add("LOtherAnnotation;");

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.GroovyGeneratedFilterTest.java

License:Open Source License

@Test
public void testOtherAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lother/Annotation;", true);

    m.visitInsn(Opcodes.ICONST_0);/*from w w w  .j  av a2s.  c om*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertNull(fromInclusive);
    assertNull(toInclusive);
}

From source file:org.jacoco.core.internal.analysis.filter.GroovyGeneratedFilterTest.java

License:Open Source License

@Test
public void testGroovyGeneratedAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lgroovy/transform/Generated;", true);

    m.visitInsn(Opcodes.ICONST_0);/*w w  w .  j  av a  2s .c  om*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertEquals(m.instructions.getFirst(), fromInclusive);
    assertEquals(m.instructions.getLast(), toInclusive);
}

From source file:org.jacoco.core.internal.analysis.filter.KotlinGeneratedFilterTest.java

License:Open Source License

@Test
public void testWithLinesForKotlinWithDebug() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lother/Annotation;", false);
    m.visitLineNumber(12, new Label());
    m.visitInsn(Opcodes.ICONST_0);/*from   w  w  w . j  ava2s . c  om*/
    m.visitInsn(Opcodes.IRETURN);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.LombokGeneratedFilterTest.java

License:Open Source License

@Test
public void testOtherAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lother/Annotation;", false);

    m.visitInsn(Opcodes.ICONST_0);/*from w ww . j  av  a 2  s  .co  m*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertNull(fromInclusive);
    assertNull(toInclusive);
}

From source file:org.jacoco.core.internal.analysis.filter.LombokGeneratedFilterTest.java

License:Open Source License

@Test
public void testLombokGeneratedAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Llombok/Generated;", false);

    m.visitInsn(Opcodes.ICONST_0);//ww  w .j  a va2s . c o  m
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertEquals(m.instructions.getFirst(), fromInclusive);
    assertEquals(m.instructions.getLast(), toInclusive);
}

From source file:pxb.android.dex2jar.v3.V3MethodAdapter.java

License:Apache License

private void build() {
    List<String> exceptions = new ArrayList<String>();
    String signature = null;/*from www  .ja  v  a2s.  c  om*/
    for (Iterator<Ann> it = anns.iterator(); it.hasNext();) {
        Ann ann = it.next();
        if ("Ldalvik/annotation/Throws;".equals(ann.type)) {
            it.remove();
            for (Item item : ann.items) {
                if (item.name.equals("value")) {
                    Ann values = (Ann) item.value;
                    for (Item i : values.items) {
                        exceptions.add(i.value.toString());
                    }
                }
            }
        } else if ("Ldalvik/annotation/Signature;".equals(ann.type)) {
            it.remove();
            for (Item item : ann.items) {
                if (item.name.equals("value")) {
                    Ann values = (Ann) item.value;
                    StringBuilder sb = new StringBuilder();
                    for (Item i : values.items) {
                        sb.append(i.value.toString());
                    }
                    signature = sb.toString();
                }
            }
        }
    }

    MethodNode methodNode = this.methodNode;
    methodNode.access = method.getAccessFlags();
    methodNode.name = method.getName();
    methodNode.desc = method.getType().getDesc();
    methodNode.signature = signature;
    methodNode.exceptions = exceptions;
    for (Ann ann : anns) {
        AnnotationVisitor av = methodNode.visitAnnotation(ann.type, ann.visible);
        V3AnnAdapter.accept(ann.items, av);
        av.visitEnd();
    }

    for (int i = 0; i < paramAnns.length; i++) {
        for (Ann ann : paramAnns[i]) {
            AnnotationVisitor av = methodNode.visitParameterAnnotation(i, ann.type, ann.visible);
            V3AnnAdapter.accept(ann.items, av);
            av.visitEnd();
        }
    }
}