View | Details | Raw Unified | Return to bug 120130
Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/SourceField.java (-31 / +32 lines)
Lines 11-20 Link Here
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import org.eclipse.jdt.core.*;
13
import org.eclipse.jdt.core.*;
14
import org.eclipse.jdt.core.IField;
15
import org.eclipse.jdt.core.IType;
16
import org.eclipse.jdt.core.JavaModelException;
17
import org.eclipse.jdt.core.Signature;
18
import org.eclipse.jdt.core.dom.ASTNode;
14
import org.eclipse.jdt.core.dom.ASTNode;
19
import org.eclipse.jdt.internal.compiler.lookup.Binding;
15
import org.eclipse.jdt.internal.compiler.lookup.Binding;
20
16
Lines 54-89 Link Here
54
			
50
			
55
	String constantSource = new String(constantSourceChars);
51
	String constantSource = new String(constantSourceChars);
56
	String signature = info.getTypeSignature();
52
	String signature = info.getTypeSignature();
57
	if (signature.equals(Signature.SIG_INT)) {
53
	try {
58
		constant = new Integer(constantSource);
54
		if (signature.equals(Signature.SIG_INT)) {
59
	} else if (signature.equals(Signature.SIG_SHORT)) {
55
			constant = new Integer(constantSource);
60
		constant = new Short(constantSource);
56
		} else if (signature.equals(Signature.SIG_SHORT)) {
61
	} else if (signature.equals(Signature.SIG_BYTE)) {
57
			constant = new Short(constantSource);
62
		constant = new Byte(constantSource);
58
		} else if (signature.equals(Signature.SIG_BYTE)) {
63
	} else if (signature.equals(Signature.SIG_BOOLEAN)) {
59
			constant = new Byte(constantSource);
64
		constant = Boolean.valueOf(constantSource);
60
		} else if (signature.equals(Signature.SIG_BOOLEAN)) {
65
	} else if (signature.equals(Signature.SIG_CHAR)) {
61
			constant = Boolean.valueOf(constantSource);
66
		if (constantSourceChars.length != 3) {
62
		} else if (signature.equals(Signature.SIG_CHAR)) {
67
			return null;
63
			if (constantSourceChars.length != 3) {
68
		}
64
				return null;
69
		constant = new Character(constantSourceChars[1]);
65
			}
70
	} else if (signature.equals(Signature.SIG_DOUBLE)) {
66
			constant = new Character(constantSourceChars[1]);
71
		constant = new Double(constantSource);
67
		} else if (signature.equals(Signature.SIG_DOUBLE)) {
72
	} else if (signature.equals(Signature.SIG_FLOAT)) {
68
			constant = new Double(constantSource);
73
		constant = new Float(constantSource);
69
		} else if (signature.equals(Signature.SIG_FLOAT)) {
74
	} else if (signature.equals(Signature.SIG_LONG)) {
70
			constant = new Float(constantSource);
75
		if (constantSource.endsWith("L") || constantSource.endsWith("l")) { //$NON-NLS-1$ //$NON-NLS-2$
71
		} else if (signature.equals(Signature.SIG_LONG)) {
76
			int index = constantSource.lastIndexOf("L");//$NON-NLS-1$
72
			if (constantSource.endsWith("L") || constantSource.endsWith("l")) { //$NON-NLS-1$ //$NON-NLS-2$
77
			if (index != -1) {
73
				int index = constantSource.lastIndexOf("L");//$NON-NLS-1$
78
				constant = new Long(constantSource.substring(0, index));
74
				if (index != -1) {
75
					constant = new Long(constantSource.substring(0, index));
76
				} else {
77
					constant = new Long(constantSource.substring(0, constantSource.lastIndexOf("l")));//$NON-NLS-1$
78
				}
79
			} else {
79
			} else {
80
				constant = new Long(constantSource.substring(0, constantSource.lastIndexOf("l")));//$NON-NLS-1$
80
				constant = new Long(constantSource);
81
			}
81
			}
82
		} else {
82
		} else if (signature.equals("QString;")) {//$NON-NLS-1$
83
			constant = new Long(constantSource);
83
			constant = constantSource;
84
		}
84
		}
85
	} else if (signature.equals("QString;")) {//$NON-NLS-1$
85
	} catch (NumberFormatException e) {
86
		constant = constantSource;
86
		// not a parsable constant
87
		return null;
87
	}
88
	}
