diff options
author | Harry Terkelsen | 2014-08-15 17:48:22 +0000 |
---|---|---|
committer | Harry Terkelsen | 2014-08-21 23:10:23 +0000 |
commit | a16b4bac72f18b88e0ee337c3ee2d4cf28bfb8b4 (patch) | |
tree | 384b76098b502d643606980f8c2376fcf252471d /org.eclipse.jdt.compiler.apt.tests | |
parent | 65f5152e309a08bb91b2b35f96e93b2fa93bae4f (diff) | |
download | eclipse.jdt.core-a16b4bac72f18b88e0ee337c3ee2d4cf28bfb8b4.tar.gz eclipse.jdt.core-a16b4bac72f18b88e0ee337c3ee2d4cf28bfb8b4.tar.xz eclipse.jdt.core-a16b4bac72f18b88e0ee337c3ee2d4cf28bfb8b4.zip |
Bug 441790 - AnnotationValue.toString is creating incorrect and
truncated text that cannot be used in source code
Change-Id: Icdbccc0d0ca80a862f8b4b17c67c999492333478
Signed-off-by: Harry Terkelsen <het@google.com>
Diffstat (limited to 'org.eclipse.jdt.compiler.apt.tests')
4 files changed, 183 insertions, 15 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 2524570f50..6c6a757b30 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 327a865eaa..6c34582700 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 @@ -10,4 +10,5 @@ org.eclipse.jdt.compiler.apt.tests.processors.elementutils.ElementUtilsProc org.eclipse.jdt.compiler.apt.tests.processors.typemirror.TypeMirrorProc org.eclipse.jdt.compiler.apt.tests.processors.typeutils.TypeUtilsProc org.eclipse.jdt.compiler.apt.tests.processors.negative.NegativeModelProc -org.eclipse.jdt.compiler.apt.tests.processors.inherited.ArgsConstructorProcessor
\ No newline at end of file +org.eclipse.jdt.compiler.apt.tests.processors.inherited.ArgsConstructorProcessor +org.eclipse.jdt.compiler.apt.tests.processors.annotationmirror.AnnotationMirrorProc
\ No newline at end of file diff --git a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/annotationmirror/AnnotationMirrorProc.java b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/annotationmirror/AnnotationMirrorProc.java new file mode 100644 index 0000000000..61b1a20392 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/annotationmirror/AnnotationMirrorProc.java @@ -0,0 +1,157 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corporation and others. + * 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: + * het@google.com - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jdt.compiler.apt.tests.processors.annotationmirror; + +import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +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.ElementKind; +import javax.lang.model.element.TypeElement; + +/** + * A processor that tests that the methods for {@link AnnotationMirror} work as + * expected. + */ +@SupportedAnnotationTypes("*") +@SupportedSourceVersion(SourceVersion.RELEASE_6) +public class AnnotationMirrorProc extends BaseProcessor { + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + if (roundEnv.processingOver()) { + // We're not interested in the postprocessing round. + 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 (!examineToString()) { + return false; + } + + reportSuccess(); + return false; + } + + /** + * Tests that the {@link AnnotationValue#toString()} returns a + * {@link String} that is "in a form suitable for representing this value in + * the source code of an annotation." + * @return true if all tests pass + */ + private boolean examineToString() { + TypeElement annotatedElement = _elementUtils.getTypeElement("targets.model.pc.AnnotatedWithManyTypes.Annotated"); + if (null == annotatedElement || annotatedElement.getKind() != ElementKind.CLASS) { + reportError("examineGetAnnotation: couldn't get AnnotatedWithManyTypes.Annotated element"); + return false; + } + final String badValue = "examineToString: unexpected value for "; + List<? extends AnnotationMirror> annoMirrors = annotatedElement.getAnnotationMirrors(); + AnnotationMirror annoByte = annoMirrors.get(0); + if (null == annoByte || !annoByte.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoByte(value = 3)")) { + reportError(badValue + "AnnoByte"); + return false; + } + AnnotationMirror annoBoolean = annoMirrors.get(1); + if (null == annoBoolean || !annoBoolean.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoBoolean(value = true)")) { + reportError(badValue + "AnnoBoolean"); + return false; + } + AnnotationMirror annoChar = annoMirrors.get(2); + if (null == annoChar || !annoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'c')")) { + reportError(badValue + "AnnoChar"); + return false; + } + AnnotationMirror annoDouble = annoMirrors.get(3); + if (null == annoDouble || !annoDouble.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoDouble(value = 6.3)")) { + reportError(badValue + "AnnoDouble"); + return false; + } + AnnotationMirror annoFloat = annoMirrors.get(4); + if (null == annoFloat || !annoFloat.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoFloat(value = 26.7)")) { + reportError(badValue + "AnnoFloat"); + return false; + } + AnnotationMirror annoInt = annoMirrors.get(5); + if (null == annoInt || !annoInt.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoInt(value = 19)")) { + reportError(badValue + "AnnoInt"); + return false; + } + AnnotationMirror annoLong = annoMirrors.get(6); + if (null == annoLong || !annoLong.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoLong(value = 300)")) { + reportError(badValue + "AnnoLong"); + return false; + } + AnnotationMirror annoShort = annoMirrors.get(7); + if (null == annoShort || !annoShort.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoShort(value = 289)")) { + reportError(badValue + "AnnoShort"); + return false; + } + AnnotationMirror annoString = annoMirrors.get(8); + if (null == annoString || !annoString.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoString(value = \"foo\")")) { + reportError(badValue + "AnnoString"); + return false; + } + AnnotationMirror annoEnumConst = annoMirrors.get(9); + if (null == annoEnumConst || !annoEnumConst.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoEnumConst(value = org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.A)")) { + reportError(badValue + "AnnoEnumConst"); + return false; + } + AnnotationMirror annoType = annoMirrors.get(10); + if (null == annoType || !annoType.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoType(value = java.lang.Exception.class)")) { + reportError(badValue + "AnnoType"); + return false; + } + AnnotationMirror annoAnnoChar = annoMirrors.get(11); + if (null == annoAnnoChar || !annoAnnoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoAnnoChar(value = @org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'x'))")) { + reportError(badValue + "AnnoAnnoChar"); + return false; + } + AnnotationMirror annoArrayInt = annoMirrors.get(12); + if (null == annoArrayInt || !annoArrayInt.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayInt(value = {5, 8, 11})")) { + reportError(badValue + "AnnoArrayInt"); + return false; + } + AnnotationMirror annoArrayString = annoMirrors.get(13); + if (null == annoArrayString || !annoArrayString.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayString(value = {\"bar\", \"quux\"})")) { + reportError(badValue + "AnnoArrayString"); + return false; + } + AnnotationMirror annoArrayEnumConst = annoMirrors.get(14); + if (null == annoArrayEnumConst || !annoArrayEnumConst.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayEnumConst(value = {org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.B, org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.Enum.C})")) { + reportError(badValue + "AnnoArrayEnumConst"); + return false; + } + AnnotationMirror annoArrayType = annoMirrors.get(15); + if (null == annoArrayType || !annoArrayType.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayType(value = {java.lang.String.class, targets.model.pc.AnnotatedWithManyTypes.Annotated.class})")) { + reportError(badValue + "AnnoArrayType"); + return false; + } + AnnotationMirror annoArrayAnnoChar = annoMirrors.get(16); + if (null == annoArrayAnnoChar || !annoArrayAnnoChar.toString().equals("@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoArrayAnnoChar(value = {@org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'y'), @org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos.AnnoChar(value = 'z')})")) { + reportError(badValue + "AnnoArrayAnnoChar"); + return false; + } + return true; + } +} 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 26df4c82bf..3e190e85c6 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2011 BEA Systems, Inc. + * Copyright (c) 2007, 2011 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 @@ -8,6 +8,7 @@ * Contributors: * wharley@bea.com - initial API and implementation * IBM Corporation - fix for 342936 + * het@google.com - fix for 441790 *******************************************************************************/ package org.eclipse.jdt.compiler.apt.tests; @@ -29,8 +30,9 @@ import junit.framework.TestCase; * @since 3.3 */ public class ModelTests extends TestCase { - + // Processor class names; see corresponding usage in the processor classes. + private static final String ANNOTATIONMIRRORPROC = "org.eclipse.jdt.compiler.apt.tests.processors.annotationmirror.AnnotationMirrorProc"; private static final String ELEMENTPROC = "org.eclipse.jdt.compiler.apt.tests.processors.elements.ElementProc"; private static final String GENERICSPROC = "org.eclipse.jdt.compiler.apt.tests.processors.generics.GenericsProc"; private static final String TYPEMIRRORPROC = "org.eclipse.jdt.compiler.apt.tests.processors.typemirror.TypeMirrorProc"; @@ -43,8 +45,16 @@ public class ModelTests extends TestCase { } /** + * Attempt to read various elements of the AnnotationMirror hierarchy. + */ + public void testAnnotationMirrorWithEclipseCompiler() throws IOException { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + internalTest(compiler, ANNOTATIONMIRRORPROC); + } + + /** * Validate the testElement test against the javac compiler. - * @throws IOException + * @throws IOException */ public void testElementWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -57,7 +67,7 @@ public class ModelTests extends TestCase { /** * Attempt to read various elements of the Element hierarchy. - * @throws IOException + * @throws IOException */ public void testElementWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); @@ -66,7 +76,7 @@ public class ModelTests extends TestCase { /** * Validate the testTypeMirror test against the javac compiler. - * @throws IOException + * @throws IOException */ public void testTypeMirrorWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -79,7 +89,7 @@ public class ModelTests extends TestCase { /** * Attempt to read various elements of the TypeMirror hierarchy. - * @throws IOException + * @throws IOException */ public void testTypeMirrorWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); @@ -88,7 +98,7 @@ public class ModelTests extends TestCase { /** * Validate the generics test against the javac compiler. - * @throws IOException + * @throws IOException */ public void testGenericsWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -101,7 +111,7 @@ public class ModelTests extends TestCase { /** * Test handling of generic types. - * @throws IOException + * @throws IOException */ public void testGenericsWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); @@ -110,7 +120,7 @@ public class ModelTests extends TestCase { /** * Validate the visitors test against the javac compiler. - * @throws IOException + * @throws IOException */ public void testVisitorsWithSystemCompiler() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -123,22 +133,22 @@ public class ModelTests extends TestCase { /** * Test the Visitor method implementations. - * @throws IOException + * @throws IOException */ public void testVisitorsWithEclipseCompiler() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); internalTest(compiler, VISITORPROC); } - + public void testReportedProblemsWithDiagnosticListener() throws IOException { JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); - internalTest(compiler, ELEMENTPROC, + internalTest(compiler, ELEMENTPROC, "The method staticMethod() from the type targets.jsr199.F is never used locally\n"); } /** * Test functionality by running a particular processor against the types in - * resources/targets. The processor must support "*" (the set of all annotations) + * 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 */ @@ -155,7 +165,7 @@ public class ModelTests extends TestCase { // if not, it will set it to an error value. assertEquals("succeeded", System.getProperty(processorClass)); } - + @SuppressWarnings({ "rawtypes", "unchecked" }) private void internalTest(JavaCompiler compiler, String processorClass, String errors) throws IOException { System.clearProperty(processorClass); |