KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > processor > apt > MirrorTypeInfo


1 package org.apache.beehive.wsm.processor.apt;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$Factory
19  */

20
21 import java.io.File JavaDoc;
22 import java.lang.annotation.Annotation JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
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 JavaDoc<JavaMethodInfo> methods = new ArrayList JavaDoc<JavaMethodInfo>();
45     
46     public MirrorTypeInfo(TypeDeclaration decl, AnnotationProcessorEnvironment env) {
47         if (null == decl) {
48             throw new IllegalArgumentException JavaDoc("type declaration: <null>");
49         }
50         this.decl = decl;
51         this.env = env;
52
53         Collection JavaDoc<? 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 JavaDoc msg) {
62         env.getMessager().printError(decl.getPosition(), msg);
63     }
64     
65     public String JavaDoc getName() {
66         return decl.getQualifiedName();
67     }
68     
69     public File JavaDoc getLocation() {
70         return decl.getPosition().file().getParentFile();
71     }
72     
73     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationType) {
74         return decl.getAnnotation(annotationType);
75     }
76     
77     public Collection JavaDoc<Annotation JavaDoc> getAnnotations() {
78         Collection JavaDoc<Annotation JavaDoc> annotations = new ArrayList JavaDoc<Annotation JavaDoc>();
79         for (AnnotationMirror am : decl.getAnnotationMirrors()) {
80             AnnotationType at = am.getAnnotationType();
81             try {
82                 Class JavaDoc clazz = Class.forName(at.getDeclaration().getQualifiedName());
83                 Annotation JavaDoc a = decl.getAnnotation(clazz);
84                 if (null != a) {
85                     annotations.add(a);
86                 }
87             } catch (ClassNotFoundException JavaDoc e) {
88             }
89         }
90         return annotations;
91     }
92     
93     public Collection JavaDoc<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