KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > AptMethod


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

19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import com.sun.mirror.declaration.AnnotationMirror;
25 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
26 import com.sun.mirror.declaration.MethodDeclaration;
27 import com.sun.mirror.declaration.ParameterDeclaration;
28 import com.sun.mirror.declaration.TypeParameterDeclaration;
29 import com.sun.mirror.type.AnnotationType;
30 import com.sun.mirror.type.DeclaredType;
31 import com.sun.mirror.type.PrimitiveType;
32 import com.sun.mirror.type.ReferenceType;
33 import com.sun.mirror.type.TypeMirror;
34 import com.sun.mirror.type.WildcardType;
35
36 import org.apache.beehive.controls.api.packaging.FeatureInfo;
37 import org.apache.beehive.controls.runtime.generator.apt.TwoPhaseAnnotationProcessor;
38
39
40 /**
41  * The AptMethod class defines a base set of utility methods for acessing method attributes
42  * based upon an APT method declaration.
43  */

44 public class AptMethod
45 {
46     //
47
// Maps primitive type names to a default value string
48
//
49
private static HashMap JavaDoc<String JavaDoc, String JavaDoc> _defaultReturnValues = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
50
51     static final HashMap JavaDoc<PrimitiveType.Kind,String JavaDoc> _primToObject =
52                         new HashMap JavaDoc<PrimitiveType.Kind,String JavaDoc>();
53
54     static
55     {
56         _defaultReturnValues.put("void", "");
57         _defaultReturnValues.put("boolean", "false");
58         _defaultReturnValues.put("char", "'\0'");
59         _defaultReturnValues.put("byte", "0");
60         _defaultReturnValues.put("short", "0");
61         _defaultReturnValues.put("int", "0");
62         _defaultReturnValues.put("long", "0");
63         _defaultReturnValues.put("float", "0.0f");
64         _defaultReturnValues.put("double", "0.0d");
65
66         _primToObject.put(PrimitiveType.Kind.BOOLEAN, "Boolean");
67         _primToObject.put(PrimitiveType.Kind.BYTE, "Byte");
68         _primToObject.put(PrimitiveType.Kind.CHAR, "Character");
69         _primToObject.put(PrimitiveType.Kind.DOUBLE, "Double");
70         _primToObject.put(PrimitiveType.Kind.FLOAT, "Float");
71         _primToObject.put(PrimitiveType.Kind.INT, "Integer");
72         _primToObject.put(PrimitiveType.Kind.LONG, "Long");
73         _primToObject.put(PrimitiveType.Kind.SHORT, "Short");
74     }
75
76
77     /**
78      * Constructs a new AptMethod instance associated with a specific method declaration
79      */

80     public AptMethod(MethodDeclaration methodDecl, TwoPhaseAnnotationProcessor ap )
81     {
82         _methodDecl = methodDecl;
83         _interceptorServiceNames = initInterceptorServiceNames();
84         _ap = ap;
85     }
86
87     /**
88      * Returns the name of the method
89      */

90     public String JavaDoc getName()
91     {
92         if ( _methodDecl == null )
93             return "";
94         
95         return _methodDecl.getSimpleName();
96     }
97
98     /**
99      * Returns the argument declaration of the method, applying the bindings in the provided
100      * type map to any parameter types
101      */

102     public String JavaDoc getArgDecl(HashMap JavaDoc<String JavaDoc,TypeMirror> bindingMap)
103     {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
105
106         if ( _methodDecl.getParameters() == null )
107             return "";
108         
109         int i = 0;
110         for (ParameterDeclaration paramDecl : _methodDecl.getParameters())
111         {
112             TypeMirror paramType = paramDecl.getType();
113             if ( paramType == null )
114                 return "";
115                 
116             if (bindingMap != null && bindingMap.containsKey(paramType.toString()))
117                 paramType = bindingMap.get(paramType.toString());
118
119             if (i != 0)
120                 sb.append(", ");
121             
122             sb.append(paramType.toString());
123
124             sb.append(' ');
125
126             // BUGBUG: when the MethodDeclaration is derived from Reflection, this seems
127
// to return 'arg0' for all arguments!
128
String JavaDoc argName = paramDecl.getSimpleName();
129             if (argName.equals("arg0"))
130                 sb.append("arg" + i);
131             else
132                 sb.append(argName);
133
134             i++;
135         }
136         return sb.toString();
137     }
138
139     /**
140      * Returns the arguments declarations for the method, with no formal parameter binding applied
141      */

142     public String JavaDoc getArgDecl()
143     {
144         return getArgDecl(null);
145     }
146
147     /**
148      * Returns the the method argument names, in a comma separated list
149      */

150     public String JavaDoc getArgList(boolean quoteDelimit)
151     {
152         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
153         int i = 0;
154
155         if ( _methodDecl.getParameters() == null )
156             return "";
157         
158         for (ParameterDeclaration paramDecl : _methodDecl.getParameters())
159         {
160             if (i != 0)
161                 sb.append(", ");
162
163             // BUGBUG: when the MethodDeclaration is derived from Reflection, this seems
164
// to return 'arg0' for all arguments!
165
String JavaDoc argName = paramDecl.getSimpleName();
166             if (quoteDelimit) sb.append('"');
167             if (argName.equals("arg0"))
168                 sb.append("arg" + i);
169             else
170                 sb.append(argName);
171             if (quoteDelimit) sb.append('"');
172             i++;
173         }
174         return sb.toString();
175     }
176
177     /**
178      * Default form of getArgList, that does not quote delimit arguments
179      */

180     public String JavaDoc getArgList() { return getArgList(false); }
181
182     /**
183      * Returns the the method argument classes, in a comma separated list
184      */

185     public String JavaDoc getArgTypes()
186     {
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
188         int i = 0;
189
190         if ( _methodDecl == null || _methodDecl.getParameters() == null )
191             return "";
192         
193         for (ParameterDeclaration paramDecl : _methodDecl.getParameters())
194         {
195             if (i++ != 0)
196                 sb.append(", ");
197
198             TypeMirror paramType = paramDecl.getType();
199             if (paramType == null)
200                 return "";
201
202             //
203
// Use the erasure here, because we only want the raw type, not the reference
204
// type
205
//
206
sb.append(_ap.getAnnotationProcessorEnvironment().getTypeUtils().getErasure(paramType));
207             sb.append(".class");
208         }
209         return sb.toString();
210     }
211
212     /**
213      * Returns 'true' if the method uses any parameterized types as parameters
214      */

215     public boolean hasParameterizedArguments()
216     {
217         for (ParameterDeclaration paramDecl : _methodDecl.getParameters())
218         {
219             TypeMirror paramType = paramDecl.getType();
220             if (paramType instanceof ReferenceType || paramType instanceof WildcardType)
221                 return true;
222         }
223         return false;
224     }
225
226     /**
227      * Returns the declaration of any generic formal types associated with the method
228      */

229     public String JavaDoc getFormalTypes()
230     {
231         if ( _methodDecl == null || _methodDecl.getReturnType() == null )
232             return "";
233
234         Collection JavaDoc<TypeParameterDeclaration> formalTypes = _methodDecl.getFormalTypeParameters();
235         if (formalTypes.size() == 0)
236             return "";
237
238         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<");
239         boolean isFirst = true;
240         for (TypeParameterDeclaration tpd: formalTypes)
241         {
242             if (isFirst)
243                 isFirst = false;
244             else
245                 sb.append(", ");
246
247             sb.append(tpd.toString());
248         }
249         sb.append(">");
250         return sb.toString();
251     }
252
253     /**
254      * Returns the method return type, applying any formal type parameter bindings defined
255      * by the provided map.
256      */

257     public String JavaDoc getReturnType(HashMap JavaDoc<String JavaDoc,TypeMirror> bindingMap)
258     {
259         if ( _methodDecl == null || _methodDecl.getReturnType() == null )
260             return "";
261         
262         String JavaDoc returnType = _methodDecl.getReturnType().toString();
263         if (bindingMap != null && bindingMap.containsKey(returnType))
264             return bindingMap.get(returnType).toString();
265
266         return returnType;
267     }
268
269     /**
270      * Returns the method return type with no type bindings applied
271      */

272     public String JavaDoc getReturnType()
273     {
274         return getReturnType(null);
275     }
276
277     /**
278      * Returns the throws clause of the operation
279      */

280     public String JavaDoc getThrowsClause()
281     {
282         if ( _methodDecl == null || _methodDecl.getThrownTypes() == null )
283             return "";
284         
285         Collection JavaDoc<ReferenceType> thrownTypes = _methodDecl.getThrownTypes();
286         if (thrownTypes.size() == 0)
287             return "";
288
289         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("throws ");
290         int i = 0;
291         for (ReferenceType exceptType : thrownTypes)
292         {
293             if (i++ != 0)
294                 sb.append(", ");
295             sb.append(exceptType.toString());
296         }
297         return sb.toString();
298     }
299
300     /**
301      * Returns an ArrayList of thrown exceptions
302      */

303     public ArrayList JavaDoc<String JavaDoc> getThrowsList()
304     {
305         ArrayList JavaDoc<String JavaDoc> throwsList = new ArrayList JavaDoc<String JavaDoc>();
306
307         if ( _methodDecl == null ||
308              _methodDecl.getThrownTypes() == null ||
309              _methodDecl.getThrownTypes().size() == 0 )
310             return throwsList;
311         
312         Collection JavaDoc<ReferenceType> thrownTypes = _methodDecl.getThrownTypes();
313         for (ReferenceType exceptType : thrownTypes)
314             throwsList.add(exceptType.toString());
315
316         return throwsList;
317     }
318
319     /**
320      * Returns a default return value string for the method, based upon bound return type
321      */

322     public String JavaDoc getDefaultReturnValue(HashMap JavaDoc<String JavaDoc,TypeMirror> typeBinding)
323     {
324         String JavaDoc returnType = getReturnType(typeBinding);
325         if (_defaultReturnValues.containsKey(returnType))
326             return _defaultReturnValues.get(returnType);
327         return "null";
328     }
329
330     /**
331      * Returns a default return value string for the method, with no type binding applied
332      */

333     public String JavaDoc getDefaultReturnValue()
334     {
335         return getDefaultReturnValue(null);
336     }
337
338     /**
339      * Returns any FeatureInfo associated with the method (or null if none)
340      */

341     public FeatureInfo getFeatureInfo()
342     {
343         if ( _methodDecl == null )
344             return null;
345         
346         return _methodDecl.getAnnotation(FeatureInfo.class);
347     }
348
349     /**
350      * Sets the unique index value for this method. If a particular method is overloaded,
351      * then each associated AptMethod will have a unique index; otherwise, the index is -1.
352      */

353     public void setIndex(int index)
354     {
355         _index = index;
356     }
357
358     /**
359      * Returns the unique index value for this method.
360      */

361     public int getIndex() { return _index; }
362
363     MethodDeclaration _methodDecl;
364     int _index = -1;
365     TwoPhaseAnnotationProcessor _ap;
366
367     /**
368      * Returns the names of interceptor service interfaces associated with this operation
369      * @return the names of the interceptor service interfaces associated with this operation
370      */

371     public Collection JavaDoc<String JavaDoc> getInterceptorServiceNames()
372     {
373         return _interceptorServiceNames;
374     }
375
376     /**
377      * Returns the names of interceptor service interfaces associated with this operation, formatted as a
378      * constant initializer string.
379      * @return the names of the interceptor service interfaces associated with this operation
380      */

381     public String JavaDoc getInterceptorDecl()
382     {
383         Collection JavaDoc<String JavaDoc> names = getInterceptorServiceNames();
384         if ( names == null || names.size() == 0 )
385             return null;
386
387         StringBuffer JavaDoc ret = new StringBuffer JavaDoc("{");
388
389         String JavaDoc [] n = names.toArray(new String JavaDoc[0]);
390         for (int i=0 ; i < n.length ; ++i)
391         {
392             ret.append('"'); ret.append( n[i] ); ret.append('"');
393             if ( i != n.length-1 )
394                 ret.append(", ");
395         }
396         ret.append( "}" );
397
398         return ret.toString();
399     }
400
401     private Collection JavaDoc<String JavaDoc> initInterceptorServiceNames()
402     {
403         ArrayList JavaDoc<String JavaDoc> ret = new ArrayList JavaDoc<String JavaDoc>();
404
405         if (_methodDecl == null)
406             return ret;
407
408         // Iterate over annotations on operation, looking for interceptor-based ones
409
Collection JavaDoc<AnnotationMirror> annotations = _methodDecl.getAnnotationMirrors();
410         for ( AnnotationMirror a : annotations )
411         {
412             AnnotationType at = a.getAnnotationType();
413             AnnotationTypeDeclaration atd = at.getDeclaration();
414             Collection JavaDoc<AnnotationMirror> metaAnnotations = atd.getAnnotationMirrors();
415
416             // Look for annotations that are meta-annotated with @InterceptorAnnotation
417
for ( AnnotationMirror ma : metaAnnotations )
418             {
419                 if ( ma.getAnnotationType().getDeclaration().getQualifiedName().
420                         equals( "org.apache.beehive.controls.spi.svc.InterceptorAnnotation" ) )
421                 {
422                     // found an interceptor-based annotation, add it!
423
AptAnnotationHelper ia = new AptAnnotationHelper( ma );
424                     DeclaredType serviceType = (DeclaredType) ia.getObjectValue("service");
425                     String JavaDoc intf = serviceType.toString();
426                     ret.add( intf );
427
428                     break;
429                 }
430             }
431         }
432
433         return ret;
434     }
435
436     Collection JavaDoc<String JavaDoc> _interceptorServiceNames;
437
438 }
439
Popular Tags