phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
93 stars 34 forks source link

Add method to AbstractJAnnotationValue to access value #10

Closed sviperll closed 9 years ago

sviperll commented 9 years ago

Annotation parameters are compile-time values. It would be convinient to access them all the way through code generation. AnnotationUse class should be extended to provide access to parameter values

Add

Object getParameterValue(String parameterName);

method or add collection of methods corresponding to AnnotationUse.param methods:

int getParameterIntValue(String parameterName); long getParameterLongValue(String parameterName); ... int[] getParameterIntArrayValue(String parameterName); long[] getParameterLongArrayValue(String parameterName); ... String getParameterStringValue(String parameterName); Annotation getParameterAnnotationValue(String parameterName); Class<?> getParameterClassValue(String parameterName); Enum<?> getParameterEnumValue(String parameterName); ... String[] getParameterStringArrayValue(String parameterName); ...

phax commented 9 years ago

Well I extended the API slightly, but providing APIs like "getParameterIntValue" is a tricky thing because the members can be of arbitrary classes (JAnnotationArrayMember, JAnnotationStringValue or JAnnotationUse itself). Instead I provided a generic "getParam" returning AbstractJAnnotationValue, a "getConstantParam" returning only JAnnotationStringValue and finally "getConstantParamValue" returning the value expression inside the JAnnotationStringValue. Now if accessors for primitives are required there are imho 2 options: 1. provide a default value in the method (like 'int getParamIntValue (String name, int defaultValue)') or 2. return the primitive wrapper type (like 'Integer getParamIntValue (String name)') which can therefore be null.

I personally think the second alternative looks nicer.

sviperll commented 9 years ago

Actually I was thinking about accessing actual parameter values, not their code model. See this two classes to get an idea of what I want:

https://github.com/sviperll/adt4j/blob/master/adt4j-core/src/main/java/com/github/sviperll/adt4j/Annotations.java

https://github.com/sviperll/adt4j/blob/master/adt4j-core/src/main/java/com/github/sviperll/adt4j/AnnotationUsage.java

phax commented 9 years ago

I do get your point. Currently the following types are supported: 8 primitives, String, Class<?> -> no problem for them IJExpression, AbstractJType, JEnumConstant -> they are already "code modelled"

I could add a method "public Object getNativeValue()" to JAnnotationStringValue that returns the original object before it is wrapped. I would really like to spare the additional map from name to value if possible. Ok?

sviperll commented 9 years ago

This sounds good, thank you.

sviperll commented 9 years ago

About code modeled values... My reasoning was that annotation parameters should be "compile-time constants", they always have some native value during compilation time, so this IJExpression, AbstractJType, JEnumConstant should always have some known value... May be it's better to deprecate this methods, and always ask for some actual value...

phax commented 9 years ago

Please check the trunk if that fits your needs. I deprecated the version with "IJExpression" but left the ones with AbstractJType and JEnumConstant, because otherwise generated enums could not be used in there

sviperll commented 9 years ago

Checked latest version. Works for my needs. Thanx.

phax commented 9 years ago

OK, so I will start the release process...

phax commented 9 years ago

2.7.6 out now - already on Maven Central