@Override public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) { AnnotationNode an = new AnnotationNode(desc); if (visible) { if (visibleAnnotations == null) { visibleAnnotations = new ArrayList<AnnotationNode>(1); } visibleAnnotations.add(an); } else { if (invisibleAnnotations == null) { invisibleAnnotations = new ArrayList<AnnotationNode>(1); } invisibleAnnotations.add(an); } return an; }
@Override public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc); if (visible) { if (visibleTypeAnnotations == null) { visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1); } visibleTypeAnnotations.add(an); } else { if (invisibleTypeAnnotations == null) { invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1); } invisibleTypeAnnotations.add(an); } return an; }
@Override public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { // Finds the last real instruction, i.e. the instruction targeted by // this annotation. AbstractInsnNode insn = instructions.getLast(); while (insn.getOpcode() == -1) { insn = insn.getPrevious(); } // Adds the annotation to this instruction. TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc); if (visible) { if (insn.visibleTypeAnnotations == null) { insn.visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>( 1); } insn.visibleTypeAnnotations.add(an); } else { if (insn.invisibleTypeAnnotations == null) { insn.invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>( 1); } insn.invisibleTypeAnnotations.add(an); } return an; }
private void visitAnnotationValue(AnnotationVisitor annotationVisitor, AnnotationValue<?> value) { String name = value.getName(); if (value instanceof EnumAnnotationValue) { annotationVisitor.visitEnum(name, ((EnumAnnotationValue) value).getTypeDesc(), (String) value.getValue()); } else if (value instanceof SimpleAnnotationValue) { annotationVisitor.visit(name, value.getValue()); } else if (value instanceof ArrayAnnotationValue) { AnnotationVisitor arrayVisitor = annotationVisitor.visitArray(name); AnnotationValue<?>[] values = ((ArrayAnnotationValue) value).getValue(); for (AnnotationValue<?> annotationValue : values) { visitAnnotationValue(arrayVisitor, annotationValue); } arrayVisitor.visitEnd(); } else if (value instanceof AnnotationAnnotationValue) { AnnotationMember annotation = ((AnnotationAnnotationValue) value).getValue(); AnnotationVisitor annVisitor = annotationVisitor.visitAnnotation(name, annotation.getName()); visitAnnotationValues(annotation, annVisitor); } }
/** * Makes the given visitor visit a given annotation value. * * @param av * an annotation visitor. Maybe <tt>null</tt>. * @param name * the value name. * @param value * the actual value. */ static void accept(final AnnotationVisitor av, final String name, final Object value) { if (av != null) { if (value instanceof String[]) { String[] typeconst = (String[]) value; av.visitEnum(name, typeconst[0], typeconst[1]); } else if (value instanceof AnnotationNode) { AnnotationNode an = (AnnotationNode) value; an.accept(av.visitAnnotation(name, an.desc)); } else if (value instanceof List) { AnnotationVisitor v = av.visitArray(name); if (v != null) { List<?> array = (List<?>) value; for (int j = 0; j < array.size(); ++j) { accept(v, null, array.get(j)); } v.visitEnd(); } } else { av.visit(name, value); } } }
@Override public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { TryCatchBlockNode tcb = tryCatchBlocks.get((typeRef & 0x00FFFF00) >> 8); TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc); if (visible) { if (tcb.visibleTypeAnnotations == null) { tcb.visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>( 1); } tcb.visibleTypeAnnotations.add(an); } else { if (tcb.invisibleTypeAnnotations == null) { tcb.invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>( 1); } tcb.invisibleTypeAnnotations.add(an); } return an; }
@Override public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) { LocalVariableAnnotationNode an = new LocalVariableAnnotationNode( typeRef, typePath, getLabelNodes(start), getLabelNodes(end), index, desc); if (visible) { if (visibleLocalVariableAnnotations == null) { visibleLocalVariableAnnotations = new ArrayList<LocalVariableAnnotationNode>( 1); } visibleLocalVariableAnnotations.add(an); } else { if (invisibleLocalVariableAnnotations == null) { invisibleLocalVariableAnnotations = new ArrayList<LocalVariableAnnotationNode>( 1); } invisibleLocalVariableAnnotations.add(an); } return an; }
@Override public AnnotationVisitor visitInsnAnnotation(final int typeRef, final TypePath typePath, final String desc, final boolean visible) { checkStartCode(); checkEndCode(); int sort = typeRef >>> 24; if (sort != TypeReference.INSTANCEOF && sort != TypeReference.NEW && sort != TypeReference.CONSTRUCTOR_REFERENCE && sort != TypeReference.METHOD_REFERENCE && sort != TypeReference.CAST && sort != TypeReference.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT && sort != TypeReference.METHOD_INVOCATION_TYPE_ARGUMENT && sort != TypeReference.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT && sort != TypeReference.METHOD_REFERENCE_TYPE_ARGUMENT) { throw new IllegalArgumentException("Invalid type reference sort 0x" + Integer.toHexString(sort)); } CheckClassAdapter.checkTypeRefAndPath(typeRef, typePath); CheckMethodAdapter.checkDesc(desc, false); return new CheckAnnotationAdapter(super.visitInsnAnnotation(typeRef, typePath, desc, visible)); }
private static void addAnnotationTree(AnnotationVisitor av0, Annotation ann, Class<? extends Annotation> annotationType) throws IllegalAccessException, InvocationTargetException { Method[] annMethods = annotationType.getDeclaredMethods(); for (Method m : annMethods) { Class<?> returnType = m.getReturnType(); if (returnType.isArray()) { Class<?> compType = returnType.getComponentType(); String compDesc = Type.getDescriptor(compType); AnnotationVisitor avArray = av0.visitArray(m.getName()); Object[] arr = (Object[]) m.invoke(ann); for (Object comp : arr) { addAnnotation(null, compType, compDesc, avArray, comp); } avArray.visitEnd(); } else { addAnnotation(m.getName(), returnType, Type.getDescriptor(returnType), av0, m.invoke(ann)); } } }
private void visitAnnotationMembers(Set<AnnotationMember> annotationMembers) { for (AnnotationMember annotation : annotationMembers) { AnnotationVisitor annotationVisitor = apiMemberAdapter.visitAnnotation(annotation.getName(), annotation.isVisible()); visitAnnotationValues(annotation, annotationVisitor); } }
@Override public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { AnnotationVisitor av = super.visitInsnAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible); return av == null ? av : new AnnotationRemapper(av, remapper); }
@Override public AnnotationVisitor visitArray(final String name) { Printer p = this.p.visitArray(name); AnnotationVisitor av = this.av == null ? null : this.av .visitArray(name); return new TraceAnnotationVisitor(av, p); }
@Override public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) { Printer p = this.p.visitFieldAnnotation(desc, visible); AnnotationVisitor av = fv == null ? null : fv.visitAnnotation(desc, visible); return new TraceAnnotationVisitor(av, p); }
public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if (desc.equals(ANNOTATION_DESCRIPTOR)) { found = true; } return new AnnotationVisitor(Opcodes.ASM5) { @Override public void visit(String name, Object value) { if (name.equals("value")) { interfaceName = value.toString(); } } }; }
@Override public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { AnnotationVisitor av = super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible); return av == null ? null : createRemappingAnnotationAdapter(av); }
@Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lorg/junit/runner/RunWith;".equals(desc)) { test = true; } return null; }
public void addConstructor(Constructor<?> constructor) throws Exception { List<Type> paramTypes = new ArrayList<Type>(); for (Class<?> paramType : constructor.getParameterTypes()) { paramTypes.add(Type.getType(paramType)); } String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES)); MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS); for (Annotation annotation : constructor.getDeclaredAnnotations()) { if (annotation.annotationType().getAnnotation(Inherited.class) != null) { continue; } Retention retention = annotation.annotationType().getAnnotation(Retention.class); AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME); annotationVisitor.visitEnd(); } methodVisitor.visitCode(); // this.super(p0 .. pn) methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); for (int i = 0; i < constructor.getParameterTypes().length; i++) { methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1); } methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); }
@Override public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { cp.newUTF8(desc); if (visible) { cp.newUTF8("RuntimeVisibleTypeAnnotations"); } else { cp.newUTF8("RuntimeInvisibleTypeAnnotations"); } return new AnnotationConstantsCollector(mv.visitTryCatchAnnotation( typeRef, typePath, desc, visible), cp); }
@Override public AnnotationVisitor visitAnnotation(final String name, final String desc) { if (values == null) { values = new ArrayList<Object>(this.desc != null ? 2 : 1); } if (this.desc != null) { values.add(name); } AnnotationNode annotation = new AnnotationNode(desc); values.add(annotation); return annotation; }
@Override public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) { Printer p = this.p.visitMethodAnnotation(desc, visible); AnnotationVisitor av = mv == null ? null : mv.visitAnnotation(desc, visible); return new TraceAnnotationVisitor(av, p); }
private HashSet<String> analyzeClass(String className) { HashSet<String> set = new HashSet<>(); Optional<InputStream> classFileInputStream = classFileFinder.apply(className); if (!classFileInputStream.isPresent()) { return set; } try(InputStream input = classFileInputStream.get()) { ClassReader reader = new ClassReader(input); reader.accept(new ClassVisitor(Opcodes.ASM6) { @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { return new MethodVisitor(Opcodes.ASM6) { @Override public AnnotationVisitor visitAnnotation(String annotationDesc, boolean visible) { if (annotationDesc.equals(OVERRIDE_ENTRY_POINT_NAME)) { set.add(name + desc); } return null; } }; } }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); } catch (IOException e) { throw new UncheckedIOException(e); } return set; }
@Override public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { AnnotationVisitor av = super.visitInsnAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible); return av == null ? av : new RemappingAnnotationAdapter(av, remapper); }
@Override public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { AnnotationVisitor av = super.visitParameterAnnotation(parameter, remapper.mapDesc(desc), visible); return av == null ? av : new RemappingAnnotationAdapter(av, remapper); }
@Override public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) { fsmCursor.transition("visitAnnotation"); final AnnotationVisitor annotationVisitor = super.visitAnnotation(desc, visible); return annotationVisitor; // TODO: add CheckAnnotationVisitorFsm }
@Override public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { AnnotationVisitor av = super.visitTryCatchAnnotation(typeRef, typePath, remapper.mapDesc(desc), visible); return av == null ? av : new AnnotationRemapper(av, remapper); }
@Override public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) { AnnotationVisitor av = super.visitLocalVariableAnnotation(typeRef, typePath, start, end, index, remapper.mapDesc(desc), visible); return av == null ? av : new AnnotationRemapper(av, remapper); }
@Override public AnnotationVisitor visitTryCatchAnnotation(final int typeRef, final TypePath typePath, final String desc, final boolean visible) { processDeferredAload(false); final AnnotationVisitor annotationVisitor = super.visitTryCatchAnnotation(typeRef, typePath, desc, visible); return annotationVisitor; }
/** * Called for each annotation defined on one of the method's parameters. Used * to store these annotations which are later used to guide alias analysis. * * @param parameter Index of the parameter to annotate. * @param desc Descriptor for the annotation type. * @param visible Visibility of annotation (unused). * @return AnnotationVisitor that imports the annotation properties. */ @Override public AnnotationVisitor visitParameterAnnotation(final int parameter, final String desc, final boolean visible) { Annotation a = new Annotation(Type.getType(desc)); if(method.getModifiers().contains(Modifier.STATIC)) { method.addParameterAnnotation(parameter, a); } else { method.addParameterAnnotation(parameter + 1, a); } return new AnnotationImporter(a); }
/** * Prepares to set the value for array methods/values. * * @param key Method to set. * @return AnnotationVisitor to import the array. */ @Override public AnnotationVisitor visitArray(String key) { List<Object> arr = new LinkedList<Object>(); annotation.set(key, arr); return new ArrayImporter(arr); }