Bug 41373 - SourceField.getConstant() returns null for final fields set in initializer
Summary: SourceField.getConstant() returns null for final fields set in initializer
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 2.1.3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-08-11 08:39 EDT by Randy Faust CLA
Modified: 2004-03-01 11:59 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Randy Faust CLA 2003-08-11 08:39:28 EDT
For

public class TestA {	
	private final int fFoo= 3;
}

given fFoo as an IField, IField.getConstant() returns null. How is this 
supposed to work?
Comment 1 Olivier Thomann CLA 2003-08-11 11:32:48 EDT
It works only if the type is an interface, but it reports a char[] in case the
field is from source. This is clearly a bug according to the doc.
Comment 2 Olivier Thomann CLA 2003-08-11 11:42:06 EDT
I think we have two bugs.
One in the doc and one in the implementation.
1) in the doc, the getConstant() method should specify that a constant is
returned only if the field is static and final or if the field is the field of
an interface type.
2) The implementation should convert the char[] to either an instance of a
subclass of Number or a String.
Comment 3 Olivier Thomann CLA 2003-08-11 11:53:25 EDT
In your example, null is the proper answer, because this field is not a constant
field in the JLS meaning. Only static final fields can have a constant.
If you change it to:
public class TestA {	
	private static final int fFoo= 3;
}

You will get the char array char[] {'3'} for the return value of getConstant()
and this doesn't match the javadoc.
/**
 * Returns the constant value associated with this field
 * or <code>null</code> if this field has none.
 * Returns either a subclass of <code>Number</code>, or a <code>String</code>,
 * depending on the type of the field.
 * For example, if the field is of type <code>short</code>, this returns
 * a <code>Short</code>.
 *
 * @return  the constant value associated with this field or <code>null</code>
if this field has none.
 * @exception JavaModelException if this element does not exist or if an
 *      exception occurs while accessing its corresponding resource
 */
public Object getConstant() throws JavaModelException;
Comment 4 Randy Faust CLA 2003-08-11 12:22:32 EDT
That's fair. Is there another way aside from getConstant() to see what the 
value of a field is set to? For the source action "Generate constructor using 
fields..." I want to include final fields in the list of possible fields to 
choose to be initialized in an auto-generated constructor, but only if this 
field hasn't already been initialized. 
Comment 5 Olivier Thomann CLA 2003-08-11 12:28:32 EDT
Moreover the constant could be converted to a Boolean instance or a Character
instance and these are not either a subclass of Number or a String. So the
javadoc should definitely be fixed. We might want to say that the constant is
converted to the proper wrapper type.

To answer your question, I don't think it is possible to do it using the IField.
If you use the DOM/AST API, it is easy to find out if a field has an initializer.
Comment 6 Olivier Thomann CLA 2003-08-28 09:01:53 EDT
Fixed and released in HEAD.
In your example, you won't get a constant, but for static final fields or 
interface's fields you will get the right constant and not a char[] anymore.
Comment 7 David Audel CLA 2003-10-08 07:27:35 EDT
Verified.
Comment 8 Olivier Thomann CLA 2003-12-04 11:46:25 EST
Backported to 2.1.3.
Comment 9 Frederic Fusier CLA 2004-03-01 11:59:06 EST
Verified for 2.1.3 with build M200402251535.