### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/SourceField.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceField.java,v retrieving revision 1.43 diff -u -r1.43 SourceField.java --- model/org/eclipse/jdt/internal/core/SourceField.java 3 May 2005 10:49:21 -0000 1.43 +++ model/org/eclipse/jdt/internal/core/SourceField.java 12 Dec 2005 18:38:19 -0000 @@ -11,10 +11,6 @@ package org.eclipse.jdt.internal.core; import org.eclipse.jdt.core.*; -import org.eclipse.jdt.core.IField; -import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.internal.compiler.lookup.Binding; @@ -54,36 +50,41 @@ String constantSource = new String(constantSourceChars); String signature = info.getTypeSignature(); - if (signature.equals(Signature.SIG_INT)) { - constant = new Integer(constantSource); - } else if (signature.equals(Signature.SIG_SHORT)) { - constant = new Short(constantSource); - } else if (signature.equals(Signature.SIG_BYTE)) { - constant = new Byte(constantSource); - } else if (signature.equals(Signature.SIG_BOOLEAN)) { - constant = Boolean.valueOf(constantSource); - } else if (signature.equals(Signature.SIG_CHAR)) { - if (constantSourceChars.length != 3) { - return null; - } - constant = new Character(constantSourceChars[1]); - } else if (signature.equals(Signature.SIG_DOUBLE)) { - constant = new Double(constantSource); - } else if (signature.equals(Signature.SIG_FLOAT)) { - constant = new Float(constantSource); - } else if (signature.equals(Signature.SIG_LONG)) { - if (constantSource.endsWith("L") || constantSource.endsWith("l")) { //$NON-NLS-1$ //$NON-NLS-2$ - int index = constantSource.lastIndexOf("L");//$NON-NLS-1$ - if (index != -1) { - constant = new Long(constantSource.substring(0, index)); + try { + if (signature.equals(Signature.SIG_INT)) { + constant = new Integer(constantSource); + } else if (signature.equals(Signature.SIG_SHORT)) { + constant = new Short(constantSource); + } else if (signature.equals(Signature.SIG_BYTE)) { + constant = new Byte(constantSource); + } else if (signature.equals(Signature.SIG_BOOLEAN)) { + constant = Boolean.valueOf(constantSource); + } else if (signature.equals(Signature.SIG_CHAR)) { + if (constantSourceChars.length != 3) { + return null; + } + constant = new Character(constantSourceChars[1]); + } else if (signature.equals(Signature.SIG_DOUBLE)) { + constant = new Double(constantSource); + } else if (signature.equals(Signature.SIG_FLOAT)) { + constant = new Float(constantSource); + } else if (signature.equals(Signature.SIG_LONG)) { + if (constantSource.endsWith("L") || constantSource.endsWith("l")) { //$NON-NLS-1$ //$NON-NLS-2$ + int index = constantSource.lastIndexOf("L");//$NON-NLS-1$ + if (index != -1) { + constant = new Long(constantSource.substring(0, index)); + } else { + constant = new Long(constantSource.substring(0, constantSource.lastIndexOf("l")));//$NON-NLS-1$ + } } else { - constant = new Long(constantSource.substring(0, constantSource.lastIndexOf("l")));//$NON-NLS-1$ + constant = new Long(constantSource); } - } else { - constant = new Long(constantSource); + } else if (signature.equals("QString;")) {//$NON-NLS-1$ + constant = constantSource; } - } else if (signature.equals("QString;")) {//$NON-NLS-1$ - constant = constantSource; + } catch (NumberFormatException e) { + // not a parsable constant + return null; } return constant; } #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.14 diff -u -r1.14 GetSourceTests.java --- src/org/eclipse/jdt/core/tests/model/GetSourceTests.java 12 Apr 2005 18:00:21 -0000 1.14 +++ src/org/eclipse/jdt/core/tests/model/GetSourceTests.java 12 Dec 2005 18:38:21 -0000 @@ -53,13 +53,25 @@ "}" ); this.cu = getCompilationUnit("/P/p/X.java"); + String cuSource = + "package p;\n" + + "public class Constants {\n" + + " static final long field1 = 938245798324893L;\n" + + " static final long field2 = 938245798324893l;\n" + + " static final long field3 = 938245798324893;\n" + + " static final char field4 = ' ';\n" + + " static final double field5 = 938245798324893D;\n" + + " static final float field6 = 123456f;\n" + + " static final int field7 = 1<<0;\n" + + "}"; + 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" }; +// TESTS_NAMES = new String[] { "testFieldConstant" }; // 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 } @@ -336,87 +348,64 @@ /** * Test the field constant */ - public void testFieldConstant() throws CoreException { - try { - String cuSource = - "package p;\n" + - "public class Y {\n" + - " static final long field1 = 938245798324893L;\n" + - " static final long field2 = 938245798324893l;\n" + - " static final long field3 = 938245798324893;\n" + - " static final char field4 = ' ';\n" + - " static final double field5 = 938245798324893D;\n" + - " static final float field6 = 123456f;\n" + - "}"; - createFile("/P/p/Y.java", cuSource); - IType type = getCompilationUnit("/P/p/Y.java").getType("Y"); - IField field = type.getField("field1"); - - String actualSource = field.getSource(); - String expectedSource = "static final long field1 = 938245798324893L;"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - Object constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Long", constant instanceof Long); - Long value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); - - field = type.getField("field2"); + 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()); + } - actualSource = field.getSource(); - expectedSource = "static final long field2 = 938245798324893l;"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Long", constant instanceof Long); - value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); - - field = type.getField("field3"); + 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()); + } - actualSource = field.getSource(); - expectedSource = "static final long field3 = 938245798324893;"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Long", constant instanceof Long); - value = (Long) constant; - assertEquals("Wrong value", 938245798324893l, value.longValue()); + 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); + } - field = type.getField("field4"); - - actualSource = field.getSource(); - expectedSource = "static final char field4 = ' ';"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Character", constant instanceof Character); - Character character = (Character) constant; - assertEquals("Wrong value", ' ', character.charValue()); - - field = type.getField("field5"); - - actualSource = field.getSource(); - expectedSource = "static final double field5 = 938245798324893D;"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Double", constant instanceof Double); - 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); + } - field = type.getField("field6"); - - actualSource = field.getSource(); - expectedSource = "static final float field6 = 123456f;"; - assertSourceEquals("Unexpected source'", expectedSource, actualSource); - constant = field.getConstant(); - assertNotNull("No constant", constant); - assertTrue("Not a Float", constant instanceof Float); - Float float1 = (Float) constant; - assertEquals("Wrong value", 123456, float1.floatValue(), 0.01f); - } finally { - deleteFile("/P/p/Y.java"); - } + public void testFieldConstant07() throws CoreException { + IField field = getConstantField("field7"); + + Object constant = field.getConstant(); + assertNull("Should not be a constant", constant); } }