1 package org.apache.beehive.controls.runtime.generator; 2 19 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.io.IOException ; 26 import java.io.Writer ; 27 28 import com.sun.mirror.apt.Filer; 29 import com.sun.mirror.declaration.*; 30 import com.sun.mirror.type.TypeMirror; 31 import com.sun.mirror.type.ClassType; 32 33 import org.apache.beehive.controls.api.events.EventHandler; 34 import org.apache.beehive.controls.runtime.generator.apt.TwoPhaseAnnotationProcessor; 35 36 40 public class AptControlClient extends AptType implements Generator 41 { 42 47 public AptControlClient(Declaration decl, TwoPhaseAnnotationProcessor ap) 48 { 49 _ap = ap; 50 if (! (decl instanceof ClassDeclaration)) 51 { 52 _ap.printError( decl, "control.illegal.usage" ); 53 return; 54 } 55 _clientDecl = (ClassDeclaration)decl; 56 setDeclaration(_clientDecl); 57 58 _controls = initControls(); 59 initEventAdaptors(); 60 61 _init = new ClientInitializer(this); 65 } 66 67 70 protected boolean needsUniqueID() 71 { 72 for (AnnotationMirror annotMirror : _clientDecl.getAnnotationMirrors()) 81 { 82 String annotType = annotMirror.getAnnotationType().toString(); 83 if (annotType.equals("org.apache.beehive.netui.pageflow.annotations.Jpf.Controller") || 84 annotType.equals("org.apache.beehive.netui.pageflow.annotations.Jpf.Backing")) 85 return true; 86 } 87 return false; 88 } 89 90 93 public String getID(AptControlField control) 94 { 95 if (!needsUniqueID()) 96 return "\"" + control.getName() + "\""; 97 98 return "client.getClass() + \"@\" + client.hashCode() + \"." + control.getName() + "\""; 99 } 100 101 104 public ArrayList <AptControlField> getControls() { return _controls; } 105 106 109 public boolean hasControls() { return _controls.size() != 0; } 110 111 114 public boolean needsFieldInit() 115 { 116 return hasControls(); 117 } 118 119 122 public AptField getField(String name) 123 { 124 for (AptField field : _controls) 125 if (field.getName().equals(name)) 126 return field; 127 128 return null; 129 } 130 131 135 public String [] getGeneratedTypes() 136 { 137 return new String [] { _init.getClassName() }; 138 } 139 140 144 public List <GeneratorOutput> getCheckOutput(Filer filer) throws IOException 145 { 146 return null; 147 } 148 149 152 public List <GeneratorOutput> getGenerateOutput(Filer filer) throws IOException 153 { 154 HashMap <String ,Object > map = new HashMap <String ,Object >(); 155 map.put("client", this); map.put("init", _init); 158 Writer writer = new IndentingWriter(filer.createSourceFile(_init.getClassName())); 159 GeneratorOutput genOut = 160 new GeneratorOutput(writer,"org/apache/beehive/controls/runtime/generator/ClientInitializer.vm", 161 map); 162 ArrayList <GeneratorOutput> genList = new ArrayList <GeneratorOutput>(1); 163 genList.add(genOut); 164 return genList; 165 } 166 167 170 protected ArrayList <AptControlField> initControls() 171 { 172 ArrayList <AptControlField> controls = new ArrayList <AptControlField>(); 173 174 if ( _clientDecl == null || _clientDecl.getFields() == null ) 175 return controls; 176 177 Collection <FieldDeclaration> declaredFields = _clientDecl.getFields(); 178 for (FieldDeclaration fieldDecl : declaredFields) 179 { 180 if (fieldDecl.getAnnotation(org.apache.beehive.controls.api.bean.Control.class) != null) 181 controls.add(new AptControlField(this, fieldDecl, _ap)); 182 } 183 return controls; 184 } 185 186 public boolean hasSuperClient() 187 { 188 return ( getSuperClientName() != null ); 189 } 190 191 195 public String getSuperClientName() 196 { 197 ClassType superType = _clientDecl.getSuperclass(); 198 199 while ( superType != null ) 200 { 201 ClassDeclaration superDecl = superType.getDeclaration(); 202 203 Collection <FieldDeclaration> declaredFields = superDecl.getFields(); 204 for (FieldDeclaration fieldDecl : declaredFields) 205 { 206 if (fieldDecl.getAnnotation(org.apache.beehive.controls.api.bean.Control.class) != null) 207 { 208 return superDecl.getQualifiedName(); 210 } 211 } 212 213 superType = superType.getSuperclass(); 214 } 215 216 return null; 217 } 218 219 222 public AptControlClient getSuperClass() { return null; } 223 224 227 protected void initEventAdaptors() 228 { 229 if ( _clientDecl == null || _clientDecl.getMethods() == null ) 230 return; 231 232 for (MethodDeclaration clientMethod : _clientDecl.getMethods()) 233 { 234 if (clientMethod.getAnnotation(EventHandler.class) == null || 238 clientMethod.toString().equals("<clinit>()")) 239 continue; 240 241 AnnotationMirror handlerMirror = null; 246 for (AnnotationMirror annot : clientMethod.getAnnotationMirrors()) 247 { 248 if ( annot == null || 249 annot.getAnnotationType() == null || 250 annot.getAnnotationType().getDeclaration() == null || 251 annot.getAnnotationType().getDeclaration().getQualifiedName() == null ) 252 return; 253 254 if ( annot.getAnnotationType().getDeclaration().getQualifiedName().equals( 255 "org.apache.beehive.controls.api.events.EventHandler")) 256 { 257 handlerMirror = annot; 258 break; 259 } 260 } 261 if (handlerMirror == null) 262 { 263 throw new CodeGenerationException("Unable to find EventHandler annotation on " + 264 clientMethod); 265 } 266 267 AptAnnotationHelper handlerAnnot = new AptAnnotationHelper(handlerMirror); 268 269 String fieldName = (String )handlerAnnot.getObjectValue("field"); 273 AptEventField eventField = (AptEventField)getField(fieldName); 274 if (eventField == null) 275 { 276 continue; 280 } 281 282 TypeMirror tm = (TypeMirror)( handlerAnnot.getObjectValue("eventSet") ); 286 if ( tm == null ) 287 continue; 288 String setName = tm.toString(); 289 290 AptControlInterface controlIntf = eventField.getControlInterface(); 291 AptEventSet eventSet = controlIntf.getEventSet(setName); 292 if (eventSet == null) 293 { 294 _ap.printError( clientMethod, "eventhandler.eventset.not.found", setName ); 295 continue; 296 } 297 298 EventAdaptor adaptor = eventField.getEventAdaptor(eventSet); 302 if (adaptor == null) 303 { 304 adaptor = new EventAdaptor(eventField, eventSet); 305 eventField.addEventAdaptor(eventSet, adaptor); 306 } 307 308 boolean found = false; 313 String eventName = (String )handlerAnnot.getObjectValue("eventName"); 314 AptMethod handlerMethod = new AptMethod(clientMethod, _ap); 315 316 while (eventSet != null) 321 { 322 for (AptEvent controlEvent : eventSet.getEvents()) 323 { 324 if (controlEvent == null || 325 controlEvent.getName() == null || 326 !controlEvent.getName().equals(eventName)) 327 continue; 328 329 if ( controlEvent.getArgTypes() == null ) 330 continue; 331 332 if (controlEvent.hasParameterizedArguments() || 338 (controlEvent.getArgTypes().equals(handlerMethod.getArgTypes()) && 339 controlEvent.getReturnType().equals(handlerMethod.getReturnType()) 340 ) 341 ) 342 { 343 HashSet <String > throwSet = new HashSet <String >(controlEvent.getThrowsList()); 344 ArrayList <String > handlerThrows = handlerMethod.getThrowsList(); 345 boolean throwsMatches = true; 346 for ( String t : handlerThrows ) 347 { 348 if ( !throwSet.contains(t) ) 349 throwsMatches = false; 350 } 351 352 if ( !throwsMatches ) 353 { 354 _ap.printError( clientMethod, "eventhandler.throws.mismatch", handlerMethod.getName() ); 355 } 356 357 adaptor.addHandler(controlEvent, 358 new AptEventHandler(controlEvent, clientMethod, _ap )); 359 found = true; 360 break; 361 } 362 } 363 if (found) break; 365 366 eventSet = eventSet.getSuperEventSet(); 370 } 371 if (!found) 372 { 373 _ap.printError( clientMethod, "eventhandler.method.not.found", setName ); 374 } 375 } 376 } 377 378 ClassDeclaration _clientDecl; 379 TwoPhaseAnnotationProcessor _ap; 380 ArrayList <AptControlField> _controls; 381 ClientInitializer _init; 382 } 383 | Popular Tags |