/** * Returns a string representation of this object. */ public String toString() { Annotation[] a = getAnnotations(); StringBuilder sbuf = new StringBuilder(); int i = 0; while (i < a.length) { sbuf.append(a[i++].toString()); if (i != a.length) sbuf.append(", "); } return sbuf.toString(); }
/** * Parses the annotations and returns a data structure representing * the annotation with the specified type. See also * <code>getAnnotations()</code> as to the returned data structure. * * @param type the annotation type. * @return null if the specified annotation type is not included. * @see #getAnnotations() */ public Annotation getAnnotation(String type) { Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) return annotations[i]; } return null; }
private List<String> getAnnotationNames(final AnnotationsAttribute... annotationsAttributes) { List<String> result = Lists.newArrayList(); if (annotationsAttributes != null) { for (AnnotationsAttribute annotationsAttribute : annotationsAttributes) { if (annotationsAttribute != null) { for (Annotation annotation : annotationsAttribute.getAnnotations()) { result.add(annotation.getTypeName()); } } } } return result; }
private List<String> getAnnotationNames(final AnnotationsAttribute... annotationsAttributes) { List<String> result = Lists.newArrayList(); if (annotationsAttributes != null) { for (AnnotationsAttribute annotationsAttribute : annotationsAttributes) { if (annotationsAttribute != null) { for (Annotation annotation : annotationsAttribute.getAnnotations()) { result.add(annotation.getTypeName()); } } } } return result; }
/** * Returns a string representation of this object. */ @Override public String toString() { Annotation[] a = getAnnotations(); StringBuilder sbuf = new StringBuilder(); int i = 0; while (i < a.length) { sbuf.append(a[i++].toString()); if (i != a.length) sbuf.append(", "); } return sbuf.toString(); }
static boolean hasAnnotationType(String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) { Annotation[] anno1, anno2; if (a1 == null) anno1 = null; else anno1 = a1.getAnnotations(); if (a2 == null) anno2 = null; else anno2 = a2.getAnnotations(); if (anno1 != null) for (int i = 0; i < anno1.length; i++) if (anno1[i].getTypeName().equals(annotationTypeName)) return true; if (anno2 != null) for (int i = 0; i < anno2.length; i++) if (anno2[i].getTypeName().equals(annotationTypeName)) return true; return false; }
/** * Parses the annotations and returns a data structure representing * the annotation with the specified type. See also * <code>getAnnotations()</code> as to the returned data structure. * * @param type the annotation type. * @return null if the specified annotation type is not included. * @see #getAnnotations() */ public Annotation getAnnotation(String type) { Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) return annotations[i]; } return null; }
/** * Removes an annotation by type. * After removing an annotation, if {@link #numAnnotations()} returns 0, * this annotations attribute has to be removed. * * @param type of annotation to remove * @return whether an annotation with the given type has been removed * @since 3.21 */ public boolean removeAnnotation(String type) { Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { Annotation[] newlist = new Annotation[annotations.length - 1]; System.arraycopy(annotations, 0, newlist, 0, i); if (i < annotations.length - 1) { System.arraycopy(annotations, i + 1, newlist, i, annotations.length - i - 1); } setAnnotations(newlist); return true; } } return false; }
static Object getAnnotationType(Class clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException { Annotation[] anno1, anno2; if (a1 == null) anno1 = null; else anno1 = a1.getAnnotations(); if (a2 == null) anno2 = null; else anno2 = a2.getAnnotations(); String typeName = clz.getName(); if (anno1 != null) for (int i = 0; i < anno1.length; i++) if (anno1[i].getTypeName().equals(typeName)) return toAnnoType(anno1[i], cp); if (anno2 != null) for (int i = 0; i < anno2.length; i++) if (anno2[i].getTypeName().equals(typeName)) return toAnnoType(anno2[i], cp); return null; }
static boolean hasAnnotationType(String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) { Annotation[] anno1, anno2; if (a1 == null) anno1 = null; else anno1 = a1.getAnnotations(); if (a2 == null) anno2 = null; else anno2 = a2.getAnnotations(); if (anno1 != null) for (int i = 0; i < anno1.length; i++) if (anno1[i].getTypeName().equals(annotationTypeName)) return true; if (anno2 != null) for (int i = 0; i < anno2.length; i++) if (anno2[i].getTypeName().equals(annotationTypeName)) return true; return false; }
protected AnnotationsAttribute stripAnnotation(ConstPool constantPool, List<?> attributes) { Iterator<?> itr = attributes.iterator(); AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(annotationClass)) { logger.debug(String.format("Stripping out %s annotation", annotationClass)); continue; } annotationsAttribute.addAnnotation(annotation); } itr.remove(); } } return annotationsAttribute; }
anno1 = a1.getAnnotations(); size1 = anno1.length; anno2 = a2.getAnnotations(); size2 = anno2.length;
/** * Removes an annotation by type. * After removing an annotation, if {@link #numAnnotations()} returns 0, * this annotations attribute has to be removed. * * @param type of annotation to remove * @return whether an annotation with the given type has been removed * @since 3.21 */ public boolean removeAnnotation(String type) { Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { Annotation[] newlist = new Annotation[annotations.length - 1]; System.arraycopy(annotations, 0, newlist, 0, i); if (i < annotations.length - 1) { System.arraycopy(annotations, i + 1, newlist, i, annotations.length - i - 1); } setAnnotations(newlist); return true; } } return false; }
/** * Adds an annotation. If there is an annotation with the same type, * it is removed before the new annotation is added. * * @param annotation the added annotation. */ public void addAnnotation(Annotation annotation) { String type = annotation.getTypeName(); Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { annotations[i] = annotation; setAnnotations(annotations); return; } } Annotation[] newlist = new Annotation[annotations.length + 1]; System.arraycopy(annotations, 0, newlist, 0, annotations.length); newlist[annotations.length] = annotation; setAnnotations(newlist); }
static Object getAnnotationType(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException { Annotation[] anno1, anno2; if (a1 == null) anno1 = null; else anno1 = a1.getAnnotations(); if (a2 == null) anno2 = null; else anno2 = a2.getAnnotations(); String typeName = clz.getName(); if (anno1 != null) for (int i = 0; i < anno1.length; i++) if (anno1[i].getTypeName().equals(typeName)) return toAnnoType(anno1[i], cp); if (anno2 != null) for (int i = 0; i < anno2.length; i++) if (anno2[i].getTypeName().equals(typeName)) return toAnnoType(anno2[i], cp); return null; }
Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { boolean containsTypeLevelAnnotation = containsTypeLevelPersistenceAnnotation(((AnnotationsAttribute) object).getAnnotations()); if (containsTypeLevelAnnotation) { LOG.debug("Marking " + convertedClassName + " as transformed");
anno1 = a1.getAnnotations(); size1 = anno1.length; anno2 = a2.getAnnotations(); size2 = anno2.length;
/** * Adds an annotation. If there is an annotation with the same type, * it is removed before the new annotation is added. * * @param annotation the added annotation. */ public void addAnnotation(Annotation annotation) { String type = annotation.getTypeName(); Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { annotations[i] = annotation; setAnnotations(annotations); return; } } Annotation[] newlist = new Annotation[annotations.length + 1]; System.arraycopy(annotations, 0, newlist, 0, annotations.length); newlist[annotations.length] = annotation; setAnnotations(newlist); }
private List<AnnotationDescriptor> getAnnotationDescriptors(AnnotationsAttribute annotationsAttr) { if (annotationsAttr == null) { return Collections.emptyList(); } List<AnnotationDescriptor> annotationDescriptors = new ArrayList<>(annotationsAttr.numAnnotations()); for (javassist.bytecode.annotation.Annotation annotation : annotationsAttr.getAnnotations()) { // Sigh: javassist uses raw collections (is this 2002?) @SuppressWarnings("unchecked") Set<String> memberNames = annotation.getMemberNames(); List<AttributeDescriptor> attributes = new ArrayList<>(); if (memberNames != null) { for (String name : memberNames) { MemberValue memberValue = annotation.getMemberValue(name); final List<String> values = new ArrayList<>(); memberValue.accept(new ListingMemberValueVisitor(values)); attributes.add(new AttributeDescriptor(name, values)); } } annotationDescriptors.add(new AnnotationDescriptor(annotation.getTypeName(), attributes)); } return annotationDescriptors; } }
@Override public void scan(final Object cls) { final ClassFile classFile = (ClassFile)cls; AnnotationsAttribute annotations = ((AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag)); if (annotations != null) { boolean isAnnotated = false; for (javassist.bytecode.annotation.Annotation a : annotations.getAnnotations()) { if (annotationsToScan.contains(a.getTypeName())) { isAnnotated = true; } } if (isAnnotated) { List<AnnotationDescriptor> classAnnotations = getAnnotationDescriptors(annotations); @SuppressWarnings("unchecked") List<FieldInfo> classFields = classFile.getFields(); List<FieldDescriptor> fieldDescriptors = new ArrayList<>(classFields.size()); for (FieldInfo field : classFields) { String fieldName = field.getName(); AnnotationsAttribute fieldAnnotations = ((AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag)); fieldDescriptors.add(new FieldDescriptor(fieldName, field.getDescriptor(), getAnnotationDescriptors(fieldAnnotations))); } functions.add(new AnnotatedClassDescriptor(classFile.getName(), classAnnotations, fieldDescriptors)); } } }