1 package org.apache.beehive.wsm.processor.apt; 2 3 20 21 import java.io.File ; 22 import java.lang.annotation.Annotation ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 26 import com.sun.mirror.declaration.AnnotationMirror; 27 import com.sun.mirror.declaration.ClassDeclaration; 28 import com.sun.mirror.declaration.ConstructorDeclaration; 29 import com.sun.mirror.declaration.InterfaceDeclaration; 30 import com.sun.mirror.declaration.MethodDeclaration; 31 import com.sun.mirror.declaration.Modifier; 32 import com.sun.mirror.declaration.TypeDeclaration; 33 34 import com.sun.mirror.apt.AnnotationProcessorEnvironment; 35 import com.sun.mirror.type.AnnotationType; 36 37 import org.apache.beehive.wsm.model.java.JavaMethodInfo; 38 import org.apache.beehive.wsm.model.java.JavaTypeInfo; 39 40 public class MirrorTypeInfo implements JavaTypeInfo { 41 42 protected TypeDeclaration decl; 43 protected AnnotationProcessorEnvironment env; 44 protected Collection <JavaMethodInfo> methods = new ArrayList <JavaMethodInfo>(); 45 46 public MirrorTypeInfo(TypeDeclaration decl, AnnotationProcessorEnvironment env) { 47 if (null == decl) { 48 throw new IllegalArgumentException ("type declaration: <null>"); 49 } 50 this.decl = decl; 51 this.env = env; 52 53 Collection <? extends MethodDeclaration> mdecls = decl.getMethods(); 54 if (null != mdecls) { 55 for (MethodDeclaration mdecl : mdecls) { 56 methods.add(new MirrorMethodInfo(mdecl, env)); 57 } 58 } 59 } 60 61 public void logError(String msg) { 62 env.getMessager().printError(decl.getPosition(), msg); 63 } 64 65 public String getName() { 66 return decl.getQualifiedName(); 67 } 68 69 public File getLocation() { 70 return decl.getPosition().file().getParentFile(); 71 } 72 73 public <A extends Annotation > A getAnnotation(Class <A> annotationType) { 74 return decl.getAnnotation(annotationType); 75 } 76 77 public Collection <Annotation > getAnnotations() { 78 Collection <Annotation > annotations = new ArrayList <Annotation >(); 79 for (AnnotationMirror am : decl.getAnnotationMirrors()) { 80 AnnotationType at = am.getAnnotationType(); 81 try { 82 Class clazz = Class.forName(at.getDeclaration().getQualifiedName()); 83 Annotation a = decl.getAnnotation(clazz); 84 if (null != a) { 85 annotations.add(a); 86 } 87 } catch (ClassNotFoundException e) { 88 } 89 } 90 return annotations; 91 } 92 93 public Collection <JavaMethodInfo> getMethods() { 94 return methods; 95 } 96 97 public boolean hasDefaultConstructor() { 98 boolean result = false; 99 if (decl instanceof InterfaceDeclaration) { 100 result = false; 101 } 102 else { 103 ClassDeclaration cdecl = (ClassDeclaration) decl; 104 for (ConstructorDeclaration c : cdecl.getConstructors()) { 105 if (c.getParameters().isEmpty()) { 106 result = true; 107 break; 108 } 109 } 110 } 111 112 return result; 113 } 114 115 public boolean hasFinalize() { 116 boolean result = false; 117 for (MethodDeclaration mdecl : decl.getMethods()) { 118 if (mdecl.getSimpleName().equals("finalize") && (null == mdecl.getParameters() || mdecl.getParameters().isEmpty())) { 119 result = true; 120 } 121 } 122 return result; 123 } 124 125 public boolean isAbstract() { 126 return decl.getModifiers().contains(Modifier.ABSTRACT); 127 } 128 129 public boolean isFinal() { 130 return decl.getModifiers().contains(Modifier.FINAL); 131 } 132 133 public boolean isPublic() { 134 return decl.getModifiers().contains(Modifier.PUBLIC); 135 } 136 137 public boolean isInterface() { 138 return (decl instanceof InterfaceDeclaration); 139 } 140 } 141 | Popular Tags |