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.apt.tests/src-annotations/org/eclipse/jdt/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.apt.tests/src-annotations/org/eclipse/jdt/apt/tests')
5 files changed, 193 insertions, 39 deletions
diff --git a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/ProcessorUtil.java b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/ProcessorUtil.java index 2061f42288..653c8b5f4d 100644 --- a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/ProcessorUtil.java +++ b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/ProcessorUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 BEA Systems, Inc. + * Copyright (c) 2014 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 @@ -7,6 +7,7 @@ * * Contributors: * wharley@bea.com - initial API and implementation + * het@google.com - Bug 441790 *******************************************************************************/ package org.eclipse.jdt.apt.tests.annotations; @@ -104,6 +105,6 @@ public final class ProcessorUtil return sb.toString(); } // boxed primitive or String - return value.toString(); + return v.toString(); } } diff --git a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessor.java b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessor.java new file mode 100644 index 0000000000..16f87d8ea6 --- /dev/null +++ b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessor.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2005, 2014 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: + * het@google.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.apt.tests.annotations.annotationvalue; + +import com.sun.mirror.apt.AnnotationProcessorEnvironment; +import com.sun.mirror.declaration.AnnotationMirror; +import com.sun.mirror.declaration.AnnotationTypeElementDeclaration; +import com.sun.mirror.declaration.AnnotationValue; +import com.sun.mirror.declaration.FieldDeclaration; +import com.sun.mirror.declaration.TypeDeclaration; + +import junit.framework.ComparisonFailure; + +import org.eclipse.jdt.apt.tests.annotations.BaseProcessor; +import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +public class AnnotationValueProcessor extends BaseProcessor { + public AnnotationValueProcessor(AnnotationProcessorEnvironment env) { + super(env); + } + + public void process() { + ProcessorTestStatus.setProcessorRan(); + try{ + TypeDeclaration typeDecl = _env.getTypeDeclaration("question.AnnotationTest"); + junit.framework.TestCase.assertNotNull("failed to locate type 'question.AnnotationTest'", typeDecl); + if( typeDecl != null){ + FieldDeclaration firstFieldDecl = null; + for (FieldDeclaration fieldDeclaration : typeDecl.getFields()) { + firstFieldDecl = fieldDeclaration; + break; + } + + AnnotationMirror rtVisibleAnnotationMirror = null; + for (AnnotationMirror annotationMirror : firstFieldDecl.getAnnotationMirrors()) { + if (annotationMirror.getAnnotationType().getDeclaration().getSimpleName().equals("RTVisibleAnno")) { + rtVisibleAnnotationMirror = annotationMirror; + break; + } + } + + final Map<String, String> namesToValues = new HashMap<String, String>(); + namesToValues.put("name", "\"Foundation\""); + namesToValues.put("boolValue", "false"); + namesToValues.put("byteValue", "16"); + namesToValues.put("charValue", "'c'"); + namesToValues.put("doubleValue", "99.0"); + namesToValues.put("floatValue", "9.0"); + namesToValues.put("intValue", "999"); + namesToValues.put("longValue", "3333"); + namesToValues.put("shortValue", "3"); + namesToValues.put("colors", "{question.Color.RED, question.Color.BLUE}"); + namesToValues.put("anno", "@question.SimpleAnnotation(value = \"core\")"); + namesToValues.put("simpleAnnos", "{@question.SimpleAnnotation(value = \"org\"), @question.SimpleAnnotation(value = \"eclipse\"), @question.SimpleAnnotation(value = \"jdt\")}"); + namesToValues.put("clazzes", "{java.lang.Object.class, java.lang.String.class}"); + namesToValues.put("clazz", "java.lang.Object.class"); + assertAnnotation(namesToValues, rtVisibleAnnotationMirror); + } + } + catch(ComparisonFailure failure) { + if (!ProcessorTestStatus.hasErrors()) { + ProcessorTestStatus.failWithoutException(failure.toString()); + } + throw failure; + } + catch(junit.framework.AssertionFailedError error) { + if (!ProcessorTestStatus.hasErrors()) { + ProcessorTestStatus.failWithoutException(error.toString()); + } + throw error; + } + } + + private void assertAnnotation(final Map<String, String> namesToValues, AnnotationMirror annotation) { + Map<AnnotationTypeElementDeclaration, AnnotationValue> values = annotation.getElementValues(); + for (Entry<AnnotationTypeElementDeclaration, AnnotationValue> e : values.entrySet()) { + String key = e.getKey().getSimpleName(); + if (namesToValues.containsKey(key)) { + junit.framework.TestCase.assertEquals(namesToValues.get(key), e.getValue().toString()); + namesToValues.remove(key); + } else { + junit.framework.TestCase.fail("Unexpected annotation element: " + key); + } + } + junit.framework.TestCase.assertEquals(0, namesToValues.size()); + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessorFactory.java b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessorFactory.java new file mode 100644 index 0000000000..e7edd09b50 --- /dev/null +++ b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/AnnotationValueProcessorFactory.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2005, 2014 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: + * het - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.apt.tests.annotations.annotationvalue; + +import java.util.Set; + +import org.eclipse.jdt.apt.tests.annotations.BaseFactory; + +import com.sun.mirror.apt.AnnotationProcessor; +import com.sun.mirror.apt.AnnotationProcessorEnvironment; +import com.sun.mirror.declaration.AnnotationTypeDeclaration; + +public class AnnotationValueProcessorFactory extends BaseFactory { + public AnnotationValueProcessorFactory() { + super("trigger.MyTrigger"); //$NON-NLS-1$ + } + + public AnnotationProcessor getProcessorFor( + Set<AnnotationTypeDeclaration> atds, + AnnotationProcessorEnvironment env) { + return new AnnotationValueProcessor(env); + } +} diff --git a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/CodeExample.java b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/CodeExample.java new file mode 100644 index 0000000000..f8f860bd5c --- /dev/null +++ b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/annotationvalue/CodeExample.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * het@google.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.apt.tests.annotations.annotationvalue; + +public class CodeExample { + public static final String PACKAGE_TRIGGER = "trigger"; + public static final String TRIGGER_CLASS = "Trigger"; + public static final String TRIGGER_CODE = + "package trigger; \n" + + "\n" + + "@MyTrigger \n" + + "public class Trigger {}"; + + public static final String MYTRIGGER_CLASS = "MyTrigger"; + public static final String MYTRIGGER_CODE = + "package trigger; \n" + + "\n" + + "public @interface MyTrigger {}"; +} diff --git a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/mirrortest/MirrorDeclarationTestAnnotationProcessor.java b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/mirrortest/MirrorDeclarationTestAnnotationProcessor.java index 9115f34188..bf32611d39 100644 --- a/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/mirrortest/MirrorDeclarationTestAnnotationProcessor.java +++ b/org.eclipse.jdt.apt.tests/src-annotations/org/eclipse/jdt/apt/tests/annotations/mirrortest/MirrorDeclarationTestAnnotationProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2014 BEA Systems, Inc. and others + * Copyright (c) 2005, 2014 BEA Systems, Inc. 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 @@ -7,7 +7,7 @@ * * Contributors: * sbandow@bea.com - initial API and implementation - * + * het@google.com - Bug 441790 *******************************************************************************/ //TODO AnnotationMirror.ElementValues() //TODO AnnotationMirror.getPosition() @@ -50,7 +50,7 @@ import com.sun.mirror.type.ReferenceType; import com.sun.mirror.util.SourcePosition; public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { - + public MirrorDeclarationTestAnnotationProcessor(AnnotationProcessorEnvironment env) { super(env); } @@ -83,36 +83,36 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { t.printStackTrace(); } } - - + + /** * Tests for: * Annotation Mirror * AnnotationTypeDeclaration * AnnotationTypeElementDeclaration * AnnotationValue - * + * * @param testClass TypeDeclaration - * + * */ private void testAnnotationImplementations(TypeDeclaration testClass) { - + //AnnotationMirror tests Collection<AnnotationMirror> annoMirrors = testClass.getAnnotationMirrors(); ProcessorTestStatus.assertEquals("Number of annotation mirrors", 1, annoMirrors.size()); - + AnnotationMirror annoMirror = annoMirrors.iterator().next(); - ProcessorTestStatus.assertTrue("Annotation mirror contents", annoMirror.toString().startsWith("@MirrorDeclarationTestAnnotation")); - + ProcessorTestStatus.assertTrue("Annotation mirror contents", annoMirror.toString().startsWith("@org.eclipse.jdt.apt.tests.annotations.mirrortest.MirrorDeclarationTestAnnotation")); + AnnotationType annoType = annoMirror.getAnnotationType(); ProcessorTestStatus.assertTrue("AnnotationType name", annoType.toString().endsWith("mirrortest.MirrorDeclarationTestAnnotation")); - + //AnnotationTypeDeclaration tests AnnotationTypeDeclaration annoTypeDecl = annoType.getDeclaration(); ProcessorTestStatus.assertEquals("AnnotationTypeDeclaration same as AnnotationType", annoType, annoTypeDecl); - - + + //AnnotationTypeElementDeclaration and AnnotationValue tests Collection<AnnotationTypeElementDeclaration> elementDeclarations = annoTypeDecl.getMethods(); ProcessorTestStatus.assertEquals("Number of methods on annotation", 2, elementDeclarations.size()); @@ -124,7 +124,7 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { elementString = ated; SourcePosition posAted = ated.getPosition(); ProcessorTestStatus.assertTrue("position should be null", posAted == null); // the anno is declared in binary - no AST. - } + } if(ated.toString().startsWith("i")) elementInt = ated; } @@ -137,19 +137,19 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { AnnotationValue valueInt = elementInt.getDefaultValue(); ProcessorTestStatus.assertEquals("", "bob", valueString.getValue()); ProcessorTestStatus.assertEquals("", new Integer(3), valueInt.getValue()); - ProcessorTestStatus.assertEquals("", "bob", valueString.toString()); + ProcessorTestStatus.assertEquals("", "\"bob\"", valueString.toString()); ProcessorTestStatus.assertEquals("", "3", valueInt.toString()); } - + /** * Tests for: * ClassDeclaration * ConstructorDeclaration - * + * * @param testClass */ private void testClassDeclaration(ClassDeclaration testClassDec) { - + //ClassDeclaration tests Collection<ConstructorDeclaration> constructDecls = testClassDec.getConstructors(); ProcessorTestStatus.assertEquals("Number of constructors", 2, constructDecls.size()); @@ -164,7 +164,7 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { } ProcessorTestStatus.assertTrue("constructor with no args", constructNoArg != null); ProcessorTestStatus.assertTrue("constructor with one (int) arg", constructIntArg != null); - + Collection<MethodDeclaration> methodDecls = testClassDec.getMethods(); ProcessorTestStatus.assertEquals("Number of methods", 5, methodDecls.size()); @@ -174,23 +174,22 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { annotationMirrors.addAll(mirrors); } ProcessorTestStatus.assertEquals("Wrong size for annotation mirrors", 3, annotationMirrors.size()); - + MethodDeclaration methodDecl = methodDecls.iterator().next(); ProcessorTestStatus.assertTrue("method declaration exists", methodDecl != null); - + ClassType superClass = testClassDec.getSuperclass(); ProcessorTestStatus.assertEquals("Object is only super", "java.lang.Object", superClass.toString()); } - + /** * Tests for: * EnumConstantDeclaration - * EnumDeclaration - * + * EnumDeclaration + * * @param testClass */ private void testEnumImplementations(TypeDeclaration testClass) { - //EnumDeclaration tests Collection<TypeDeclaration> nestedTypes = testClass.getNestedTypes(); EnumDeclaration enumDecl = null; @@ -212,14 +211,13 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { } ProcessorTestStatus.assertTrue("enum constant \"aardvark\" exists", enumConstAardvark != null); ProcessorTestStatus.assertTrue("enum constant \"anteater\" exists", enumConstAnteater != null); - + //EnumConstantDeclaration tests EnumDeclaration declaringTypeAardvark = enumConstAardvark.getDeclaringType(); EnumDeclaration declaringTypeAnteater = enumConstAnteater.getDeclaringType(); ProcessorTestStatus.assertEquals("Declaring type is EnumDec", "mirrortestpackage.DeclarationTestClass.EnumDec", declaringTypeAardvark.toString()); ProcessorTestStatus.assertEquals("Declaring type is EnumDec", "mirrortestpackage.DeclarationTestClass.EnumDec", declaringTypeAnteater.toString()); - - + //Modifier tests Modifier[] valuesArray = Modifier.values(); int valuesArrayLength = valuesArray.length; @@ -238,15 +236,14 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { ProcessorTestStatus.assertEquals("Modifier.VOLATILE", "volatile", Modifier.VOLATILE.toString()); ProcessorTestStatus.assertEquals("Modifier.valueOf(\"PUBLIC\")", Modifier.PUBLIC, Modifier.valueOf("PUBLIC")); } - + /** * Tests for: * FieldDeclaration - * + * * @param testClassDec */ private void testFieldDeclaration(ClassDeclaration testClassDec) { - //FieldDeclaration tests Collection<FieldDeclaration> fieldDecls = testClassDec.getFields(); ProcessorTestStatus.assertEquals("Number of fields", 4, fieldDecls.size()); @@ -281,16 +278,15 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { ProcessorTestStatus.assertEquals("Field gc constant value is null", null, fieldGC.getConstantValue()); ProcessorTestStatus.assertEquals("Field gc type is java.util.GregorianCalendar", "java.util.GregorianCalendar", fieldGC.getType().toString()); } - + /** * Tests for: * MethodDeclaration * ParameterDeclaration - * + * * @param testClassDec */ private void testMethodDeclaration(ClassDeclaration testClassDec) { - //Tests for MethodDeclaration Collection<MethodDeclaration> methodDecls = testClassDec.getMethods(); MethodDeclaration methodDec = null; @@ -305,7 +301,7 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { Collection<ReferenceType> thrownTypes = methodDec.getThrownTypes(); ProcessorTestStatus.assertEquals("Number of types thrown", 1, thrownTypes.size()); - + ReferenceType thrownType = thrownTypes.iterator().next(); ProcessorTestStatus.assertEquals("methodDec throws Exception", "java.lang.Exception", thrownType.toString()); ProcessorTestStatus.assertTrue("methodDec is varargs", methodDec.isVarArgs()); @@ -313,7 +309,7 @@ public class MirrorDeclarationTestAnnotationProcessor extends BaseProcessor { ProcessorTestStatus.assertEquals("Number of types thrown", 0, methodDecNoArg.getThrownTypes().size()); ProcessorTestStatus.assertTrue("methodDecNoArg is not varargs", !methodDecNoArg.isVarArgs()); - + //Tests for ParameterDeclaration Collection<ParameterDeclaration> paramDecls = methodDec.getParameters(); ParameterDeclaration paramDeclInt = null; |