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.

LazyClassGen.java 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /* *******************************************************************
  2. * Copyright (c) 2002-2010 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Andy Clement 6Jul05 generics - signature attribute
  12. * Abraham Nevado
  13. * ******************************************************************/
  14. package org.aspectj.weaver.bcel;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.PrintStream;
  19. import java.lang.reflect.Modifier;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.SortedMap;
  30. import java.util.TreeMap;
  31. import java.util.Vector;
  32. import org.aspectj.apache.bcel.Constants;
  33. import org.aspectj.apache.bcel.classfile.Attribute;
  34. import org.aspectj.apache.bcel.classfile.ConstantPool;
  35. import org.aspectj.apache.bcel.classfile.Field;
  36. import org.aspectj.apache.bcel.classfile.JavaClass;
  37. import org.aspectj.apache.bcel.classfile.Method;
  38. import org.aspectj.apache.bcel.classfile.Signature;
  39. import org.aspectj.apache.bcel.classfile.Synthetic;
  40. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  41. import org.aspectj.apache.bcel.generic.BasicType;
  42. import org.aspectj.apache.bcel.generic.ClassGen;
  43. import org.aspectj.apache.bcel.generic.FieldGen;
  44. import org.aspectj.apache.bcel.generic.InstructionConstants;
  45. import org.aspectj.apache.bcel.generic.InstructionFactory;
  46. import org.aspectj.apache.bcel.generic.InstructionHandle;
  47. import org.aspectj.apache.bcel.generic.InstructionList;
  48. import org.aspectj.apache.bcel.generic.ObjectType;
  49. import org.aspectj.apache.bcel.generic.Type;
  50. import org.aspectj.bridge.IMessage;
  51. import org.aspectj.bridge.ISourceLocation;
  52. import org.aspectj.bridge.SourceLocation;
  53. import org.aspectj.weaver.AjAttribute;
  54. import org.aspectj.weaver.AjAttribute.WeaverState;
  55. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  56. import org.aspectj.weaver.BCException;
  57. import org.aspectj.weaver.Member;
  58. import org.aspectj.weaver.NameMangler;
  59. import org.aspectj.weaver.ResolvedMember;
  60. import org.aspectj.weaver.ResolvedType;
  61. import org.aspectj.weaver.Shadow;
  62. import org.aspectj.weaver.SignatureUtils;
  63. import org.aspectj.weaver.TypeVariable;
  64. import org.aspectj.weaver.UnresolvedType;
  65. import org.aspectj.weaver.UnresolvedType.TypeKind;
  66. import org.aspectj.weaver.WeaverMessages;
  67. import org.aspectj.weaver.WeaverStateInfo;
  68. import org.aspectj.weaver.World;
  69. import org.aspectj.weaver.bcel.asm.AsmDetector;
  70. import org.aspectj.weaver.bcel.asm.StackMapAdder;
  71. /**
  72. * Lazy lazy lazy. We don't unpack the underlying class unless necessary. Things like new methods and annotations accumulate in here
  73. * until they must be written out, don't add them to the underlying MethodGen! Things are slightly different if this represents an
  74. * Aspect.
  75. */
  76. public final class LazyClassGen {
  77. private static final int ACC_SYNTHETIC = 0x1000;
  78. private static final String[] NO_STRINGS = new String[0];
  79. int highestLineNumber = 0; // ---- JSR 45 info
  80. private final SortedMap<String, InlinedSourceFileInfo> inlinedFiles = new TreeMap<String, InlinedSourceFileInfo>();
  81. private boolean regenerateGenericSignatureAttribute = false;
  82. private BcelObjectType myType; // XXX is not set for types we create
  83. private ClassGen myGen;
  84. private final ConstantPool cp;
  85. private final World world;
  86. private final String packageName = null;
  87. private final List<BcelField> fields = new ArrayList<BcelField>();
  88. private final List<LazyMethodGen> methodGens = new ArrayList<LazyMethodGen>();
  89. private final List<LazyClassGen> classGens = new ArrayList<LazyClassGen>();
  90. private final List<AnnotationGen> annotations = new ArrayList<AnnotationGen>();
  91. private int childCounter = 0;
  92. private final InstructionFactory fact;
  93. private boolean isSerializable = false;
  94. private boolean hasSerialVersionUIDField = false;
  95. private boolean serialVersionUIDRequiresInitialization = false;
  96. private long calculatedSerialVersionUID;
  97. private boolean hasClinit = false;
  98. private ResolvedType[] extraSuperInterfaces = null;
  99. private ResolvedType superclass = null;
  100. // ---
  101. static class InlinedSourceFileInfo {
  102. int highestLineNumber;
  103. int offset; // calculated
  104. InlinedSourceFileInfo(int highestLineNumber) {
  105. this.highestLineNumber = highestLineNumber;
  106. }
  107. }
  108. void addInlinedSourceFileInfo(String fullpath, int highestLineNumber) {
  109. Object o = inlinedFiles.get(fullpath);
  110. if (o != null) {
  111. InlinedSourceFileInfo info = (InlinedSourceFileInfo) o;
  112. if (info.highestLineNumber < highestLineNumber) {
  113. info.highestLineNumber = highestLineNumber;
  114. }
  115. } else {
  116. inlinedFiles.put(fullpath, new InlinedSourceFileInfo(highestLineNumber));
  117. }
  118. }
  119. void calculateSourceDebugExtensionOffsets() {
  120. int i = roundUpToHundreds(highestLineNumber);
  121. for (InlinedSourceFileInfo element : inlinedFiles.values()) {
  122. element.offset = i;
  123. i = roundUpToHundreds(i + element.highestLineNumber);
  124. }
  125. }
  126. private static int roundUpToHundreds(int i) {
  127. return ((i / 100) + 1) * 100;
  128. }
  129. int getSourceDebugExtensionOffset(String fullpath) {
  130. return inlinedFiles.get(fullpath).offset;
  131. }
  132. // private Unknown getSourceDebugExtensionAttribute() {
  133. // int nameIndex = cp.addUtf8("SourceDebugExtension");
  134. // String data = getSourceDebugExtensionString();
  135. // //System.err.println(data);
  136. // byte[] bytes = Utility.stringToUTF(data);
  137. // int length = bytes.length;
  138. //
  139. // return new Unknown(nameIndex, length, bytes, cp);
  140. // }
  141. // private LazyClassGen() {}
  142. // public static void main(String[] args) {
  143. // LazyClassGen m = new LazyClassGen();
  144. // m.highestLineNumber = 37;
  145. // m.inlinedFiles.put("boo/baz/foo.java", new InlinedSourceFileInfo( 83));
  146. // m.inlinedFiles.put("boo/barz/foo.java", new InlinedSourceFileInfo(292));
  147. // m.inlinedFiles.put("boo/baz/moo.java", new InlinedSourceFileInfo(128));
  148. // m.calculateSourceDebugExtensionOffsets();
  149. // System.err.println(m.getSourceDebugExtensionString());
  150. // }
  151. // For the entire pathname, we're using package names. This is probably
  152. // wrong.
  153. // private String getSourceDebugExtensionString() {
  154. // StringBuffer out = new StringBuffer();
  155. // String myFileName = getFileName();
  156. // // header section
  157. // out.append("SMAP\n");
  158. // out.append(myFileName);
  159. // out.append("\nAspectJ\n");
  160. // // stratum section
  161. // out.append("*S AspectJ\n");
  162. // // file section
  163. // out.append("*F\n");
  164. // out.append("1 ");
  165. // out.append(myFileName);
  166. // out.append("\n");
  167. // int i = 2;
  168. // for (Iterator iter = inlinedFiles.keySet().iterator(); iter.hasNext();) {
  169. // String element = (String) iter.next();
  170. // int ii = element.lastIndexOf('/');
  171. // if (ii == -1) {
  172. // out.append(i++); out.append(' ');
  173. // out.append(element); out.append('\n');
  174. // } else {
  175. // out.append("+ "); out.append(i++); out.append(' ');
  176. // out.append(element.substring(ii+1)); out.append('\n');
  177. // out.append(element); out.append('\n');
  178. // }
  179. // }
  180. // // emit line section
  181. // out.append("*L\n");
  182. // out.append("1#1,");
  183. // out.append(highestLineNumber);
  184. // out.append(":1,1\n");
  185. // i = 2;
  186. // for (Iterator iter = inlinedFiles.values().iterator(); iter.hasNext();) {
  187. // InlinedSourceFileInfo element = (InlinedSourceFileInfo) iter.next();
  188. // out.append("1#");
  189. // out.append(i++); out.append(',');
  190. // out.append(element.highestLineNumber); out.append(":");
  191. // out.append(element.offset + 1); out.append(",1\n");
  192. // }
  193. // // end section
  194. // out.append("*E\n");
  195. // // and finish up...
  196. // return out.toString();
  197. // }
  198. // ---- end JSR45-related stuff
  199. /** Emit disassembled class and newline to out */
  200. public static void disassemble(String path, String name, PrintStream out) throws IOException {
  201. if (null == out) {
  202. return;
  203. }
  204. // out.println("classPath: " + classPath);
  205. BcelWorld world = new BcelWorld(path);
  206. UnresolvedType ut = UnresolvedType.forName(name);
  207. ut.setNeedsModifiableDelegate(true);
  208. LazyClassGen clazz = new LazyClassGen(BcelWorld.getBcelObjectType(world.resolve(ut)));
  209. clazz.print(out);
  210. out.println();
  211. }
  212. public String getNewGeneratedNameTag() {
  213. return new Integer(childCounter++).toString();
  214. }
  215. // ----
  216. public LazyClassGen(String class_name, String super_class_name, String file_name, int access_flags, String[] interfaces,
  217. World world) {
  218. myGen = new ClassGen(class_name, super_class_name, file_name, access_flags, interfaces);
  219. cp = myGen.getConstantPool();
  220. fact = new InstructionFactory(myGen, cp);
  221. regenerateGenericSignatureAttribute = true;
  222. this.world = world;
  223. }
  224. // Non child type, so it comes from a real type in the world.
  225. public LazyClassGen(BcelObjectType myType) {
  226. myGen = new ClassGen(myType.getJavaClass());
  227. cp = myGen.getConstantPool();
  228. fact = new InstructionFactory(myGen, cp);
  229. this.myType = myType;
  230. world = myType.getResolvedTypeX().getWorld();
  231. /* Does this class support serialization */
  232. if (implementsSerializable(getType())) {
  233. isSerializable = true;
  234. // ResolvedMember[] fields = getType().getDeclaredFields();
  235. // for (int i = 0; i < fields.length; i++) {
  236. // ResolvedMember field = fields[i];
  237. // if (field.getName().equals("serialVersionUID")
  238. // && field.isStatic() && field.getType().equals(UnresolvedType.LONG))
  239. // {
  240. // hasSerialVersionUIDField = true;
  241. // }
  242. // }
  243. hasSerialVersionUIDField = hasSerialVersionUIDField(getType());
  244. ResolvedMember[] methods = getType().getDeclaredMethods();
  245. for (int i = 0; i < methods.length; i++) {
  246. ResolvedMember method = methods[i];
  247. if (method.getName().equals("<clinit>")) {
  248. if (method.getKind() != Member.STATIC_INITIALIZATION) {
  249. throw new RuntimeException("qui?");
  250. }
  251. hasClinit = true;
  252. }
  253. }
  254. // Do we need to calculate an SUID and add it?
  255. if (!getType().isInterface() && !hasSerialVersionUIDField && world.isAddSerialVerUID()) {
  256. calculatedSerialVersionUID = myGen.getSUID();
  257. FieldGen fg = new FieldGen(Constants.ACC_PRIVATE | Constants.ACC_FINAL | Constants.ACC_STATIC, BasicType.LONG,
  258. "serialVersionUID", getConstantPool());
  259. addField(fg);
  260. hasSerialVersionUIDField = true;
  261. serialVersionUIDRequiresInitialization = true;
  262. // warn about what we've done?
  263. if (world.getLint().calculatingSerialVersionUID.isEnabled()) {
  264. world.getLint().calculatingSerialVersionUID.signal(
  265. new String[] { getClassName(), Long.toString(calculatedSerialVersionUID) + "L" }, null, null);
  266. }
  267. }
  268. }
  269. ResolvedMember[] methods = myType.getDeclaredMethods();
  270. for (int i = 0; i < methods.length; i++) {
  271. addMethodGen(new LazyMethodGen((BcelMethod) methods[i], this));
  272. }
  273. // Method[] methods = myGen.getMethods();
  274. // for (int i = 0; i < methods.length; i++) {
  275. // addMethodGen(new LazyMethodGen(methods[i], this));
  276. // }
  277. ResolvedMember[] fields = myType.getDeclaredFields();
  278. for (int i = 0; i < fields.length; i++) {
  279. this.fields.add((BcelField) fields[i]);
  280. }
  281. }
  282. public static boolean hasSerialVersionUIDField(ResolvedType type) {
  283. ResolvedMember[] fields = type.getDeclaredFields();
  284. for (int i = 0; i < fields.length; i++) {
  285. ResolvedMember field = fields[i];
  286. if (field.getName().equals("serialVersionUID") && Modifier.isStatic(field.getModifiers())
  287. && field.getType().equals(UnresolvedType.LONG)) {
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. // public void addAttribute(Attribute i) {
  294. // myGen.addAttribute(i);
  295. // }
  296. // ----
  297. public String getInternalClassName() {
  298. return getConstantPool().getConstantString_CONSTANTClass(myGen.getClassNameIndex());
  299. // getConstantPool().getConstantString(
  300. // myGen.getClassNameIndex(),
  301. // Constants.CONSTANT_Class);
  302. }
  303. public String getInternalFileName() {
  304. String str = getInternalClassName();
  305. int index = str.lastIndexOf('/');
  306. if (index == -1) {
  307. return getFileName();
  308. } else {
  309. return str.substring(0, index + 1) + getFileName();
  310. }
  311. }
  312. /**
  313. * Returns the packagename - if its the default package we return an empty string
  314. */
  315. public String getPackageName() {
  316. if (packageName != null) {
  317. return packageName;
  318. }
  319. String str = getInternalClassName();
  320. int index = str.indexOf("<");
  321. if (index != -1) {
  322. str = str.substring(0, index); // strip off the generics guff
  323. }
  324. index = str.lastIndexOf("/");
  325. if (index == -1) {
  326. return "";
  327. }
  328. return str.substring(0, index).replace('/', '.');
  329. }
  330. public void addMethodGen(LazyMethodGen gen) {
  331. // assert gen.getClassName() == super.getClassName();
  332. methodGens.add(gen);
  333. if (highestLineNumber < gen.highestLineNumber) {
  334. highestLineNumber = gen.highestLineNumber;
  335. }
  336. }
  337. public boolean removeMethodGen(LazyMethodGen gen) {
  338. return methodGens.remove(gen);
  339. }
  340. public void addMethodGen(LazyMethodGen gen, ISourceLocation sourceLocation) {
  341. addMethodGen(gen);
  342. if (!gen.getMethod().isPrivate()) {
  343. warnOnAddedMethod(gen.getMethod(), sourceLocation);
  344. }
  345. }
  346. public void errorOnAddedField(FieldGen field, ISourceLocation sourceLocation) {
  347. if (isSerializable && !hasSerialVersionUIDField) {
  348. getWorld().getLint().serialVersionUIDBroken.signal(
  349. new String[] { myType.getResolvedTypeX().getName(), field.getName() }, sourceLocation, null);
  350. }
  351. }
  352. public void warnOnAddedInterface(String name, ISourceLocation sourceLocation) {
  353. warnOnModifiedSerialVersionUID(sourceLocation, "added interface " + name);
  354. }
  355. public void warnOnAddedMethod(Method method, ISourceLocation sourceLocation) {
  356. warnOnModifiedSerialVersionUID(sourceLocation, "added non-private method " + method.getName());
  357. }
  358. public void warnOnAddedStaticInitializer(Shadow shadow, ISourceLocation sourceLocation) {
  359. if (!hasClinit) {
  360. warnOnModifiedSerialVersionUID(sourceLocation, "added static initializer");
  361. }
  362. }
  363. public void warnOnModifiedSerialVersionUID(ISourceLocation sourceLocation, String reason) {
  364. if (isSerializable && !hasSerialVersionUIDField) {
  365. getWorld().getLint().needsSerialVersionUIDField.signal(new String[] { myType.getResolvedTypeX().getName().toString(),
  366. reason }, sourceLocation, null);
  367. }
  368. }
  369. public World getWorld() {
  370. return world;
  371. }
  372. public List<LazyMethodGen> getMethodGens() {
  373. return methodGens; // ???Collections.unmodifiableList(methodGens);
  374. }
  375. public List<BcelField> getFieldGens() {
  376. return fields;
  377. }
  378. public boolean fieldExists(String name) {
  379. // Field[] allFields = myGen.getFields();
  380. // if (allFields!=null) {
  381. // for (int i=0;i<allFields.length;i++) {
  382. // Field f = allFields[i];
  383. // if (f.getName().equals(name)) {
  384. // return f;
  385. // }
  386. // }
  387. // }
  388. for (BcelField f: fields) {
  389. if (f.getName().equals(name)) {
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. private void writeBack(BcelWorld world) {
  396. if (getConstantPool().getSize() > Short.MAX_VALUE) {
  397. reportClassTooBigProblem();
  398. return;
  399. }
  400. if (annotations.size() > 0) {
  401. for (AnnotationGen element : annotations) {
  402. myGen.addAnnotation(element);
  403. }
  404. // Attribute[] annAttributes =
  405. // org.aspectj.apache.bcel.classfile.Utility.getAnnotationAttributes(
  406. // getConstantPool(),annotations);
  407. // for (int i = 0; i < annAttributes.length; i++) {
  408. // Attribute attribute = annAttributes[i];
  409. // System.err.println("Adding attribute for "+attribute);
  410. // myGen.addAttribute(attribute);
  411. // }
  412. }
  413. // Add a weaver version attribute to the file being produced (if
  414. // necessary...)
  415. if (!myGen.hasAttribute("org.aspectj.weaver.WeaverVersion")) {
  416. myGen.addAttribute(Utility.bcelAttribute(new AjAttribute.WeaverVersionInfo(), getConstantPool()));
  417. }
  418. // 352389: don't add another one (there will already be one there and this new one won't deserialize correctly)
  419. if (!world.isOverWeaving() || !myGen.hasAttribute(WeaverState.AttributeName)) {
  420. if (myType != null && myType.getWeaverState() != null) {
  421. myGen.addAttribute(Utility.bcelAttribute(new AjAttribute.WeaverState(myType.getWeaverState()), getConstantPool()));
  422. }
  423. }
  424. // FIXME ATAJ needed only for slow Aspects.aspectOf() - keep or remove
  425. // make a lot of test fail since the test compare weaved class file
  426. // based on some test data as text files...
  427. // if (!myGen.isInterface()) {
  428. // addAjClassField();
  429. // }
  430. addAjcInitializers();
  431. // 17Feb05 - ASC - Skip this for now - it crashes IBM 1.4.2 jvms
  432. // (pr80430). Will be revisited when contents
  433. // of attribute are confirmed to be correct.
  434. boolean sourceDebugExtensionSupportSwitchedOn = false;
  435. if (sourceDebugExtensionSupportSwitchedOn) {
  436. calculateSourceDebugExtensionOffsets();
  437. }
  438. int len = methodGens.size();
  439. myGen.setMethods(Method.NoMethods);
  440. for (LazyMethodGen gen : methodGens) {
  441. // we skip empty clinits
  442. if (isEmptyClinit(gen)) {
  443. continue;
  444. }
  445. myGen.addMethod(gen.getMethod());
  446. }
  447. len = fields.size();
  448. myGen.setFields(Field.NoFields);
  449. for (int i = 0; i < len; i++) {
  450. BcelField gen = fields.get(i);
  451. myGen.addField(gen.getField(cp));
  452. }
  453. if (sourceDebugExtensionSupportSwitchedOn) {
  454. if (inlinedFiles.size() != 0) {
  455. if (hasSourceDebugExtensionAttribute(myGen)) {
  456. world.showMessage(IMessage.WARNING, WeaverMessages.format(WeaverMessages.OVERWRITE_JSR45, getFileName()), null,
  457. null);
  458. }
  459. // myGen.addAttribute(getSourceDebugExtensionAttribute());
  460. }
  461. }
  462. fixupGenericSignatureAttribute();
  463. }
  464. /**
  465. * When working with Java generics, a signature attribute is attached to the type which indicates how it was declared. This
  466. * routine ensures the signature attribute for the class we are about to write out is correct. Basically its responsibilities
  467. * are:
  468. * <ol>
  469. * <li>
  470. * Checking whether the attribute needs changing (ie. did weaving change the type hierarchy) - if it did, remove the old
  471. * attribute
  472. * <li>
  473. * Check if we need an attribute at all, are we generic? are our supertypes parameterized/generic?
  474. * <li>
  475. * Build the new attribute which includes all typevariable, supertype and superinterface information
  476. * </ol>
  477. */
  478. private void fixupGenericSignatureAttribute() {
  479. if (getWorld() != null && !getWorld().isInJava5Mode()) {
  480. return;
  481. }
  482. // TODO asc generics Temporarily assume that types we generate dont need
  483. // a signature attribute (closure/etc).. will need
  484. // revisiting no doubt...
  485. // if (myType == null) {
  486. // return;
  487. // }
  488. // 1. Has anything changed that would require us to modify this
  489. // attribute?
  490. if (!regenerateGenericSignatureAttribute) {
  491. return;
  492. }
  493. // 2. Find the old attribute
  494. Signature sigAttr = null;
  495. if (myType != null) { // if null, this is a type built from scratch, it
  496. // won't already have a sig attribute
  497. sigAttr = (Signature) myGen.getAttribute("Signature");
  498. }
  499. // 3. Do we need an attribute?
  500. boolean needAttribute = false;
  501. // If we had one before, we definetly still need one as types can't be
  502. // 'removed' from the hierarchy
  503. if (sigAttr != null) {
  504. needAttribute = true;
  505. }
  506. // check the interfaces
  507. if (!needAttribute) {
  508. if (myType != null) {
  509. ResolvedType[] interfaceRTXs = myType.getDeclaredInterfaces();
  510. for (int i = 0; i < interfaceRTXs.length; i++) {
  511. ResolvedType typeX = interfaceRTXs[i];
  512. if (typeX.isGenericType() || typeX.isParameterizedType()) {
  513. needAttribute = true;
  514. }
  515. }
  516. if (extraSuperInterfaces != null) {
  517. for (int i = 0; i < extraSuperInterfaces.length; i++) {
  518. ResolvedType interfaceType = extraSuperInterfaces[i];
  519. if (interfaceType.isGenericType() || interfaceType.isParameterizedType()) {
  520. needAttribute = true;
  521. }
  522. }
  523. }
  524. }
  525. if (myType == null) {
  526. ResolvedType superclassRTX = superclass;
  527. if (superclassRTX != null) {
  528. if (superclassRTX.isGenericType() || superclassRTX.isParameterizedType()) {
  529. needAttribute = true;
  530. }
  531. }
  532. } else {
  533. // check the supertype
  534. ResolvedType superclassRTX = getSuperClass();
  535. if (superclassRTX.isGenericType() || superclassRTX.isParameterizedType()) {
  536. needAttribute = true;
  537. }
  538. }
  539. }
  540. if (needAttribute) {
  541. StringBuffer signature = new StringBuffer();
  542. // first, the type variables...
  543. if (myType != null) {
  544. TypeVariable[] tVars = myType.getTypeVariables();
  545. if (tVars.length > 0) {
  546. signature.append("<");
  547. for (int i = 0; i < tVars.length; i++) {
  548. TypeVariable variable = tVars[i];
  549. signature.append(variable.getSignatureForAttribute());
  550. }
  551. signature.append(">");
  552. }
  553. }
  554. // now the supertype
  555. String supersig = getSuperClass().getSignatureForAttribute();
  556. signature.append(supersig);
  557. if (myType != null) {
  558. ResolvedType[] interfaceRTXs = myType.getDeclaredInterfaces();
  559. for (int i = 0; i < interfaceRTXs.length; i++) {
  560. String s = interfaceRTXs[i].getSignatureForAttribute();
  561. signature.append(s);
  562. }
  563. if (extraSuperInterfaces != null) {
  564. for (int i = 0; i < extraSuperInterfaces.length; i++) {
  565. String s = extraSuperInterfaces[i].getSignatureForAttribute();
  566. signature.append(s);
  567. }
  568. }
  569. }
  570. if (sigAttr != null) {
  571. myGen.removeAttribute(sigAttr);
  572. }
  573. myGen.addAttribute(createSignatureAttribute(signature.toString()));
  574. }
  575. }
  576. /**
  577. * Helper method to create a signature attribute based on a string signature: e.g. "Ljava/lang/Object;LI<Ljava/lang/Double;>;"
  578. */
  579. private Signature createSignatureAttribute(String signature) {
  580. int nameIndex = cp.addUtf8("Signature");
  581. int sigIndex = cp.addUtf8(signature);
  582. return new Signature(nameIndex, 2, sigIndex, cp);
  583. }
  584. /**
  585. *
  586. */
  587. private void reportClassTooBigProblem() {
  588. // PR 59208
  589. // we've generated a class that is just toooooooooo big (you've been
  590. // generating programs
  591. // again haven't you? come on, admit it, no-one writes classes this big
  592. // by hand).
  593. // create an empty myGen so that we can give back a return value that
  594. // doesn't upset the
  595. // rest of the process.
  596. myGen = new ClassGen(myGen.getClassName(), myGen.getSuperclassName(), myGen.getFileName(), myGen.getModifiers(),
  597. myGen.getInterfaceNames());
  598. // raise an error against this compilation unit.
  599. getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CLASS_TOO_BIG, this.getClassName()),
  600. new SourceLocation(new File(myGen.getFileName()), 0), null);
  601. }
  602. private static boolean hasSourceDebugExtensionAttribute(ClassGen gen) {
  603. return gen.hasAttribute("SourceDebugExtension");
  604. }
  605. public JavaClass getJavaClass(BcelWorld world) {
  606. writeBack(world);
  607. return myGen.getJavaClass();
  608. }
  609. public byte[] getJavaClassBytesIncludingReweavable(BcelWorld world) {
  610. writeBack(world);
  611. byte[] wovenClassFileData = myGen.getJavaClass().getBytes();
  612. // if is java 6 class file
  613. if (((myGen.getMajor() == Constants.MAJOR_1_6 && world.shouldGenerateStackMaps()) || myGen.getMajor() > Constants.MAJOR_1_6)
  614. && AsmDetector.isAsmAround) {
  615. wovenClassFileData = StackMapAdder.addStackMaps(world, wovenClassFileData);
  616. }
  617. WeaverStateInfo wsi = myType.getWeaverState();// getOrCreateWeaverStateInfo();
  618. if (wsi != null && wsi.isReweavable()) { // && !reweavableDataInserted
  619. // reweavableDataInserted = true;
  620. return wsi.replaceKeyWithDiff(wovenClassFileData);
  621. } else {
  622. return wovenClassFileData;
  623. }
  624. }
  625. public void addGeneratedInner(LazyClassGen newClass) {
  626. classGens.add(newClass);
  627. }
  628. public void addInterface(ResolvedType newInterface, ISourceLocation sourceLocation) {
  629. regenerateGenericSignatureAttribute = true;
  630. if (extraSuperInterfaces == null) {
  631. extraSuperInterfaces = new ResolvedType[1];
  632. extraSuperInterfaces[0] = newInterface;
  633. } else {
  634. ResolvedType[] x = new ResolvedType[extraSuperInterfaces.length + 1];
  635. System.arraycopy(extraSuperInterfaces, 0, x, 1, extraSuperInterfaces.length);
  636. x[0] = newInterface;
  637. extraSuperInterfaces = x;
  638. }
  639. myGen.addInterface(newInterface.getRawName());
  640. if (!newInterface.equals(UnresolvedType.SERIALIZABLE)) {
  641. warnOnAddedInterface(newInterface.getName(), sourceLocation);
  642. }
  643. }
  644. public void setSuperClass(ResolvedType newSuperclass) {
  645. regenerateGenericSignatureAttribute = true;
  646. superclass = newSuperclass;
  647. // myType.addParent(typeX); // used for the attribute
  648. if (newSuperclass.getGenericType() != null) {
  649. newSuperclass = newSuperclass.getGenericType();
  650. }
  651. myGen.setSuperclassName(newSuperclass.getName()); // used in the real
  652. // class data
  653. }
  654. // public String getSuperClassname() {
  655. // return myGen.getSuperclassName();
  656. // }
  657. public ResolvedType getSuperClass() {
  658. if (superclass != null) {
  659. return superclass;
  660. }
  661. return myType.getSuperclass();
  662. }
  663. public String[] getInterfaceNames() {
  664. return myGen.getInterfaceNames();
  665. }
  666. // non-recursive, may be a bug, ha ha.
  667. private List<LazyClassGen> getClassGens() {
  668. List<LazyClassGen> ret = new ArrayList<LazyClassGen>();
  669. ret.add(this);
  670. ret.addAll(classGens);
  671. return ret;
  672. }
  673. public List<UnwovenClassFile.ChildClass> getChildClasses(BcelWorld world) {
  674. if (classGens.isEmpty()) {
  675. return Collections.emptyList();
  676. }
  677. List<UnwovenClassFile.ChildClass> ret = new ArrayList<UnwovenClassFile.ChildClass>();
  678. for (LazyClassGen clazz : classGens) {
  679. byte[] bytes = clazz.getJavaClass(world).getBytes();
  680. String name = clazz.getName();
  681. int index = name.lastIndexOf('$');
  682. // XXX this could be bad, check use of dollar signs.
  683. name = name.substring(index + 1);
  684. ret.add(new UnwovenClassFile.ChildClass(name, bytes));
  685. }
  686. return ret;
  687. }
  688. @Override
  689. public String toString() {
  690. return toShortString();
  691. }
  692. public String toShortString() {
  693. String s = org.aspectj.apache.bcel.classfile.Utility.accessToString(myGen.getModifiers(), true);
  694. if (s != "") {
  695. s += " ";
  696. }
  697. s += org.aspectj.apache.bcel.classfile.Utility.classOrInterface(myGen.getModifiers());
  698. s += " ";
  699. s += myGen.getClassName();
  700. return s;
  701. }
  702. public String toLongString() {
  703. ByteArrayOutputStream s = new ByteArrayOutputStream();
  704. print(new PrintStream(s));
  705. return new String(s.toByteArray());
  706. }
  707. public void print() {
  708. print(System.out);
  709. }
  710. public void print(PrintStream out) {
  711. List<LazyClassGen> classGens = getClassGens();
  712. for (Iterator<LazyClassGen> iter = classGens.iterator(); iter.hasNext();) {
  713. LazyClassGen element = iter.next();
  714. element.printOne(out);
  715. if (iter.hasNext()) {
  716. out.println();
  717. }
  718. }
  719. }
  720. private void printOne(PrintStream out) {
  721. out.print(toShortString());
  722. out.print(" extends ");
  723. out.print(org.aspectj.apache.bcel.classfile.Utility.compactClassName(myGen.getSuperclassName(), false));
  724. int size = myGen.getInterfaces().length;
  725. if (size > 0) {
  726. out.print(" implements ");
  727. for (int i = 0; i < size; i++) {
  728. out.print(myGen.getInterfaceNames()[i]);
  729. if (i < size - 1) {
  730. out.print(", ");
  731. }
  732. }
  733. }
  734. out.print(":");
  735. out.println();
  736. // XXX make sure to pass types correctly around, so this doesn't happen.
  737. if (myType != null) {
  738. myType.printWackyStuff(out);
  739. }
  740. Field[] fields = myGen.getFields();
  741. for (int i = 0, len = fields.length; i < len; i++) {
  742. out.print(" ");
  743. out.println(fields[i]);
  744. }
  745. List<LazyMethodGen> methodGens = getMethodGens();
  746. for (Iterator<LazyMethodGen> iter = methodGens.iterator(); iter.hasNext();) {
  747. LazyMethodGen gen = iter.next();
  748. // we skip empty clinits
  749. if (isEmptyClinit(gen)) {
  750. continue;
  751. }
  752. gen.print(out, (myType != null ? myType.getWeaverVersionAttribute() : WeaverVersionInfo.UNKNOWN));
  753. if (iter.hasNext()) {
  754. out.println();
  755. }
  756. }
  757. // out.println(" ATTRIBS: " + Arrays.asList(myGen.getAttributes()));
  758. out.println("end " + toShortString());
  759. }
  760. private boolean isEmptyClinit(LazyMethodGen gen) {
  761. if (!gen.getName().equals("<clinit>")) {
  762. return false;
  763. }
  764. // System.err.println("checking clinig: " + gen);
  765. InstructionHandle start = gen.getBody().getStart();
  766. while (start != null) {
  767. if (Range.isRangeHandle(start) || (start.getInstruction().opcode == Constants.RETURN)) {
  768. start = start.getNext();
  769. } else {
  770. return false;
  771. }
  772. }
  773. return true;
  774. }
  775. public ConstantPool getConstantPool() {
  776. return cp;
  777. }
  778. public String getName() {
  779. return myGen.getClassName();
  780. }
  781. public boolean isWoven() {
  782. return myType.getWeaverState() != null;
  783. }
  784. public boolean isReweavable() {
  785. if (myType.getWeaverState() == null) {
  786. return true;
  787. }
  788. return myType.getWeaverState().isReweavable();
  789. }
  790. public Set<String> getAspectsAffectingType() {
  791. if (myType.getWeaverState() == null) {
  792. return null;
  793. }
  794. return myType.getWeaverState().getAspectsAffectingType();
  795. }
  796. public WeaverStateInfo getOrCreateWeaverStateInfo(boolean inReweavableMode) {
  797. WeaverStateInfo ret = myType.getWeaverState();
  798. if (ret != null) {
  799. return ret;
  800. }
  801. ret = new WeaverStateInfo(inReweavableMode);
  802. myType.setWeaverState(ret);
  803. return ret;
  804. }
  805. public InstructionFactory getFactory() {
  806. return fact;
  807. }
  808. public LazyMethodGen getStaticInitializer() {
  809. for (LazyMethodGen gen : methodGens) {
  810. // OPTIMIZE persist kind of member into the gen object? for clinit
  811. if (gen.getName().equals("<clinit>")) {
  812. return gen;
  813. }
  814. }
  815. LazyMethodGen clinit = new LazyMethodGen(Modifier.STATIC, Type.VOID, "<clinit>", new Type[0], NO_STRINGS, this);
  816. clinit.getBody().insert(InstructionConstants.RETURN);
  817. methodGens.add(clinit);
  818. return clinit;
  819. }
  820. /**
  821. * Retrieve the ajc$preClinit method - this method captures any initialization AspectJ wants to ensure happens in a class. It is
  822. * called from the static initializer. Maintaining this separation enables overweaving to ignore join points added due to
  823. * earlier weaves. If the ajc$preClinit method cannot be found, it is created and a call to it is placed in the real static
  824. * initializer (the call is placed at the start of the static initializer).
  825. *
  826. * @return the LazyMethodGen representing the ajc$ clinit
  827. */
  828. public LazyMethodGen getAjcPreClinit() {
  829. if (this.isInterface()) {
  830. throw new IllegalStateException();
  831. }
  832. for (LazyMethodGen methodGen : methodGens) {
  833. if (methodGen.getName().equals(NameMangler.AJC_PRE_CLINIT_NAME)) {
  834. return methodGen;
  835. }
  836. }
  837. LazyMethodGen ajcPreClinit = new LazyMethodGen(Modifier.PRIVATE | Modifier.STATIC, Type.VOID,
  838. NameMangler.AJC_PRE_CLINIT_NAME, Type.NO_ARGS, NO_STRINGS, this);
  839. ajcPreClinit.getBody().insert(InstructionConstants.RETURN);
  840. methodGens.add(ajcPreClinit);
  841. getStaticInitializer().getBody().insert(Utility.createInvoke(fact, ajcPreClinit));
  842. return ajcPreClinit;
  843. }
  844. /**
  845. * factory method for building multiple extended clinit methods. Constructs a new clinit method that invokes the previous one
  846. * and then returns it. The index is used as a name suffix.
  847. *
  848. * @param previousPreClinit
  849. * @param i
  850. */
  851. public LazyMethodGen createExtendedAjcPreClinit(LazyMethodGen previousPreClinit, int i) {
  852. LazyMethodGen ajcPreClinit = new LazyMethodGen(Modifier.PRIVATE | Modifier.STATIC, Type.VOID,
  853. NameMangler.AJC_PRE_CLINIT_NAME + i, Type.NO_ARGS, NO_STRINGS, this);
  854. ajcPreClinit.getBody().insert(InstructionConstants.RETURN);
  855. methodGens.add(ajcPreClinit);
  856. previousPreClinit.getBody().insert(Utility.createInvoke(fact, ajcPreClinit));
  857. return ajcPreClinit;
  858. }
  859. //
  860. // reflective thisJoinPoint support
  861. private Map<BcelShadow, Field> tjpFields = new HashMap<BcelShadow, Field>();
  862. Map<CacheKey, Field> annotationCachingFieldCache = new HashMap<CacheKey, Field>();
  863. private int tjpFieldsCounter = -1; // -1 means not yet initialized
  864. private int annoFieldsCounter = 0;
  865. public static final ObjectType proceedingTjpType = new ObjectType("org.aspectj.lang.ProceedingJoinPoint");
  866. public static final ObjectType tjpType = new ObjectType("org.aspectj.lang.JoinPoint");
  867. public static final ObjectType staticTjpType = new ObjectType("org.aspectj.lang.JoinPoint$StaticPart");
  868. public static final ObjectType typeForAnnotation = new ObjectType("java.lang.annotation.Annotation");
  869. public static final ObjectType enclosingStaticTjpType = new ObjectType("org.aspectj.lang.JoinPoint$EnclosingStaticPart");
  870. private static final ObjectType sigType = new ObjectType("org.aspectj.lang.Signature");
  871. // private static final ObjectType slType =
  872. // new ObjectType("org.aspectj.lang.reflect.SourceLocation");
  873. private static final ObjectType factoryType = new ObjectType("org.aspectj.runtime.reflect.Factory");
  874. private static final ObjectType classType = new ObjectType("java.lang.Class");
  875. public Field getTjpField(BcelShadow shadow, final boolean isEnclosingJp) {
  876. Field tjpField = tjpFields.get(shadow);
  877. if (tjpField != null) {
  878. return tjpField;
  879. }
  880. int modifiers = Modifier.STATIC | Modifier.FINAL ;
  881. // XXX - Do we ever inline before or after advice? If we do, then we
  882. // better include them in the check below. (or just change it to
  883. // shadow.getEnclosingMethod().getCanInline())
  884. // If the enclosing method is around advice, we could inline the join
  885. // point that has led to this shadow. If we do that then the TJP we are
  886. // creating here must be PUBLIC so it is visible to the type in which the
  887. // advice is inlined. (PR71377)
  888. LazyMethodGen encMethod = shadow.getEnclosingMethod();
  889. boolean shadowIsInAroundAdvice = false;
  890. if (encMethod != null && encMethod.getName().startsWith(NameMangler.PREFIX + "around")) {
  891. shadowIsInAroundAdvice = true;
  892. }
  893. if (getType().isInterface() || shadowIsInAroundAdvice) {
  894. modifiers |= Modifier.PUBLIC;
  895. } else {
  896. modifiers |= Modifier.PRIVATE;
  897. }
  898. ObjectType jpType = null;
  899. // Did not have different static joinpoint types in 1.2
  900. if (world.isTargettingAspectJRuntime12()) {
  901. jpType = staticTjpType;
  902. } else {
  903. jpType = isEnclosingJp ? enclosingStaticTjpType : staticTjpType;
  904. }
  905. if (tjpFieldsCounter == -1) {
  906. // not yet initialized, do it now
  907. if (!world.isOverWeaving()) {
  908. tjpFieldsCounter = 0;
  909. } else {
  910. List<BcelField> existingFields = getFieldGens();
  911. if (existingFields == null) {
  912. tjpFieldsCounter = 0;
  913. } else {
  914. BcelField lastField = null;
  915. // OPTIMIZE: go from last to first?
  916. for (BcelField field : existingFields) {
  917. if (field.getName().startsWith("ajc$tjp_")) {
  918. lastField = field;
  919. }
  920. }
  921. if (lastField == null) {
  922. tjpFieldsCounter = 0;
  923. } else {
  924. tjpFieldsCounter = Integer.parseInt(lastField.getName().substring(8)) + 1;
  925. // System.out.println("tjp counter starting at " + tjpFieldsCounter);
  926. }
  927. }
  928. }
  929. }
  930. if (!isInterface() && world.isTransientTjpFields()) {
  931. modifiers|=Modifier.TRANSIENT;
  932. }
  933. FieldGen fGen = new FieldGen(modifiers, jpType, "ajc$tjp_" + tjpFieldsCounter++, getConstantPool());
  934. addField(fGen);
  935. tjpField = fGen.getField();
  936. tjpFields.put(shadow, tjpField);
  937. return tjpField;
  938. }
  939. /**
  940. * Create a field in the type containing the shadow where the annotation retrieved during binding can be stored - for later fast
  941. * access.
  942. *
  943. * @param shadow the shadow at which the @annotation result is being cached
  944. * @return a field
  945. */
  946. public Field getAnnotationCachingField(BcelShadow shadow, ResolvedType toType, boolean isWithin) {
  947. // Multiple annotation types at a shadow. A different field would be required for each
  948. CacheKey cacheKey = new CacheKey(shadow, toType, isWithin);
  949. Field field = annotationCachingFieldCache.get(cacheKey);
  950. // System.out.println(field + " for shadow " + shadow);
  951. if (field == null) {
  952. // private static Annotation ajc$anno$<nnn>
  953. StringBuilder sb = new StringBuilder();
  954. sb.append(NameMangler.ANNOTATION_CACHE_FIELD_NAME);
  955. sb.append(annoFieldsCounter++);
  956. FieldGen annotationCacheField = new FieldGen(Modifier.PRIVATE | Modifier.STATIC, typeForAnnotation, sb.toString(), cp);
  957. addField(annotationCacheField);
  958. field = annotationCacheField.getField();
  959. annotationCachingFieldCache.put(cacheKey, field);
  960. }
  961. return field;
  962. }
  963. static class CacheKey {
  964. private Object key;
  965. private ResolvedType annotationType;
  966. // If the annotation is being accessed via @annotation on a shadow then we can use the shadows toString() (so two shadows
  967. // the same share a variable), but if it is @withincode() or @within() we can't share them (as the shadows may look the same
  968. // but be occurring 'within' different things). In the within cases we continue to use the shadow itself as the key.
  969. CacheKey(BcelShadow shadow, ResolvedType annotationType, boolean isWithin) {
  970. this.key = isWithin ? shadow : shadow.toString();
  971. this.annotationType = annotationType;
  972. }
  973. @Override
  974. public int hashCode() {
  975. return key.hashCode() * 37 + annotationType.hashCode();
  976. }
  977. @Override
  978. public boolean equals(Object other) {
  979. if (!(other instanceof CacheKey)) {
  980. return false;
  981. }
  982. CacheKey oCacheKey = (CacheKey) other;
  983. return key.equals(oCacheKey.key) && annotationType.equals(oCacheKey.annotationType);
  984. }
  985. }
  986. // FIXME ATAJ needed only for slow Aspects.aspectOf - keep or remove
  987. // private void addAjClassField() {
  988. // // Andy: Why build it again??
  989. // Field ajClassField = new FieldGen(
  990. // Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC,
  991. // classType,
  992. // "aj$class",
  993. // getConstantPool()).getField();
  994. // addField(ajClassField);
  995. //
  996. // InstructionList il = new InstructionList();
  997. // il.append(new PUSH(getConstantPool(), getClassName()));
  998. // il.append(fact.createInvoke("java.lang.Class", "forName", classType,
  999. // new Type[] {Type.STRING}, Constants.INVOKESTATIC));
  1000. // il.append(fact.createFieldAccess(getClassName(), ajClassField.getName(),
  1001. // classType, Constants.PUTSTATIC));
  1002. //
  1003. // getStaticInitializer().getBody().insert(il);
  1004. // }
  1005. private void addAjcInitializers() {
  1006. if (tjpFields.size() == 0 && !serialVersionUIDRequiresInitialization) {
  1007. return;
  1008. }
  1009. InstructionList[] il = null;
  1010. if (tjpFields.size() > 0) {
  1011. il = initializeAllTjps();
  1012. }
  1013. if (serialVersionUIDRequiresInitialization) {
  1014. InstructionList[] ilSVUID = new InstructionList[1];
  1015. ilSVUID[0] = new InstructionList();
  1016. ilSVUID[0].append(InstructionFactory.PUSH(getConstantPool(), calculatedSerialVersionUID));
  1017. ilSVUID[0].append(getFactory().createFieldAccess(getClassName(), "serialVersionUID", BasicType.LONG,
  1018. Constants.PUTSTATIC));
  1019. if (il == null) {
  1020. il = ilSVUID;
  1021. } else {
  1022. InstructionList[] newIl = new InstructionList[il.length + ilSVUID.length];
  1023. System.arraycopy(il, 0, newIl, 0, il.length);
  1024. System.arraycopy(ilSVUID, 0, newIl, il.length, ilSVUID.length);
  1025. il = newIl;
  1026. }
  1027. }
  1028. LazyMethodGen prevMethod;
  1029. LazyMethodGen nextMethod = null;
  1030. if (this.isInterface()) { // Cannot sneak stuff into another static method in an interface
  1031. prevMethod = getStaticInitializer();
  1032. } else {
  1033. prevMethod = getAjcPreClinit();
  1034. }
  1035. for (int counter = 1; counter <= il.length; counter++) {
  1036. if (il.length > counter) {
  1037. nextMethod = createExtendedAjcPreClinit(prevMethod, counter);
  1038. }
  1039. prevMethod.getBody().insert(il[counter - 1]);
  1040. prevMethod = nextMethod;
  1041. }
  1042. }
  1043. private InstructionList initInstructionList() {
  1044. InstructionList list = new InstructionList();
  1045. InstructionFactory fact = getFactory();
  1046. // make a new factory
  1047. list.append(fact.createNew(factoryType));
  1048. list.append(InstructionFactory.createDup(1));
  1049. list.append(InstructionFactory.PUSH(getConstantPool(), getFileName()));
  1050. // load the current Class object
  1051. // XXX check that this works correctly for inners/anonymous
  1052. list.append(fact.PUSHCLASS(cp, myGen.getClassName()));
  1053. // XXX do we need to worry about the fact the theorectically this could
  1054. // throw
  1055. // a ClassNotFoundException
  1056. list.append(fact.createInvoke(factoryType.getClassName(), "<init>", Type.VOID, new Type[] { Type.STRING, classType },
  1057. Constants.INVOKESPECIAL));
  1058. list.append(InstructionFactory.createStore(factoryType, 0));
  1059. return list;
  1060. }
  1061. private InstructionList[] initializeAllTjps() {
  1062. Vector<InstructionList> lists = new Vector<InstructionList>();
  1063. InstructionList list = initInstructionList();
  1064. lists.add(list);
  1065. List<Map.Entry<BcelShadow, Field>> entries = new ArrayList<Map.Entry<BcelShadow, Field>>(tjpFields.entrySet());
  1066. Collections.sort(entries, new Comparator<Map.Entry<BcelShadow, Field>>() {
  1067. public int compare(Map.Entry<BcelShadow, Field> a, Map.Entry<BcelShadow, Field> b) {
  1068. return (a.getValue()).getName().compareTo((b.getValue()).getName());
  1069. }
  1070. });
  1071. long estimatedSize = 0;
  1072. for (Iterator<Map.Entry<BcelShadow, Field>> i = entries.iterator(); i.hasNext();) {
  1073. Map.Entry<BcelShadow, Field> entry = i.next();
  1074. if (estimatedSize > Constants.MAX_CODE_SIZE) {
  1075. estimatedSize = 0;
  1076. list = initInstructionList();
  1077. lists.add(list);
  1078. }
  1079. estimatedSize += entry.getValue().getSignature().getBytes().length;
  1080. initializeTjp(fact, list, entry.getValue(), entry.getKey());
  1081. }
  1082. InstructionList listArrayModel[] = new InstructionList[1];
  1083. return lists.toArray(listArrayModel);
  1084. }
  1085. private void initializeTjp(InstructionFactory fact, InstructionList list, Field field, BcelShadow shadow) {
  1086. boolean fastSJP = false;
  1087. // avoid fast SJP if it is for an enclosing joinpoint
  1088. boolean isFastSJPAvailable = shadow.getWorld().isTargettingRuntime1_6_10()
  1089. && !enclosingStaticTjpType.equals(field.getType());
  1090. Member sig = shadow.getSignature();
  1091. // load the factory
  1092. list.append(InstructionFactory.createLoad(factoryType, 0));
  1093. // load the kind
  1094. list.append(InstructionFactory.PUSH(getConstantPool(), shadow.getKind().getName()));
  1095. // create the signature
  1096. if (world.isTargettingAspectJRuntime12() || !isFastSJPAvailable || !sig.getKind().equals(Member.METHOD)) {
  1097. list.append(InstructionFactory.createLoad(factoryType, 0));
  1098. }
  1099. String signatureMakerName = SignatureUtils.getSignatureMakerName(sig);
  1100. ObjectType signatureType = new ObjectType(SignatureUtils.getSignatureType(sig));
  1101. UnresolvedType[] exceptionTypes = null;
  1102. if (world.isTargettingAspectJRuntime12()) { // TAG:SUPPORTING12: We
  1103. // didn't have optimized
  1104. // factory methods in 1.2
  1105. list.append(InstructionFactory.PUSH(cp, SignatureUtils.getSignatureString(sig, shadow.getWorld())));
  1106. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY1,
  1107. Constants.INVOKEVIRTUAL));
  1108. } else if (sig.getKind().equals(Member.METHOD)) {
  1109. BcelWorld w = shadow.getWorld();
  1110. // For methods, push the parts of the signature on.
  1111. list.append(InstructionFactory.PUSH(cp, makeString(sig.getModifiers(w))));
  1112. list.append(InstructionFactory.PUSH(cp, sig.getName()));
  1113. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1114. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterTypes())));
  1115. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterNames(w))));
  1116. exceptionTypes = sig.getExceptions(w);
  1117. if (isFastSJPAvailable && exceptionTypes.length == 0) {
  1118. fastSJP = true;
  1119. } else {
  1120. list.append(InstructionFactory.PUSH(cp, makeString(exceptionTypes)));
  1121. }
  1122. list.append(InstructionFactory.PUSH(cp, makeString(sig.getReturnType())));
  1123. // And generate a call to the variant of makeMethodSig() that takes the strings
  1124. if (isFastSJPAvailable) {
  1125. fastSJP = true;
  1126. } else {
  1127. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY7,
  1128. Constants.INVOKEVIRTUAL));
  1129. }
  1130. } else if (sig.getKind().equals(Member.MONITORENTER)) {
  1131. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1132. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY1,
  1133. Constants.INVOKEVIRTUAL));
  1134. } else if (sig.getKind().equals(Member.MONITOREXIT)) {
  1135. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1136. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY1,
  1137. Constants.INVOKEVIRTUAL));
  1138. } else if (sig.getKind().equals(Member.HANDLER)) {
  1139. BcelWorld w = shadow.getWorld();
  1140. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1141. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterTypes())));
  1142. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterNames(w))));
  1143. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY3,
  1144. Constants.INVOKEVIRTUAL));
  1145. } else if (sig.getKind().equals(Member.CONSTRUCTOR)) {
  1146. BcelWorld w = shadow.getWorld();
  1147. if (w.isJoinpointArrayConstructionEnabled() && sig.getDeclaringType().isArray()) {
  1148. // its the magical new jp
  1149. list.append(InstructionFactory.PUSH(cp, makeString(Modifier.PUBLIC)));
  1150. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1151. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterTypes())));
  1152. list.append(InstructionFactory.PUSH(cp, "")); // sig.getParameterNames?
  1153. list.append(InstructionFactory.PUSH(cp, ""));// sig.getExceptions?
  1154. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY5,
  1155. Constants.INVOKEVIRTUAL));
  1156. } else {
  1157. list.append(InstructionFactory.PUSH(cp, makeString(sig.getModifiers(w))));
  1158. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1159. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterTypes())));
  1160. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterNames(w))));
  1161. list.append(InstructionFactory.PUSH(cp, makeString(sig.getExceptions(w))));
  1162. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY5,
  1163. Constants.INVOKEVIRTUAL));
  1164. }
  1165. } else if (sig.getKind().equals(Member.FIELD)) {
  1166. BcelWorld w = shadow.getWorld();
  1167. list.append(InstructionFactory.PUSH(cp, makeString(sig.getModifiers(w))));
  1168. list.append(InstructionFactory.PUSH(cp, sig.getName()));
  1169. // see pr227401
  1170. UnresolvedType dType = sig.getDeclaringType();
  1171. if (dType.getTypekind() == TypeKind.PARAMETERIZED || dType.getTypekind() == TypeKind.GENERIC) {
  1172. dType = sig.getDeclaringType().resolve(world).getGenericType();
  1173. }
  1174. list.append(InstructionFactory.PUSH(cp, makeString(dType)));
  1175. list.append(InstructionFactory.PUSH(cp, makeString(sig.getReturnType())));
  1176. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY4,
  1177. Constants.INVOKEVIRTUAL));
  1178. } else if (sig.getKind().equals(Member.ADVICE)) {
  1179. BcelWorld w = shadow.getWorld();
  1180. list.append(InstructionFactory.PUSH(cp, makeString(sig.getModifiers(w))));
  1181. list.append(InstructionFactory.PUSH(cp, sig.getName()));
  1182. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1183. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterTypes())));
  1184. list.append(InstructionFactory.PUSH(cp, makeString(sig.getParameterNames(w))));
  1185. list.append(InstructionFactory.PUSH(cp, makeString(sig.getExceptions(w))));
  1186. list.append(InstructionFactory.PUSH(cp, makeString((sig.getReturnType()))));
  1187. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, new Type[] { Type.STRING,
  1188. Type.STRING, Type.STRING, Type.STRING, Type.STRING, Type.STRING, Type.STRING }, Constants.INVOKEVIRTUAL));
  1189. } else if (sig.getKind().equals(Member.STATIC_INITIALIZATION)) {
  1190. BcelWorld w = shadow.getWorld();
  1191. list.append(InstructionFactory.PUSH(cp, makeString(sig.getModifiers(w))));
  1192. list.append(InstructionFactory.PUSH(cp, makeString(sig.getDeclaringType())));
  1193. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY2,
  1194. Constants.INVOKEVIRTUAL));
  1195. } else {
  1196. list.append(InstructionFactory.PUSH(cp, SignatureUtils.getSignatureString(sig, shadow.getWorld())));
  1197. list.append(fact.createInvoke(factoryType.getClassName(), signatureMakerName, signatureType, Type.STRINGARRAY1,
  1198. Constants.INVOKEVIRTUAL));
  1199. }
  1200. // XXX should load source location from shadow
  1201. list.append(Utility.createConstant(fact, shadow.getSourceLine()));
  1202. final String factoryMethod;
  1203. // TAG:SUPPORTING12: We didn't have makeESJP() in 1.2
  1204. if (world.isTargettingAspectJRuntime12()) {
  1205. list.append(fact.createInvoke(factoryType.getClassName(), "makeSJP", staticTjpType, new Type[] { Type.STRING, sigType,
  1206. Type.INT }, Constants.INVOKEVIRTUAL));
  1207. // put it in the field
  1208. list.append(fact.createFieldAccess(getClassName(), field.getName(), staticTjpType, Constants.PUTSTATIC));
  1209. } else {
  1210. if (staticTjpType.equals(field.getType())) {
  1211. factoryMethod = "makeSJP";
  1212. } else if (enclosingStaticTjpType.equals(field.getType())) {
  1213. factoryMethod = "makeESJP";
  1214. } else {
  1215. throw new Error("should not happen");
  1216. }
  1217. if (fastSJP) {
  1218. if (exceptionTypes != null && exceptionTypes.length != 0) {
  1219. list.append(fact.createInvoke(factoryType.getClassName(), factoryMethod, field.getType(), ARRAY_8STRING_INT,
  1220. Constants.INVOKEVIRTUAL));
  1221. } else {
  1222. list.append(fact.createInvoke(factoryType.getClassName(), factoryMethod, field.getType(), ARRAY_7STRING_INT,
  1223. Constants.INVOKEVIRTUAL));
  1224. }
  1225. } else {
  1226. list.append(fact.createInvoke(factoryType.getClassName(), factoryMethod, field.getType(), new Type[] { Type.STRING,
  1227. sigType, Type.INT }, Constants.INVOKEVIRTUAL));
  1228. }
  1229. // put it in the field
  1230. list.append(fact.createFieldAccess(getClassName(), field.getName(), field.getType(), Constants.PUTSTATIC));
  1231. }
  1232. }
  1233. private static final Type[] ARRAY_7STRING_INT = new Type[] { Type.STRING, Type.STRING, Type.STRING, Type.STRING, Type.STRING,
  1234. Type.STRING, Type.STRING, Type.INT };
  1235. private static final Type[] ARRAY_8STRING_INT = new Type[] { Type.STRING, Type.STRING, Type.STRING, Type.STRING, Type.STRING,
  1236. Type.STRING, Type.STRING, Type.STRING, Type.INT };
  1237. protected String makeString(int i) {
  1238. return Integer.toString(i, 16); // ??? expensive
  1239. }
  1240. protected String makeString(UnresolvedType t) {
  1241. // this is the inverse of the odd behavior for Class.forName w/ arrays
  1242. if (t.isArray()) {
  1243. // this behavior matches the string used by the eclipse compiler for
  1244. // Foo.class literals
  1245. return t.getSignature().replace('/', '.');
  1246. } else {
  1247. if (t.isParameterizedType()) {
  1248. return t.getRawType().getName();
  1249. } else {
  1250. return t.getName();
  1251. }
  1252. }
  1253. }
  1254. protected String makeString(UnresolvedType[] types) {
  1255. if (types == null) {
  1256. return "";
  1257. }
  1258. StringBuilder buf = new StringBuilder();
  1259. for (int i = 0, len = types.length; i < len; i++) {
  1260. if (i > 0) {
  1261. buf.append(':');
  1262. }
  1263. buf.append(makeString(types[i]));
  1264. }
  1265. return buf.toString();
  1266. }
  1267. protected String makeString(String[] names) {
  1268. if (names == null) {
  1269. return "";
  1270. }
  1271. StringBuilder buf = new StringBuilder();
  1272. for (int i = 0, len = names.length; i < len; i++) {
  1273. if (i > 0) {
  1274. buf.append(':');
  1275. }
  1276. buf.append(names[i]);
  1277. }
  1278. return buf.toString();
  1279. }
  1280. public ResolvedType getType() {
  1281. if (myType == null) {
  1282. return null;
  1283. }
  1284. return myType.getResolvedTypeX();
  1285. }
  1286. public BcelObjectType getBcelObjectType() {
  1287. return myType;
  1288. }
  1289. public String getFileName() {
  1290. return myGen.getFileName();
  1291. }
  1292. // for *new* fields
  1293. private void addField(FieldGen field) {
  1294. makeSyntheticAndTransientIfNeeded(field);
  1295. BcelField bcelField = null;
  1296. if (getBcelObjectType() != null) {
  1297. bcelField = new BcelField(getBcelObjectType(), field.getField());
  1298. } else {
  1299. bcelField = new BcelField(getName(), field.getField(), world);
  1300. }
  1301. fields.add(bcelField);
  1302. // myGen.addField(field.getField());
  1303. }
  1304. private void makeSyntheticAndTransientIfNeeded(FieldGen field) {
  1305. if (field.getName().startsWith(NameMangler.PREFIX) && !field.getName().startsWith("ajc$interField$")
  1306. && !field.getName().startsWith("ajc$instance$")) {
  1307. // it's an aj added field
  1308. // first do transient
  1309. if (!field.isStatic()) {
  1310. field.setModifiers(field.getModifiers() | Constants.ACC_TRANSIENT);
  1311. }
  1312. // then do synthetic
  1313. if (getWorld().isInJava5Mode()) {
  1314. // add the synthetic modifier flag
  1315. field.setModifiers(field.getModifiers() | ACC_SYNTHETIC);
  1316. }
  1317. if (!hasSyntheticAttribute(field.getAttributes())) {
  1318. // belt and braces, do the attribute even on Java 5 in addition
  1319. // to the modifier flag
  1320. // Attribute[] oldAttrs = field.getAttributes();
  1321. // Attribute[] newAttrs = new Attribute[oldAttrs.length + 1];
  1322. // System.arraycopy(oldAttrs, 0, newAttrs, 0, oldAttrs.length);
  1323. ConstantPool cpg = myGen.getConstantPool();
  1324. int index = cpg.addUtf8("Synthetic");
  1325. Attribute synthetic = new Synthetic(index, 0, new byte[0], cpg);
  1326. field.addAttribute(synthetic);
  1327. // newAttrs[newAttrs.length - 1] = synthetic;
  1328. // field.setAttributes(newAttrs);
  1329. }
  1330. }
  1331. }
  1332. private boolean hasSyntheticAttribute(List<Attribute> attributes) {
  1333. for (int i = 0; i < attributes.size(); i++) {
  1334. if ((attributes.get(i)).getName().equals("Synthetic")) {
  1335. return true;
  1336. }
  1337. }
  1338. return false;
  1339. }
  1340. public void addField(FieldGen field, ISourceLocation sourceLocation) {
  1341. addField(field);
  1342. if (!(field.isPrivate() && (field.isStatic() || field.isTransient()))) {
  1343. errorOnAddedField(field, sourceLocation);
  1344. }
  1345. }
  1346. public String getClassName() {
  1347. return myGen.getClassName();
  1348. }
  1349. public boolean isInterface() {
  1350. return myGen.isInterface();
  1351. }
  1352. public boolean isAbstract() {
  1353. return myGen.isAbstract();
  1354. }
  1355. public LazyMethodGen getLazyMethodGen(Member m) {
  1356. return getLazyMethodGen(m.getName(), m.getSignature(), false);
  1357. }
  1358. public LazyMethodGen getLazyMethodGen(String name, String signature) {
  1359. return getLazyMethodGen(name, signature, false);
  1360. }
  1361. public LazyMethodGen getLazyMethodGen(String name, String signature, boolean allowMissing) {
  1362. for (LazyMethodGen gen : methodGens) {
  1363. if (gen.getName().equals(name) && gen.getSignature().equals(signature)) {
  1364. return gen;
  1365. }
  1366. }
  1367. if (!allowMissing) {
  1368. throw new BCException("Class " + this.getName() + " does not have a method " + name + " with signature " + signature);
  1369. }
  1370. return null;
  1371. }
  1372. public void forcePublic() {
  1373. myGen.setModifiers(Utility.makePublic(myGen.getModifiers()));
  1374. }
  1375. public boolean hasAnnotation(UnresolvedType t) {
  1376. // annotations on the real thing
  1377. AnnotationGen agens[] = myGen.getAnnotations();
  1378. if (agens == null) {
  1379. return false;
  1380. }
  1381. for (int i = 0; i < agens.length; i++) {
  1382. AnnotationGen gen = agens[i];
  1383. if (t.equals(UnresolvedType.forSignature(gen.getTypeSignature()))) {
  1384. return true;
  1385. }
  1386. }
  1387. // annotations added during this weave
  1388. return false;
  1389. }
  1390. public void addAnnotation(AnnotationGen a) {
  1391. if (!hasAnnotation(UnresolvedType.forSignature(a.getTypeSignature()))) {
  1392. annotations.add(new AnnotationGen(a, getConstantPool(), true));
  1393. }
  1394. }
  1395. public void addAttribute(AjAttribute attribute) {
  1396. myGen.addAttribute(Utility.bcelAttribute(attribute, getConstantPool()));
  1397. }
  1398. public void addAttribute(Attribute attribute) {
  1399. myGen.addAttribute(attribute);
  1400. }
  1401. public Collection<Attribute> getAttributes() {
  1402. return myGen.getAttributes();
  1403. }
  1404. // this test is like asking:
  1405. // if
  1406. // (UnresolvedType.SERIALIZABLE.resolve(getType().getWorld()).isAssignableFrom
  1407. // (getType())) {
  1408. // only we don't do that because this forces us to find all the supertypes
  1409. // of the type,
  1410. // and if one of them is missing we fail, and it's not worth failing just to
  1411. // put out
  1412. // a warning message!
  1413. private boolean implementsSerializable(ResolvedType aType) {
  1414. if (aType.getSignature().equals(UnresolvedType.SERIALIZABLE.getSignature())) {
  1415. return true;
  1416. }
  1417. ResolvedType[] interfaces = aType.getDeclaredInterfaces();
  1418. for (int i = 0; i < interfaces.length; i++) {
  1419. if (interfaces[i].isMissing()) {
  1420. continue;
  1421. }
  1422. if (implementsSerializable(interfaces[i])) {
  1423. return true;
  1424. }
  1425. }
  1426. ResolvedType superType = aType.getSuperclass();
  1427. if (superType != null && !superType.isMissing()) {
  1428. return implementsSerializable(superType);
  1429. }
  1430. return false;
  1431. }
  1432. public boolean isAtLeastJava5() {
  1433. return (myGen.getMajor() >= Constants.MAJOR_1_5);
  1434. }
  1435. /**
  1436. * Return the next available field name with the specified 'prefix', e.g. for prefix 'class$' where class$0, class$1 exist then
  1437. * return class$2
  1438. */
  1439. public String allocateField(String prefix) {
  1440. int highestAllocated = -1;
  1441. List<BcelField> fs = getFieldGens();
  1442. for (BcelField field : fs) {
  1443. if (field.getName().startsWith(prefix)) {
  1444. try {
  1445. int num = Integer.parseInt(field.getName().substring(prefix.length()));
  1446. if (num > highestAllocated) {
  1447. highestAllocated = num;
  1448. }
  1449. } catch (NumberFormatException nfe) {
  1450. // something wrong with the number on the end of that
  1451. // field...
  1452. }
  1453. }
  1454. }
  1455. return prefix + Integer.toString(highestAllocated + 1);
  1456. }
  1457. }