diff options
author | Walter Harley | 2007-04-08 19:54:57 +0000 |
---|---|---|
committer | Walter Harley | 2007-04-08 19:54:57 +0000 |
commit | ad2281f1520933e65d479408c685e1055d82a8b2 (patch) | |
tree | c3bc29cd45ca2c28593f4d72505618480ccbbc4b /org.eclipse.jdt.compiler.apt.tests | |
parent | c7e13eb999aeeb70fd34b19df0f27a778109206c (diff) | |
download | eclipse.jdt.core-ad2281f1520933e65d479408c685e1055d82a8b2.tar.gz eclipse.jdt.core-ad2281f1520933e65d479408c685e1055d82a8b2.tar.xz eclipse.jdt.core-ad2281f1520933e65d479408c685e1055d82a8b2.zip |
More work on Elements
Diffstat (limited to 'org.eclipse.jdt.compiler.apt.tests')
9 files changed, 246 insertions, 0 deletions
diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar Binary files differindex b283902645..3512fa4d23 100644 --- a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar +++ b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar diff --git a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elementutils/ElementUtilsProc.java b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elementutils/ElementUtilsProc.java index cbf995369b..18ec26cbb9 100644 --- a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elementutils/ElementUtilsProc.java +++ b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elementutils/ElementUtilsProc.java @@ -19,12 +19,15 @@ import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.util.ElementFilter; +import javax.lang.model.util.Elements; import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; @@ -40,6 +43,11 @@ public class ElementUtilsProc extends BaseProcessor // Initialized in collectElements() private TypeElement _elementF; private TypeElement _elementG; + private TypeElement _elementH; + private TypeElement _elementAnnoX; + private ExecutableElement _annoXValue; + private TypeElement _elementAnnoY; + private ExecutableElement _annoYValue; // Always return false from this processor, because it supports "*". // The return value does not signify success or failure! @@ -60,10 +68,18 @@ public class ElementUtilsProc extends BaseProcessor return false; } + if (!examineGetAllAnnotations()) { + return false; + } + if (!examineGetAllMembers()) { return false; } + if (!examineIsDeprecated()) { + return false; + } + reportSuccess(); return true; } @@ -84,10 +100,100 @@ public class ElementUtilsProc extends BaseProcessor reportError("element G was not found or was not a class"); return false; } + _elementH = _elementUtils.getTypeElement("targets.model.pc.H"); + if (_elementH == null || _elementH.getKind() != ElementKind.CLASS) { + reportError("element H was not found or was not a class"); + return false; + } + + _elementAnnoX = _elementUtils.getTypeElement("targets.model.pc.AnnoX"); + if (null == _elementAnnoX || _elementAnnoX.getKind() != ElementKind.ANNOTATION_TYPE) { + reportError("annotation type annoX was not found or was not an annotation"); + return false; + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementAnnoX.getEnclosedElements())) { + if ("value".equals(method.getSimpleName().toString())) { + _annoXValue = method; + } + } + if (null == _annoXValue) { + reportError("Could not find value() method in annotation type AnnoX"); + return false; + } + + _elementAnnoY = _elementUtils.getTypeElement("targets.model.pc.AnnoY"); + if (null == _elementAnnoY || _elementAnnoY.getKind() != ElementKind.ANNOTATION_TYPE) { + reportError("annotation type annoY was not found or was not an annotation"); + return false; + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementAnnoY.getEnclosedElements())) { + if ("value".equals(method.getSimpleName().toString())) { + _annoYValue = method; + } + } + if (null == _annoYValue) { + reportError("Could not find value() method in annotation type AnnoY"); + return false; + } + return true; } /** + * Test the {@link Elements#getAllAnnotationMirrors()} method + * @return true if all tests passed + */ + private boolean examineGetAllAnnotations() + { + List<? extends AnnotationMirror> annotationsH = _elementUtils.getAllAnnotationMirrors(_elementH); + if (null == annotationsH) { + reportError("getAllAnnotationMirrors(_elementH) returned null"); + return false; + } + // H has AnnoY("on H"), G has AnnoX("on G"), and F has hidden AnnoY("on F"). + int foundF = 0; + int foundG = 0; + int foundH = 0; + for (AnnotationMirror anno : annotationsH) { + Map<? extends ExecutableElement, ? extends AnnotationValue> values = anno.getElementValues(); + AnnotationValue valueY = values.get(_annoYValue); + if (null != valueY) { + if ("on F".equals(valueY.getValue())) { + foundF++; + } + else if ("on H".equals(valueY.getValue())) { + foundH++; + } + else { + reportError("unexpected value for annotation AnnoY"); + return false; + } + } + else { + AnnotationValue valueX = values.get(_annoXValue); + if (null != valueX) { + if ("on G".equals(valueX.getValue())) { + foundG++; + } + else { + reportError("unexpected value for annotation AnnoX"); + return false; + } + } + else { + reportError("getAllAnnotationMirrors(_elementH) returned a mirror with no value()"); + return false; + } + } + } + if (0 != foundF || 1 != foundG || 1 != foundH) { + reportError("getAllAnnotationMirrors() found wrong number of annotations on H"); + return false; + } + return true; + } + + /** * Test the {@link Elements#getAllMembers()} method * @return true if all tests passed */ @@ -182,4 +288,89 @@ public class ElementUtilsProc extends BaseProcessor return true; } + /** + * Test the {@link Elements#isDeprecated()} method + * @return true if all tests passed + */ + private boolean examineIsDeprecated() + { + Element _deprecatedElem = _elementUtils.getTypeElement("targets.model.pc.Deprecation"); + if (null == _deprecatedElem) { + reportError("Couldn't find targets.model.pc.Deprecation"); + return false; + } + ExecutableElement methodDeprecated = null; + ExecutableElement methodNotDeprecated = null; + for (ExecutableElement method : ElementFilter.methodsIn(_deprecatedElem.getEnclosedElements())) { + if ("deprecatedMethod".equals(method.getSimpleName().toString())) { + methodDeprecated = method; + } + else if ("nonDeprecatedMethod".equals(method.getSimpleName().toString())) { + methodNotDeprecated = method; + } + } + if (null == methodDeprecated || null == methodNotDeprecated) { + reportError("Could not find methods Deprecation.deprecatedMethod() or Deprecation.nonDeprecatedMethod()"); + return false; + } + if (_elementUtils.isDeprecated(methodNotDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.nonDeprecatedMethod()) is true"); + return false; + } + if (!_elementUtils.isDeprecated(methodDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.deprecatedMethod()) is false"); + return false; + } + TypeElement classDeprecated = null; + TypeElement classNotDeprecated = null; + TypeElement interfaceDeprecated = null; + TypeElement interfaceNotDeprecated = null; + for (TypeElement type : ElementFilter.typesIn(_deprecatedElem.getEnclosedElements())) { + if ("deprecatedClass".equals(type.getSimpleName().toString())) { + classDeprecated = type; + } + else if ("nonDeprecatedClass".equals(type.getSimpleName().toString())) { + classNotDeprecated = type; + } + else if ("deprecatedInterface".equals(type.getSimpleName().toString())) { + interfaceDeprecated = type; + } + else if ("nonDeprecatedInterface".equals(type.getSimpleName().toString())) { + interfaceNotDeprecated = type; + } + } + if (null == classDeprecated || null == classNotDeprecated) { + reportError("Could not find methods Deprecation.deprecatedClass() or Deprecation.nonDeprecatedClass()"); + return false; + } + if (null == interfaceDeprecated || null == interfaceNotDeprecated) { + reportError("Could not find methods Deprecation.deprecatedInterface() or Deprecation.nonDeprecatedInterface()"); + return false; + } + if (_elementUtils.isDeprecated(classNotDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.nonDeprecatedClass()) is true"); + return false; + } + if (!_elementUtils.isDeprecated(classDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.deprecatedClass()) is false"); + return false; + } + if (_elementUtils.isDeprecated(interfaceNotDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.nonDeprecatedInterface()) is true"); + return false; + } + if (!_elementUtils.isDeprecated(interfaceDeprecated)) { + reportError("ElementUtils.isDeprecated(Deprecation.deprecatedInterface()) is false"); + return false; + } + + TypeElement deprecatedInnerClass = _elementUtils.getTypeElement("targets.model.pc.Deprecation.deprecatedClass"); + if (null == deprecatedInnerClass) { + reportError("Couldn't find class Deprecation.deprecatedClass"); + return false; + } + + return true; + } + } diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoX.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoX.java new file mode 100644 index 0000000000..a5e69d05d7 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoX.java @@ -0,0 +1,8 @@ +package targets.model.pc; + +import java.lang.annotation.Inherited; + +@Inherited +@interface AnnoX { + String value(); +}
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoY.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoY.java new file mode 100644 index 0000000000..f483834c0e --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/AnnoY.java @@ -0,0 +1,5 @@ +package targets.model.pc; + +@interface AnnoY { + String value(); +}
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/Deprecation.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/Deprecation.java new file mode 100644 index 0000000000..74671e594b --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/Deprecation.java @@ -0,0 +1,30 @@ +package targets.model.pc; + +@SuppressWarnings("deprecation") +@Deprecated +public class Deprecation { + @Deprecated + public class deprecatedClass {} + + @Deprecated + public enum deprecatedEnum { Val1 } + + @Deprecated + public interface deprecatedInterface {} + + @Deprecated + public String deprecatedField; + + @Deprecated + void deprecatedMethod() {} + + public class nonDeprecatedClass {} + + public enum nonDeprecatedEnum { Val1 } + + public interface nonDeprecatedInterface {} + + public String nonDeprecatedField; + + void nonDeprecatedMethod() {} +}
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/F.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/F.java index bc45ecb902..9b12b50154 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/F.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/F.java @@ -1,5 +1,6 @@ package targets.model.pc; +@AnnoY("on F") public class F<T1> { public class FChild { } @@ -13,6 +14,7 @@ public class F<T1> { int fieldInt; + @AnnoY("on F.method_T1") T1 method_T1(T1 param1) { return null; @@ -23,4 +25,10 @@ public class F<T1> { _fieldT1_private = param1; return _fieldT1_private.toString(); } + + @SuppressWarnings("deprecation") + @Deprecated + void deprecatedMethod() + { + } }
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/G.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/G.java index 8abdfa6ce5..027ae9ea16 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/G.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/G.java @@ -2,12 +2,14 @@ package targets.model.pc; import targets.model.pa.IA; +@AnnoX("on G") public abstract class G extends F<String> implements IA, IF { public String _fieldString; int fieldInt; // hides definition in F @Override + @AnnoY("on G.method_T1") String method_T1(String param1) { return null; diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/H.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/H.java index ea1dac28b7..5499cf51cb 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/H.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/H.java @@ -1,5 +1,6 @@ package targets.model.pc; +@AnnoY("on H") public class H extends G { int fieldInt; // hides definition in G diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/IF.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/IF.java index 5b9d505968..8533c46e08 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/IF.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/IF.java @@ -1,5 +1,6 @@ package targets.model.pc; +@AnnoY("on IF") public interface IF { public class IFChild { |