You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BcelTypeMunger.java 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.lang.reflect.Modifier;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Set;
  17. import org.aspectj.apache.bcel.Constants;
  18. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  19. import org.aspectj.apache.bcel.generic.FieldGen;
  20. import org.aspectj.apache.bcel.generic.INVOKESPECIAL;
  21. import org.aspectj.apache.bcel.generic.InstructionConstants;
  22. import org.aspectj.apache.bcel.generic.InstructionFactory;
  23. import org.aspectj.apache.bcel.generic.InstructionHandle;
  24. import org.aspectj.apache.bcel.generic.InstructionList;
  25. import org.aspectj.apache.bcel.generic.Type;
  26. import org.aspectj.bridge.IMessage;
  27. import org.aspectj.bridge.ISourceLocation;
  28. import org.aspectj.bridge.Message;
  29. import org.aspectj.bridge.MessageUtil;
  30. import org.aspectj.bridge.WeaveMessage;
  31. import org.aspectj.weaver.AjcMemberMaker;
  32. import org.aspectj.weaver.AnnotationOnTypeMunger;
  33. import org.aspectj.weaver.AsmRelationshipProvider;
  34. import org.aspectj.weaver.ConcreteTypeMunger;
  35. import org.aspectj.weaver.Member;
  36. import org.aspectj.weaver.NameMangler;
  37. import org.aspectj.weaver.NewConstructorTypeMunger;
  38. import org.aspectj.weaver.NewFieldTypeMunger;
  39. import org.aspectj.weaver.NewMethodTypeMunger;
  40. import org.aspectj.weaver.NewParentTypeMunger;
  41. import org.aspectj.weaver.PerObjectInterfaceTypeMunger;
  42. //import org.aspectj.weaver.PerTypeWithinTargetTypeMunger;
  43. import org.aspectj.weaver.PrivilegedAccessMunger;
  44. import org.aspectj.weaver.ResolvedMember;
  45. import org.aspectj.weaver.ResolvedTypeMunger;
  46. import org.aspectj.weaver.ResolvedTypeX;
  47. import org.aspectj.weaver.TypeX;
  48. import org.aspectj.weaver.WeaverMessages;
  49. import org.aspectj.weaver.WeaverStateInfo;
  50. import org.aspectj.weaver.patterns.Pointcut;
  51. //XXX addLazyMethodGen is probably bad everywhere
  52. public class BcelTypeMunger extends ConcreteTypeMunger {
  53. public BcelTypeMunger(ResolvedTypeMunger munger, ResolvedTypeX aspectType) {
  54. super(munger, aspectType);
  55. }
  56. public String toString() {
  57. return "(BcelTypeMunger " + getMunger() + ")";
  58. }
  59. public boolean munge(BcelClassWeaver weaver) {
  60. boolean changed = false;
  61. boolean worthReporting = true;
  62. if (munger.getKind() == ResolvedTypeMunger.Field) {
  63. changed = mungeNewField(weaver, (NewFieldTypeMunger)munger);
  64. } else if (munger.getKind() == ResolvedTypeMunger.Method) {
  65. changed = mungeNewMethod(weaver, (NewMethodTypeMunger)munger);
  66. } else if (munger.getKind() == ResolvedTypeMunger.PerObjectInterface) {
  67. changed = mungePerObjectInterface(weaver, (PerObjectInterfaceTypeMunger)munger);
  68. worthReporting = false;
  69. } else if (munger.getKind() == ResolvedTypeMunger.PerTypeWithinInterface) {
  70. // PTWIMPL Transform the target type (add the aspect instance field)
  71. changed = mungePerTypeWithinTransformer(weaver);
  72. worthReporting = false;
  73. } else if (munger.getKind() == ResolvedTypeMunger.PrivilegedAccess) {
  74. changed = mungePrivilegedAccess(weaver, (PrivilegedAccessMunger)munger);
  75. worthReporting = false;
  76. } else if (munger.getKind() == ResolvedTypeMunger.Constructor) {
  77. changed = mungeNewConstructor(weaver, (NewConstructorTypeMunger)munger);
  78. } else if (munger.getKind() == ResolvedTypeMunger.Parent) {
  79. changed = mungeNewParent(weaver, (NewParentTypeMunger)munger);
  80. } else if (munger.getKind() == ResolvedTypeMunger.AnnotationOnType) {
  81. changed = mungeNewAnnotationOnType(weaver,(AnnotationOnTypeMunger)munger);
  82. } else {
  83. throw new RuntimeException("unimplemented");
  84. }
  85. if (changed && munger.changesPublicSignature()) {
  86. WeaverStateInfo info =
  87. weaver.getLazyClassGen().getOrCreateWeaverStateInfo();
  88. info.addConcreteMunger(this);
  89. }
  90. // Whilst type mungers aren't persisting their source locations, we add this relationship during
  91. // compilation time (see other reference to ResolvedTypeMunger.persist)
  92. if (ResolvedTypeMunger.persistSourceLocation) {
  93. if (changed && worthReporting) {
  94. if (munger.getKind().equals(ResolvedTypeMunger.Parent)) {
  95. AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType());
  96. } else {
  97. AsmRelationshipProvider.getDefault().addRelationship(weaver.getLazyClassGen().getType(), munger,getAspectType());
  98. }
  99. }
  100. }
  101. // TAG: WeavingMessage
  102. if (changed && worthReporting && munger!=null && !weaver.getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)) {
  103. String tName = weaver.getLazyClassGen().getType().getSourceLocation().getSourceFile().getName();
  104. if (tName.indexOf("no debug info available")!=-1) tName = "no debug info available";
  105. else tName = getShortname(weaver.getLazyClassGen().getType().getSourceLocation().getSourceFile().getPath());
  106. String fName = getShortname(getAspectType().getSourceLocation().getSourceFile().getPath());
  107. if (munger.getKind().equals(ResolvedTypeMunger.Parent)) {
  108. // This message could come out of AjLookupEnvironment.addParent if doing parents
  109. // munging at compile time only...
  110. NewParentTypeMunger parentTM = (NewParentTypeMunger)munger;
  111. if (parentTM.getNewParent().isInterface()) {
  112. weaver.getWorld().getMessageHandler().handleMessage(WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_DECLAREPARENTSIMPLEMENTS,
  113. new String[]{weaver.getLazyClassGen().getType().getName(),
  114. tName,parentTM.getNewParent().getName(),fName},
  115. weaver.getLazyClassGen().getClassName(), getAspectType().getName()));
  116. } else {
  117. weaver.getWorld().getMessageHandler().handleMessage(
  118. WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_DECLAREPARENTSEXTENDS,
  119. new String[]{weaver.getLazyClassGen().getType().getName(),
  120. tName,parentTM.getNewParent().getName(),fName
  121. }));
  122. // TAG: WeavingMessage DECLARE PARENTS: EXTENDS
  123. // reportDeclareParentsMessage(WeaveMessage.WEAVEMESSAGE_DECLAREPARENTSEXTENDS,sourceType,parent);
  124. }
  125. } else {
  126. weaver.getWorld().getMessageHandler().handleMessage(WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_ITD,
  127. new String[]{weaver.getLazyClassGen().getType().getName(),
  128. tName,munger.getKind().toString().toLowerCase(),
  129. getAspectType().getName(),
  130. fName+":'"+munger.getSignature()+"'"},
  131. weaver.getLazyClassGen().getClassName(), getAspectType().getName()));
  132. }
  133. }
  134. return changed;
  135. }
  136. private String getShortname(String path) {
  137. int takefrom = path.lastIndexOf('/');
  138. if (takefrom == -1) {
  139. takefrom = path.lastIndexOf('\\');
  140. }
  141. return path.substring(takefrom+1);
  142. }
  143. private boolean mungeNewAnnotationOnType(BcelClassWeaver weaver,AnnotationOnTypeMunger munger) {
  144. // FIXME asc this has already been done up front, need to do it here too?
  145. weaver.getLazyClassGen().addAnnotation(munger.getNewAnnotation().getBcelAnnotation());
  146. return true;
  147. }
  148. /**
  149. * For a long time, AspectJ did not allow binary weaving of declare parents. This restriction is now lifted
  150. * but could do with more testing!
  151. */
  152. private boolean mungeNewParent(BcelClassWeaver weaver, NewParentTypeMunger munger) {
  153. LazyClassGen newParentTarget = weaver.getLazyClassGen();
  154. ResolvedTypeX newParent = munger.getNewParent();
  155. boolean cont = true; // Set to false when we error, so we don't actually *do* the munge
  156. cont = enforceDecpRule1_abstractMethodsImplemented(weaver, munger.getSourceLocation(),newParentTarget, newParent);
  157. cont = enforceDecpRule2_cantExtendFinalClass(weaver,munger.getSourceLocation(),newParentTarget,newParent) && cont;
  158. List methods = newParent.getMethodsWithoutIterator();
  159. for (Iterator iter = methods.iterator(); iter.hasNext();) {
  160. BcelMethod superMethod = (BcelMethod) iter.next();
  161. if (!superMethod.getName().equals("<init>")) {
  162. LazyMethodGen subMethod = findMatchingMethod(newParentTarget, superMethod);
  163. if (subMethod!=null) {
  164. cont = enforceDecpRule3_visibilityChanges(weaver, newParent, superMethod, subMethod) && cont;
  165. cont = enforceDecpRule4_compatibleReturnTypes(weaver, superMethod, subMethod) && cont;
  166. cont = enforceDecpRule5_cantChangeFromStaticToNonstatic(weaver,munger.getSourceLocation(),superMethod,subMethod) && cont;
  167. }
  168. }
  169. }
  170. if (!cont) return false; // A rule was violated and an error message already reported
  171. if (newParent.isClass()) { // Changing the supertype
  172. if (!attemptToModifySuperCalls(weaver,newParentTarget,newParent)) return false;
  173. newParentTarget.setSuperClass(newParent);
  174. } else { // Adding a new interface
  175. newParentTarget.addInterface(newParent,getSourceLocation());
  176. }
  177. return true;
  178. }
  179. /**
  180. * Rule 1: For the declare parents to be allowed, the target type must override and implement
  181. * inherited abstract methods (if the type is not declared abstract)
  182. */
  183. private boolean enforceDecpRule1_abstractMethodsImplemented(BcelClassWeaver weaver, ISourceLocation mungerLoc,LazyClassGen newParentTarget, ResolvedTypeX newParent) {
  184. boolean ruleCheckingSucceeded = true;
  185. if (!(newParentTarget.isAbstract() || newParentTarget.isInterface())) { // Ignore abstract classes or interfaces
  186. List methods = newParent.getMethodsWithoutIterator();
  187. for (Iterator i = methods.iterator(); i.hasNext();) {
  188. BcelMethod o = (BcelMethod)i.next();
  189. if (o.isAbstract() && !o.getName().startsWith("ajc$interField")) { // Ignore abstract methods of ajc$interField prefixed methods
  190. BcelMethod discoveredImpl = null;
  191. List newParentTargetMethods = newParentTarget.getType().getMethodsWithoutIterator();
  192. for (Iterator ii = newParentTargetMethods.iterator(); ii.hasNext() && discoveredImpl==null;) {
  193. BcelMethod gen2 = (BcelMethod) ii.next();
  194. if (gen2.getName().equals(o.getName()) &&
  195. gen2.getParameterSignature().equals(o.getParameterSignature()) && !gen2.isAbstract()) {
  196. discoveredImpl = gen2; // Found a valid implementation !
  197. }
  198. }
  199. if (discoveredImpl == null) {
  200. // didnt find a valid implementation, lets check the ITDs on this type to see if they satisfy it
  201. boolean satisfiedByITD = false;
  202. for (Iterator ii = newParentTarget.getType().getInterTypeMungersIncludingSupers().iterator(); ii.hasNext(); ) {
  203. ConcreteTypeMunger m = (ConcreteTypeMunger)ii.next();
  204. if (m.getMunger() instanceof NewMethodTypeMunger) {
  205. ResolvedMember sig = m.getSignature();
  206. if (!Modifier.isAbstract(sig.getModifiers())) {
  207. if (ResolvedTypeX
  208. .matches(
  209. AjcMemberMaker.interMethod(
  210. sig,m.getAspectType(),sig.getDeclaringType().isInterface(weaver.getWorld())),o)) {
  211. satisfiedByITD = true;
  212. }
  213. }
  214. }
  215. }
  216. if (!satisfiedByITD) {
  217. error(weaver,
  218. "The type " + newParentTarget.getName() + " must implement the inherited abstract method "+o.getDeclaringType()+"."+o.getName()+o.getParameterSignature(),
  219. newParentTarget.getType().getSourceLocation(),new ISourceLocation[]{o.getSourceLocation(),mungerLoc});
  220. ruleCheckingSucceeded=false;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. return ruleCheckingSucceeded;
  227. }
  228. /**
  229. * Rule 2. Can't extend final types
  230. */
  231. private boolean enforceDecpRule2_cantExtendFinalClass(BcelClassWeaver weaver, ISourceLocation mungerLoc,
  232. LazyClassGen newParentTarget, ResolvedTypeX newParent) {
  233. if (newParent.isFinal()) {
  234. error(weaver,"Cannot make type "+newParentTarget.getName()+" extend final class "+newParent.getName(),
  235. newParentTarget.getType().getSourceLocation(),
  236. new ISourceLocation[]{mungerLoc});
  237. return false;
  238. }
  239. return true;
  240. }
  241. /**
  242. * Rule 3. Can't narrow visibility of methods when overriding
  243. */
  244. private boolean enforceDecpRule3_visibilityChanges(BcelClassWeaver weaver, ResolvedTypeX newParent, BcelMethod superMethod, LazyMethodGen subMethod) {
  245. boolean cont = true;
  246. if (superMethod.isPublic()) {
  247. if (subMethod.isProtected() || subMethod.isDefault() || subMethod.isPrivate()) {
  248. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  249. "Cannot reduce the visibility of the inherited method '"+superMethod+"' from "+newParent.getName(),
  250. superMethod.getSourceLocation()));
  251. cont=false;
  252. }
  253. } else if (superMethod.isProtected()) {
  254. if (subMethod.isDefault() || subMethod.isPrivate()) {
  255. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  256. "Cannot reduce the visibility of the inherited method '"+superMethod+"' from "+newParent.getName(),
  257. superMethod.getSourceLocation()));
  258. cont=false;
  259. }
  260. } else if (superMethod.isDefault()) {
  261. if (subMethod.isPrivate()) {
  262. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  263. "Cannot reduce the visibility of the inherited method '"+superMethod+"' from "+newParent.getName(),
  264. superMethod.getSourceLocation()));
  265. cont=false;
  266. }
  267. }
  268. return cont;
  269. }
  270. /**
  271. * Rule 4. Can't have incompatible return types
  272. */
  273. private boolean enforceDecpRule4_compatibleReturnTypes(BcelClassWeaver weaver, BcelMethod superMethod, LazyMethodGen subMethod) {
  274. boolean cont = true;
  275. String superReturnTypeSig = superMethod.getReturnType().getSignature();
  276. String subReturnTypeSig = subMethod.getReturnType().getSignature();
  277. if (!superReturnTypeSig.equals(subReturnTypeSig)) {
  278. // Allow for covariance - wish I could test this (need Java5...)
  279. ResolvedTypeX subType = weaver.getWorld().resolve(subMethod.getReturnType());
  280. ResolvedTypeX superType = weaver.getWorld().resolve(superMethod.getReturnType());
  281. if (!subType.isAssignableFrom(superType)) {
  282. ISourceLocation sloc = subMethod.getSourceLocation();
  283. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  284. "The return type is incompatible with "+superMethod.getDeclaringType()+"."+superMethod.getName()+superMethod.getParameterSignature(),
  285. subMethod.getSourceLocation()));
  286. cont=false;
  287. }
  288. }
  289. return cont;
  290. }
  291. /**
  292. * Rule5. Method overrides can't change the staticality (word?) - you can't override and make an instance
  293. * method static or override and make a static method an instance method.
  294. */
  295. private boolean enforceDecpRule5_cantChangeFromStaticToNonstatic(BcelClassWeaver weaver,ISourceLocation mungerLoc,BcelMethod superMethod, LazyMethodGen subMethod ) {
  296. if (superMethod.isStatic() && !subMethod.isStatic()) {
  297. error(weaver,"This instance method "+subMethod.getName()+subMethod.getParameterSignature()+
  298. " cannot override the static method from "+superMethod.getDeclaringType().getName(),
  299. subMethod.getSourceLocation(),new ISourceLocation[]{mungerLoc});
  300. return false;
  301. } else if (!superMethod.isStatic() && subMethod.isStatic()) {
  302. error(weaver,"The static method "+subMethod.getName()+subMethod.getParameterSignature()+
  303. " cannot hide the instance method from "+superMethod.getDeclaringType().getName(),
  304. subMethod.getSourceLocation(),new ISourceLocation[]{mungerLoc});
  305. return false;
  306. }
  307. return true;
  308. }
  309. public void error(BcelClassWeaver weaver,String text,ISourceLocation primaryLoc,ISourceLocation[] extraLocs) {
  310. IMessage msg = new Message(text, primaryLoc, true, extraLocs);
  311. weaver.getWorld().getMessageHandler().handleMessage(msg);
  312. }
  313. private LazyMethodGen findMatchingMethod(LazyClassGen newParentTarget, BcelMethod m) {
  314. LazyMethodGen found = null;
  315. // Search the type for methods overriding super methods (methods that come from the new parent)
  316. // Don't use the return value in the comparison as overriding doesnt
  317. for (Iterator i = newParentTarget.getMethodGens().iterator(); i.hasNext() && found==null;) {
  318. LazyMethodGen gen = (LazyMethodGen) i.next();
  319. if (gen.getName().equals(m.getName()) &&
  320. gen.getParameterSignature().equals(m.getParameterSignature())) {
  321. found = gen;
  322. }
  323. }
  324. return found;
  325. }
  326. /**
  327. * The main part of implementing declare parents extends. Modify super ctor calls to target the new type.
  328. */
  329. public boolean attemptToModifySuperCalls(BcelClassWeaver weaver,LazyClassGen newParentTarget, ResolvedTypeX newParent) {
  330. String currentParent = newParentTarget.getSuperClassname();
  331. List mgs = newParentTarget.getMethodGens();
  332. // Look for ctors to modify
  333. for (Iterator iter = mgs.iterator(); iter.hasNext();) {
  334. LazyMethodGen aMethod = (LazyMethodGen) iter.next();
  335. if (aMethod.getName().equals("<init>")) {
  336. InstructionList insList = aMethod.getBody();
  337. InstructionHandle handle = insList.getStart();
  338. while (handle!= null) {
  339. if (handle.getInstruction() instanceof INVOKESPECIAL) {
  340. ConstantPoolGen cpg = newParentTarget.getConstantPoolGen();
  341. INVOKESPECIAL invokeSpecial = (INVOKESPECIAL)handle.getInstruction();
  342. if (invokeSpecial.getClassName(cpg).equals(currentParent) && invokeSpecial.getMethodName(cpg).equals("<init>")) {
  343. // System.err.println("Transforming super call '<init>"+sp.getSignature(cpg)+"'");
  344. // 1. Check there is a ctor in the new parent with the same signature
  345. ResolvedMember newCtor = getConstructorWithSignature(newParent,invokeSpecial.getSignature(cpg));
  346. if (newCtor == null) {
  347. // 2. Check ITDCs to see if the necessary ctor is provided that way
  348. boolean satisfiedByITDC = false;
  349. for (Iterator ii = newParentTarget.getType().getInterTypeMungersIncludingSupers().iterator(); ii.hasNext() && !satisfiedByITDC; ) {
  350. ConcreteTypeMunger m = (ConcreteTypeMunger)ii.next();
  351. if (m.getMunger() instanceof NewConstructorTypeMunger) {
  352. if (m.getSignature().getSignature().equals(invokeSpecial.getSignature(cpg))) {
  353. satisfiedByITDC = true;
  354. }
  355. }
  356. }
  357. if (!satisfiedByITDC) {
  358. String csig = createReadableCtorSig(newParent, cpg, invokeSpecial);
  359. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  360. "Unable to modify hierarchy for "+newParentTarget.getClassName()+" - the constructor "+
  361. csig+" is missing",this.getSourceLocation()));
  362. return false;
  363. }
  364. }
  365. int idx = cpg.addMethodref(newParent.getClassName(), invokeSpecial.getMethodName(cpg), invokeSpecial.getSignature(cpg));
  366. invokeSpecial.setIndex(idx);
  367. }
  368. }
  369. handle = handle.getNext();
  370. }
  371. }
  372. }
  373. return true;
  374. }
  375. /**
  376. * Creates a nice signature for the ctor, something like "(int,Integer,String)"
  377. */
  378. private String createReadableCtorSig(ResolvedTypeX newParent, ConstantPoolGen cpg, INVOKESPECIAL invokeSpecial) {
  379. StringBuffer sb = new StringBuffer();
  380. Type[] ctorArgs = invokeSpecial.getArgumentTypes(cpg);
  381. sb.append(newParent.getClassName());
  382. sb.append("(");
  383. for (int i = 0; i < ctorArgs.length; i++) {
  384. String argtype = ctorArgs[i].toString();
  385. if (argtype.lastIndexOf(".")!=-1)
  386. sb.append(argtype.substring(argtype.lastIndexOf(".")+1));
  387. else
  388. sb.append(argtype);
  389. if (i+1<ctorArgs.length) sb.append(",");
  390. }
  391. sb.append(")");
  392. return sb.toString();
  393. }
  394. private ResolvedMember getConstructorWithSignature(ResolvedTypeX tx,String signature) {
  395. ResolvedMember[] mems = tx.getDeclaredJavaMethods();
  396. for (int i = 0; i < mems.length; i++) {
  397. ResolvedMember rm = mems[i];
  398. if (rm.getName().equals("<init>")) {
  399. if (rm.getSignature().equals(signature)) return rm;
  400. }
  401. }
  402. return null;
  403. }
  404. private boolean mungePrivilegedAccess(
  405. BcelClassWeaver weaver,
  406. PrivilegedAccessMunger munger)
  407. {
  408. LazyClassGen gen = weaver.getLazyClassGen();
  409. ResolvedMember member = munger.getMember();
  410. ResolvedTypeX onType = weaver.getWorld().resolve(member.getDeclaringType(),munger.getSourceLocation());
  411. //System.out.println("munging: " + gen + " with " + member);
  412. if (onType.equals(gen.getType())) {
  413. if (member.getKind() == Member.FIELD) {
  414. //System.out.println("matched: " + gen);
  415. addFieldGetter(gen, member,
  416. AjcMemberMaker.privilegedAccessMethodForFieldGet(aspectType, member));
  417. addFieldSetter(gen, member,
  418. AjcMemberMaker.privilegedAccessMethodForFieldSet(aspectType, member));
  419. return true;
  420. } else if (member.getKind() == Member.METHOD) {
  421. addMethodDispatch(gen, member,
  422. AjcMemberMaker.privilegedAccessMethodForMethod(aspectType, member));
  423. return true;
  424. } else if (member.getKind() == Member.CONSTRUCTOR) {
  425. for (Iterator i = gen.getMethodGens().iterator(); i.hasNext(); ) {
  426. LazyMethodGen m = (LazyMethodGen)i.next();
  427. if (m.getMemberView() != null
  428. && m.getMemberView().getKind() == Member.CONSTRUCTOR) {
  429. // m.getMemberView().equals(member)) {
  430. m.forcePublic();
  431. //return true;
  432. }
  433. }
  434. return true;
  435. //throw new BCException("no match for " + member + " in " + gen);
  436. } else if (member.getKind() == Member.STATIC_INITIALIZATION) {
  437. gen.forcePublic();
  438. return true;
  439. } else {
  440. throw new RuntimeException("unimplemented");
  441. }
  442. }
  443. return false;
  444. }
  445. private void addFieldGetter(
  446. LazyClassGen gen,
  447. ResolvedMember field,
  448. ResolvedMember accessMethod)
  449. {
  450. LazyMethodGen mg = makeMethodGen(gen, accessMethod);
  451. InstructionList il = new InstructionList();
  452. InstructionFactory fact = gen.getFactory();
  453. if (field.isStatic()) {
  454. il.append(fact.createFieldAccess(
  455. gen.getClassName(),
  456. field.getName(),
  457. BcelWorld.makeBcelType(field.getType()), Constants.GETSTATIC));
  458. } else {
  459. il.append(InstructionConstants.ALOAD_0);
  460. il.append(fact.createFieldAccess(
  461. gen.getClassName(),
  462. field.getName(),
  463. BcelWorld.makeBcelType(field.getType()), Constants.GETFIELD));
  464. }
  465. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(field.getType())));
  466. mg.getBody().insert(il);
  467. gen.addMethodGen(mg,getSignature().getSourceLocation());
  468. }
  469. private void addFieldSetter(
  470. LazyClassGen gen,
  471. ResolvedMember field,
  472. ResolvedMember accessMethod)
  473. {
  474. LazyMethodGen mg = makeMethodGen(gen, accessMethod);
  475. InstructionList il = new InstructionList();
  476. InstructionFactory fact = gen.getFactory();
  477. Type fieldType = BcelWorld.makeBcelType(field.getType());
  478. if (field.isStatic()) {
  479. il.append(InstructionFactory.createLoad(fieldType, 0));
  480. il.append(fact.createFieldAccess(
  481. gen.getClassName(),
  482. field.getName(),
  483. fieldType, Constants.PUTSTATIC));
  484. } else {
  485. il.append(InstructionConstants.ALOAD_0);
  486. il.append(InstructionFactory.createLoad(fieldType, 1));
  487. il.append(fact.createFieldAccess(
  488. gen.getClassName(),
  489. field.getName(),
  490. fieldType, Constants.PUTFIELD));
  491. }
  492. il.append(InstructionFactory.createReturn(Type.VOID));
  493. mg.getBody().insert(il);
  494. gen.addMethodGen(mg,getSignature().getSourceLocation());
  495. }
  496. private void addMethodDispatch(
  497. LazyClassGen gen,
  498. ResolvedMember method,
  499. ResolvedMember accessMethod)
  500. {
  501. LazyMethodGen mg = makeMethodGen(gen, accessMethod);
  502. InstructionList il = new InstructionList();
  503. InstructionFactory fact = gen.getFactory();
  504. //Type fieldType = BcelWorld.makeBcelType(field.getType());
  505. Type[] paramTypes = BcelWorld.makeBcelTypes(method.getParameterTypes());
  506. int pos = 0;
  507. if (!method.isStatic()) {
  508. il.append(InstructionConstants.ALOAD_0);
  509. pos++;
  510. }
  511. for (int i = 0, len = paramTypes.length; i < len; i++) {
  512. Type paramType = paramTypes[i];
  513. il.append(InstructionFactory.createLoad(paramType, pos));
  514. pos+=paramType.getSize();
  515. }
  516. il.append(Utility.createInvoke(fact, (BcelWorld)aspectType.getWorld(),
  517. method));
  518. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(method.getReturnType())));
  519. mg.getBody().insert(il);
  520. gen.addMethodGen(mg);
  521. }
  522. protected LazyMethodGen makeMethodGen(LazyClassGen gen, ResolvedMember member) {
  523. LazyMethodGen ret = new LazyMethodGen(
  524. member.getModifiers(),
  525. BcelWorld.makeBcelType(member.getReturnType()),
  526. member.getName(),
  527. BcelWorld.makeBcelTypes(member.getParameterTypes()),
  528. TypeX.getNames(member.getExceptions()),
  529. gen);
  530. // 43972 : Static crosscutting makes interfaces unusable for javac
  531. // ret.makeSynthetic();
  532. return ret;
  533. }
  534. protected FieldGen makeFieldGen(LazyClassGen gen, ResolvedMember member) {
  535. return new FieldGen(
  536. member.getModifiers(),
  537. BcelWorld.makeBcelType(member.getReturnType()),
  538. member.getName(),
  539. gen.getConstantPoolGen());
  540. }
  541. private boolean mungePerObjectInterface(
  542. BcelClassWeaver weaver,
  543. PerObjectInterfaceTypeMunger munger)
  544. {
  545. LazyClassGen gen = weaver.getLazyClassGen();
  546. if (couldMatch(gen.getBcelObjectType(), munger.getTestPointcut())) {
  547. FieldGen fg = makeFieldGen(gen,
  548. AjcMemberMaker.perObjectField(gen.getType(), aspectType));
  549. gen.addField(fg.getField(),getSourceLocation());
  550. Type fieldType = BcelWorld.makeBcelType(aspectType);
  551. LazyMethodGen mg = new LazyMethodGen(
  552. Modifier.PUBLIC,
  553. fieldType,
  554. NameMangler.perObjectInterfaceGet(aspectType),
  555. new Type[0], new String[0],
  556. gen);
  557. InstructionList il = new InstructionList();
  558. InstructionFactory fact = gen.getFactory();
  559. il.append(InstructionConstants.ALOAD_0);
  560. il.append(fact.createFieldAccess(
  561. gen.getClassName(),
  562. fg.getName(),
  563. fieldType, Constants.GETFIELD));
  564. il.append(InstructionFactory.createReturn(fieldType));
  565. mg.getBody().insert(il);
  566. gen.addMethodGen(mg);
  567. LazyMethodGen mg1 = new LazyMethodGen(
  568. Modifier.PUBLIC,
  569. Type.VOID,
  570. NameMangler.perObjectInterfaceSet(aspectType),
  571. new Type[]{fieldType,}, new String[0],
  572. gen);
  573. InstructionList il1 = new InstructionList();
  574. il1.append(InstructionConstants.ALOAD_0);
  575. il1.append(InstructionFactory.createLoad(fieldType, 1));
  576. il1.append(fact.createFieldAccess(
  577. gen.getClassName(),
  578. fg.getName(),
  579. fieldType, Constants.PUTFIELD));
  580. il1.append(InstructionFactory.createReturn(Type.VOID));
  581. mg1.getBody().insert(il1);
  582. gen.addMethodGen(mg1);
  583. gen.addInterface(munger.getInterfaceType(),getSourceLocation());
  584. return true;
  585. } else {
  586. return false;
  587. }
  588. }
  589. // PTWIMPL Add field to hold aspect instance and an accessor
  590. private boolean mungePerTypeWithinTransformer(BcelClassWeaver weaver) {
  591. LazyClassGen gen = weaver.getLazyClassGen();
  592. // if (couldMatch(gen.getBcelObjectType(), munger.getTestPointcut())) {
  593. // Add (to the target type) the field that will hold the aspect instance
  594. // e.g ajc$com_blah_SecurityAspect$ptwAspectInstance
  595. FieldGen fg = makeFieldGen(gen, AjcMemberMaker.perTypeWithinField(gen.getType(), aspectType));
  596. gen.addField(fg.getField(),getSourceLocation());
  597. // Add an accessor for this new field, the ajc$<aspectname>$localAspectOf() method
  598. // e.g. "public com_blah_SecurityAspect ajc$com_blah_SecurityAspect$localAspectOf()"
  599. Type fieldType = BcelWorld.makeBcelType(aspectType);
  600. LazyMethodGen mg = new LazyMethodGen(
  601. Modifier.PUBLIC | Modifier.STATIC,fieldType,
  602. NameMangler.perTypeWithinLocalAspectOf(aspectType),
  603. new Type[0], new String[0],gen);
  604. InstructionList il = new InstructionList();
  605. //PTWIMPL ?? Should check if it is null and throw NoAspectBoundException
  606. InstructionFactory fact = gen.getFactory();
  607. il.append(fact.createFieldAccess(
  608. gen.getClassName(),
  609. fg.getName(),
  610. fieldType, Constants.GETSTATIC));
  611. il.append(InstructionFactory.createReturn(fieldType));
  612. mg.getBody().insert(il);
  613. gen.addMethodGen(mg);
  614. return true;
  615. // } else {
  616. // return false;
  617. // }
  618. }
  619. // ??? Why do we have this method? I thought by now we would know if it matched or not
  620. private boolean couldMatch(
  621. BcelObjectType bcelObjectType,
  622. Pointcut pointcut) {
  623. return !bcelObjectType.isInterface();
  624. }
  625. private boolean mungeNewMethod(BcelClassWeaver weaver, NewMethodTypeMunger munger) {
  626. ResolvedMember signature = munger.getSignature();
  627. ResolvedMember dispatchMethod = munger.getDispatchMethod(aspectType);
  628. LazyClassGen gen = weaver.getLazyClassGen();
  629. ResolvedTypeX onType = weaver.getWorld().resolve(signature.getDeclaringType(),munger.getSourceLocation());
  630. boolean onInterface = onType.isInterface();
  631. if (onType.isAnnotation(weaver.getWorld())) {
  632. signalError(WeaverMessages.ITDM_ON_ANNOTATION_NOT_ALLOWED,weaver,onType);
  633. return false;
  634. }
  635. if (onType.isEnum(weaver.getWorld())) {
  636. signalError(WeaverMessages.ITDM_ON_ENUM_NOT_ALLOWED,weaver,onType);
  637. return false;
  638. }
  639. if (onType.equals(gen.getType())) {
  640. ResolvedMember introMethod =
  641. AjcMemberMaker.interMethod(signature, aspectType, onInterface);
  642. LazyMethodGen mg = makeMethodGen(gen, introMethod);
  643. if (!onInterface && !Modifier.isAbstract(introMethod.getModifiers())) {
  644. InstructionList body = mg.getBody();
  645. InstructionFactory fact = gen.getFactory();
  646. int pos = 0;
  647. if (!signature.isStatic()) {
  648. body.append(InstructionFactory.createThis());
  649. pos++;
  650. }
  651. Type[] paramTypes = BcelWorld.makeBcelTypes(introMethod.getParameterTypes());
  652. for (int i = 0, len = paramTypes.length; i < len; i++) {
  653. Type paramType = paramTypes[i];
  654. body.append(InstructionFactory.createLoad(paramType, pos));
  655. pos+=paramType.getSize();
  656. }
  657. body.append(Utility.createInvoke(fact, weaver.getWorld(), dispatchMethod));
  658. body.append(
  659. InstructionFactory.createReturn(
  660. BcelWorld.makeBcelType(introMethod.getReturnType())));
  661. } else {
  662. //??? this is okay
  663. //if (!(mg.getBody() == null)) throw new RuntimeException("bas");
  664. }
  665. // XXX make sure to check that we set exceptions properly on this guy.
  666. weaver.addLazyMethodGen(mg);
  667. weaver.getLazyClassGen().warnOnAddedMethod(mg.getMethod(),getSignature().getSourceLocation());
  668. addNeededSuperCallMethods(weaver, onType, munger.getSuperMethodsCalled());
  669. return true;
  670. } else if (onInterface && !Modifier.isAbstract(signature.getModifiers())) {
  671. // This means the 'gen' should be the top most implementor
  672. // - if it is *not* then something went wrong after we worked
  673. // out that it was the top most implementor (see pr49657)
  674. if (!gen.getType().isTopmostImplementor(onType)) {
  675. ResolvedTypeX rtx = gen.getType().getTopmostImplementor(onType);
  676. if (!rtx.isExposedToWeaver()) {
  677. ISourceLocation sLoc = munger.getSourceLocation();
  678. weaver.getWorld().getMessageHandler().handleMessage(MessageUtil.error(
  679. WeaverMessages.format(WeaverMessages.ITD_NON_EXPOSED_IMPLEMENTOR,rtx,getAspectType().getName()),
  680. (sLoc==null?getAspectType().getSourceLocation():sLoc)));
  681. } else {
  682. // XXX what does this state mean?
  683. // We have incorrectly identified what is the top most implementor and its not because
  684. // a type wasn't exposed to the weaver
  685. }
  686. return false;
  687. } else {
  688. ResolvedMember introMethod =
  689. AjcMemberMaker.interMethod(signature, aspectType, false);
  690. LazyMethodGen mg = makeMethodGen(gen, introMethod);
  691. Type[] paramTypes = BcelWorld.makeBcelTypes(introMethod.getParameterTypes());
  692. Type returnType = BcelWorld.makeBcelType(introMethod.getReturnType());
  693. InstructionList body = mg.getBody();
  694. InstructionFactory fact = gen.getFactory();
  695. int pos = 0;
  696. if (!introMethod.isStatic()) {
  697. body.append(InstructionFactory.createThis());
  698. pos++;
  699. }
  700. for (int i = 0, len = paramTypes.length; i < len; i++) {
  701. Type paramType = paramTypes[i];
  702. body.append(InstructionFactory.createLoad(paramType, pos));
  703. pos+=paramType.getSize();
  704. }
  705. body.append(Utility.createInvoke(fact, weaver.getWorld(), dispatchMethod));
  706. body.append(InstructionFactory.createReturn(returnType));
  707. mg.definingType = onType;
  708. weaver.addOrReplaceLazyMethodGen(mg);
  709. addNeededSuperCallMethods(weaver, onType, munger.getSuperMethodsCalled());
  710. return true;
  711. }
  712. } else {
  713. return false;
  714. }
  715. }
  716. private void addNeededSuperCallMethods(
  717. BcelClassWeaver weaver,
  718. ResolvedTypeX onType,
  719. Set neededSuperCalls)
  720. {
  721. LazyClassGen gen = weaver.getLazyClassGen();
  722. for (Iterator iter = neededSuperCalls.iterator(); iter.hasNext();) {
  723. ResolvedMember superMethod = (ResolvedMember) iter.next();
  724. if (weaver.addDispatchTarget(superMethod)) {
  725. //System.err.println("super type: " + superMethod.getDeclaringType() + ", " + gen.getType());
  726. boolean isSuper = !superMethod.getDeclaringType().equals(gen.getType());
  727. String dispatchName;
  728. if (isSuper)
  729. dispatchName =
  730. NameMangler.superDispatchMethod(onType, superMethod.getName());
  731. else
  732. dispatchName =
  733. NameMangler.protectedDispatchMethod(
  734. onType,
  735. superMethod.getName());
  736. LazyMethodGen dispatcher =
  737. makeDispatcher(
  738. gen,
  739. dispatchName,
  740. superMethod,
  741. weaver.getWorld(),
  742. isSuper);
  743. weaver.addLazyMethodGen(dispatcher);
  744. }
  745. }
  746. }
  747. private void signalError(String msgid,BcelClassWeaver weaver,TypeX onType) {
  748. IMessage msg = MessageUtil.error(
  749. WeaverMessages.format(msgid,onType.getName()),getSourceLocation());
  750. weaver.getWorld().getMessageHandler().handleMessage(msg);
  751. }
  752. private boolean mungeNewConstructor(
  753. BcelClassWeaver weaver,
  754. NewConstructorTypeMunger newConstructorTypeMunger)
  755. {
  756. final LazyClassGen currentClass = weaver.getLazyClassGen();
  757. final InstructionFactory fact = currentClass.getFactory();
  758. ResolvedMember newConstructorMember = newConstructorTypeMunger.getSyntheticConstructor();
  759. TypeX onType = newConstructorMember.getDeclaringType();
  760. if (onType.isAnnotation(weaver.getWorld())) {
  761. signalError(WeaverMessages.ITDC_ON_ANNOTATION_NOT_ALLOWED,weaver,onType);
  762. return false;
  763. }
  764. if (onType.isEnum(weaver.getWorld())) {
  765. signalError(WeaverMessages.ITDC_ON_ENUM_NOT_ALLOWED,weaver,onType);
  766. return false;
  767. }
  768. if (! onType.equals(currentClass.getType())) return false;
  769. ResolvedMember explicitConstructor = newConstructorTypeMunger.getExplicitConstructor();
  770. //int declaredParameterCount = newConstructorTypeMunger.getDeclaredParameterCount();
  771. LazyMethodGen freshConstructor =
  772. makeMethodGen(currentClass, newConstructorMember);
  773. currentClass.addMethodGen(freshConstructor);
  774. //weaver.addLazyMethodGen(freshConstructor);
  775. InstructionList body = freshConstructor.getBody();
  776. // add to body: push arts for call to pre, from actual args starting at 1 (skipping this), going to
  777. // declared argcount + 1
  778. TypeX[] declaredParams = newConstructorTypeMunger.getSignature().getParameterTypes();
  779. Type[] paramTypes = freshConstructor.getArgumentTypes();
  780. int frameIndex = 1;
  781. for (int i = 0, len = declaredParams.length; i < len; i++) {
  782. body.append(InstructionFactory.createLoad(paramTypes[i], frameIndex));
  783. frameIndex += paramTypes[i].getSize();
  784. }
  785. // do call to pre
  786. Member preMethod =
  787. AjcMemberMaker.preIntroducedConstructor(aspectType, onType, declaredParams);
  788. body.append(Utility.createInvoke(fact, null, preMethod));
  789. // create a local, and store return pre stuff into it.
  790. int arraySlot = freshConstructor.allocateLocal(1);
  791. body.append(InstructionFactory.createStore(Type.OBJECT, arraySlot));
  792. // put this on the stack
  793. body.append(InstructionConstants.ALOAD_0);
  794. // unpack pre args onto stack
  795. TypeX[] superParamTypes = explicitConstructor.getParameterTypes();
  796. for (int i = 0, len = superParamTypes.length; i < len; i++) {
  797. body.append(InstructionFactory.createLoad(Type.OBJECT, arraySlot));
  798. body.append(Utility.createConstant(fact, i));
  799. body.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  800. body.append(
  801. Utility.createConversion(
  802. fact,
  803. Type.OBJECT,
  804. BcelWorld.makeBcelType(superParamTypes[i])));
  805. }
  806. // call super/this
  807. body.append(Utility.createInvoke(fact, null, explicitConstructor));
  808. // put this back on the stack
  809. body.append(InstructionConstants.ALOAD_0);
  810. // unpack params onto stack
  811. Member postMethod =
  812. AjcMemberMaker.postIntroducedConstructor(aspectType, onType, declaredParams);
  813. TypeX[] postParamTypes = postMethod.getParameterTypes();
  814. for (int i = 1, len = postParamTypes.length; i < len; i++) {
  815. body.append(InstructionFactory.createLoad(Type.OBJECT, arraySlot));
  816. body.append(Utility.createConstant(fact, superParamTypes.length + i-1));
  817. body.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  818. body.append(
  819. Utility.createConversion(
  820. fact,
  821. Type.OBJECT,
  822. BcelWorld.makeBcelType(postParamTypes[i])));
  823. }
  824. // call post
  825. body.append(Utility.createInvoke(fact, null, postMethod));
  826. // don't forget to return!!
  827. body.append(InstructionConstants.RETURN);
  828. return true;
  829. }
  830. private static LazyMethodGen makeDispatcher(
  831. LazyClassGen onGen,
  832. String dispatchName,
  833. ResolvedMember superMethod,
  834. BcelWorld world,
  835. boolean isSuper)
  836. {
  837. Type[] paramTypes = BcelWorld.makeBcelTypes(superMethod.getParameterTypes());
  838. Type returnType = BcelWorld.makeBcelType(superMethod.getReturnType());
  839. int modifiers = Modifier.PUBLIC;
  840. if (onGen.isInterface()) modifiers |= Modifier.ABSTRACT;
  841. LazyMethodGen mg =
  842. new LazyMethodGen(
  843. modifiers,
  844. returnType,
  845. dispatchName,
  846. paramTypes,
  847. TypeX.getNames(superMethod.getExceptions()),
  848. onGen);
  849. InstructionList body = mg.getBody();
  850. if (onGen.isInterface()) return mg;
  851. // assert (!superMethod.isStatic())
  852. InstructionFactory fact = onGen.getFactory();
  853. int pos = 0;
  854. body.append(InstructionFactory.createThis());
  855. pos++;
  856. for (int i = 0, len = paramTypes.length; i < len; i++) {
  857. Type paramType = paramTypes[i];
  858. body.append(InstructionFactory.createLoad(paramType, pos));
  859. pos+=paramType.getSize();
  860. }
  861. if (isSuper) {
  862. body.append(Utility.createSuperInvoke(fact, world, superMethod));
  863. } else {
  864. body.append(Utility.createInvoke(fact, world, superMethod));
  865. }
  866. body.append(InstructionFactory.createReturn(returnType));
  867. return mg;
  868. }
  869. private boolean mungeNewField(BcelClassWeaver weaver, NewFieldTypeMunger munger) {
  870. /*ResolvedMember initMethod = */munger.getInitMethod(aspectType);
  871. LazyClassGen gen = weaver.getLazyClassGen();
  872. ResolvedMember field = munger.getSignature();
  873. ResolvedTypeX onType = weaver.getWorld().resolve(field.getDeclaringType(),munger.getSourceLocation());
  874. boolean onInterface = onType.isInterface();
  875. if (onType.isAnnotation(weaver.getWorld())) {
  876. signalError(WeaverMessages.ITDF_ON_ANNOTATION_NOT_ALLOWED,weaver,onType);
  877. return false;
  878. }
  879. if (onType.isEnum(weaver.getWorld())) {
  880. signalError(WeaverMessages.ITDF_ON_ENUM_NOT_ALLOWED,weaver,onType);
  881. return false;
  882. }
  883. if (onType.equals(gen.getType())) {
  884. if (onInterface) {
  885. LazyMethodGen mg = makeMethodGen(gen,
  886. AjcMemberMaker.interFieldInterfaceGetter(field, onType, aspectType));
  887. gen.addMethodGen(mg);
  888. LazyMethodGen mg1 = makeMethodGen(gen,
  889. AjcMemberMaker.interFieldInterfaceSetter(field, onType, aspectType));
  890. gen.addMethodGen(mg1);
  891. } else {
  892. weaver.addInitializer(this);
  893. FieldGen fg = makeFieldGen(gen,
  894. AjcMemberMaker.interFieldClassField(field, aspectType));
  895. gen.addField(fg.getField(),getSourceLocation());
  896. }
  897. return true;
  898. } else if (onInterface && gen.getType().isTopmostImplementor(onType)) {
  899. // wew know that we can't be static since we don't allow statics on interfaces
  900. if (field.isStatic()) throw new RuntimeException("unimplemented");
  901. weaver.addInitializer(this);
  902. //System.err.println("impl body on " + gen.getType() + " for " + munger);
  903. Type fieldType = BcelWorld.makeBcelType(field.getType());
  904. FieldGen fg = makeFieldGen(gen,
  905. AjcMemberMaker.interFieldInterfaceField(field, onType, aspectType));
  906. gen.addField(fg.getField(),getSourceLocation());
  907. //this uses a shadow munger to add init method to constructors
  908. //weaver.getShadowMungers().add(makeInitCallShadowMunger(initMethod));
  909. LazyMethodGen mg = makeMethodGen(gen,
  910. AjcMemberMaker.interFieldInterfaceGetter(field, gen.getType(), aspectType));
  911. InstructionList il = new InstructionList();
  912. InstructionFactory fact = gen.getFactory();
  913. if (field.isStatic()) {
  914. il.append(fact.createFieldAccess(
  915. gen.getClassName(),
  916. fg.getName(),
  917. fieldType, Constants.GETSTATIC));
  918. } else {
  919. il.append(InstructionConstants.ALOAD_0);
  920. il.append(fact.createFieldAccess(
  921. gen.getClassName(),
  922. fg.getName(),
  923. fieldType, Constants.GETFIELD));
  924. }
  925. il.append(InstructionFactory.createReturn(fieldType));
  926. mg.getBody().insert(il);
  927. gen.addMethodGen(mg);
  928. LazyMethodGen mg1 = makeMethodGen(gen,
  929. AjcMemberMaker.interFieldInterfaceSetter(field, gen.getType(), aspectType));
  930. InstructionList il1 = new InstructionList();
  931. if (field.isStatic()) {
  932. il1.append(InstructionFactory.createLoad(fieldType, 0));
  933. il1.append(fact.createFieldAccess(
  934. gen.getClassName(),
  935. fg.getName(),
  936. fieldType, Constants.PUTSTATIC));
  937. } else {
  938. il1.append(InstructionConstants.ALOAD_0);
  939. il1.append(InstructionFactory.createLoad(fieldType, 1));
  940. il1.append(fact.createFieldAccess(
  941. gen.getClassName(),
  942. fg.getName(),
  943. fieldType, Constants.PUTFIELD));
  944. }
  945. il1.append(InstructionFactory.createReturn(Type.VOID));
  946. mg1.getBody().insert(il1);
  947. gen.addMethodGen(mg1);
  948. return true;
  949. } else {
  950. return false;
  951. }
  952. }
  953. }