diff options
author | Jay Arthanareeswaran | 2017-08-28 16:32:18 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2017-08-28 18:30:34 +0000 |
commit | 42ca031ba70542ff8e7bc35f8a425fcb55e007a0 (patch) | |
tree | 35349d69cc2e447e306f589b91fd35333c5d5072 /org.eclipse.jdt.compiler.apt.tests | |
parent | 8471311fa0f716709945458c597063abb0c610bf (diff) | |
download | eclipse.jdt.core-42ca031ba70542ff8e7bc35f8a425fcb55e007a0.tar.gz eclipse.jdt.core-42ca031ba70542ff8e7bc35f8a425fcb55e007a0.tar.xz eclipse.jdt.core-42ca031ba70542ff8e7bc35f8a425fcb55e007a0.zip |
stored as regular annotations
Change-Id: Idab02aa6d0543a5e19d49201c9f7775066971f6d
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Diffstat (limited to 'org.eclipse.jdt.compiler.apt.tests')
5 files changed, 160 insertions, 2 deletions
diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar Binary files differindex d07b69ed96..e8edc6750a 100644 --- a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar +++ b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar diff --git a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java8ElementProcessor.java b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java8ElementProcessor.java index 5f00bd21ae..abb0616d17 100644 --- a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java8ElementProcessor.java +++ b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java8ElementProcessor.java @@ -601,8 +601,7 @@ public class Java8ElementProcessor extends BaseProcessor { assertTrue("Found unexpected extra elements", expectedElementNames.isEmpty()); } - public void testTypeAnnotations12() { - TypeElement annotatedType = _elementUtils.getTypeElement("targets.model8.X"); + private void tTypeAnnotations12(TypeElement annotatedType) { List<? extends Element> members = _elementUtils.getAllMembers(annotatedType); ExecutableElement bar2 = null; ExecutableElement constr = null; @@ -631,6 +630,16 @@ public class Java8ElementProcessor extends BaseProcessor { type = (ExecutableType) constr2.asType(); verifyAnnotations(type, new String[]{}); } + + public void testTypeAnnotations12() { + TypeElement annotatedType = _elementUtils.getTypeElement("targets.model8.X"); + tTypeAnnotations12(annotatedType); + } + + public void testTypeAnnotations12Binary() { + TypeElement annotatedType = _elementUtils.getTypeElement("targets.model9.X"); + tTypeAnnotations12(annotatedType); + } public void testTypeAnnotations13() { TypeElement annotatedType = _elementUtils.getTypeElement("targets.model8.X"); diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/model9/X.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model9/X.java new file mode 100644 index 0000000000..812e6e67a4 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/model9/X.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2017 IBM Corporation. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package targets.model9; + +import org.eclipse.jdt.compiler.apt.tests.annotations.Type; +import org.eclipse.jdt.compiler.apt.tests.annotations.Type1; +import org.eclipse.jdt.compiler.apt.tests.annotations.Type$1; +import org.eclipse.jdt.compiler.apt.tests.annotations.Type.One; + +@Type("c") +public class X extends @Type("s") Object implements @Type("i1") I, @Type("i2") J { + @Type("f") String _field = null; + @Type("f1") X _field1 = null; + @Type("f1") X _field11 = null; + @Type$1 @One String _field2 = null; + X _field3 = null; + int _i = 10; + public void noAnnotationHere() { + } + @Deprecated @Type("m") String foo() { + return null; + } + void bar(@Type("p1") String p1, @Type("p2") String p2) {} + public void bar2(@Type("receiver") X this) {} + // Static methods and top level constructors do not have receivers + public static void main(String[] args) {} + @Type("constr1") public X(){} + @Type1("constr2") public X(int i){} +} + +interface I {} +interface J {} diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java index 1a033af730..099cb11166 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java @@ -26,6 +26,10 @@ import java.io.Reader; import java.io.StringWriter; import java.net.URL; import java.nio.charset.Charset; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -36,6 +40,7 @@ import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; /** * Helper class to support compilation and results checking for tests running in batch mode. @@ -135,6 +140,60 @@ public class BatchTestUtils { junit.framework.TestCase.assertTrue("Compilation failed : " + errorOutput, false); } } + /* + * First compiles the given files without processor, then processes them + * with the just compiled binaries. + */ + public static void compileTreeAndProcessBinaries(JavaCompiler compiler, List<String> options, String processor, + File targetFolder, DiagnosticListener<? super JavaFileObject> listener) { + StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Iterable<? extends File> location = manager.getLocation(StandardLocation.CLASS_PATH); + // create new list containing inputfile + List<File> files = new ArrayList<File>(); + findFilesUnder(targetFolder, files); + Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + List<String> copyOptions = new ArrayList<>(); + copyOptions.addAll(options); + copyOptions.add("-d"); + copyOptions.add(_tmpBinFolderName); + copyOptions.add("-s"); + copyOptions.add(_tmpGenFolderName); + addProcessorPaths(copyOptions, true, true); + options.add("-XprintRounds"); + CompilationTask task = compiler.getTask(printWriter, manager, listener, copyOptions, null, units); + Boolean result = task.call(); + + if (!result.booleanValue()) { + String errorOutput = stringWriter.getBuffer().toString(); + System.err.println("Compilation failed: " + errorOutput); + junit.framework.TestCase.assertTrue("Compilation failed : " + errorOutput, false); + } + List<String> classes = new ArrayList<>(); + try { + System.clearProperty(processor); + copyOptions = new ArrayList<>(); + copyOptions.addAll(options); + copyOptions.add("-cp"); + copyOptions.add(_tmpBinFolderName + File.pathSeparator + _jls8ProcessorJarPath + File.pathSeparator + _tmpGenFolderName); + copyOptions.add("-processorpath"); + copyOptions.add(_jls8ProcessorJarPath); + classes.add("java.lang.Object"); // This is required to make sure BTB for Object is fully populated. + findClassesUnder(Paths.get(_tmpBinFolderName), null, classes); + manager.setLocation(StandardLocation.CLASS_PATH, location); + task = compiler.getTask(printWriter, manager, listener, copyOptions, classes, null); + result = task.call(); + if (!result.booleanValue()) { + String errorOutput = stringWriter.getBuffer().toString(); + System.err.println("Compilation failed: " + errorOutput); + junit.framework.TestCase.assertTrue("Compilation failed : " + errorOutput, false); + } + } catch (IOException e) { + // print the stack just in case. + e.printStackTrace(); + } + } /** * Compile the contents of a directory tree, collecting errors so that they can be @@ -206,6 +265,24 @@ public class BatchTestUtils { } } + protected static void findClassesUnder(Path root, Path folder, List<String> classes) throws IOException { + if (folder == null) + folder = root; + try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder)) { + for (Path entry : stream) { + if (Files.isDirectory(entry)) { + findClassesUnder(root, entry, classes); + } else { + if (entry.getFileName().toString().endsWith(".class")) { + String className = root.relativize(entry).toString(); + className = className.substring(0, className.indexOf(".class")); + className = className.replace(File.separatorChar, '.'); + classes.add(className); + } + } + } + } + } /** @return the name of the folder where class files will be saved */ public static String getBinFolderName() { return _tmpBinFolderName; diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java8ElementsTests.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java8ElementsTests.java index bee51c6d80..0b65a0d0d2 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java8ElementsTests.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java8ElementsTests.java @@ -160,6 +160,10 @@ public class Java8ElementsTests extends TestCase { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); internalTest(compiler, JAVA8_ANNOTATION_PROC, "testTypeAnnotations12"); } + public void testTypeAnnotations12Binary() throws Exception { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + internalTestWithBinary(compiler, JAVA8_ANNOTATION_PROC, "testTypeAnnotations12Binary", null, "model9"); + } public void testTypeAnnotations13() throws Exception { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); internalTest(compiler, JAVA8_ANNOTATION_PROC, "testTypeAnnotations13"); @@ -374,6 +378,34 @@ public class Java8ElementsTests extends TestCase { // if not, it will set it to an error value. assertEquals("succeeded", System.getProperty(processor)); } + private void internalTestWithBinary(JavaCompiler compiler, String processor, String testMethod, String testClass, String resourceArea) throws IOException { + if (!canRunJava8()) { + return; + } + System.clearProperty(processor); + File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", resourceArea); + if (testClass == null || testClass.equals("")) { + BatchTestUtils.copyResources("targets/" + resourceArea, targetFolder); + } else { + BatchTestUtils.copyResource("targets/" + resourceArea + "/" + testClass, targetFolder); + } + + + List<String> options = new ArrayList<String>(); + options.add("-A" + processor); + options.add("-A" + testMethod); + options.add("-processor"); + options.add(processor); + // Javac 1.8 doesn't (yet?) support the -1.8 option + if (compiler instanceof EclipseCompiler) { + options.add("-1.8"); + } + BatchTestUtils.compileTreeAndProcessBinaries(compiler, options, processor, targetFolder, null); + + // 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(processor)); + } public boolean canRunJava8() { try { SourceVersion.valueOf("RELEASE_8"); |