### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/core/IMemberValuePair.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IMemberValuePair.java,v retrieving revision 1.6 diff -u -r1.6 IMemberValuePair.java --- model/org/eclipse/jdt/core/IMemberValuePair.java 27 Jun 2008 16:04:01 -0000 1.6 +++ model/org/eclipse/jdt/core/IMemberValuePair.java 6 Nov 2009 08:22:48 -0000 @@ -125,7 +125,7 @@ * analyzed to determine its kind. For example, in @MyAnnot({3.4, 1 + 2.3}), * the kind of the second element "1 + 2.3" is unknown. *
  • the value is an array that contains heterogeneous values, e.g. - * @MyAnnot(1, 2.3, "abc")
  • + * @MyAnnot({1, 2.3, "abc"}) * * If the value kind is unknown, the returned value is always either null, or an * array containing {@link Object}s and/or nulls for unknown elements. Index: model/org/eclipse/jdt/internal/core/CompilationUnitStructureRequestor.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnitStructureRequestor.java,v retrieving revision 1.85 diff -u -r1.85 CompilationUnitStructureRequestor.java --- model/org/eclipse/jdt/internal/core/CompilationUnitStructureRequestor.java 7 Mar 2009 00:58:56 -0000 1.85 +++ model/org/eclipse/jdt/internal/core/CompilationUnitStructureRequestor.java 6 Nov 2009 08:22:49 -0000 @@ -29,15 +29,17 @@ import org.eclipse.jdt.internal.compiler.ast.Literal; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.NullLiteral; +import org.eclipse.jdt.internal.compiler.ast.OperatorIds; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; +import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; import org.eclipse.jdt.internal.compiler.parser.Parser; import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner; import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt; import org.eclipse.jdt.internal.core.util.ReferenceInfoAdapter; import org.eclipse.jdt.internal.core.util.Util; - +import org.eclipse.jdt.internal.compiler.ast.ASTNode; /** * A requestor for the fuzzy parser, used to compute the children of an ICompilationUnit. */ @@ -719,7 +721,26 @@ if (memberValuePair.valueKind == -1) memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return values; + } else if (expression instanceof UnaryExpression) { // to deal with negative numerals (see bug - 248312) + UnaryExpression unaryExpression = (UnaryExpression) expression; + if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) { + if (unaryExpression.expression instanceof QualifiedNameReference) { + QualifiedNameReference subExpression = (QualifiedNameReference) unaryExpression.expression; + char[] qualifiedName = CharOperation.concatWith((subExpression).tokens, '.'); + char[] sign = {'-'}; + qualifiedName = CharOperation.concat(sign, qualifiedName); + memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME; + return new String(qualifiedName); + } else if (unaryExpression.expression instanceof Literal) { + Literal subExpression = (Literal) unaryExpression.expression; + subExpression.computeConstant(); + return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant); + } + } + memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; + return null; } else { + memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } } Index: model/org/eclipse/jdt/internal/core/LocalVariable.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/LocalVariable.java,v retrieving revision 1.29 diff -u -r1.29 LocalVariable.java --- model/org/eclipse/jdt/internal/core/LocalVariable.java 21 Aug 2008 10:15:27 -0000 1.29 +++ model/org/eclipse/jdt/internal/core/LocalVariable.java 6 Nov 2009 08:22:49 -0000 @@ -17,13 +17,16 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.*; import org.eclipse.jdt.core.compiler.CharOperation; +import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.Literal; import org.eclipse.jdt.internal.compiler.ast.NullLiteral; +import org.eclipse.jdt.internal.compiler.ast.OperatorIds; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; +import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner; import org.eclipse.jdt.internal.core.util.MementoTokenizer; import org.eclipse.jdt.internal.core.util.Util; @@ -198,6 +201,24 @@ if (memberValuePair.valueKind == -1) memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return values; + } else if (expression instanceof UnaryExpression) { //to deal with negative numerals (see bug - 248312) + UnaryExpression unaryExpression = (UnaryExpression) expression; + if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) { + if (unaryExpression.expression instanceof QualifiedNameReference) { + QualifiedNameReference subExpression = (QualifiedNameReference) unaryExpression.expression; + char[] qualifiedName = CharOperation.concatWith((subExpression).tokens, '.'); + char[] sign = {'-'}; + qualifiedName = CharOperation.concat(sign, qualifiedName); + memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME; + return new String(qualifiedName); + } else if (unaryExpression.expression instanceof Literal) { + Literal subExpression = (Literal) unaryExpression.expression; + subExpression.computeConstant(); + return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant); + } + } + memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; + return null; } else { memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; Index: model/org/eclipse/jdt/internal/core/util/Util.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java,v retrieving revision 1.136 diff -u -r1.136 Util.java --- model/org/eclipse/jdt/internal/core/util/Util.java 26 Oct 2009 17:20:02 -0000 1.136 +++ model/org/eclipse/jdt/internal/core/util/Util.java 6 Nov 2009 08:22:51 -0000 @@ -3160,6 +3160,34 @@ return null; } } + + /* + * Creates a member value from the given constant in case of negative numerals, + * and sets the valueKind on the given memberValuePair + */ + public static Object getNegativeAnnotationMemberValue(MemberValuePair memberValuePair, Constant constant) { + if (constant == null) { + memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; + return null; + } + switch (constant.typeID()) { + case TypeIds.T_int : + memberValuePair.valueKind = IMemberValuePair.K_INT; + return new Integer(constant.intValue() * -1); + case TypeIds.T_float : + memberValuePair.valueKind = IMemberValuePair.K_FLOAT; + return new Float(constant.floatValue() * -1.0f); + case TypeIds.T_double : + memberValuePair.valueKind = IMemberValuePair.K_DOUBLE; + return new Double(constant.doubleValue() * -1.0); + case TypeIds.T_long : + memberValuePair.valueKind = IMemberValuePair.K_LONG; + return new Long(constant.longValue() * -1L); + default: + memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; + return null; + } + } /** * Split signatures of all levels from a type unique key. * #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/model/ClassFileTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ClassFileTests.java,v retrieving revision 1.47 diff -u -r1.47 ClassFileTests.java --- src/org/eclipse/jdt/core/tests/model/ClassFileTests.java 13 Aug 2009 01:22:32 -0000 1.47 +++ src/org/eclipse/jdt/core/tests/model/ClassFileTests.java 6 Nov 2009 08:22:54 -0000 @@ -124,6 +124,14 @@ " void foo12() {}\n" + " @MyAnnot(_array={1, 2, 3})\n" + " void foo13() {}\n" + + " @MyAnnot(_neg_int = -2)\n" + + " void foo14() {}\n" + + " @MyAnnot(_neg_float=-2.0f)\n" + + " void foo15() {}\n" + + " @MyAnnot(_neg_double=-2.0)\n" + + " void foo16() {}\n" + + " @MyAnnot(_neg_long=-2L)\n" + + " void foo17() {}\n" + "}\n" + "@interface MyAnnot {\n" + " int _int() default 0;\n" + @@ -139,6 +147,10 @@ " Class _class() default Object.class;\n" + " MyEnum _enum() default MyEnum.FIRST;\n" + " int[] _array() default {};\n" + + " int _neg_int() default -1;\n" + + " float _neg_float() default -1.0f;\n" + + " double _neg_double() default -1.0;\n" + + " long _neg_long() default -1L;\n" + "}\n" + "@interface MyOtherAnnot {\n" + "}\n" + @@ -455,6 +467,54 @@ } /* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that an annotation with a negative int value is correct + */ +public void testAnnotations22() throws JavaModelException { + IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType(); + IMethod method = type.getMethod("foo14", new String[0]); + assertAnnotationsEqual( + "@annotated.MyAnnot(_neg_int=(int)-2)\n", + method.getAnnotations()); +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that an annotation with a negative float value is correct + */ +public void testAnnotations23() throws JavaModelException { + IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType(); + IMethod method = type.getMethod("foo15", new String[0]); + assertAnnotationsEqual( + "@annotated.MyAnnot(_neg_float=-2.0f)\n", + method.getAnnotations()); +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that an annotation with a negative double value is correct + */ +public void testAnnotations24() throws JavaModelException { + IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType(); + IMethod method = type.getMethod("foo16", new String[0]); + assertAnnotationsEqual( + "@annotated.MyAnnot(_neg_double=(double)-2.0)\n", + method.getAnnotations()); +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that an annotation with a negative long value is correct + */ +public void testAnnotations25() throws JavaModelException { + IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType(); + IMethod method = type.getMethod("foo17", new String[0]); + assertAnnotationsEqual( + "@annotated.MyAnnot(_neg_long=-2L)\n", + method.getAnnotations()); +} + +/* * Ensures that no exception is thrown for a .class file name with a dot * (regression test for bug 114140 assertion failed when opening a class file not not the classpath) */ Index: src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java,v retrieving revision 1.69 diff -u -r1.69 CompilationUnitTests.java --- src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java 28 Jan 2009 12:34:52 -0000 1.69 +++ src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java 6 Nov 2009 08:22:55 -0000 @@ -285,6 +285,114 @@ } /* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that the default value (a negative int) for an annotation method is correct. + */ +public void testDefaultValue7() throws CoreException { + try { + String cuSource = + "package p;\n" + + "public @interface Y {\n" + + " public int member() default -1;\n" + + "}"; + createFile("/P/src/p/Y.java", cuSource); + IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]); + assertMemberValuePairEquals( + "member=(int)-1", + method.getDefaultValue()); + } finally { + deleteFile("/P/src/p/Y.java"); + } +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that the default value (a negative float) for an annotation method is correct. + */ +public void testDefaultValue8() throws CoreException { + try { + String cuSource = + "package p;\n" + + "public @interface Y {\n" + + " public float member() default -1.0f;\n" + + "}"; + createFile("/P/src/p/Y.java", cuSource); + IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]); + assertMemberValuePairEquals( + "member=-1.0f", + method.getDefaultValue()); + } finally { + deleteFile("/P/src/p/Y.java"); + } +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that the default value (a negative double) for an annotation method is correct. + */ +public void testDefaultValue9() throws CoreException { + try { + String cuSource = + "package p;\n" + + "public @interface Y {\n" + + " public double member() default -1.0;\n" + + "}"; + createFile("/P/src/p/Y.java", cuSource); + IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]); + assertMemberValuePairEquals( + "member=(double)-1.0", + method.getDefaultValue()); + } finally { + deleteFile("/P/src/p/Y.java"); + } +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that the default value (a negative long) for an annotation method is correct. + */ +public void testDefaultValue10() throws CoreException { + try { + String cuSource = + "package p;\n" + + "public @interface Y {\n" + + " public long member() default -1L;\n" + + "}"; + createFile("/P/src/p/Y.java", cuSource); + IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]); + assertMemberValuePairEquals( + "member=-1L", + method.getDefaultValue()); + } finally { + deleteFile("/P/src/p/Y.java"); + } +} + +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that the default value (a sign appended Qualified Name Reference) for an annotation method is correct. + */ +public void testDefaultValue11() throws CoreException { + try { + String cuSource = + "package p;\n" + + "interface A {\n" + + " static int VAL = 1;\n" + + "}\n" + + "public @interface Y {\n" + + " public int member() default -A.VAL;\n" + + "}"; + createFile("/P/src/p/Y.java", cuSource); + IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]); + assertMemberValuePairEquals( + "member=-A.VAL", + method.getDefaultValue()); + } finally { + deleteFile("/P/src/p/Y.java"); + } +} + +/* * Ensure that the deprecated flag is correctly reported * (regression test fo bug 23207 Flags.isDeprecated(IMethod.getFlags()) doesn't work) */ @@ -2290,5 +2398,41 @@ this.workingCopy.getSource()); } +/* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312 + * Ensures that negative values work while annotating local variables. + */ +public void testBug248312() throws CoreException{ + createWorkingCopy( + "package p;\n" + + "interface A {\n" + + " static int VAL = 2;\n" + + "}\n" + + "public @interface Y {\n" + + " public int member_int() default -1;\n" + + " public int member_int2() default -1;\n" + + " public float member_float() default -1.0f\n" + + " public double member_double=-1.0\n" + + " public long member_long=-1L\n" + + "}\n" + + "public class Test{\n" + + " void testMethod(){\n" + + " @Y(member_int=-2) @Y(member_float=-2.0f)\n" + + " @Y(member_double=-2.0) @Y(member_long=-2L)\n" + + " @Y(member_int2=-A.VAL)\n" + + " Object testField1\n" + + " }\n" + + "}" + ); + ILocalVariable variable1 = selectLocalVariable(this.workingCopy, "testField1"); + IAnnotation[] annotations = variable1.getAnnotations(); + assertAnnotationsEqual( + "@Y(member_int=(int)-2)\n" + + "@Y(member_float=-2.0f)\n" + + "@Y(member_double=(double)-2.0)\n" + + "@Y(member_long=-2L)\n" + + "@Y(member_int2=-A.VAL)\n", + annotations); +} }