Two processing questions
Mohan Radhakrishnan
radhakrishnan.mohan at gmail.com
Mon Dec 16 02:47:28 PST 2013
Hi,
I have these two questions.
1. How do I get the annotations and the targets from within a method ?
----------------------Source Method----------------------------------
public static void main( @NonNull String... argv ){
Object o = ( @TypeUse String ) new String( "Test" );
if( o instanceof @TypeUse String ){
}
try{
}catch( @NonNull Exception e){
}
}
-------------------Test
processor------------------------------------------------------------
private void processEnclosedElements(List<? extends Element>
enclosedElements,
RoundEnvironment roundEnv) {
for( Element e : enclosedElements ){
if( e.getKind() == ElementKind.ANNOTATION_TYPE &&
!roundEnv.getElementsAnnotatedWith((TypeElement)
e).isEmpty()){
System.out.println( "Annotation [" + e + "] [" +
roundEnv.getElementsAnnotatedWith((TypeElement) e)
+ "]");
}else if( e.getKind() == ElementKind.METHOD ){
for (VariableElement variableElement : ((ExecutableElement)
e).getParameters()) {
System.out.println("Annotation [" +
variableElement.getAnnotationMirrors() + "] [" +
variableElement + "]");
e.asType().accept(visitor(),null);
}
}else if (e.getKind() == ElementKind.CLASS){
System.out.println( "Annotation [" +
e.getAnnotationMirrors() + "] [" + e + "]");
processClassTypeParameters(e);
}
}
}
private TypeVisitor<Boolean, Void> visitor() {
return new SimpleTypeVisitor8<Boolean, Void>() {
public Boolean visitExecutable(ExecutableType t, Void v) {
System.out.println(t);
return true;
}
};
}
2. How do I get the annotation applied on the bound ?
------------------------Source class--------------------
@TypeUse class Test1<@TypeParameter @TestTypeParameter T extends @TypeUse
Integer> {
/**
* A type annotation is permitted in front of a constructor
declaration, where declaration annotations are already
permitted.
*/
@TypeUse <@TypeParameter @TestTypeParameter S> Test1(String s){
}
}
-----------------Test Processor---------------------
private void processClassTypeParameters(Element e) {
for (TypeParameterElement typeParameterElement : ((TypeElement)
e).getTypeParameters()) {
//System.out.println( "Annotation [" +
typeParameterElement.getAnnotationMirrors() + "] [" + typeParameterElement
+ "]");
for (TypeMirror typeMirror : typeParameterElement.getBounds()) {
TypeElement typeElement = (TypeElement)
((DeclaredType)typeMirror).asElement();
System.out.println( "Annotation [" +
typeElement.getAnnotationMirrors() + "] [" + typeMirror + "]");
}
}
}
Thanks,
Mohan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/type-annotations-dev/attachments/20131216/f5187ab6/attachment.html
More information about the type-annotations-dev
mailing list