diff options
author | Walter Harley | 2007-03-27 07:08:47 +0000 |
---|---|---|
committer | Walter Harley | 2007-03-27 07:08:47 +0000 |
commit | 2254b84309b2e5733188a240f2a612a1b002f2ba (patch) | |
tree | 975755cb7b8c4c139f0af95854a6d99a1839d6de /org.eclipse.jdt.compiler.apt.tests | |
parent | 6942ba89260ced8761513584f97535ed598c868d (diff) | |
download | eclipse.jdt.core-2254b84309b2e5733188a240f2a612a1b002f2ba.tar.gz eclipse.jdt.core-2254b84309b2e5733188a240f2a612a1b002f2ba.tar.xz eclipse.jdt.core-2254b84309b2e5733188a240f2a612a1b002f2ba.zip |
Some support for generics
Diffstat (limited to 'org.eclipse.jdt.compiler.apt.tests')
6 files changed, 202 insertions, 43 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 d803df36e3..4c6a290a06 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/META-INF/services/javax.annotation.processing.Processor b/org.eclipse.jdt.compiler.apt.tests/processors/META-INF/services/javax.annotation.processing.Processor index d07fe781d5..3a6cdf244d 100644 --- a/org.eclipse.jdt.compiler.apt.tests/processors/META-INF/services/javax.annotation.processing.Processor +++ b/org.eclipse.jdt.compiler.apt.tests/processors/META-INF/services/javax.annotation.processing.Processor @@ -1,4 +1,5 @@ org.eclipse.jdt.compiler.apt.tests.processors.genclass.GenClassProc org.eclipse.jdt.compiler.apt.tests.processors.checkargs.CheckArgsProc org.eclipse.jdt.compiler.apt.tests.processors.elements.ElementProc +org.eclipse.jdt.compiler.apt.tests.processors.generics.GenericsProc org.eclipse.jdt.compiler.apt.tests.processors.visitors.VisitorProc
\ No newline at end of file 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 1f93f9395e..e989396819 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 @@ -53,8 +53,6 @@ import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; @SupportedSourceVersion(SourceVersion.RELEASE_6) public class ElementProc extends BaseProcessor { - private static final String CLASSNAME = ElementProc.class.getName(); - // The set of elements we expect getRootElements to return private static final String[] ROOT_ELEMENT_NAMES = new String[] {"AnnoZ", "A", "IA", "AB", "AC", "D", "IB", "IC"}; @@ -78,7 +76,7 @@ public class ElementProc extends BaseProcessor { return false; } Map<String, String> options = processingEnv.getOptions(); - if (!options.containsKey(CLASSNAME)) { + if (!options.containsKey(this.getClass().getName())) { // Disable this processor unless we are intentionally performing the test. return false; } diff --git a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/generics/GenericsProc.java b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/generics/GenericsProc.java new file mode 100644 index 0000000000..e0179f1b90 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/generics/GenericsProc.java @@ -0,0 +1,139 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jdt.compiler.apt.tests.processors.generics; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.processing.ProcessingEnvironment; +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.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.TypeParameterElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeMirror; + +import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; + +/** + * Processor that tests our handling of parameterized types + * by exploring the parameterized types in resources/targets. + */ +@SupportedAnnotationTypes({"*"}) +@SupportedSourceVersion(SourceVersion.RELEASE_6) +public class GenericsProc extends BaseProcessor +{ + // Initialized in collectElements() + private TypeElement _elementAC; + private TypeElement _elementString; + private TypeElement _elementIterator; + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) + { + super.init(processingEnv); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) + { + if (roundEnv.processingOver()) { + return false; + } + Map<String, String> options = processingEnv.getOptions(); + if (!options.containsKey(this.getClass().getName())) { + // Disable this processor unless we are intentionally performing the test. + return false; + } + + if (!collectElements()) { + return false; + } + + if (!examineACTypeParams()) { + return false; + } + + reportSuccess(); + return false; + } + + /** + * Collect some elements that will be reused in various tests + * @return true if all tests passed + */ + private boolean collectElements() { + _elementAC = _elementUtils.getTypeElement("targets.model.pb.AC"); + if (_elementAC == null) { + reportError("element AC was not found"); + return false; + } + if (_elementAC.getKind() != ElementKind.CLASS) { + reportError("AC claims to not be a class"); + return false; + } + _elementString = _elementUtils.getTypeElement("java.lang.String"); + _elementIterator = _elementUtils.getTypeElement("java.util.Iterator"); + return true; + } + + /** + * Examine the type parameters of element AC + * @return true if all tests passed + */ + private boolean examineACTypeParams() + { + List<? extends TypeParameterElement> params = _elementAC.getTypeParameters(); + if (null == params || params.size() != 2) { + reportError("element AC does not report 2 type parameters"); + return false; + } + Iterator<? extends TypeParameterElement> iter = params.iterator(); + TypeParameterElement t1 = iter.next(); + TypeParameterElement t2 = iter.next(); + if (!"T1".equals(t1.getSimpleName().toString()) || + !"T2".equals(t2.getSimpleName().toString())) { + reportError("Type parameters of element AC are not named T1 and T2"); + return false; + } + if (t1.getKind() != ElementKind.TYPE_PARAMETER) { + reportError("Type parameter T1 of element AC claims not to be ElementKind.TYPE_PARAMTER"); + return false; + } + if (!_elementAC.equals(t2.getGenericElement())) { + reportError("Type parameter T2 of element AC does not return AC from getGenericElement()"); + return false; + } + List<? extends TypeMirror> boundsT1 = t1.getBounds(); + if (null == boundsT1 || boundsT1.size() != 2) { + reportError("Type parameter T1 of element AC has wrong number of bounds"); + return false; + } + TypeMirror boundT1_0 = boundsT1.get(0); + if (!(boundT1_0 instanceof DeclaredType) || !_elementString.equals(((DeclaredType)boundT1_0).asElement())) { + reportError("Bound[0] of type parameter T1 of element AC is not String"); + return false; + } + TypeMirror boundT1_1 = boundsT1.get(1); + if (!(boundT1_1 instanceof DeclaredType) || !_elementIterator.equals(((DeclaredType)boundT1_1).asElement())) { + reportError("Bound[1] of type parameter T1 of element AC is not Iterator"); + return false; + } + return true; + } + +} diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pb/AC.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pb/AC.java index b3616e9c71..2ab5983444 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pb/AC.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model/pb/AC.java @@ -1,7 +1,32 @@ package targets.model.pb; +import java.util.*; import targets.model.pa.IA; -public class AC implements IC, IA { - public String methodIAString(int int1) { return null; } +public class AC<T1 extends String & Iterator, T2> implements IC, IA { + + private List<String> _fieldListString = null; + + public String methodIAString(int int1) { + return null; + } + + public T1 methodGetT1(T2 paramT2) { + return null; + } + + public List<T1> methodGetListT1() { + return null; + } + + public Map<T1, List<T2>> methodGetMapT1ListT2( Iterator<T2> paramIterT2 ) { + return null; + } + + public List<? extends T1> methodGetQExtendsT1() { + return null; + } + + public <T3 extends List<T2>> void methodT3Void(T3 paramT3) { + } } diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/ModelTests.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/ModelTests.java index b626be296b..37370949a0 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/ModelTests.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/ModelTests.java @@ -17,22 +17,22 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import javax.annotation.processing.Processor; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; +import org.eclipse.jdt.compiler.apt.tests.processors.elements.ElementProc; +import org.eclipse.jdt.compiler.apt.tests.processors.generics.GenericsProc; +import org.eclipse.jdt.compiler.apt.tests.processors.visitors.VisitorProc; + import junit.framework.TestCase; /** - * + * Tests of the type system implementation * @since 3.3 */ public class ModelTests extends TestCase { - // See corresponding usages in the ElementProc class - private static final String ELEMENTPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.elements.ElementProc"; - // See corresponding usages in the VisitorProc class - private static final String VISITORPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.visitors.VisitorProc"; - @Override protected void setUp() throws Exception { super.setUp(); @@ -45,7 +45,7 @@ public class ModelTests extends TestCase { */ public void testElementWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - internalTestElement(compiler); + internalTest(compiler, ElementProc.class); } /** @@ -54,72 +54,68 @@ public class ModelTests extends TestCase { */ public void testElementWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); - internalTestElement(compiler); + internalTest(compiler, ElementProc.class); } /** - * Validate the testElement test against the javac compiler. + * Validate the generics test against the javac compiler. * @throws IOException */ - public void testVisitorsWithSystemCompiler() throws IOException { + public void testGenericsWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - internalTestVisitors(compiler); + internalTest(compiler, GenericsProc.class); } /** - * Attempt to read various elements of the Element hierarchy. + * Test handling of generic types. * @throws IOException */ - public void testVisitorsWithEclipseCompiler() throws IOException { + public void testGenericsWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); - internalTestVisitors(compiler); + internalTest(compiler, GenericsProc.class); } /** - * Attempt to read various elements of the Element hierarchy. - * @throws IOException + * Validate the visitors test against the javac compiler. + * @throws IOException */ - private void internalTestElement(JavaCompiler compiler) throws IOException { - System.clearProperty(ELEMENTPROCNAME); - File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", "model"); - BatchTestUtils.copyResources("targets/model", targetFolder); - - List<String> options = new ArrayList<String>(); - options.add("-A" + ELEMENTPROCNAME); - BatchTestUtils.compileTree(compiler, options, targetFolder); + public void testVisitorsWithSystemCompiler() throws IOException { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + internalTest(compiler, VisitorProc.class); + } - // If it succeeded, the processor will have set this property to "succeeded"; - // if not, it will set it to an error value. - assertEquals("succeeded", System.getProperty(ELEMENTPROCNAME)); + /** + * Test the Visitor method implementations. + * @throws IOException + */ + public void testVisitorsWithEclipseCompiler() throws IOException { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + internalTest(compiler, VisitorProc.class); } /** - * Test Visitor functionality. + * Test functionality by running a particular processor against the types in + * resources/targets. The processor must support "*" (the set of all annotations) + * and must report its errors or success via the methods in BaseProcessor. * @throws IOException */ - private void internalTestVisitors(JavaCompiler compiler) throws IOException { - System.clearProperty(VISITORPROCNAME); + private void internalTest(JavaCompiler compiler, Class<? extends Processor> processor) throws IOException { + System.clearProperty(processor.getName()); File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", "model"); BatchTestUtils.copyResources("targets/model", targetFolder); List<String> options = new ArrayList<String>(); - options.add("-A" + VISITORPROCNAME); + options.add("-A" + processor.getName()); BatchTestUtils.compileTree(compiler, options, targetFolder); // If it succeeded, the processor will have set this property to "succeeded"; // if not, it will set it to an error value. - assertEquals("succeeded", System.getProperty(VISITORPROCNAME)); + assertEquals("succeeded", System.getProperty(processor.getName())); } - /* (non-Javadoc) - * @see junit.framework.TestCase#tearDown() - */ @Override protected void tearDown() throws Exception { - System.clearProperty(ELEMENTPROCNAME); super.tearDown(); } - - } |