KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.annotation.Annotation JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc returnType;
44     protected List JavaDoc<JavaParameterInfo> parameters = new ArrayList JavaDoc<JavaParameterInfo>();
45     
46     public MirrorMethodInfo(MethodDeclaration decl, AnnotationProcessorEnvironment env) {
47         if (null == decl) {
48             throw new IllegalArgumentException JavaDoc("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 JavaDoc e) {
57             logError("cannot find return type: " + decl.getReturnType());
58         }
59         
60         Collection JavaDoc<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 JavaDoc 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 JavaDoc getMethodName() {
81         return decl.getSimpleName();
82     }
83     
84     public Class JavaDoc getReturnType() {
85         return returnType;
86     }
87     
88     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationType) {
89         return decl.getAnnotation(annotationType);
90     }
91     
92     public Collection JavaDoc<Annotation JavaDoc> getAnnotations() {
93         Collection JavaDoc<Annotation JavaDoc> annotations = new ArrayList JavaDoc<Annotation JavaDoc>();
94         for (AnnotationMirror am : decl.getAnnotationMirrors()) {
95             AnnotationType at = am.getAnnotationType();
96             try {
97                 Class JavaDoc clazz = Class.forName(at.getDeclaration().getQualifiedName());
98                 Annotation JavaDoc a = decl.getAnnotation(clazz);
99                 if (null != a) {
100                     annotations.add(a);
101                 }
102             } catch (ClassNotFoundException JavaDoc e) {
103             }
104         }
105         return annotations;
106     }
107     
108     public List JavaDoc<JavaParameterInfo> getParameters() {
109         return parameters;
110     }
111 }
112
Popular Tags