Summary: | SourceField.getConstant() returns null for final fields set in initializer | ||
---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | Randy Faust <rfaust> |
Component: | Core | Assignee: | Olivier Thomann <Olivier_Thomann> |
Status: | VERIFIED FIXED | QA Contact: | |
Severity: | normal | ||
Priority: | P3 | ||
Version: | 3.0 | ||
Target Milestone: | 2.1.3 | ||
Hardware: | PC | ||
OS: | Windows XP | ||
Whiteboard: |
Description
Randy Faust
2003-08-11 08:39:28 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. 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. 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; 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. 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. 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. Verified. Backported to 2.1.3. Verified for 2.1.3 with build M200402251535. |