1 package org.apache.beehive.controls.runtime.generator; 2 19 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Collection ; 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 44 public class AptMethod 45 { 46 private static HashMap <String , String > _defaultReturnValues = new HashMap <String ,String >(); 50 51 static final HashMap <PrimitiveType.Kind,String > _primToObject = 52 new HashMap <PrimitiveType.Kind,String >(); 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 80 public AptMethod(MethodDeclaration methodDecl, TwoPhaseAnnotationProcessor ap ) 81 { 82 _methodDecl = methodDecl; 83 _interceptorServiceNames = initInterceptorServiceNames(); 84 _ap = ap; 85 } 86 87 90 public String getName() 91 { 92 if ( _methodDecl == null ) 93 return ""; 94 95 return _methodDecl.getSimpleName(); 96 } 97 98 102 public String getArgDecl(HashMap <String ,TypeMirror> bindingMap) 103 { 104 StringBuffer sb = new StringBuffer (); 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 String 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 142 public String getArgDecl() 143 { 144 return getArgDecl(null); 145 } 146 147 150 public String getArgList(boolean quoteDelimit) 151 { 152 StringBuffer sb = new StringBuffer (); 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 String 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 180 public String getArgList() { return getArgList(false); } 181 182 185 public String getArgTypes() 186 { 187 StringBuffer sb = new StringBuffer (); 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 sb.append(_ap.getAnnotationProcessorEnvironment().getTypeUtils().getErasure(paramType)); 207 sb.append(".class"); 208 } 209 return sb.toString(); 210 } 211 212 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 229 public String getFormalTypes() 230 { 231 if ( _methodDecl == null || _methodDecl.getReturnType() == null ) 232 return ""; 233 234 Collection <TypeParameterDeclaration> formalTypes = _methodDecl.getFormalTypeParameters(); 235 if (formalTypes.size() == 0) 236 return ""; 237 238 StringBuffer sb = new StringBuffer ("<"); 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 257 public String getReturnType(HashMap <String ,TypeMirror> bindingMap) 258 { 259 if ( _methodDecl == null || _methodDecl.getReturnType() == null ) 260 return ""; 261 262 String returnType = _methodDecl.getReturnType().toString(); 263 if (bindingMap != null && bindingMap.containsKey(returnType)) 264 return bindingMap.get(returnType).toString(); 265 266 return returnType; 267 } 268 269 272 public String getReturnType() 273 { 274 return getReturnType(null); 275 } 276 277 280 public String getThrowsClause() 281 { 282 if ( _methodDecl == null || _methodDecl.getThrownTypes() == null ) 283 return ""; 284 285 Collection <ReferenceType> thrownTypes = _methodDecl.getThrownTypes(); 286 if (thrownTypes.size() == 0) 287 return ""; 288 289 StringBuffer sb = new StringBuffer ("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 303 public ArrayList <String > getThrowsList() 304 { 305 ArrayList <String > throwsList = new ArrayList <String >(); 306 307 if ( _methodDecl == null || 308 _methodDecl.getThrownTypes() == null || 309 _methodDecl.getThrownTypes().size() == 0 ) 310 return throwsList; 311 312 Collection <ReferenceType> thrownTypes = _methodDecl.getThrownTypes(); 313 for (ReferenceType exceptType : thrownTypes) 314 throwsList.add(exceptType.toString()); 315 316 return throwsList; 317 } 318 319 322 public String getDefaultReturnValue(HashMap <String ,TypeMirror> typeBinding) 323 { 324 String returnType = getReturnType(typeBinding); 325 if (_defaultReturnValues.containsKey(returnType)) 326 return _defaultReturnValues.get(returnType); 327 return "null"; 328 } 329 330 333 public String getDefaultReturnValue() 334 { 335 return getDefaultReturnValue(null); 336 } 337 338 341 public FeatureInfo getFeatureInfo() 342 { 343 if ( _methodDecl == null ) 344 return null; 345 346 return _methodDecl.getAnnotation(FeatureInfo.class); 347 } 348 349 353 public void setIndex(int index) 354 { 355 _index = index; 356 } 357 358 361 public int getIndex() { return _index; } 362 363 MethodDeclaration _methodDecl; 364 int _index = -1; 365 TwoPhaseAnnotationProcessor _ap; 366 367 371 public Collection <String > getInterceptorServiceNames() 372 { 373 return _interceptorServiceNames; 374 } 375 376 381 public String getInterceptorDecl() 382 { 383 Collection <String > names = getInterceptorServiceNames(); 384 if ( names == null || names.size() == 0 ) 385 return null; 386 387 StringBuffer ret = new StringBuffer ("{"); 388 389 String [] n = names.toArray(new String [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 <String > initInterceptorServiceNames() 402 { 403 ArrayList <String > ret = new ArrayList <String >(); 404 405 if (_methodDecl == null) 406 return ret; 407 408 Collection <AnnotationMirror> annotations = _methodDecl.getAnnotationMirrors(); 410 for ( AnnotationMirror a : annotations ) 411 { 412 AnnotationType at = a.getAnnotationType(); 413 AnnotationTypeDeclaration atd = at.getDeclaration(); 414 Collection <AnnotationMirror> metaAnnotations = atd.getAnnotationMirrors(); 415 416 for ( AnnotationMirror ma : metaAnnotations ) 418 { 419 if ( ma.getAnnotationType().getDeclaration().getQualifiedName(). 420 equals( "org.apache.beehive.controls.spi.svc.InterceptorAnnotation" ) ) 421 { 422 AptAnnotationHelper ia = new AptAnnotationHelper( ma ); 424 DeclaredType serviceType = (DeclaredType) ia.getObjectValue("service"); 425 String intf = serviceType.toString(); 426 ret.add( intf ); 427 428 break; 429 } 430 } 431 } 432 433 return ret; 434 } 435 436 Collection <String > _interceptorServiceNames; 437 438 } 439 | Popular Tags |