### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/model/GetSourceTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/GetSourceTests.java,v retrieving revision 1.21 diff -u -r1.21 GetSourceTests.java --- src/org/eclipse/jdt/core/tests/model/GetSourceTests.java 30 Nov 2007 11:48:27 -0000 1.21 +++ src/org/eclipse/jdt/core/tests/model/GetSourceTests.java 10 Apr 2008 12:02:55 -0000 @@ -19,9 +19,32 @@ ICompilationUnit cu; + // Use this static initializer to specify subset for tests + // All specified tests which do not belong to the class are skipped... + static { + // Names of tests to run: can be "testBugXXXX" or "BugXXXX") +// TESTS_NAMES = new String[] { "TypeParameterBug73884" }; + // Numbers of tests to run: "test" will be run for each number of this array +// TESTS_NUMBERS = new int[] { 13 }; + // Range numbers of tests to run: all tests between "test" and "test" will be run for { first, last } +// TESTS_RANGE = new int[] { 16, -1 }; + } + public GetSourceTests(String name) { super(name); } + + public static Test suite() { + return buildModelTestSuite(GetSourceTests.class); + } + /** + * Test the field constant + */ + private IField getConstantField(String fieldName) { + IType type = getCompilationUnit("/P/p/Constants.java").getType("Constants"); + IField field = type.getField(fieldName); + return field; + } public void setUpSuite() throws Exception { super.setUpSuite(); @@ -66,20 +89,6 @@ "}"; createFile("/P/p/Constants.java", cuSource); } - - // Use this static initializer to specify subset for tests - // All specified tests which do not belong to the class are skipped... - static { - // Names of tests to run: can be "testBugXXXX" or "BugXXXX") -// TESTS_NAMES = new String[] { "TypeParameterBug73884" }; - // Numbers of tests to run: "test" will be run for each number of this array -// TESTS_NUMBERS = new int[] { 13 }; - // Range numbers of tests to run: all tests between "test" and "test" will be run for { first, last } -// TESTS_RANGE = new int[] { 16, -1 }; - } - public static Test suite() { - return buildModelTestSuite(GetSourceTests.class); - } public void tearDownSuite() throws Exception { deleteProject("P"); @@ -99,6 +108,61 @@ assertSourceEquals("Unexpected source'", expectedSource, actualSource); } + public void testFieldConstant01() throws CoreException { + IField field = getConstantField("field1"); + + Object constant = field.getConstant(); + Long value = (Long) constant; + assertEquals("Wrong value", 938245798324893l, value.longValue()); + } + + public void testFieldConstant02() throws CoreException { + IField field = getConstantField("field2"); + + Object constant = field.getConstant(); + Long value = (Long) constant; + assertEquals("Wrong value", 938245798324893l, value.longValue()); + } + + public void testFieldConstant03() throws CoreException { + IField field = getConstantField("field3"); + + Object constant = field.getConstant(); + Long value = (Long) constant; + assertEquals("Wrong value", 938245798324893l, value.longValue()); + } + + public void testFieldConstant04() throws CoreException { + IField field = getConstantField("field4"); + + Object constant = field.getConstant(); + Character character = (Character) constant; + assertEquals("Wrong value", ' ', character.charValue()); + } + + public void testFieldConstant05() throws CoreException { + IField field = getConstantField("field5"); + + Object constant = field.getConstant(); + Double double1 = (Double) constant; + assertEquals("Wrong value", 938245798324893l, double1.doubleValue(), 0.01); + } + + public void testFieldConstant06() throws CoreException { + IField field = getConstantField("field6"); + + Object constant = field.getConstant(); + Float float1 = (Float) constant; + assertEquals("Wrong value", 123456, float1.floatValue(), 0.01f); + } + + public void testFieldConstant07() throws CoreException { + IField field = getConstantField("field7"); + + Object constant = field.getConstant(); + assertNull("Should not be a constant", constant); + } + /** * Ensure the source for an import contains the 'import' keyword, * name, and terminator. @@ -110,7 +174,7 @@ String expectedSource = "import java.lang.*;"; assertSourceEquals("Unexpected source'", expectedSource, actualSource); } - + /* * Ensures the source for a local variable contains the modifiers, type and name. */ @@ -121,7 +185,7 @@ String expectedSource = "final int var1 = 2;"; assertSourceEquals("Unexpected source'", expectedSource, actualSource); } - + /* * Ensures the source for a local variable contains the modifiers, type and name. */ @@ -132,7 +196,7 @@ String expectedSource = "Object var2;"; assertSourceEquals("Unexpected source'", expectedSource, actualSource); } - + /* * Ensures the source for a local variable contains the modifiers, type and name. */ @@ -143,7 +207,7 @@ String expectedSource = "int i = 0"; // semi-colon is not part of the local declaration in a for statement assertSourceEquals("Unexpected source'", expectedSource, actualSource); } - + /* * Ensures the source for a local variable contains the modifiers, type and name. */ @@ -172,65 +236,62 @@ } /* - * Ensures the name range for a method with syntax errors in its header is correct. - * (regression test for bug 43139 Delete member in Outliner not working) + * Ensures that the name range for an annotation is correct. */ - public void testNameRangeMethodWithSyntaxError() throws CoreException { + public void testNameRange01() throws CoreException { // was testAnnotationNameRange1 try { String cuSource = "package p;\n" + + "@ MyAnnot (1)\n" + "public class Y {\n" + - " void foo() {\n" + - " }\n" + - " void static bar() {}\n" + "}"; createFile("/P/p/Y.java", cuSource); - IMethod method= getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("bar", new String[0]); - - String actualSource = getNameSource(cuSource, method); - String expectedSource = "bar"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); + IAnnotation annotation = getCompilationUnit("/P/p/Y.java").getType("Y").getAnnotation("MyAnnot"); + assertSourceEquals( + "Unexpected source'", + "MyAnnot", + getNameSource(cuSource, annotation)); } finally { deleteFile("/P/p/Y.java"); } } /* - * Ensures that the source range for an anonymous type is correct. - * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=207775) + * Ensures that the name range for an annotation is correct. */ - public void testAnonymousSourceRange() throws CoreException { + public void testNameRange02() throws CoreException { // was testAnnotationNameRange2 try { String cuSource = "package p;\n" + + "@x. y . z.MyAnnot (1)\n" + "public class Y {\n" + - " void foo() {\n" + - " new Object() {};\n" + - " }\n" + "}"; createFile("/P/p/Y.java", cuSource); - IType type = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getType("", 1); + IAnnotation annotation = getCompilationUnit("/P/p/Y.java").getType("Y").getAnnotation("x.y.z.MyAnnot"); assertSourceEquals( "Unexpected source'", - "new Object() {}", - getSource(cuSource, type.getSourceRange())); + "x. y . z.MyAnnot", + getNameSource(cuSource, annotation)); } finally { deleteFile("/P/p/Y.java"); } } /* - * Ensures that the name range for an annotation is correct. + * Ensures that the name range for an annotation on a local variable is correct. + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823) */ - public void testAnnotationNameRange1() throws CoreException { + public void testNameRange03() throws CoreException { // was testAnnotationNameRange3 try { String cuSource = "package p;\n" + - "@ MyAnnot (1)\n" + "public class Y {\n" + + " void foo() {\n" + + " @MyAnnot int local;\n" + + " }\n" + "}"; createFile("/P/p/Y.java", cuSource); - IAnnotation annotation = getCompilationUnit("/P/p/Y.java").getType("Y").getAnnotation("MyAnnot"); + IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot"); assertSourceEquals( "Unexpected source'", "MyAnnot", @@ -239,96 +300,92 @@ deleteFile("/P/p/Y.java"); } } - + /* - * Ensures that the name range for an annotation is correct. + * Ensures the name range for an anonymous class is correct. + * (regression test for bug 44450 Strange name range for anonymous classes) */ - public void testAnnotationNameRange2() throws CoreException { + public void testNameRange04() throws CoreException { // was testNameRangeAnonymous try { String cuSource = "package p;\n" + - "@x. y . z.MyAnnot (1)\n" + "public class Y {\n" + + " void foo() {\n" + + " Y y = new Y() {};\n" + + " class C {\n" + + " }\n"+ + " }\n" + "}"; createFile("/P/p/Y.java", cuSource); - IAnnotation annotation = getCompilationUnit("/P/p/Y.java").getType("Y").getAnnotation("x.y.z.MyAnnot"); - assertSourceEquals( - "Unexpected source'", - "x. y . z.MyAnnot", - getNameSource(cuSource, annotation)); + IType anonymous = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getType("", 1); + + String actualSource = getNameSource(cuSource, anonymous); + String expectedSource = "Y"; + assertSourceEquals("Unexpected source'", expectedSource, actualSource); } finally { deleteFile("/P/p/Y.java"); } } - + /* - * Ensures that the name range for an annotation on a local variable is correct. - * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823) + * Ensures the name range for a type parameter is correct. */ - public void testAnnotationNameRange3() throws CoreException { + public void testNameRange05() throws CoreException { // was testNameRangeTypeParameter1 try { String cuSource = "package p;\n" + - "public class Y {\n" + - " void foo() {\n" + - " @MyAnnot int local;\n" + - " }\n" + + "public class Y {\n" + "}"; createFile("/P/p/Y.java", cuSource); - IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot"); + ITypeParameter typeParameter = getCompilationUnit("/P/p/Y.java").getType("Y").getTypeParameter("T"); assertSourceEquals( "Unexpected source'", - "MyAnnot", - getNameSource(cuSource, annotation)); + "T", + getNameSource(cuSource, typeParameter)); } finally { deleteFile("/P/p/Y.java"); } } /* - * Ensures that the source range for an annotation on a local variable is correct. - * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823) + * Ensures the name range for a type parameter is correct. */ - public void testAnnotationSourceRange() throws CoreException { + public void testNameRange06() throws CoreException { // was testNameRangeTypeParameter2 try { String cuSource = "package p;\n" + "public class Y {\n" + - " void foo() {\n" + - " @MyAnnot int local;\n" + - " }\n" + + " void foo() {} \n" + "}"; createFile("/P/p/Y.java", cuSource); - IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot"); + ITypeParameter typeParameter = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getTypeParameter("U"); assertSourceEquals( "Unexpected source'", - "@MyAnnot", - getSource(cuSource, annotation.getSourceRange())); + "U", + getNameSource(cuSource, typeParameter)); } finally { deleteFile("/P/p/Y.java"); } } /* - * Ensures the name range for an anonymous class is correct. - * (regression test for bug 44450 Strange name range for anonymous classes) + * Ensures the name range for a method with syntax errors in its header is correct. + * (regression test for bug 43139 Delete member in Outliner not working) */ - public void testNameRangeAnonymous() throws CoreException { + public void testNameRange07() throws CoreException { // was testNameRangeMethodWithSyntaxError try { String cuSource = "package p;\n" + "public class Y {\n" + " void foo() {\n" + - " Y y = new Y() {};\n" + - " class C {\n" + - " }\n"+ " }\n" + + " void static bar() {}\n" + "}"; createFile("/P/p/Y.java", cuSource); - IType anonymous = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getType("", 1); + IMethod method= getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("bar", new String[0]); - String actualSource = getNameSource(cuSource, anonymous); - String expectedSource = "Y"; + String actualSource = getNameSource(cuSource, method); + String expectedSource = "bar"; assertSourceEquals("Unexpected source'", expectedSource, actualSource); } finally { deleteFile("/P/p/Y.java"); @@ -336,45 +393,97 @@ } /* - * Ensures the name range for a type parameter is correct. + * Ensures the name range for an anonymous enum constant is correct + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226313 ) */ - public void testNameRangeTypeParameter1() throws CoreException { + public void testNameRange08() throws CoreException { + try { + createJavaProject("P15", new String[] {""}, new String[0], "", "1.5"); + String cuSource = + "public enum X {\n" + + " GREEN() {\n" + + " };\n" + + "}"; + createFile("/P15/X.java", cuSource); + IType type = getCompilationUnit("/P15/X.java").getType("X").getField("GREEN").getType("", 1); + assertSourceEquals( + "Unexpected source'", + "GREEN", + getNameSource(cuSource, type)); + } finally { + deleteProject("P15"); + } + } + + /* + * Ensures that the source range for an annotation on a local variable is correct. + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823) + */ + public void testSourceRange01() throws CoreException { // was testAnnotationSourceRange try { String cuSource = "package p;\n" + - "public class Y {\n" + + "public class Y {\n" + + " void foo() {\n" + + " @MyAnnot int local;\n" + + " }\n" + "}"; createFile("/P/p/Y.java", cuSource); - ITypeParameter typeParameter = getCompilationUnit("/P/p/Y.java").getType("Y").getTypeParameter("T"); + IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot"); assertSourceEquals( "Unexpected source'", - "T", - getNameSource(cuSource, typeParameter)); + "@MyAnnot", + getSource(cuSource, annotation.getSourceRange())); } finally { deleteFile("/P/p/Y.java"); } } - + /* - * Ensures the name range for a type parameter is correct. + * Ensures that the source range for an anonymous type is correct. + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=207775) */ - public void testNameRangeTypeParameter2() throws CoreException { + public void testSourceRange02() throws CoreException { // was testAnonymousSourceRange try { String cuSource = "package p;\n" + "public class Y {\n" + - " void foo() {} \n" + + " void foo() {\n" + + " new Object() {};\n" + + " }\n" + "}"; createFile("/P/p/Y.java", cuSource); - ITypeParameter typeParameter = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getTypeParameter("U"); + IType type = getCompilationUnit("/P/p/Y.java").getType("Y").getMethod("foo", new String[0]).getType("", 1); assertSourceEquals( "Unexpected source'", - "U", - getNameSource(cuSource, typeParameter)); + "new Object() {}", + getSource(cuSource, type.getSourceRange())); } finally { deleteFile("/P/p/Y.java"); } } + + /* + * Ensures the source range for an anonymous enum constant is correct + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226313 ) + */ + public void testSourceRange03() throws CoreException { + try { + createJavaProject("P15", new String[] {""}, new String[0], "", "1.5"); + String cuSource = + "public enum X {\n" + + " GREEN() {};\n" + + "}"; + createFile("/P15/X.java", cuSource); + IType type = getCompilationUnit("/P15/X.java").getType("X").getField("GREEN").getType("", 1); + assertSourceEquals( + "Unexpected source'", + "GREEN() {}", + getSource(cuSource, type.getSourceRange())); + } finally { + deleteProject("P15"); + } + } /* * Ensures the source for a type parameter is correct. @@ -395,7 +504,7 @@ deleteFile("/P/p/Y.java"); } } - + /* * Ensures the source for a type parameter is correct. */ @@ -416,7 +525,7 @@ deleteFile("/P/p/Y.java"); } } - + /** * Ensure the source for a field contains the modifiers, field * type, name, and terminator, and unicode characters. @@ -429,7 +538,7 @@ String expectedSource = "private int s\\u0069ze;"; assertSourceEquals("Unexpected source'", expectedSource, actualSource); } - + /** * Ensure the source for a field contains the modifiers, field * type, name, and terminator, and unicode characters. @@ -450,68 +559,4 @@ " }"; assertSourceEquals("Unexpected source", expectedSource, actualSource); } - - /** - * Test the field constant - */ - private IField getConstantField(String fieldName) { - IType type = getCompilationUnit("/P/p/Constants.java").getType("Constants"); - IField field = type.getField(fieldName); - return field; - } - - public void testFieldConstant01() throws CoreException { - IField field = getConstantField("field1"); - - Object constant = field.getConstant(); - Long value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); - } - - public void testFieldConstant02() throws CoreException { - IField field = getConstantField("field2"); - - Object constant = field.getConstant(); - Long value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); - } - - public void testFieldConstant03() throws CoreException { - IField field = getConstantField("field3"); - - Object constant = field.getConstant(); - Long value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); - } - - public void testFieldConstant04() throws CoreException { - IField field = getConstantField("field4"); - - Object constant = field.getConstant(); - Character character = (Character) constant; - assertEquals("Wrong value", ' ', character.charValue()); - } - - public void testFieldConstant05() throws CoreException { - IField field = getConstantField("field5"); - - Object constant = field.getConstant(); - Double double1 = (Double) constant; - assertEquals("Wrong value", 938245798324893l, double1.doubleValue(), 0.01); - } - - public void testFieldConstant06() throws CoreException { - IField field = getConstantField("field6"); - - Object constant = field.getConstant(); - Float float1 = (Float) constant; - assertEquals("Wrong value", 123456, float1.floatValue(), 0.01f); - } - - public void testFieldConstant07() throws CoreException { - IField field = getConstantField("field7"); - - Object constant = field.getConstant(); - assertNull("Should not be a constant", constant); - } } #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/util/HandleFactory.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/HandleFactory.java,v retrieving revision 1.44 diff -u -r1.44 HandleFactory.java --- model/org/eclipse/jdt/internal/core/util/HandleFactory.java 26 Feb 2008 10:11:47 -0000 1.44 +++ model/org/eclipse/jdt/internal/core/util/HandleFactory.java 10 Apr 2008 12:02:57 -0000 @@ -204,7 +204,7 @@ int occurenceCount = 1; for (int i = 0, length = type.fields.length; i < length; i++) { FieldDeclaration field = type.fields[i]; - if (field.declarationSourceStart < elementPosition && field.declarationSourceEnd > elementPosition) { + if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) { switch (field.getKind()) { case AbstractVariableDeclaration.FIELD : case AbstractVariableDeclaration.ENUM_CONSTANT : Index: model/org/eclipse/jdt/internal/compiler/SourceElementNotifier.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/compiler/SourceElementNotifier.java,v retrieving revision 1.1 diff -u -r1.1 SourceElementNotifier.java --- model/org/eclipse/jdt/internal/compiler/SourceElementNotifier.java 6 Mar 2008 15:31:31 -0000 1.1 +++ model/org/eclipse/jdt/internal/compiler/SourceElementNotifier.java 10 Apr 2008 12:02:57 -0000 @@ -581,10 +581,16 @@ superclassName = getSuperclassName(typeDeclaration); } ISourceElementRequestor.TypeInfo typeInfo = new ISourceElementRequestor.TypeInfo(); - typeInfo.declarationStart = typeDeclaration.allocation == null ? typeDeclaration.declarationSourceStart : typeDeclaration.allocation.sourceStart; + if (typeDeclaration.allocation == null) { + typeInfo.declarationStart = typeDeclaration.declarationSourceStart; + } else if (isEnumInit) { + typeInfo.declarationStart = typeDeclaration.allocation.enumConstant.sourceStart; + } else { + typeInfo.declarationStart = typeDeclaration.allocation.sourceStart; + } typeInfo.modifiers = deprecated ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated : currentModifiers & ExtraCompilerModifiers.AccJustFlag; typeInfo.name = typeDeclaration.name; - typeInfo.nameSourceStart = typeDeclaration.sourceStart; + typeInfo.nameSourceStart = isEnumInit ? typeDeclaration.allocation.enumConstant.sourceStart : typeDeclaration.sourceStart; typeInfo.nameSourceEnd = sourceEnd(typeDeclaration); typeInfo.superclass = superclassName; typeInfo.superinterfaces = interfaceNames; @@ -709,8 +715,8 @@ private int sourceEnd(TypeDeclaration typeDeclaration) { if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) { QualifiedAllocationExpression allocation = typeDeclaration.allocation; - if (allocation.type == null) // case of enum constant body - return typeDeclaration.sourceEnd; + if (allocation.enumConstant != null) // case of enum constant body + return allocation.enumConstant.sourceEnd; return allocation.type.sourceEnd; } else { return typeDeclaration.sourceEnd;