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.

BcelWorld.java 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 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. * Alexandre Vasseur perClause support for @AJ aspects
  12. * ******************************************************************/
  13. package org.aspectj.weaver.bcel;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.lang.reflect.Modifier;
  17. import java.net.MalformedURLException;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.StringTokenizer;
  25. import org.aspectj.apache.bcel.Constants;
  26. import org.aspectj.apache.bcel.classfile.ClassParser;
  27. import org.aspectj.apache.bcel.classfile.ConstantPool;
  28. import org.aspectj.apache.bcel.classfile.JavaClass;
  29. import org.aspectj.apache.bcel.generic.FieldInstruction;
  30. import org.aspectj.apache.bcel.generic.INVOKEINTERFACE;
  31. import org.aspectj.apache.bcel.generic.Instruction;
  32. import org.aspectj.apache.bcel.generic.InstructionHandle;
  33. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  34. import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
  35. import org.aspectj.apache.bcel.generic.ObjectType;
  36. import org.aspectj.apache.bcel.generic.Type;
  37. import org.aspectj.apache.bcel.util.ClassLoaderReference;
  38. import org.aspectj.apache.bcel.util.ClassLoaderRepository;
  39. import org.aspectj.apache.bcel.util.ClassPath;
  40. import org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository;
  41. import org.aspectj.apache.bcel.util.Repository;
  42. import org.aspectj.asm.AsmManager;
  43. import org.aspectj.asm.IRelationship;
  44. import org.aspectj.asm.internal.CharOperation;
  45. import org.aspectj.bridge.IMessage;
  46. import org.aspectj.bridge.IMessageHandler;
  47. import org.aspectj.bridge.ISourceLocation;
  48. import org.aspectj.bridge.Message;
  49. import org.aspectj.bridge.MessageUtil;
  50. import org.aspectj.bridge.WeaveMessage;
  51. import org.aspectj.weaver.Advice;
  52. import org.aspectj.weaver.AdviceKind;
  53. import org.aspectj.weaver.AnnotationAJ;
  54. import org.aspectj.weaver.AnnotationOnTypeMunger;
  55. import org.aspectj.weaver.BCException;
  56. import org.aspectj.weaver.Checker;
  57. import org.aspectj.weaver.ICrossReferenceHandler;
  58. import org.aspectj.weaver.IWeavingSupport;
  59. import org.aspectj.weaver.Member;
  60. import org.aspectj.weaver.MemberImpl;
  61. import org.aspectj.weaver.MemberKind;
  62. import org.aspectj.weaver.NewParentTypeMunger;
  63. import org.aspectj.weaver.ReferenceType;
  64. import org.aspectj.weaver.ReferenceTypeDelegate;
  65. import org.aspectj.weaver.ResolvedMember;
  66. import org.aspectj.weaver.ResolvedMemberImpl;
  67. import org.aspectj.weaver.ResolvedType;
  68. import org.aspectj.weaver.ResolvedTypeMunger;
  69. import org.aspectj.weaver.Shadow;
  70. import org.aspectj.weaver.ShadowMunger;
  71. import org.aspectj.weaver.UnresolvedType;
  72. import org.aspectj.weaver.World;
  73. import org.aspectj.weaver.loadtime.definition.Definition;
  74. import org.aspectj.weaver.loadtime.definition.DocumentParser;
  75. import org.aspectj.weaver.model.AsmRelationshipProvider;
  76. import org.aspectj.weaver.patterns.DeclareAnnotation;
  77. import org.aspectj.weaver.patterns.DeclareParents;
  78. import org.aspectj.weaver.patterns.ParserException;
  79. import org.aspectj.weaver.patterns.PatternParser;
  80. import org.aspectj.weaver.patterns.TypePattern;
  81. import org.aspectj.weaver.tools.Trace;
  82. import org.aspectj.weaver.tools.TraceFactory;
  83. public class BcelWorld extends World implements Repository {
  84. private final ClassPathManager classPath;
  85. protected Repository delegate;
  86. private BcelWeakClassLoaderReference loaderRef;
  87. private final BcelWeavingSupport bcelWeavingSupport = new BcelWeavingSupport();
  88. private boolean isXmlConfiguredWorld = false;
  89. private WeavingXmlConfig xmlConfiguration;
  90. private List<TypeDelegateResolver> typeDelegateResolvers;
  91. private static Trace trace = TraceFactory.getTraceFactory().getTrace(BcelWorld.class);
  92. public BcelWorld() {
  93. this("");
  94. }
  95. public BcelWorld(String cp) {
  96. this(makeDefaultClasspath(cp), IMessageHandler.THROW, null);
  97. }
  98. public IRelationship.Kind determineRelKind(ShadowMunger munger) {
  99. AdviceKind ak = ((Advice) munger).getKind();
  100. if (ak.getKey() == AdviceKind.Before.getKey()) {
  101. return IRelationship.Kind.ADVICE_BEFORE;
  102. } else if (ak.getKey() == AdviceKind.After.getKey()) {
  103. return IRelationship.Kind.ADVICE_AFTER;
  104. } else if (ak.getKey() == AdviceKind.AfterThrowing.getKey()) {
  105. return IRelationship.Kind.ADVICE_AFTERTHROWING;
  106. } else if (ak.getKey() == AdviceKind.AfterReturning.getKey()) {
  107. return IRelationship.Kind.ADVICE_AFTERRETURNING;
  108. } else if (ak.getKey() == AdviceKind.Around.getKey()) {
  109. return IRelationship.Kind.ADVICE_AROUND;
  110. } else if (ak.getKey() == AdviceKind.CflowEntry.getKey() || ak.getKey() == AdviceKind.CflowBelowEntry.getKey()
  111. || ak.getKey() == AdviceKind.InterInitializer.getKey() || ak.getKey() == AdviceKind.PerCflowEntry.getKey()
  112. || ak.getKey() == AdviceKind.PerCflowBelowEntry.getKey() || ak.getKey() == AdviceKind.PerThisEntry.getKey()
  113. || ak.getKey() == AdviceKind.PerTargetEntry.getKey() || ak.getKey() == AdviceKind.Softener.getKey()
  114. || ak.getKey() == AdviceKind.PerTypeWithinEntry.getKey()) {
  115. // System.err.println("Dont want a message about this: "+ak);
  116. return null;
  117. }
  118. throw new RuntimeException("Shadow.determineRelKind: What the hell is it? " + ak);
  119. }
  120. @Override
  121. public void reportMatch(ShadowMunger munger, Shadow shadow) {
  122. if (getCrossReferenceHandler() != null) {
  123. getCrossReferenceHandler().addCrossReference(munger.getSourceLocation(), // What is being applied
  124. shadow.getSourceLocation(), // Where is it being applied
  125. determineRelKind(munger).getName(), // What kind of advice?
  126. ((Advice) munger).hasDynamicTests() // Is a runtime test being stuffed in the code?
  127. );
  128. }
  129. if (!getMessageHandler().isIgnoring(IMessage.WEAVEINFO)) {
  130. reportWeavingMessage(munger, shadow);
  131. }
  132. if (getModel() != null) {
  133. AsmRelationshipProvider.addAdvisedRelationship(getModelAsAsmManager(), shadow, munger);
  134. }
  135. }
  136. /*
  137. * Report a message about the advice weave that has occurred. Some messing about to make it pretty ! This code is just asking
  138. * for an NPE to occur ...
  139. */
  140. private void reportWeavingMessage(ShadowMunger munger, Shadow shadow) {
  141. Advice advice = (Advice) munger;
  142. AdviceKind aKind = advice.getKind();
  143. // Only report on interesting advice kinds ...
  144. if (aKind == null || advice.getConcreteAspect() == null) {
  145. // We suspect someone is programmatically driving the weaver
  146. // (e.g. IdWeaveTestCase in the weaver testcases)
  147. return;
  148. }
  149. if (!(aKind.equals(AdviceKind.Before) || aKind.equals(AdviceKind.After) || aKind.equals(AdviceKind.AfterReturning)
  150. || aKind.equals(AdviceKind.AfterThrowing) || aKind.equals(AdviceKind.Around) || aKind.equals(AdviceKind.Softener))) {
  151. return;
  152. }
  153. // synchronized blocks are implemented with multiple monitor_exit instructions in the bytecode
  154. // (one for normal exit from the method, one for abnormal exit), we only want to tell the user
  155. // once we have advised the end of the sync block, even though under the covers we will have
  156. // woven both exit points
  157. if (shadow.getKind() == Shadow.SynchronizationUnlock) {
  158. if (advice.lastReportedMonitorExitJoinpointLocation == null) {
  159. // this is the first time through, let's continue...
  160. advice.lastReportedMonitorExitJoinpointLocation = shadow.getSourceLocation();
  161. } else {
  162. if (areTheSame(shadow.getSourceLocation(), advice.lastReportedMonitorExitJoinpointLocation)) {
  163. // Don't report it again!
  164. advice.lastReportedMonitorExitJoinpointLocation = null;
  165. return;
  166. }
  167. // hmmm, this means some kind of nesting is going on, urgh
  168. advice.lastReportedMonitorExitJoinpointLocation = shadow.getSourceLocation();
  169. }
  170. }
  171. String description = advice.getKind().toString();
  172. String advisedType = shadow.getEnclosingType().getName();
  173. String advisingType = advice.getConcreteAspect().getName();
  174. Message msg = null;
  175. if (advice.getKind().equals(AdviceKind.Softener)) {
  176. msg = WeaveMessage.constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_SOFTENS, new String[] { advisedType,
  177. beautifyLocation(shadow.getSourceLocation()), advisingType, beautifyLocation(munger.getSourceLocation()) },
  178. advisedType, advisingType);
  179. } else {
  180. boolean runtimeTest = advice.hasDynamicTests();
  181. String joinPointDescription = shadow.toString();
  182. msg = WeaveMessage
  183. .constructWeavingMessage(WeaveMessage.WEAVEMESSAGE_ADVISES,
  184. new String[] { joinPointDescription, advisedType, beautifyLocation(shadow.getSourceLocation()),
  185. description, advisingType, beautifyLocation(munger.getSourceLocation()),
  186. (runtimeTest ? " [with runtime test]" : "") }, advisedType, advisingType);
  187. // Boolean.toString(runtimeTest)});
  188. }
  189. getMessageHandler().handleMessage(msg);
  190. }
  191. private boolean areTheSame(ISourceLocation locA, ISourceLocation locB) {
  192. if (locA == null) {
  193. return locB == null;
  194. }
  195. if (locB == null) {
  196. return false;
  197. }
  198. if (locA.getLine() != locB.getLine()) {
  199. return false;
  200. }
  201. File fA = locA.getSourceFile();
  202. File fB = locA.getSourceFile();
  203. if (fA == null) {
  204. return fB == null;
  205. }
  206. if (fB == null) {
  207. return false;
  208. }
  209. return fA.getName().equals(fB.getName());
  210. }
  211. /*
  212. * Ensure we report a nice source location - particular in the case where the source info is missing (binary weave).
  213. */
  214. private String beautifyLocation(ISourceLocation isl) {
  215. StringBuffer nice = new StringBuffer();
  216. if (isl == null || isl.getSourceFile() == null || isl.getSourceFile().getName().indexOf("no debug info available") != -1) {
  217. nice.append("no debug info available");
  218. } else {
  219. // can't use File.getName() as this fails when a Linux box encounters a path created on Windows and vice-versa
  220. int takeFrom = isl.getSourceFile().getPath().lastIndexOf('/');
  221. if (takeFrom == -1) {
  222. takeFrom = isl.getSourceFile().getPath().lastIndexOf('\\');
  223. }
  224. int binary = isl.getSourceFile().getPath().lastIndexOf('!');
  225. if (binary != -1 && binary < takeFrom) {
  226. // we have been woven by a binary aspect
  227. String pathToBinaryLoc = isl.getSourceFile().getPath().substring(0, binary + 1);
  228. if (pathToBinaryLoc.indexOf(".jar") != -1) {
  229. // only want to add the extra info if we're from a jar file
  230. int lastSlash = pathToBinaryLoc.lastIndexOf('/');
  231. if (lastSlash == -1) {
  232. lastSlash = pathToBinaryLoc.lastIndexOf('\\');
  233. }
  234. nice.append(pathToBinaryLoc.substring(lastSlash + 1));
  235. }
  236. }
  237. nice.append(isl.getSourceFile().getPath().substring(takeFrom + 1));
  238. if (isl.getLine() != 0) {
  239. nice.append(":").append(isl.getLine());
  240. }
  241. // if it's a binary file then also want to give the file name
  242. if (isl.getSourceFileName() != null) {
  243. nice.append("(from " + isl.getSourceFileName() + ")");
  244. }
  245. }
  246. return nice.toString();
  247. }
  248. private static List<String> makeDefaultClasspath(String cp) {
  249. List<String> classPath = new ArrayList<String>();
  250. classPath.addAll(getPathEntries(cp));
  251. classPath.addAll(getPathEntries(ClassPath.getClassPath()));
  252. return classPath;
  253. }
  254. private static List<String> getPathEntries(String s) {
  255. List<String> ret = new ArrayList<String>();
  256. StringTokenizer tok = new StringTokenizer(s, File.pathSeparator);
  257. while (tok.hasMoreTokens()) {
  258. ret.add(tok.nextToken());
  259. }
  260. return ret;
  261. }
  262. public BcelWorld(List classPath, IMessageHandler handler, ICrossReferenceHandler xrefHandler) {
  263. // this.aspectPath = new ClassPathManager(aspectPath, handler);
  264. this.classPath = new ClassPathManager(classPath, handler);
  265. setMessageHandler(handler);
  266. setCrossReferenceHandler(xrefHandler);
  267. // Tell BCEL to use us for resolving any classes
  268. delegate = this;
  269. }
  270. public BcelWorld(ClassPathManager cpm, IMessageHandler handler, ICrossReferenceHandler xrefHandler) {
  271. classPath = cpm;
  272. setMessageHandler(handler);
  273. setCrossReferenceHandler(xrefHandler);
  274. // Tell BCEL to use us for resolving any classes
  275. delegate = this;
  276. }
  277. /**
  278. * Build a World from a ClassLoader, for LTW support
  279. *
  280. * @param loader
  281. * @param handler
  282. * @param xrefHandler
  283. */
  284. public BcelWorld(ClassLoader loader, IMessageHandler handler, ICrossReferenceHandler xrefHandler) {
  285. classPath = null;
  286. loaderRef = new BcelWeakClassLoaderReference(loader);
  287. setMessageHandler(handler);
  288. setCrossReferenceHandler(xrefHandler);
  289. // Tell BCEL to use us for resolving any classes
  290. // delegate = getClassLoaderRepositoryFor(loader);
  291. }
  292. public void ensureRepositorySetup() {
  293. if (delegate == null) {
  294. delegate = getClassLoaderRepositoryFor(loaderRef);
  295. }
  296. }
  297. public Repository getClassLoaderRepositoryFor(ClassLoaderReference loader) {
  298. if (bcelRepositoryCaching) {
  299. return new ClassLoaderRepository(loader);
  300. } else {
  301. return new NonCachingClassLoaderRepository(loader);
  302. }
  303. }
  304. public void addPath(String name) {
  305. classPath.addPath(name, this.getMessageHandler());
  306. }
  307. // ---- various interactions with bcel
  308. public static Type makeBcelType(UnresolvedType type) {
  309. return Type.getType(type.getErasureSignature());
  310. }
  311. static Type[] makeBcelTypes(UnresolvedType[] types) {
  312. Type[] ret = new Type[types.length];
  313. for (int i = 0, len = types.length; i < len; i++) {
  314. ret[i] = makeBcelType(types[i]);
  315. }
  316. return ret;
  317. }
  318. static String[] makeBcelTypesAsClassNames(UnresolvedType[] types) {
  319. String[] ret = new String[types.length];
  320. for (int i = 0, len = types.length; i < len; i++) {
  321. ret[i] = types[i].getName();
  322. }
  323. return ret;
  324. }
  325. public static UnresolvedType fromBcel(Type t) {
  326. return UnresolvedType.forSignature(t.getSignature());
  327. }
  328. static UnresolvedType[] fromBcel(Type[] ts) {
  329. UnresolvedType[] ret = new UnresolvedType[ts.length];
  330. for (int i = 0, len = ts.length; i < len; i++) {
  331. ret[i] = fromBcel(ts[i]);
  332. }
  333. return ret;
  334. }
  335. public ResolvedType resolve(Type t) {
  336. return resolve(fromBcel(t));
  337. }
  338. @Override
  339. protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) {
  340. String name = ty.getName();
  341. ensureAdvancedConfigurationProcessed();
  342. JavaClass jc = lookupJavaClass(classPath, name);
  343. if (jc == null) {
  344. // Anyone else to ask?
  345. if (typeDelegateResolvers != null) {
  346. for (TypeDelegateResolver tdr : typeDelegateResolvers) {
  347. ReferenceTypeDelegate delegate = tdr.getDelegate(ty);
  348. if (delegate != null) {
  349. return delegate;
  350. }
  351. }
  352. }
  353. return null;
  354. } else {
  355. return buildBcelDelegate(ty, jc, false, false);
  356. }
  357. }
  358. public BcelObjectType buildBcelDelegate(ReferenceType type, JavaClass jc, boolean artificial, boolean exposedToWeaver) {
  359. BcelObjectType ret = new BcelObjectType(type, jc, artificial, exposedToWeaver);
  360. return ret;
  361. }
  362. private JavaClass lookupJavaClass(ClassPathManager classPath, String name) {
  363. if (classPath == null) {
  364. try {
  365. ensureRepositorySetup();
  366. JavaClass jc = delegate.loadClass(name);
  367. if (trace.isTraceEnabled()) {
  368. trace.event("lookupJavaClass", this, new Object[] { name, jc });
  369. }
  370. return jc;
  371. } catch (ClassNotFoundException e) {
  372. if (trace.isTraceEnabled()) {
  373. trace.error("Unable to find class '" + name + "' in repository", e);
  374. }
  375. return null;
  376. }
  377. }
  378. ClassPathManager.ClassFile file = null;
  379. try {
  380. file = classPath.find(UnresolvedType.forName(name));
  381. if (file == null) {
  382. return null;
  383. }
  384. ClassParser parser = new ClassParser(file.getInputStream(), file.getPath());
  385. JavaClass jc = parser.parse();
  386. return jc;
  387. } catch (IOException ioe) {
  388. return null;
  389. } finally {
  390. if (file != null) {
  391. file.close();
  392. }
  393. }
  394. }
  395. // public BcelObjectType addSourceObjectType(JavaClass jc) {
  396. // return addSourceObjectType(jc.getClassName(), jc, -1);
  397. // }
  398. public BcelObjectType addSourceObjectType(JavaClass jc, boolean artificial) {
  399. return addSourceObjectType(jc.getClassName(), jc, artificial);
  400. }
  401. public BcelObjectType addSourceObjectType(String classname, JavaClass jc, boolean artificial) {
  402. BcelObjectType ret = null;
  403. if (!jc.getClassName().equals(classname)) {
  404. throw new RuntimeException(jc.getClassName() + "!=" + classname);
  405. }
  406. String signature = UnresolvedType.forName(jc.getClassName()).getSignature();
  407. ResolvedType fromTheMap = typeMap.get(signature);
  408. if (fromTheMap != null && !(fromTheMap instanceof ReferenceType)) {
  409. // what on earth is it then? See pr 112243
  410. StringBuffer exceptionText = new StringBuffer();
  411. exceptionText.append("Found invalid (not a ReferenceType) entry in the type map. ");
  412. exceptionText.append("Signature=[" + signature + "] Found=[" + fromTheMap + "] Class=[" + fromTheMap.getClass() + "]");
  413. throw new BCException(exceptionText.toString());
  414. }
  415. ReferenceType nameTypeX = (ReferenceType) fromTheMap;
  416. if (nameTypeX == null) {
  417. if (jc.isGeneric() && isInJava5Mode()) {
  418. nameTypeX = ReferenceType.fromTypeX(UnresolvedType.forRawTypeName(jc.getClassName()), this);
  419. ret = buildBcelDelegate(nameTypeX, jc, artificial, true);
  420. ReferenceType genericRefType = new ReferenceType(UnresolvedType.forGenericTypeSignature(signature,
  421. ret.getDeclaredGenericSignature()), this);
  422. nameTypeX.setDelegate(ret);
  423. genericRefType.setDelegate(ret);
  424. nameTypeX.setGenericType(genericRefType);
  425. typeMap.put(signature, nameTypeX);
  426. } else {
  427. nameTypeX = new ReferenceType(signature, this);
  428. ret = buildBcelDelegate(nameTypeX, jc, artificial, true);
  429. typeMap.put(signature, nameTypeX);
  430. }
  431. } else {
  432. ret = buildBcelDelegate(nameTypeX, jc, artificial, true);
  433. }
  434. return ret;
  435. }
  436. public BcelObjectType addSourceObjectType(String classname, byte[] bytes, boolean artificial) {
  437. BcelObjectType ret = null;
  438. String signature = UnresolvedType.forName(classname).getSignature();
  439. ResolvedType fromTheMap = typeMap.get(signature);
  440. if (fromTheMap != null && !(fromTheMap instanceof ReferenceType)) {
  441. // what on earth is it then? See pr 112243
  442. StringBuffer exceptionText = new StringBuffer();
  443. exceptionText.append("Found invalid (not a ReferenceType) entry in the type map. ");
  444. exceptionText.append("Signature=[" + signature + "] Found=[" + fromTheMap + "] Class=[" + fromTheMap.getClass() + "]");
  445. throw new BCException(exceptionText.toString());
  446. }
  447. ReferenceType nameTypeX = (ReferenceType) fromTheMap;
  448. if (nameTypeX == null) {
  449. JavaClass jc = Utility.makeJavaClass(classname, bytes);
  450. if (jc.isGeneric() && isInJava5Mode()) {
  451. nameTypeX = ReferenceType.fromTypeX(UnresolvedType.forRawTypeName(jc.getClassName()), this);
  452. ret = buildBcelDelegate(nameTypeX, jc, artificial, true);
  453. ReferenceType genericRefType = new ReferenceType(UnresolvedType.forGenericTypeSignature(signature,
  454. ret.getDeclaredGenericSignature()), this);
  455. nameTypeX.setDelegate(ret);
  456. genericRefType.setDelegate(ret);
  457. nameTypeX.setGenericType(genericRefType);
  458. typeMap.put(signature, nameTypeX);
  459. } else {
  460. nameTypeX = new ReferenceType(signature, this);
  461. ret = buildBcelDelegate(nameTypeX, jc, artificial, true);
  462. typeMap.put(signature, nameTypeX);
  463. }
  464. } else {
  465. Object o = nameTypeX.getDelegate();
  466. if (!(o instanceof BcelObjectType)) {
  467. throw new IllegalStateException("For " + classname + " should be BcelObjectType, but is " + o.getClass());
  468. }
  469. ret = (BcelObjectType) o;
  470. // byte[] bs = ret.javaClass.getBytes();
  471. // if (bs.length != bytes.length) {
  472. // throw new RuntimeException("");
  473. // }
  474. // If the type is already exposed to the weaver (ret.isExposedToWeaver()) then this is likely
  475. // to be a hotswap reweave so build a new delegate, dont accidentally use the old data
  476. if (ret.isArtificial() || ret.isExposedToWeaver()) {
  477. // System.out.println("Rebuilding " + nameTypeX.getName());
  478. ret = buildBcelDelegate(nameTypeX, Utility.makeJavaClass(classname, bytes), artificial, true);
  479. } else {
  480. ret.setExposedToWeaver(true);
  481. }
  482. }
  483. return ret;
  484. }
  485. void deleteSourceObjectType(UnresolvedType ty) {
  486. typeMap.remove(ty.getSignature());
  487. }
  488. public static Member makeFieldJoinPointSignature(LazyClassGen cg, FieldInstruction fi) {
  489. ConstantPool cpg = cg.getConstantPool();
  490. return MemberImpl.field(fi.getClassName(cpg),
  491. (fi.opcode == Constants.GETSTATIC || fi.opcode == Constants.PUTSTATIC) ? Modifier.STATIC : 0, fi.getName(cpg),
  492. fi.getSignature(cpg));
  493. }
  494. public Member makeJoinPointSignatureFromMethod(LazyMethodGen mg, MemberKind kind) {
  495. Member ret = mg.getMemberView();
  496. if (ret == null) {
  497. int mods = mg.getAccessFlags();
  498. if (mg.getEnclosingClass().isInterface()) {
  499. mods |= Modifier.INTERFACE;
  500. }
  501. return new ResolvedMemberImpl(kind, UnresolvedType.forName(mg.getClassName()), mods, fromBcel(mg.getReturnType()),
  502. mg.getName(), fromBcel(mg.getArgumentTypes()));
  503. } else {
  504. return ret;
  505. }
  506. }
  507. public Member makeJoinPointSignatureForMonitorEnter(LazyClassGen cg, InstructionHandle h) {
  508. return MemberImpl.monitorEnter();
  509. }
  510. public Member makeJoinPointSignatureForMonitorExit(LazyClassGen cg, InstructionHandle h) {
  511. return MemberImpl.monitorExit();
  512. }
  513. public Member makeJoinPointSignatureForArrayConstruction(LazyClassGen cg, InstructionHandle handle) {
  514. Instruction i = handle.getInstruction();
  515. ConstantPool cpg = cg.getConstantPool();
  516. Member retval = null;
  517. if (i.opcode == Constants.ANEWARRAY) {
  518. // ANEWARRAY arrayInstruction = (ANEWARRAY)i;
  519. Type ot = i.getType(cpg);
  520. UnresolvedType ut = fromBcel(ot);
  521. ut = UnresolvedType.makeArray(ut, 1);
  522. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", new ResolvedType[] { INT });
  523. } else if (i instanceof MULTIANEWARRAY) {
  524. MULTIANEWARRAY arrayInstruction = (MULTIANEWARRAY) i;
  525. UnresolvedType ut = null;
  526. short dimensions = arrayInstruction.getDimensions();
  527. ObjectType ot = arrayInstruction.getLoadClassType(cpg);
  528. if (ot != null) {
  529. ut = fromBcel(ot);
  530. ut = UnresolvedType.makeArray(ut, dimensions);
  531. } else {
  532. Type t = arrayInstruction.getType(cpg);
  533. ut = fromBcel(t);
  534. }
  535. ResolvedType[] parms = new ResolvedType[dimensions];
  536. for (int ii = 0; ii < dimensions; ii++) {
  537. parms[ii] = INT;
  538. }
  539. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", parms);
  540. } else if (i.opcode == Constants.NEWARRAY) {
  541. // NEWARRAY arrayInstruction = (NEWARRAY)i;
  542. Type ot = i.getType();
  543. UnresolvedType ut = fromBcel(ot);
  544. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", new ResolvedType[] { INT });
  545. } else {
  546. throw new BCException("Cannot create array construction signature for this non-array instruction:" + i);
  547. }
  548. return retval;
  549. }
  550. public Member makeJoinPointSignatureForMethodInvocation(LazyClassGen cg, InvokeInstruction ii) {
  551. ConstantPool cpg = cg.getConstantPool();
  552. String name = ii.getName(cpg);
  553. String declaring = ii.getClassName(cpg);
  554. UnresolvedType declaringType = null;
  555. String signature = ii.getSignature(cpg);
  556. int modifier = (ii instanceof INVOKEINTERFACE) ? Modifier.INTERFACE
  557. : (ii.opcode == Constants.INVOKESTATIC) ? Modifier.STATIC : (ii.opcode == Constants.INVOKESPECIAL && !name
  558. .equals("<init>")) ? Modifier.PRIVATE : 0;
  559. // in Java 1.4 and after, static method call of super class within
  560. // subclass method appears
  561. // as declared by the subclass in the bytecode - but they are not
  562. // see #104212
  563. if (ii.opcode == Constants.INVOKESTATIC) {
  564. ResolvedType appearsDeclaredBy = resolve(declaring);
  565. // look for the method there
  566. for (Iterator<ResolvedMember> iterator = appearsDeclaredBy.getMethods(true, true); iterator.hasNext();) {
  567. ResolvedMember method = iterator.next();
  568. if (Modifier.isStatic(method.getModifiers())) {
  569. if (name.equals(method.getName()) && signature.equals(method.getSignature())) {
  570. // we found it
  571. declaringType = method.getDeclaringType();
  572. break;
  573. }
  574. }
  575. }
  576. }
  577. if (declaringType == null) {
  578. if (declaring.charAt(0) == '[') {
  579. declaringType = UnresolvedType.forSignature(declaring);
  580. } else {
  581. declaringType = UnresolvedType.forName(declaring);
  582. }
  583. }
  584. return MemberImpl.method(declaringType, modifier, name, signature);
  585. }
  586. @Override
  587. public String toString() {
  588. StringBuffer buf = new StringBuffer();
  589. buf.append("BcelWorld(");
  590. // buf.append(shadowMungerMap);
  591. buf.append(")");
  592. return buf.toString();
  593. }
  594. /**
  595. * Retrieve a bcel delegate for an aspect - this will return NULL if the delegate is an EclipseSourceType and not a
  596. * BcelObjectType - this happens quite often when incrementally compiling.
  597. */
  598. public static BcelObjectType getBcelObjectType(ResolvedType concreteAspect) {
  599. if (concreteAspect == null) {
  600. return null;
  601. }
  602. if (!(concreteAspect instanceof ReferenceType)) { // Might be Missing
  603. return null;
  604. }
  605. ReferenceTypeDelegate rtDelegate = ((ReferenceType) concreteAspect).getDelegate();
  606. if (rtDelegate instanceof BcelObjectType) {
  607. return (BcelObjectType) rtDelegate;
  608. } else {
  609. return null;
  610. }
  611. }
  612. public void tidyUp() {
  613. // At end of compile, close any open files so deletion of those archives
  614. // is possible
  615. classPath.closeArchives();
  616. typeMap.report();
  617. typeMap.demote(true);
  618. // ResolvedType.resetPrimitives();
  619. }
  620. // / The repository interface methods
  621. public JavaClass findClass(String className) {
  622. return lookupJavaClass(classPath, className);
  623. }
  624. public JavaClass loadClass(String className) throws ClassNotFoundException {
  625. return lookupJavaClass(classPath, className);
  626. }
  627. public void storeClass(JavaClass clazz) {
  628. // doesn't need to do anything
  629. }
  630. public void removeClass(JavaClass clazz) {
  631. throw new RuntimeException("Not implemented");
  632. }
  633. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  634. throw new RuntimeException("Not implemented");
  635. }
  636. public void clear() {
  637. delegate.clear();
  638. // throw new RuntimeException("Not implemented");
  639. }
  640. /**
  641. * The aim of this method is to make sure a particular type is 'ok'. Some operations on the delegate for a type modify it and
  642. * this method is intended to undo that... see pr85132
  643. */
  644. @Override
  645. public void validateType(UnresolvedType type) {
  646. ResolvedType result = typeMap.get(type.getSignature());
  647. if (result == null) {
  648. return; // We haven't heard of it yet
  649. }
  650. if (!result.isExposedToWeaver()) {
  651. return; // cant need resetting
  652. }
  653. result.ensureConsistent();
  654. // If we want to rebuild it 'from scratch' then:
  655. // ClassParser cp = new ClassParser(new
  656. // ByteArrayInputStream(newbytes),new String(cs));
  657. // try {
  658. // rt.setDelegate(makeBcelObjectType(rt,cp.parse(),true));
  659. // } catch (ClassFormatException e) {
  660. // e.printStackTrace();
  661. // } catch (IOException e) {
  662. // e.printStackTrace();
  663. // }
  664. }
  665. /**
  666. * Apply a single declare parents - return true if we change the type
  667. */
  668. private boolean applyDeclareParents(DeclareParents p, ResolvedType onType) {
  669. boolean didSomething = false;
  670. List<ResolvedType> newParents = p.findMatchingNewParents(onType, true);
  671. if (!newParents.isEmpty()) {
  672. didSomething = true;
  673. BcelObjectType classType = BcelWorld.getBcelObjectType(onType);
  674. // System.err.println("need to do declare parents for: " + onType);
  675. for (ResolvedType newParent : newParents) {
  676. // We set it here so that the imminent matching for ITDs can
  677. // succeed - we still haven't done the necessary changes to the class file
  678. // itself (like transform super calls) - that is done in
  679. // BcelTypeMunger.mungeNewParent()
  680. // classType.addParent(newParent);
  681. onType.addParent(newParent);
  682. ResolvedTypeMunger newParentMunger = new NewParentTypeMunger(newParent, p.getDeclaringType());
  683. newParentMunger.setSourceLocation(p.getSourceLocation());
  684. onType.addInterTypeMunger(new BcelTypeMunger(newParentMunger, getCrosscuttingMembersSet()
  685. .findAspectDeclaringParents(p)), false);
  686. }
  687. }
  688. return didSomething;
  689. }
  690. /**
  691. * Apply a declare @type - return true if we change the type
  692. */
  693. private boolean applyDeclareAtType(DeclareAnnotation decA, ResolvedType onType, boolean reportProblems) {
  694. boolean didSomething = false;
  695. if (decA.matches(onType)) {
  696. if (onType.hasAnnotation(decA.getAnnotation().getType())) {
  697. // already has it
  698. return false;
  699. }
  700. AnnotationAJ annoX = decA.getAnnotation();
  701. // check the annotation is suitable for the target
  702. boolean isOK = checkTargetOK(decA, onType, annoX);
  703. if (isOK) {
  704. didSomething = true;
  705. ResolvedTypeMunger newAnnotationTM = new AnnotationOnTypeMunger(annoX);
  706. newAnnotationTM.setSourceLocation(decA.getSourceLocation());
  707. onType.addInterTypeMunger(new BcelTypeMunger(newAnnotationTM, decA.getAspect().resolve(this)), false);
  708. decA.copyAnnotationTo(onType);
  709. }
  710. }
  711. return didSomething;
  712. }
  713. /**
  714. * Checks for an @target() on the annotation and if found ensures it allows the annotation to be attached to the target type
  715. * that matched.
  716. */
  717. private boolean checkTargetOK(DeclareAnnotation decA, ResolvedType onType, AnnotationAJ annoX) {
  718. if (annoX.specifiesTarget()) {
  719. if ((onType.isAnnotation() && !annoX.allowedOnAnnotationType()) || (!annoX.allowedOnRegularType())) {
  720. return false;
  721. }
  722. }
  723. return true;
  724. }
  725. // Hmmm - very similar to the code in BcelWeaver.weaveParentTypeMungers -
  726. // this code
  727. // doesn't need to produce errors/warnings though as it won't really be
  728. // weaving.
  729. protected void weaveInterTypeDeclarations(ResolvedType onType) {
  730. List<DeclareParents> declareParentsList = getCrosscuttingMembersSet().getDeclareParents();
  731. if (onType.isRawType()) {
  732. onType = onType.getGenericType();
  733. }
  734. onType.clearInterTypeMungers();
  735. List<DeclareParents> decpToRepeat = new ArrayList<DeclareParents>();
  736. boolean aParentChangeOccurred = false;
  737. boolean anAnnotationChangeOccurred = false;
  738. // First pass - apply all decp mungers
  739. for (Iterator<DeclareParents> i = declareParentsList.iterator(); i.hasNext();) {
  740. DeclareParents decp = i.next();
  741. boolean typeChanged = applyDeclareParents(decp, onType);
  742. if (typeChanged) {
  743. aParentChangeOccurred = true;
  744. } else { // Perhaps it would have matched if a 'dec @type' had
  745. // modified the type
  746. if (!decp.getChild().isStarAnnotation()) {
  747. decpToRepeat.add(decp);
  748. }
  749. }
  750. }
  751. // Still first pass - apply all dec @type mungers
  752. for (DeclareAnnotation decA : getCrosscuttingMembersSet().getDeclareAnnotationOnTypes()) {
  753. boolean typeChanged = applyDeclareAtType(decA, onType, true);
  754. if (typeChanged) {
  755. anAnnotationChangeOccurred = true;
  756. }
  757. }
  758. while ((aParentChangeOccurred || anAnnotationChangeOccurred) && !decpToRepeat.isEmpty()) {
  759. anAnnotationChangeOccurred = aParentChangeOccurred = false;
  760. List<DeclareParents> decpToRepeatNextTime = new ArrayList<DeclareParents>();
  761. for (Iterator<DeclareParents> iter = decpToRepeat.iterator(); iter.hasNext();) {
  762. DeclareParents decp = iter.next();
  763. boolean typeChanged = applyDeclareParents(decp, onType);
  764. if (typeChanged) {
  765. aParentChangeOccurred = true;
  766. } else {
  767. decpToRepeatNextTime.add(decp);
  768. }
  769. }
  770. for (Iterator iter = getCrosscuttingMembersSet().getDeclareAnnotationOnTypes().iterator(); iter.hasNext();) {
  771. DeclareAnnotation decA = (DeclareAnnotation) iter.next();
  772. boolean typeChanged = applyDeclareAtType(decA, onType, false);
  773. if (typeChanged) {
  774. anAnnotationChangeOccurred = true;
  775. }
  776. }
  777. decpToRepeat = decpToRepeatNextTime;
  778. }
  779. }
  780. @Override
  781. public IWeavingSupport getWeavingSupport() {
  782. return bcelWeavingSupport;
  783. }
  784. @Override
  785. public void reportCheckerMatch(Checker checker, Shadow shadow) {
  786. IMessage iMessage = new Message(checker.getMessage(shadow), shadow.toString(), checker.isError() ? IMessage.ERROR
  787. : IMessage.WARNING, shadow.getSourceLocation(), null, new ISourceLocation[] { checker.getSourceLocation() }, true,
  788. 0, -1, -1);
  789. getMessageHandler().handleMessage(iMessage);
  790. if (getCrossReferenceHandler() != null) {
  791. getCrossReferenceHandler()
  792. .addCrossReference(
  793. checker.getSourceLocation(),
  794. shadow.getSourceLocation(),
  795. (checker.isError() ? IRelationship.Kind.DECLARE_ERROR.getName() : IRelationship.Kind.DECLARE_WARNING
  796. .getName()), false);
  797. }
  798. if (getModel() != null) {
  799. AsmRelationshipProvider.addDeclareErrorOrWarningRelationship(getModelAsAsmManager(), shadow, checker);
  800. }
  801. }
  802. public AsmManager getModelAsAsmManager() {
  803. return (AsmManager) getModel(); // For now... always an AsmManager in a bcel environment
  804. }
  805. void raiseError(String message) {
  806. getMessageHandler().handleMessage(MessageUtil.error(message));
  807. }
  808. /**
  809. * These are aop.xml files that can be used to alter the aspects that actually apply from those passed in - and also their scope
  810. * of application to other files in the system.
  811. *
  812. * @param xmlFiles list of File objects representing any aop.xml files passed in to configure the build process
  813. */
  814. public void setXmlFiles(List<File> xmlFiles) {
  815. if (!isXmlConfiguredWorld && !xmlFiles.isEmpty()) {
  816. raiseError("xml configuration files only supported by the compiler when -xmlConfigured option specified");
  817. return;
  818. }
  819. if (!xmlFiles.isEmpty()) {
  820. xmlConfiguration = new WeavingXmlConfig(this, WeavingXmlConfig.MODE_COMPILE);
  821. }
  822. for (File xmlfile : xmlFiles) {
  823. try {
  824. Definition d = DocumentParser.parse(xmlfile.toURI().toURL());
  825. xmlConfiguration.add(d);
  826. } catch (MalformedURLException e) {
  827. raiseError("Unexpected problem processing XML config file '" + xmlfile.getName() + "' :" + e.getMessage());
  828. } catch (Exception e) {
  829. raiseError("Unexpected problem processing XML config file '" + xmlfile.getName() + "' :" + e.getMessage());
  830. }
  831. }
  832. }
  833. /**
  834. * Add a scoped aspects where the scoping was defined in an aop.xml file and this world is being used in a LTW configuration
  835. */
  836. public void addScopedAspect(String name, String scope) {
  837. this.isXmlConfiguredWorld = true;
  838. if (xmlConfiguration == null) {
  839. xmlConfiguration = new WeavingXmlConfig(this, WeavingXmlConfig.MODE_LTW);
  840. }
  841. xmlConfiguration.addScopedAspect(name, scope);
  842. }
  843. public void setXmlConfigured(boolean b) {
  844. this.isXmlConfiguredWorld = b;
  845. }
  846. @Override
  847. public boolean isXmlConfigured() {
  848. return isXmlConfiguredWorld && xmlConfiguration != null;
  849. }
  850. public WeavingXmlConfig getXmlConfiguration() {
  851. return xmlConfiguration;
  852. }
  853. @Override
  854. public boolean isAspectIncluded(ResolvedType aspectType) {
  855. if (!isXmlConfigured()) {
  856. return true;
  857. }
  858. return xmlConfiguration.specifiesInclusionOfAspect(aspectType.getName());
  859. }
  860. @Override
  861. public TypePattern getAspectScope(ResolvedType declaringType) {
  862. return xmlConfiguration.getScopeFor(declaringType.getName());
  863. }
  864. @Override
  865. public boolean hasUnsatisfiedDependency(ResolvedType aspectType) {
  866. if (!aspectRequiredTypesProcessed) {
  867. if (aspectRequiredTypes != null) {
  868. List<String> forRemoval = new ArrayList<String>();
  869. for (Map.Entry<String, String> entry : aspectRequiredTypes.entrySet()) {
  870. ResolvedType rt = this.resolve(UnresolvedType.forName(entry.getValue()));
  871. if (!rt.isMissing()) {
  872. forRemoval.add(entry.getKey());
  873. } else {
  874. if (!getMessageHandler().isIgnoring(IMessage.INFO)) {
  875. getMessageHandler().handleMessage(
  876. MessageUtil.info("deactivating aspect '" + aspectType.getName() + "' as it requires type '"
  877. + rt.getName() + "' which cannot be found on the classpath"));
  878. }
  879. }
  880. }
  881. for (String key : forRemoval) {
  882. aspectRequiredTypes.remove(key);
  883. }
  884. }
  885. aspectRequiredTypesProcessed = true;
  886. }
  887. if (aspectRequiredTypes == null) {
  888. return false;
  889. }
  890. return aspectRequiredTypes.containsKey(aspectType.getName());
  891. }
  892. private boolean aspectRequiredTypesProcessed = false;
  893. private Map<String, String> aspectRequiredTypes = null;
  894. public void addAspectRequires(String name, String requiredType) {
  895. if (aspectRequiredTypes == null) {
  896. aspectRequiredTypes = new HashMap<String, String>();
  897. }
  898. aspectRequiredTypes.put(name, requiredType);
  899. }
  900. /**
  901. * A WeavingXmlConfig is initially a collection of definitions from XML files - once the world is ready and weaving is running
  902. * it will initialize and transform those definitions into an optimized set of values (eg. resolve type patterns and string
  903. * names to real entities). It can then answer questions quickly: (1) is this aspect included in the weaving? (2) Is there a
  904. * scope specified for this aspect and does it include type X?
  905. *
  906. */
  907. static class WeavingXmlConfig {
  908. final static int MODE_COMPILE = 1;
  909. final static int MODE_LTW = 2;
  910. private int mode;
  911. private boolean initialized = false; // Lazily done
  912. private List<Definition> definitions = new ArrayList<Definition>();
  913. private List<String> resolvedIncludedAspects = new ArrayList<String>();
  914. private Map<String, TypePattern> scopes = new HashMap<String, TypePattern>();
  915. // these are not set for LTW mode (exclusion of these fast match patterns is handled before the weaver/world are used)
  916. private List<String> includedFastMatchPatterns = Collections.emptyList();
  917. private List<TypePattern> includedPatterns = Collections.emptyList();
  918. private List<String> excludedFastMatchPatterns = Collections.emptyList();
  919. private List<TypePattern> excludedPatterns = Collections.emptyList();
  920. private BcelWorld world;
  921. public WeavingXmlConfig(BcelWorld bcelWorld, int mode) {
  922. this.world = bcelWorld;
  923. this.mode = mode;
  924. }
  925. public void add(Definition d) {
  926. definitions.add(d);
  927. }
  928. public void addScopedAspect(String aspectName, String scope) {
  929. ensureInitialized();
  930. resolvedIncludedAspects.add(aspectName);
  931. try {
  932. TypePattern scopePattern = new PatternParser(scope).parseTypePattern();
  933. scopePattern.resolve(world);
  934. scopes.put(aspectName, scopePattern);
  935. if (!world.getMessageHandler().isIgnoring(IMessage.INFO)) {
  936. world.getMessageHandler().handleMessage(
  937. MessageUtil.info("Aspect '" + aspectName + "' is scoped to apply against types matching pattern '"
  938. + scopePattern.toString() + "'"));
  939. }
  940. } catch (Exception e) {
  941. world.getMessageHandler().handleMessage(
  942. MessageUtil.error("Unable to parse scope as type pattern. Scope was '" + scope + "': " + e.getMessage()));
  943. }
  944. }
  945. public void ensureInitialized() {
  946. if (!initialized) {
  947. try {
  948. resolvedIncludedAspects = new ArrayList<String>();
  949. // Process the definitions into something more optimal
  950. for (Definition definition : definitions) {
  951. List<String> aspectNames = definition.getAspectClassNames();
  952. for (String name : aspectNames) {
  953. resolvedIncludedAspects.add(name);
  954. // TODO check for existence?
  955. // ResolvedType resolvedAspect = resolve(UnresolvedType.forName(name));
  956. // if (resolvedAspect.isMissing()) {
  957. // // ERROR
  958. // } else {
  959. // resolvedIncludedAspects.add(resolvedAspect);
  960. // }
  961. String scope = definition.getScopeForAspect(name);
  962. if (scope != null) {
  963. // Resolve the type pattern
  964. try {
  965. TypePattern scopePattern = new PatternParser(scope).parseTypePattern();
  966. scopePattern.resolve(world);
  967. scopes.put(name, scopePattern);
  968. if (!world.getMessageHandler().isIgnoring(IMessage.INFO)) {
  969. world.getMessageHandler().handleMessage(
  970. MessageUtil.info("Aspect '" + name
  971. + "' is scoped to apply against types matching pattern '"
  972. + scopePattern.toString() + "'"));
  973. }
  974. } catch (Exception e) {
  975. // TODO definitions should remember which file they came from, for inclusion in this message
  976. world.getMessageHandler().handleMessage(
  977. MessageUtil.error("Unable to parse scope as type pattern. Scope was '" + scope + "': "
  978. + e.getMessage()));
  979. }
  980. }
  981. }
  982. try {
  983. List<String> includePatterns = definition.getIncludePatterns();
  984. if (includePatterns.size() > 0) {
  985. includedPatterns = new ArrayList<TypePattern>();
  986. includedFastMatchPatterns = new ArrayList<String>();
  987. }
  988. for (String includePattern : includePatterns) {
  989. if (includePattern.endsWith("..*")) {
  990. // from 'blah.blah.blah..*' leave the 'blah.blah.blah.'
  991. includedFastMatchPatterns.add(includePattern.substring(0, includePattern.length() - 2));
  992. } else {
  993. TypePattern includedPattern = new PatternParser(includePattern).parseTypePattern();
  994. includedPatterns.add(includedPattern);
  995. }
  996. }
  997. List<String> excludePatterns = definition.getExcludePatterns();
  998. if (excludePatterns.size() > 0) {
  999. excludedPatterns = new ArrayList<TypePattern>();
  1000. excludedFastMatchPatterns = new ArrayList<String>();
  1001. }
  1002. for (String excludePattern : excludePatterns) {
  1003. if (excludePattern.endsWith("..*")) {
  1004. // from 'blah.blah.blah..*' leave the 'blah.blah.blah.'
  1005. excludedFastMatchPatterns.add(excludePattern.substring(0, excludePattern.length() - 2));
  1006. } else {
  1007. TypePattern excludedPattern = new PatternParser(excludePattern).parseTypePattern();
  1008. excludedPatterns.add(excludedPattern);
  1009. }
  1010. }
  1011. } catch (ParserException pe) {
  1012. // TODO definitions should remember which file they came from, for inclusion in this message
  1013. world.getMessageHandler().handleMessage(
  1014. MessageUtil.error("Unable to parse type pattern: " + pe.getMessage()));
  1015. }
  1016. }
  1017. } finally {
  1018. initialized = true;
  1019. }
  1020. }
  1021. }
  1022. public boolean specifiesInclusionOfAspect(String name) {
  1023. ensureInitialized();
  1024. return resolvedIncludedAspects.contains(name);
  1025. }
  1026. public TypePattern getScopeFor(String name) {
  1027. return scopes.get(name);
  1028. }
  1029. // Can't quite follow the same rules for exclusion as used for loadtime weaving:
  1030. // "The set of types to be woven are those types matched by at least one weaver include element and not matched by any
  1031. // weaver
  1032. // exclude element. If there are no weaver include statements then all non-excluded types are included."
  1033. // Since if the weaver is seeing it during this kind of build, the type is implicitly included. So all we should check
  1034. // for is exclusion
  1035. public boolean excludesType(ResolvedType type) {
  1036. if (mode == MODE_LTW) {
  1037. return false;
  1038. }
  1039. String typename = type.getName();
  1040. boolean excluded = false;
  1041. for (String excludedPattern : excludedFastMatchPatterns) {
  1042. if (typename.startsWith(excludedPattern)) {
  1043. excluded = true;
  1044. break;
  1045. }
  1046. }
  1047. if (!excluded) {
  1048. for (TypePattern excludedPattern : excludedPatterns) {
  1049. if (excludedPattern.matchesStatically(type)) {
  1050. excluded = true;
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. return excluded;
  1056. }
  1057. }
  1058. public TypeMap getTypeMap() {
  1059. return typeMap;
  1060. }
  1061. public boolean isLoadtimeWeaving() {
  1062. return false;
  1063. }
  1064. public void addTypeDelegateResolver(TypeDelegateResolver typeDelegateResolver) {
  1065. if (typeDelegateResolvers == null) {
  1066. typeDelegateResolvers = new ArrayList<TypeDelegateResolver>();
  1067. }
  1068. typeDelegateResolvers.add(typeDelegateResolver);
  1069. }
  1070. public void classWriteEvent(char[][] compoundName) {
  1071. typeMap.classWriteEvent(new String(CharOperation.concatWith(compoundName, '.')));
  1072. }
  1073. /**
  1074. * Force demote a type.
  1075. */
  1076. public void demote(ResolvedType type) {
  1077. typeMap.demote(type);
  1078. }
  1079. }