88
	return constant;
89
	return constant;
89
}
90
}
(-)src/org/eclipse/jdt/core/tests/model/GetSourceTests.java (-79 / +68 lines)
Lines 53-65 Link Here
53
			"}"
53
			"}"
54
		);
54
		);
55
		this.cu = getCompilationUnit("/P/p/X.java");
55
		this.cu = getCompilationUnit("/P/p/X.java");
56
		String cuSource = 
57
			"package p;\n" +
58
			"public class Constants {\n" +
59
			"  static final long field1 = 938245798324893L;\n" +
60
			"  static final long field2 = 938245798324893l;\n" +
61
			"  static final long field3 = 938245798324893;\n" +
62
			"  static final char field4 = ' ';\n" +
63
			"  static final double field5 = 938245798324893D;\n" +
64
			"  static final float field6 = 123456f;\n" +
65
			"  static final int field7 = 1<<0;\n" +
66
			"}";
67
		createFile("/P/p/Constants.java", cuSource);
56
	}
68
	}
57
69
58
	// Use this static initializer to specify subset for tests
70
	// Use this static initializer to specify subset for tests
59
	// All specified tests which do not belong to the class are skipped...
71
	// All specified tests which do not belong to the class are skipped...
60
	static {
72
	static {
61
		// Names of tests to run: can be "testBugXXXX" or "BugXXXX")
73
		// Names of tests to run: can be "testBugXXXX" or "BugXXXX")
62
//		TESTS_NAMES = new String[] { "TypeParameterBug73884" };
74
//		TESTS_NAMES = new String[] { "testFieldConstant" };
63
		// Numbers of tests to run: "test<number>" will be run for each number of this array
75
		// Numbers of tests to run: "test<number>" will be run for each number of this array
64
//		TESTS_NUMBERS = new int[] { 13 };
76
//		TESTS_NUMBERS = new int[] { 13 };
65
		// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last }
77
		// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last }
Lines 336-422 Link Here
336
	/**
348
	/**
337
	 * Test the field constant
349
	 * Test the field constant
338
	 */
350
	 */
339
	public void testFieldConstant() throws CoreException {
351
	private IField getConstantField(String fieldName) {
340
		try {
352
		IType type = getCompilationUnit("/P/p/Constants.java").getType("Constants");
341
			String cuSource = 
353
		IField field = type.getField(fieldName);
342
				"package p;\n" +
354
		return field;
343
				"public class Y {\n" +
355
	}
344
				"  static final long field1 = 938245798324893L;\n" +
356
345
				"  static final long field2 = 938245798324893l;\n" +
357
	public void testFieldConstant01() throws CoreException {
346
				"  static final long field3 = 938245798324893;\n" +
358
		IField field = getConstantField("field1");
347
				"  static final char field4 = ' ';\n" +
359
	
348
				"  static final double field5 = 938245798324893D;\n" +
360
		Object constant = field.getConstant();
349
				"  static final float field6 = 123456f;\n" +
361
		Long value = (Long) constant;
350
				"}";
362
		assertEquals("Wrong value", 938245798324893l, value.longValue());
351
			createFile("/P/p/Y.java", cuSource);
363
	}
352
			IType type = getCompilationUnit("/P/p/Y.java").getType("Y");
364
	
353
			IField field = type.getField("field1");
365
	public void testFieldConstant02() throws CoreException {
354
		
366
		IField field = getConstantField("field2");
355
			String actualSource = field.getSource();
367
	
356
			String expectedSource = "static final long field1 = 938245798324893L;";
368
		Object constant = field.getConstant();
357
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
369
		Long value = (Long) constant;
358
			Object constant = field.getConstant();
370
		assertEquals("Wrong value", 938245798324893l, value.longValue());
359
			assertNotNull("No constant", constant);
371
	}
360
			assertTrue("Not a Long", constant instanceof Long);
361
			Long value = (Long) constant;
362
			assertEquals("Wrong value", 938245798324893l, value.longValue());
363
			
364
			field = type.getField("field2");
365
		
372
		
366
			actualSource = field.getSource();
373
	public void testFieldConstant03() throws CoreException {
367
			expectedSource = "static final long field2 = 938245798324893l;";
374
		IField field = getConstantField("field3");
368
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
375
	
369
			constant = field.getConstant();
376
		Object constant = field.getConstant();
370
			assertNotNull("No constant", constant);
377
		Long value = (Long) constant;
371
			assertTrue("Not a Long", constant instanceof Long);
378
		assertEquals("Wrong value", 938245798324893l, value.longValue());
372
			value = (Long) constant;
379
	}
373
			assertEquals("Wrong value", 938245798324893l, value.longValue());
380
374
			
381
	public void testFieldConstant04() throws CoreException {
375
			field = type.getField("field3");
382
		IField field = getConstantField("field4");
383
	
384
		Object constant = field.getConstant();
385
		Character character = (Character) constant;
386
		assertEquals("Wrong value", ' ', character.charValue());
387
	}
376
		
388
		
377
			actualSource = field.getSource();
389
	public void testFieldConstant05() throws CoreException {
378
			expectedSource = "static final long field3 = 938245798324893;";
390
		IField field = getConstantField("field5");
379
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
391
	
380
			constant = field.getConstant();
392
		Object constant = field.getConstant();
381
			assertNotNull("No constant", constant);
393
		Double double1 = (Double) constant;
382
			assertTrue("Not a Long", constant instanceof Long);
394
		assertEquals("Wrong value", 938245798324893l, double1.doubleValue(), 0.01);
383
			value = (Long) constant;
395
	}
384
			assertEquals("Wrong value", 938245798324893l, value.longValue());
385
396
386
			field = type.getField("field4");
397
	public void testFieldConstant06() throws CoreException {
387
			
398
		IField field = getConstantField("field6");
388
			actualSource = field.getSource();
399
	
389
			expectedSource = "static final char field4 = ' ';";
400
		Object constant = field.getConstant();
390
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
401
		Float float1 = (Float) constant;
391
			constant = field.getConstant();
402
		assertEquals("Wrong value", 123456, float1.floatValue(), 0.01f);
392
			assertNotNull("No constant", constant);
403
	}
393
			assertTrue("Not a Character", constant instanceof Character);
394
			Character character = (Character) constant;
395
			assertEquals("Wrong value", ' ', character.charValue());
396
			
397
			field = type.getField("field5");
398
			
399
			actualSource = field.getSource();
400
			expectedSource = "static final double field5 = 938245798324893D;";
401
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
402
			constant = field.getConstant();
403
			assertNotNull("No constant", constant);
404
			assertTrue("Not a Double", constant instanceof Double);
405
			Double double1 = (Double) constant;
406
			assertEquals("Wrong value", 938245798324893l, double1.doubleValue(), 0.01);			
407
404
408
			field = type.getField("field6");
405
	public void testFieldConstant07() throws CoreException {
409
			
406
		IField field = getConstantField("field7");
410
			actualSource = field.getSource();
407
	
411
			expectedSource = "static final float field6 = 123456f;";
408
		Object constant = field.getConstant();
412
			assertSourceEquals("Unexpected source'", expectedSource, actualSource);
409
		assertNull("Should not be a constant", constant);
413
			constant = field.getConstant();
414
			assertNotNull("No constant", constant);
415
			assertTrue("Not a Float", constant instanceof Float);
416
			Float float1 = (Float) constant;
417
			assertEquals("Wrong value", 123456, float1.floatValue(), 0.01f);			
418
		} finally {
419
			deleteFile("/P/p/Y.java");
420
		}
421
	}
410
	}
422
}
411
}

Return to bug 120130