Proposal: Change behavior of AST getMemberClassValue() and equivalents

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: Change behavior of AST getMemberClassValue() and equivalents

Christopher Smith-2
Currently, the method AbstractASTTransformation#getMemberClassValue
takes as a parameter a "default value" (parameter defaulted to null),
which is returned if the annotation instance does not provide a value
for that parameter. This means that if an annotation is declared with
a "default Foo.class", that expected default value is not taken into
account, and the value returned to AST transformations is null.

As the core structure of annotations provides for setting a default,
it is exceedingly surprising for the default not to be respected and
to have to be manually resolved by any transformation using it.

I propose that as a breaking change for 4.0, if an annotation provides
a default value and an instance of the annotation does not override
that value, then that default value should be returned by the
getMember*Value methods.
Reply | Threaded
Open this post in threaded view
|

RE: Proposal: Change behavior of AST getMemberClassValue() and equivalents

Milles, Eric (TR Technology)
I think it is implemented this way because it must support an unresolved annotation.  That is, you may have a transform that runs before ResolveVisitor fills in all the type information.  The transform can only ask for what is present in the AST.  If it wants to use the default values of any annotation method, it can supply the annotation class.  You can get the default value by reflection like this and that could be passed as the default to return or you could use ?: logic.

// excerpt of void visit(ASTNode[] nodes, SourceUnit source)
AnnotationNode node = (AnnotationNode) nodes[0];
String value = getMemberStringValue(node, "name", AnnoType.class.getMethod("name").getDefaultValue());