diff options
author | Walter Harley | 2007-04-17 20:29:49 +0000 |
---|---|---|
committer | Walter Harley | 2007-04-17 20:29:49 +0000 |
commit | b0991463d796f8d722da593e2f8f5c9ff730ab5e (patch) | |
tree | f285e29adead589d953b80734d293903bb4ad7e1 /org.eclipse.jdt.compiler.apt.tests | |
parent | 57eb24f7ca5bd638b11e2a1afda2d825225d880e (diff) | |
download | eclipse.jdt.core-b0991463d796f8d722da593e2f8f5c9ff730ab5e.tar.gz eclipse.jdt.core-b0991463d796f8d722da593e2f8f5c9ff730ab5e.tar.xz eclipse.jdt.core-b0991463d796f8d722da593e2f8f5c9ff730ab5e.zip |
Work on Elements.hides(). Give TypeMirrorImpl and ElementImpl a BaseProcessingEnv to work with.
Diffstat (limited to 'org.eclipse.jdt.compiler.apt.tests')
7 files changed, 305 insertions, 2 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 9985bbe4b9..00e75a53be 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/elements/ElementProc.java b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elements/ElementProc.java index 86184206df..85a7f95ab9 100644 --- a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elements/ElementProc.java +++ b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/elements/ElementProc.java @@ -395,12 +395,10 @@ public class ElementProc extends BaseProcessor { reportError("The first parameter of A.methodIAString() is not of int type"); return false; } - /* TODO: can't do this, because our implementation doesn't have access to the names of primitive-typed parameters if (!("int1".equals(int1.getSimpleName().toString()))) { reportError("The first parameter of A.methodIAString() is not named int1"); return false; } - */ // FIELDS List<VariableElement> fieldsA = ElementFilter.fieldsIn(enclosedA); 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 71f87b0a26..68438b1b34 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 @@ -47,6 +47,7 @@ public class ElementUtilsProc extends BaseProcessor private TypeElement _elementFEnum; private TypeElement _elementG; private TypeElement _elementH; + private TypeElement _elementJ; private TypeElement _elementAnnoX; private ExecutableElement _annoXValue; private TypeElement _elementAnnoY; @@ -91,6 +92,18 @@ public class ElementUtilsProc extends BaseProcessor return false; } + if (!examineHidesField()) { + return false; + } + + if (!examineHidesClass()) { + return false; + } + + if (!examineHidesMethod()) { + return false; + } + reportSuccess(); return false; } @@ -126,6 +139,11 @@ public class ElementUtilsProc extends BaseProcessor reportError("element H was not found or was not a class"); return false; } + _elementJ = _elementUtils.getTypeElement("targets.model.pc.J"); + if (_elementJ == null || _elementJ.getKind() != ElementKind.CLASS) { + reportError("element J 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) { @@ -464,4 +482,250 @@ public class ElementUtilsProc extends BaseProcessor return true; } + + /** + * Test the {@link Elements#hides(Element, Element)} method for fields + * @return true if all tests passed + */ + private boolean examineHidesField() { + VariableElement fieldIntJ = null; + VariableElement fieldIntH = null; + VariableElement fieldIntG = null; + VariableElement fieldIntF = null; + ExecutableElement methodFieldIntJ = null; + for (VariableElement field : ElementFilter.fieldsIn(_elementF.getEnclosedElements())) { + if ("fieldInt".equals(field.getSimpleName().toString())) { + fieldIntF = field; + break; + } + } + for (VariableElement field : ElementFilter.fieldsIn(_elementG.getEnclosedElements())) { + if ("fieldInt".equals(field.getSimpleName().toString())) { + fieldIntG = field; + break; + } + } + for (VariableElement field : ElementFilter.fieldsIn(_elementH.getEnclosedElements())) { + if ("fieldInt".equals(field.getSimpleName().toString())) { + fieldIntH = field; + break; + } + } + for (VariableElement field : ElementFilter.fieldsIn(_elementJ.getEnclosedElements())) { + if ("fieldInt".equals(field.getSimpleName().toString())) { + fieldIntJ = field; + break; + } + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementJ.getEnclosedElements())) { + if ("fieldInt".equals(method.getSimpleName().toString())) { + methodFieldIntJ = method; + break; + } + } + if (null == fieldIntJ || null == fieldIntH || null == fieldIntG || null == fieldIntF) { + reportError("Failed to find field \"fieldInt\" in either F, G, H, or J"); + return false; + } + if (null == methodFieldIntJ) { + reportError("Failed to find method \"fieldInt()\" in J"); + return false; + } + if (_elementUtils.hides(fieldIntF, fieldIntG)) { + reportError("F.fieldInt should not hide G.fieldInt"); + return false; + } + if (!_elementUtils.hides(fieldIntG, fieldIntF)) { + reportError("G.fieldInt should hide F.fieldInt"); + return false; + } + if (!_elementUtils.hides(fieldIntH, fieldIntF)) { + reportError("H.fieldInt should hide F.fieldInt"); + return false; + } + if (_elementUtils.hides(fieldIntJ, fieldIntG)) { + reportError("J.fieldInt should not hide G.fieldInt"); + return false; + } + if (_elementUtils.hides(fieldIntJ, methodFieldIntJ)) { + reportError("field J.fieldInt should not hide method J.fieldInt()"); + return false; + } + return true; + } + + /** + * Test the {@link Elements#hides(Element, Element)} method for nested classes + * @return true if all tests passed + */ + private boolean examineHidesClass() { + TypeElement elementFChildOnF = null; + TypeElement elementFChildOnH = null; + TypeElement elementFOnJ = null; + TypeElement elementFChildOnJ = null; + TypeElement elementIFChildOnIF = null; + TypeElement elementIFChildOnH = null; + TypeElement elementIF = _elementUtils.getTypeElement("targets.model.pc.IF"); + for (TypeElement element : ElementFilter.typesIn(elementIF.getEnclosedElements())) { + String name = element.getSimpleName().toString(); + if ("IFChild".equals(name)) { + elementIFChildOnIF = element; + break; + } + } + for (TypeElement element : ElementFilter.typesIn(_elementF.getEnclosedElements())) { + String name = element.getSimpleName().toString(); + if ("FChild".equals(name)) { + elementFChildOnF = element; + break; + } + } + for (TypeElement element : ElementFilter.typesIn(_elementH.getEnclosedElements())) { + String name = element.getSimpleName().toString(); + if ("FChild".equals(name)) { + elementFChildOnH = element; + } + else if ("IFChild".equals(name)) { + elementIFChildOnH = element; + } + } + for (TypeElement element : ElementFilter.typesIn(_elementJ.getEnclosedElements())) { + String name = element.getSimpleName().toString(); + if ("FChild".equals(name)) { + elementFChildOnJ = element; + } + else if ("F".equals(name)) { + elementFOnJ = element; + } + } + + if (!_elementUtils.hides(elementFChildOnH, elementFChildOnF)) { + reportError("H.FChild should hide F.FChild"); + return false; + } + if (!_elementUtils.hides(elementIFChildOnH, elementIFChildOnIF)) { + reportError("H.IFChild should hide IF.IFChild"); + return false; + } + if (_elementUtils.hides(elementIFChildOnH, elementFChildOnF)) { + reportError("H.IFChild should not hide F.FChild"); + return false; + } + if (_elementUtils.hides(elementFChildOnF, elementFChildOnH)) { + reportError("F.FChild should not hide H.FChild"); + return false; + } + if (_elementUtils.hides(elementFChildOnJ, elementFChildOnF)) { + reportError("J.FChild should not hide F.FChild"); + return false; + } + if (_elementUtils.hides(_elementF, elementFOnJ)) { + reportError("J.F should not hide F"); + return false; + } + return true; + } + + /** + * Test the {@link Elements#hides(Element, Element)} method for methods + * @return true if all tests passed + */ + private boolean examineHidesMethod() { + ExecutableElement methodStaticOnF = null; + ExecutableElement methodStatic2OnF = null; + ExecutableElement methodT1OnF = null; + ExecutableElement methodStaticOnG = null; + ExecutableElement methodT1OnG = null; + ExecutableElement methodStaticOnH = null; + ExecutableElement methodStaticIntOnH = null; + ExecutableElement methodStaticOnJ = null; + for (ExecutableElement method : ElementFilter.methodsIn(_elementF.getEnclosedElements())) { + String name = method.getSimpleName().toString(); + if ("staticMethod".equals(name)) { + methodStaticOnF = method; + } + else if ("staticMethod2".equals(name)) { + methodStatic2OnF = method; + } + else if ("method_T1".equals(name)) { + methodT1OnF = method; + } + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementG.getEnclosedElements())) { + String name = method.getSimpleName().toString(); + if ("staticMethod".equals(name)) { + methodStaticOnG = method; + } + else if ("method_T1".equals(name)) { + methodT1OnG = method; + } + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementH.getEnclosedElements())) { + String name = method.getSimpleName().toString(); + if ("staticMethod".equals(name)) { + if (method.getParameters().isEmpty()) { + methodStaticOnH = method; + } + else { + methodStaticIntOnH = method; + } + } + } + for (ExecutableElement method : ElementFilter.methodsIn(_elementJ.getEnclosedElements())) { + String name = method.getSimpleName().toString(); + if ("staticMethod".equals(name)) { + methodStaticOnJ = method; + break; + } + } + if (methodStaticOnF == null || methodStatic2OnF == null || methodT1OnF == null) { + reportError("Failed to find an expected method on F"); + return false; + } + if (methodStaticOnG == null || methodT1OnG == null) { + reportError("Failed to find an expected method on G"); + return false; + } + if (methodStaticOnH == null || methodStaticIntOnH == null) { + reportError("Failed to find an expected method on H"); + return false; + } + if (methodStaticOnJ == null) { + reportError("Failed to find an expected method on J"); + return false; + } + + // The should-not-hide cases + if (_elementUtils.hides(methodStaticOnF, methodStaticOnG)) { + reportError("F.staticMethod() should not hide G.staticMethod()"); + return false; + } + if (_elementUtils.hides(methodStaticOnG, methodStatic2OnF)) { + reportError("G.staticMethod() should not hide F.staticMethod2()"); + return false; + } + if (_elementUtils.hides(methodStaticOnJ, methodStaticOnF)) { + reportError("J.staticMethod() should not hide F.staticMethod()"); + return false; + } + if (_elementUtils.hides(methodStaticIntOnH, methodStaticOnG)) { + reportError("H.staticMethod(int) should not hide G.staticMethod()"); + return false; + } + if (_elementUtils.hides(methodT1OnG, methodT1OnF)) { + reportError("G.methodT1() should not hide F.methodT1(), because they aren't static (JLS 8.4.8.2)"); + return false; + } + + // The should-hide cases + if (!_elementUtils.hides(methodStaticOnG, methodStaticOnF)) { + reportError("G.staticMethod() should hide F.staticMethod()"); + return false; + } + if (!_elementUtils.hides(methodStaticOnH, methodStaticOnF)) { + reportError("H.staticMethod() should hide F.staticMethod()"); + return false; + } + return true; + } } 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 5c1e4bb8c8..967c65dfd4 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 @@ -44,6 +44,16 @@ public class F<T1> { _fieldT1_private = param1; return _fieldT1_private.toString(); } + + // hidden by G.staticMethod() + public static void staticMethod() + { + } + + // not hidden by G.staticMethod - different name + public static void staticMethod2() + { + } @SuppressWarnings("deprecation") @Deprecated 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 027ae9ea16..e3e69f38bf 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 @@ -20,6 +20,11 @@ public abstract class G extends F<String> implements IA, IF { return null; } + // hides F.staticMethod() + public static void staticMethod() + { + } + // Method declared in an interface but not implemented: //public String methodIAString(int int1) //{ 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 5499cf51cb..2b653ee319 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 @@ -8,4 +8,17 @@ public class H extends G { { return null; } + + // hides G.staticMethod and F.staticMethod + public static void staticMethod() + { + } + + // different signature; does not hide G.staticMethod + public static void staticMethod(int int1) + { + } + + public class FChild {} // hides definition in F + public class IFChild {} // hides definition in IF }
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/J.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/J.java new file mode 100644 index 0000000000..acb5aaec16 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pc/J.java @@ -0,0 +1,13 @@ +package targets.model.pc; + +public class J { + int fieldInt; // same-named fields in F, G, and H do NOT hide this; unrelated enclosing classes + + int fieldInt() { return 0; } // does not hide, and is not hidden by, any of the fields named fieldInt in F, G, H, or this. + + public class FChild {} // does not hide, and is not hidden by, same class in F or H + + public class F {} // does not hide, and is not hidden by, outer class F + + public static void staticMethod() {} // does not hide, and is not hidden by, F.staticMethod() +} |