1 package org.apache.beehive.wsm.processor.apt; 2 3 20 21 import java.lang.annotation.Annotation ; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.List ; 26 27 import com.sun.mirror.declaration.AnnotationMirror; 28 import com.sun.mirror.declaration.MethodDeclaration; 29 import com.sun.mirror.declaration.Modifier; 30 import com.sun.mirror.declaration.ParameterDeclaration; 31 32 import com.sun.mirror.apt.AnnotationProcessorEnvironment; 33 34 import com.sun.mirror.type.AnnotationType; 35 36 import org.apache.beehive.wsm.model.java.JavaMethodInfo; 37 import org.apache.beehive.wsm.model.java.JavaParameterInfo; 38 39 public class MirrorMethodInfo implements JavaMethodInfo { 40 41 protected MethodDeclaration decl; 42 protected AnnotationProcessorEnvironment env; 43 protected Class returnType; 44 protected List <JavaParameterInfo> parameters = new ArrayList <JavaParameterInfo>(); 45 46 public MirrorMethodInfo(MethodDeclaration decl, AnnotationProcessorEnvironment env) { 47 if (null == decl) { 48 throw new IllegalArgumentException ("method declaration: <null>"); 49 } 50 this.decl = decl; 51 this.env = env; 52 53 try { 54 returnType = TypeMirrorUtil.classForName(decl.getReturnType()); 55 } 56 catch (ClassNotFoundException e) { 57 logError("cannot find return type: " + decl.getReturnType()); 58 } 59 60 Collection <ParameterDeclaration> pdecls = decl.getParameters(); 61 if (null != pdecls) { 62 for (ParameterDeclaration pdecl : pdecls) { 63 parameters.add(new MirrorParameterInfo(pdecl, env)); 64 } 65 } 66 } 67 68 public void logError(String msg) { 69 env.getMessager().printError(decl.getPosition(), msg); 70 } 71 72 public boolean isPublic() { 73 return decl.getModifiers().contains(Modifier.PUBLIC); 74 } 75 76 public boolean throwsExceptions() { 77 return 0 < decl.getThrownTypes().size(); 78 } 79 80 public String getMethodName() { 81 return decl.getSimpleName(); 82 } 83 84 public Class getReturnType() { 85 return returnType; 86 } 87 88 public <A extends Annotation > A getAnnotation(Class <A> annotationType) { 89 return decl.getAnnotation(annotationType); 90 } 91 92 public Collection <Annotation > getAnnotations() { 93 Collection <Annotation > annotations = new ArrayList <Annotation >(); 94 for (AnnotationMirror am : decl.getAnnotationMirrors()) { 95 AnnotationType at = am.getAnnotationType(); 96 try { 97 Class clazz = Class.forName(at.getDeclaration().getQualifiedName()); 98 Annotation a = decl.getAnnotation(clazz); 99 if (null != a) { 100 annotations.add(a); 101 } 102 } catch (ClassNotFoundException e) { 103 } 104 } 105 return annotations; 106 } 107 108 public List <JavaParameterInfo> getParameters() { 109 return parameters; 110 } 111 } 112 | Popular Tags |