Author: ebourg Date: Fri Feb 20 01:23:20 2015 New Revision: 1661052 URL: http://svn.apache.org/r1661052 Log: Refactored JavaClass and FieldOrMethod to avoid a code duplication in the getAnnotationEntries methods. Thanks to Charles Honton (BCEL-165)
Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java?rev=1661052&r1=1661051&r2=1661052&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java Fri Feb 20 01:23:20 2015 @@ -22,6 +22,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.bcel.Constants; @@ -149,4 +150,16 @@ public class AnnotationEntry implements } return result.toString(); } + + public static AnnotationEntry[] createAnnotationEntries(Attribute[] attrs) { + // Find attributes that contain annotation data + List<AnnotationEntry> accumulatedAnnotations = new ArrayList<AnnotationEntry>(attrs.length); + for (Attribute attribute : attrs) { + if (attribute instanceof Annotations) { + Annotations runtimeAnnotations = (Annotations) attribute; + Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getAnnotationEntries()); + } + } + return accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]); + } } Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java?rev=1661052&r1=1661051&r2=1661052&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java Fri Feb 20 01:23:20 2015 @@ -21,8 +21,6 @@ import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import org.apache.bcel.Constants; import org.apache.bcel.classfile.Attribute; import org.apache.bcel.classfile.Signature; @@ -46,10 +44,6 @@ public abstract class FieldOrMethod exte private String signatureAttributeString = null; private boolean searchedForSignatureAttribute = false; - - // Annotations are collected from certain attributes, don't do it more than necessary! - private boolean annotationsOutOfDate = true; - FieldOrMethod() { } @@ -233,50 +227,16 @@ public abstract class FieldOrMethod exte } /** - * Ensure we have unpacked any attributes that contain annotations. - * We don't remove these annotation attributes from the attributes list, they - * remain there. - */ - private void ensureAnnotationsUpToDate() - { - if (annotationsOutOfDate) - { - // Find attributes that contain annotation data - Attribute[] attrs = getAttributes(); - List<AnnotationEntry> accumulatedAnnotations = new ArrayList<AnnotationEntry>(); - for (Attribute attribute : attrs) { - if (attribute instanceof Annotations) - { - Annotations annotations = (Annotations) attribute; - for (int j = 0; j < annotations.getAnnotationEntries().length; j++) - { - accumulatedAnnotations.add(annotations - .getAnnotationEntries()[j]); - } - } - } - annotationEntries = accumulatedAnnotations - .toArray(new AnnotationEntry[accumulatedAnnotations.size()]); - annotationsOutOfDate = false; + * @return Annotations on the field or method + */ + public AnnotationEntry[] getAnnotationEntries() { + if (annotationEntries == null) { + annotationEntries = AnnotationEntry.createAnnotationEntries(getAttributes()); } - } - - public AnnotationEntry[] getAnnotationEntries() - { - ensureAnnotationsUpToDate(); + return annotationEntries; } - public void addAnnotationEntry(AnnotationEntry a) - { - ensureAnnotationsUpToDate(); - int len = annotationEntries.length; - AnnotationEntry[] newAnnotations = new AnnotationEntry[len + 1]; - System.arraycopy(annotationEntries, 0, newAnnotations, 0, len); - newAnnotations[len] = a; - annotationEntries = newAnnotations; - } - /** * Hunts for a signature attribute on the member and returns its contents. So where the 'regular' signature * may be (Ljava/util/Vector;)V the signature attribute may in fact say 'Ljava/lang/Vector<Ljava/lang/String>;' Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java?rev=1661052&r1=1661051&r2=1661052&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/JavaClass.java Fri Feb 20 01:23:20 2015 @@ -73,10 +73,7 @@ public class JavaClass extends AccessFla public static final byte ZIP = 3; static boolean debug = false; // Debugging on/off final static char sep = File.separatorChar; // directory separator - - // Annotations are collected from certain attributes, don't do it more than necessary! - private boolean annotationsOutOfDate = true; - + private static BCELComparator _cmp = new BCELComparator() { public boolean equals( Object o1, Object o2 ) { @@ -144,7 +141,6 @@ public class JavaClass extends AccessFla this.fields = fields; this.methods = methods; this.attributes = attributes; - annotationsOutOfDate = true; this.source = source; // Get source file name if available for (Attribute attribute : attributes) { @@ -337,24 +333,17 @@ public class JavaClass extends AccessFla return attributes; } + /** + * @return Annotations on the class + */ public AnnotationEntry[] getAnnotationEntries() { - if (annotationsOutOfDate) { - // Find attributes that contain annotation data - Attribute[] attrs = getAttributes(); - List<AnnotationEntry> accumulatedAnnotations = new ArrayList<AnnotationEntry>(); - for (Attribute attribute : attrs) { - if (attribute instanceof Annotations) { - Annotations runtimeAnnotations = (Annotations)attribute; - for(int j = 0; j < runtimeAnnotations.getAnnotationEntries().length; j++) { - accumulatedAnnotations.add(runtimeAnnotations.getAnnotationEntries()[j]); - } - } - } - annotations = accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]); - annotationsOutOfDate = false; - } - return annotations; - } + if (annotations == null) { + annotations = AnnotationEntry.createAnnotationEntries(getAttributes()); + } + + return annotations; + } + /** * @return Class name. */