1 package org.apache.beehive.controls.runtime.generator; 2 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashSet ; 23 24 import com.sun.mirror.declaration.InterfaceDeclaration; 25 import com.sun.mirror.declaration.MethodDeclaration; 26 import com.sun.mirror.declaration.TypeDeclaration; 27 import com.sun.mirror.declaration.TypeParameterDeclaration; 28 import com.sun.mirror.type.InterfaceType; 29 30 import org.apache.beehive.controls.api.events.EventSet; 31 import org.apache.beehive.controls.api.packaging.EventSetInfo; 32 33 import org.apache.beehive.controls.runtime.generator.apt.*; 34 35 39 public class AptEventSet extends AptType 40 { 41 47 public AptEventSet(AptControlInterface controlIntf, InterfaceDeclaration eventSet, 48 TwoPhaseAnnotationProcessor ap) 49 { 50 _controlIntf = controlIntf; 51 _eventSet = eventSet; 52 _ap = ap; 53 setDeclaration(eventSet); 54 55 EventSet eventSetAnnot = eventSet.getAnnotation(EventSet.class); 56 if (eventSetAnnot != null) 57 _unicast = eventSetAnnot.unicast(); 58 59 TypeDeclaration intfDecl = controlIntf.getTypeDeclaration(); 68 for (TypeParameterDeclaration estpd : _eventSet.getFormalTypeParameters()) 69 { 70 boolean found = false; 71 for (TypeParameterDeclaration citpd : intfDecl.getFormalTypeParameters()) 72 { 73 if (estpd.getSimpleName().equals(citpd.getSimpleName())) 74 { 75 found = true; 76 break; 77 } 78 } 79 if (! found) 80 { 81 89 _ap.printError( eventSet, "eventset.formal.parameter.mismatch" ); 90 break; 91 } 92 } 93 94 _superEventSet = initSuperEventSet(); 95 96 _events = initEvents(); 97 } 98 99 103 public AptEventSet initSuperEventSet() 104 { 105 AptControlInterface superControl = _controlIntf.getSuperClass(); 107 if (superControl == null) 108 return null; 109 110 HashSet <String > extendNames = new HashSet <String >(); 112 for (InterfaceType superType: _eventSet.getSuperinterfaces()) 113 { 114 InterfaceDeclaration superDecl = superType.getDeclaration(); 115 if (superDecl != null) 116 extendNames.add(superDecl.getQualifiedName()); 117 } 118 119 while (superControl != null) 123 { 124 Collection <AptEventSet> superEventSets = superControl.getEventSets(); 125 for (AptEventSet superEventSet : superEventSets) 126 { 127 if (extendNames.contains(superEventSet.getClassName())) 128 return superEventSet; 129 } 130 131 superControl = superControl.getSuperClass(); 132 } 133 134 return null; 136 } 137 138 141 public AptEventSet getSuperEventSet() { return _superEventSet; } 142 143 146 protected AptMethodSet<AptEvent> initEvents() 147 { 148 AptMethodSet<AptEvent> events = new AptMethodSet<AptEvent>(); 149 if ( _eventSet == null || _eventSet.getMethods() == null ) 150 return events; 151 152 ArrayList <InterfaceDeclaration> intfList = new ArrayList <InterfaceDeclaration>(); 157 intfList.add(_eventSet); 158 for (int i = 0; i < intfList.size(); i++) 159 { 160 InterfaceDeclaration intfDecl = intfList.get(i); 161 162 if (_superEventSet != null && 166 _superEventSet.getClassName().equals(intfDecl.getQualifiedName())) 167 continue; 168 169 for (MethodDeclaration methodDecl : intfDecl.getMethods()) 171 if (!methodDecl.toString().equals("<clinit>()")) 172 events.add(new AptEvent(this, methodDecl, _ap)); 173 174 for (InterfaceType superType: intfDecl.getSuperinterfaces()) 178 { 179 InterfaceDeclaration superDecl = superType.getDeclaration(); 180 if (superDecl != null && !intfList.contains(superDecl)) 181 intfList.add(superDecl); 182 } 183 } 184 185 return events; 186 } 187 188 191 public Collection <AptEvent> getEvents() { return _events.getMethods(); } 192 193 197 public boolean isUnicast() 198 { 199 return _unicast; 200 } 201 202 205 public int getEventCount() 206 { 207 int count = _events.size(); 208 if (_superEventSet != null) 209 count += _superEventSet.getEventCount(); 210 return count; 211 } 212 213 217 public String getDescriptorName() 218 { 219 String name = getShortName(); 225 return Character.toLowerCase(name.charAt(0)) + name.substring(1); 226 } 227 228 231 public String getNotifierClass() 232 { 233 StringBuffer sb = new StringBuffer (getShortName()); 234 sb.append("Notifier"); 235 236 sb.append(getFormalTypeParameterNames()); 243 return sb.toString(); 244 } 245 246 249 public String getNotifierExtends() 250 { 251 if (_superEventSet == null) 256 { 257 if (_unicast) 258 return "org.apache.beehive.controls.runtime.bean.UnicastEventNotifier"; 259 else 260 return "org.apache.beehive.controls.runtime.bean.EventNotifier"; 261 } 262 263 return _superEventSet.getNotifierClass(); 267 } 268 269 272 public String getAddListenerMethod() 273 { 274 return "add" + getShortName() + "Listener"; 275 } 276 277 280 public String getRemoveListenerMethod() 281 { 282 return "remove" + getShortName() + "Listener"; 283 } 284 285 288 public String getGetListenersMethod() 289 { 290 return "get" + getShortName() + "Listeners"; 291 } 292 293 297 public String getInfoInitializer() 298 { 299 return "init" + getShortName() + "Events"; 300 } 301 302 305 public EventSetInfo getEventSetInfo() 306 { 307 if ( _eventSet == null ) 308 return null; 309 310 return _eventSet.getAnnotation(EventSetInfo.class); 311 } 312 313 316 public InterfaceDeclaration getDeclaration() 317 { 318 return _eventSet; 319 } 320 321 private TwoPhaseAnnotationProcessor _ap; 322 private InterfaceDeclaration _eventSet; 323 private AptEventSet _superEventSet; 324 private AptControlInterface _controlIntf; 325 private AptMethodSet<AptEvent> _events; 326 private boolean _unicast; 327 } 328 | Popular Tags |