TypeOracle to allow access to the list of annotations on an element. This brings HasAnnotations up to spec with java.lang.reflect.AnnotatedElement. http://gwt-code-reviews.appspot.com/1352805/show Review by: tobyr@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9712 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/ext/typeinfo/HasAnnotations.java b/dev/core/src/com/google/gwt/core/ext/typeinfo/HasAnnotations.java index 7707c2f..3b353a9 100644 --- a/dev/core/src/com/google/gwt/core/ext/typeinfo/HasAnnotations.java +++ b/dev/core/src/com/google/gwt/core/ext/typeinfo/HasAnnotations.java
@@ -36,6 +36,17 @@ <T extends Annotation> T getAnnotation(Class<T> annotationClass); /** + * Returns all the annotations present on this element. + */ + Annotation[] getAnnotations(); + + /** + * Returns the annotations declared directly on this element; does not include + * any inherited annotations. + */ + Annotation[] getDeclaredAnnotations(); + + /** * Returns <code>true</code> if this item has an annotation of the specified * type. *
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/Annotations.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/Annotations.java index 5246151..e9265c7 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/Annotations.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/Annotations.java
@@ -21,7 +21,10 @@ import java.lang.annotation.Annotation; import java.lang.annotation.Inherited; -import java.util.Collection; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -30,6 +33,18 @@ */ class Annotations implements HasAnnotations { + private static final Comparator<Annotation> ANNOTATION_COMPARATOR = new Comparator<Annotation>() { + /** + * An element can only be annotated with one annotation of a particular + * type. So we only need to sort by annotation type name, since there won't + * be any duplicates. + */ + public int compare(Annotation o1, Annotation o2) { + return o1.annotationType().getName().compareTo( + o2.annotationType().getName()); + } + }; + private static Map<Class<? extends Annotation>, Annotation> copyOfAnnotations( Annotations otherAnnotations) { Map<Class<? extends Annotation>, Annotation> declaredAnnotations = new HashMap<Class<? extends Annotation>, Annotation>(); @@ -90,12 +105,16 @@ public Annotation[] getAnnotations() { initializeAnnotations(); - Collection<Annotation> values = lazyAnnotations.values(); + List<Annotation> values = new ArrayList<Annotation>( + lazyAnnotations.values()); + Collections.sort(values, ANNOTATION_COMPARATOR); return values.toArray(new Annotation[values.size()]); } public Annotation[] getDeclaredAnnotations() { - Collection<Annotation> values = declaredAnnotations.values(); + List<Annotation> values = new ArrayList<Annotation>( + declaredAnnotations.values()); + Collections.sort(values, ANNOTATION_COMPARATOR); return values.toArray(new Annotation[values.size()]); }
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JAbstractMethod.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JAbstractMethod.java index 76e8c47..74294aa 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JAbstractMethod.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JAbstractMethod.java
@@ -77,6 +77,14 @@ return annotations.getAnnotation(annotationClass); } + public Annotation[] getAnnotations() { + return annotations.getAnnotations(); + } + + public Annotation[] getDeclaredAnnotations() { + return annotations.getDeclaredAnnotations(); + } + /** * Gets the type in which this method or constructor was declared. */ @@ -233,20 +241,6 @@ thrownTypes = Lists.add(thrownTypes, type); } - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getAnnotations() { - return annotations.getAnnotations(); - } - - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getDeclaredAnnotations() { - return annotations.getDeclaredAnnotations(); - } - // Called only by a JParameter, passing itself as a reference for lookup. String getRealParameterName(JParameter parameter) { if (realParameterNames == null) {
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JArrayType.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JArrayType.java index 1ccf17c..fa3a264 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JArrayType.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JArrayType.java
@@ -70,6 +70,11 @@ return null; } + @Override + public Annotation[] getAnnotations() { + return TypeOracle.NO_ANNOTATIONS; + } + public JType getComponentType() { return componentType; } @@ -86,6 +91,11 @@ } @Override + public Annotation[] getDeclaredAnnotations() { + return TypeOracle.NO_ANNOTATIONS; + } + + @Override public JClassType getEnclosingType() { return null; } @@ -413,22 +423,6 @@ return null; } - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getAnnotations() { - return TypeOracle.NO_ANNOTATIONS; - } - - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getDeclaredAnnotations() { - return TypeOracle.NO_ANNOTATIONS; - } - @Override JArrayType getSubstitutedType(JParameterizedType parameterizedType) { if (getComponentType() instanceof JClassType) {
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JClassType.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JClassType.java index 33b442c..a05a8ad 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JClassType.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JClassType.java
@@ -445,11 +445,15 @@ public abstract <T extends Annotation> T getAnnotation( Class<T> annotationClass); + public abstract Annotation[] getAnnotations(); + public abstract JConstructor getConstructor(JType[] paramTypes) throws NotFoundException; public abstract JConstructor[] getConstructors(); + public abstract Annotation[] getDeclaredAnnotations(); + public abstract JClassType getEnclosingType(); public abstract JClassType getErasedType(); @@ -771,20 +775,6 @@ abstract JClassType findNestedTypeImpl(String[] typeName, int index); /** - * Returns all of the annotations declared or inherited by this instance. - * - * NOTE: This method is for testing purposes only. - */ - abstract Annotation[] getAnnotations(); - - /** - * Returns all of the annotations declared on this instance. - * - * NOTE: This method is for testing purposes only. - */ - abstract Annotation[] getDeclaredAnnotations(); - - /** * Returns either the substitution of this type based on the parameterized * type or this instance. *
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JDelegatingClassType.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JDelegatingClassType.java index 97a3947..ce63a89 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JDelegatingClassType.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JDelegatingClassType.java
@@ -63,6 +63,11 @@ return baseType.getAnnotation(annotationClass); } + @Override + public Annotation[] getAnnotations() { + return baseType.getAnnotations(); + } + public JClassType getBaseType() { return baseType; } @@ -85,6 +90,11 @@ } @Override + public Annotation[] getDeclaredAnnotations() { + return baseType.getDeclaredAnnotations(); + } + + @Override public JClassType getEnclosingType() { // TODO this can be wrong if the enclosing type is a parameterized type. For // example, if a generic class has a non-static generic inner class. @@ -348,22 +358,6 @@ return baseType.findNestedTypeImpl(typeName, index); } - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getAnnotations() { - return baseType.getAnnotations(); - } - - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getDeclaredAnnotations() { - return baseType.getDeclaredAnnotations(); - } - @Override void notifySuperTypes() { }
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JField.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JField.java index f14af37..8875156 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JField.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JField.java
@@ -61,6 +61,14 @@ return annotations.getAnnotation(annotationClass); } + public Annotation[] getAnnotations() { + return annotations.getAnnotations(); + } + + public Annotation[] getDeclaredAnnotations() { + return annotations.getDeclaredAnnotations(); + } + public JClassType getEnclosingType() { return enclosingType; } @@ -148,20 +156,6 @@ this.modifierBits |= modifierBits; } - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getAnnotations() { - return annotations.getAnnotations(); - } - - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getDeclaredAnnotations() { - return annotations.getDeclaredAnnotations(); - } - void setType(JType type) { this.type = type; }
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JPackage.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JPackage.java index edf9cc8..807181f 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JPackage.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JPackage.java
@@ -49,6 +49,14 @@ return annotations.getAnnotation(annotationClass); } + public Annotation[] getAnnotations() { + return annotations.getAnnotations(); + } + + public Annotation[] getDeclaredAnnotations() { + return annotations.getDeclaredAnnotations(); + } + public String getName() { return name; }
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JParameter.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JParameter.java index f781132..aeda808 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JParameter.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JParameter.java
@@ -69,6 +69,14 @@ return annotations.getAnnotation(annotationClass); } + public Annotation[] getAnnotations() { + return annotations.getAnnotations(); + } + + public Annotation[] getDeclaredAnnotations() { + return annotations.getDeclaredAnnotations(); + } + public JAbstractMethod getEnclosingMethod() { return enclosingMethod; } @@ -108,20 +116,6 @@ return sb.toString(); } - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getAnnotations() { - return annotations.getAnnotations(); - } - - /** - * NOTE: This method is for testing purposes only. - */ - Annotation[] getDeclaredAnnotations() { - return annotations.getDeclaredAnnotations(); - } - // Only called by JAbstractMethod after real parameter names are fetched. void setName(String name) { this.name = StringInterner.get().intern(name);
diff --git a/dev/core/src/com/google/gwt/dev/javac/typemodel/JRealClassType.java b/dev/core/src/com/google/gwt/dev/javac/typemodel/JRealClassType.java index 66d9ebe..eb4c3d8 100644 --- a/dev/core/src/com/google/gwt/dev/javac/typemodel/JRealClassType.java +++ b/dev/core/src/com/google/gwt/dev/javac/typemodel/JRealClassType.java
@@ -139,6 +139,11 @@ } @Override + public Annotation[] getAnnotations() { + return annotations.getAnnotations(); + } + + @Override public JConstructor getConstructor(JType[] paramTypes) throws NotFoundException { return members.getConstructor(paramTypes); @@ -150,6 +155,11 @@ } @Override + public Annotation[] getDeclaredAnnotations() { + return annotations.getDeclaredAnnotations(); + } + + @Override public JClassType getEnclosingType() { return enclosingType; } @@ -529,22 +539,6 @@ interfaces = Lists.add(interfaces, intf); } - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getAnnotations() { - return annotations.getAnnotations(); - } - - /** - * NOTE: This method is for testing purposes only. - */ - @Override - Annotation[] getDeclaredAnnotations() { - return annotations.getDeclaredAnnotations(); - } - @Override JRealClassType getSubstitutedType(JParameterizedType parameterizedType) { return this;