Bug 209958 - Support getting default value from IMethod
Summary: Support getting default value from IMethod
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P2 enhancement with 1 vote (vote)
Target Milestone: 3.4 M4   Edit
Assignee: Jerome Lanneluc CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-15 10:15 EST by Leho Nigul CLA
Modified: 2007-12-13 07:10 EST (History)
2 users (show)

See Also:


Attachments
Corresponding implementation and tests (25.35 KB, patch)
2007-11-22 06:27 EST, Jerome Lanneluc CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Leho Nigul CLA 2007-11-15 10:15:09 EST
If we have the following method in the code

public abstract java.lang.String mappedName() default "TEST";

There is no way to get the default value OR string representation of the default value.
  Since this information is part of the method declaration, it would be very useful to get it from IMethod.
Comment 1 Jerome Lanneluc CLA 2007-11-15 10:23:54 EST
To be added for 3.4M4
Comment 2 Jerome Lanneluc CLA 2007-11-16 05:04:57 EST
Proposed API on IMethod:

/**
 * Returns the source of the default value of this annotation method.
 * Returns <code>null</code> if this method's parent is not an annotation 
 * type, if it is a binary method, or if it has no default value.
 * 
 * @exception JavaModelException if this element does not exist or if an
 *      exception occurs while accessing its corresponding resource.
 * @since 3.4
 */
String getDefaultValueSource() throws JavaModelException;

Leho, does it correspond to what you expect?
Comment 3 Leho Nigul CLA 2007-11-19 09:15:12 EST
Jerome, I am concerned that this won't work for binary methods. Is there any way to get this information for binary method using Java model? ( If not, does it mean that we will be forced to use some kind of reflection calls (getDefaultValue() on java.lang.reflect.Method) to get it ?).
Thanks !
Comment 4 Jerome Lanneluc CLA 2007-11-20 17:52:51 EST
(In reply to comment #3)
> Jerome, I am concerned that this won't work for binary methods. Is there any
> way to get this information for binary method using Java model? ( If not, does
> it mean that we will be forced to use some kind of reflection calls
> (getDefaultValue() on java.lang.reflect.Method) to get it ?).
> Thanks !
> 
I guess we could use the attached source for binary method. However this would still return null if no source is attached.

But do you really need the source of the default value? Or would you rather have a default IMemberValuePair? This way you could get the value (using IMemberValuePair#getValue()) in both source and binary case. However note that getValue() would return null in the case of:
  public abstract java.lang.String mappedName() default "TEST" + 1;
Would that be ok?
Comment 5 Leho Nigul CLA 2007-11-21 08:49:37 EST
Hi Jerome, your proposal sounds promising :), however could you explain a bit more what do you mean by "default IMemberValuePair" ? Are you thinking of introducing new method on IAnnotation ? (something like getDefaultMemberValuePairs ?) that would certainly work for us if that is what you are thinking.
Comment 6 Jerome Lanneluc CLA 2007-11-21 10:12:36 EST
In fact, we will not be able to provide a getDefaultMemberValuePairs() method on IAnnotation as this would need resolving the annotation reference.

Instead here is a new API proposal on IMethod:

/**
 * Returns a {@link IMemberValuePair member value pair} representing the default 
 * value of this annotation method.
 * Returns <code>null</code> if this method's parent is not an annotation 
 * type, or if it does not have a default value.
 * <p>
 * Note that {@link IMemberValuePair#getValue()} might return <code>null</code>. 
 * Please see this method for more details.
 * </p>
 * 
 * @exception JavaModelException if this element does not exist or if an
 *      exception occurs while accessing its corresponding resource.
 * @since 3.4
 */
IMemberValuePair getDefaultValue() throws JavaModelException;
Comment 7 Leho Nigul CLA 2007-11-21 10:24:52 EST
This works for us, thanks A LOT Jerome !
Comment 8 Jerome Lanneluc CLA 2007-11-22 06:27:32 EST
Created attachment 83518 [details]
Corresponding implementation and tests
Comment 9 Jerome Lanneluc CLA 2007-11-22 06:52:27 EST
API, implementation and tests released for 3.4M4
Comment 10 Maxime Daniel CLA 2007-12-12 04:02:31 EST
Implementation and tests are here in I20071211-0010.
I would suggest that you improve the documentation.
Comment 11 Jerome Lanneluc CLA 2007-12-12 06:43:32 EST
I'm not sure what needs to be improved. Any suggestion?
Comment 12 Frederic Fusier CLA 2007-12-13 04:30:43 EST
Documentation looks OK for me.
Verified for 3.4M4 looking at v_829 contents
Comment 13 Jerome Lanneluc CLA 2007-12-13 07:10:54 EST
After Maxime's feedback, the latest version of this API has been released as:
/**
 * Returns a {@link IMemberValuePair member value pair} representing the default 
 * value of this method if any, or <code>null</code> if this method's parent is 
 * not an annotation type, or else if this method does not have a default value.
 * <p>
 * Note that {@link IMemberValuePair#getValue()} might return <code>null</code>. 
 * Please see this method for more details.
 * </p>
 * 
 * @exception JavaModelException if this element does not exist or if an
 *      exception occurs while accessing its corresponding resource.
 * @return a member pair value if any, or <code>null</code> if none
 * @since 3.4
 */
IMemberValuePair getDefaultValue() throws JavaModelException;