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 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. public static Type[] makeBcelTypes(String[] types) {
  319. if (types == null || types.length==0 ) {
  320. return null;
  321. }
  322. Type[] ret = new Type[types.length];
  323. for (int i=0, len=types.length; i<len; i++) {
  324. ret[i] = makeBcelType(types[i]);
  325. }
  326. return ret;
  327. }
  328. public static Type makeBcelType(String type) {
  329. return Type.getType(type);
  330. }
  331. static String[] makeBcelTypesAsClassNames(UnresolvedType[] types) {
  332. String[] ret = new String[types.length];
  333. for (int i = 0, len = types.length; i < len; i++) {
  334. ret[i] = types[i].getName();
  335. }
  336. return ret;
  337. }
  338. public static UnresolvedType fromBcel(Type t) {
  339. return UnresolvedType.forSignature(t.getSignature());
  340. }
  341. static UnresolvedType[] fromBcel(Type[] ts) {
  342. UnresolvedType[] ret = new UnresolvedType[ts.length];
  343. for (int i = 0, len = ts.length; i < len; i++) {
  344. ret[i] = fromBcel(ts[i]);
  345. }
  346. return ret;
  347. }
  348. public ResolvedType resolve(Type t) {
  349. return resolve(fromBcel(t));
  350. }
  351. @Override
  352. protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) {
  353. String name = ty.getName();
  354. ensureAdvancedConfigurationProcessed();
  355. JavaClass jc = lookupJavaClass(classPath, name);
  356. if (jc == null) {
  357. // Anyone else to ask?
  358. if (typeDelegateResolvers != null) {
  359. for (TypeDelegateResolver tdr : typeDelegateResolvers) {
  360. ReferenceTypeDelegate delegate = tdr.getDelegate(ty);
  361. if (delegate != null) {
  362. return delegate;
  363. }
  364. }
  365. }
  366. return null;
  367. } else {
  368. return buildBcelDelegate(ty, jc, false, false);
  369. }
  370. }
  371. public BcelObjectType buildBcelDelegate(ReferenceType type, JavaClass jc, boolean artificial, boolean exposedToWeaver) {
  372. BcelObjectType ret = new BcelObjectType(type, jc, artificial, exposedToWeaver);
  373. return ret;
  374. }
  375. private JavaClass lookupJavaClass(ClassPathManager classPath, String name) {
  376. if (classPath == null) {
  377. try {
  378. ensureRepositorySetup();
  379. JavaClass jc = delegate.loadClass(name);
  380. if (trace.isTraceEnabled()) {
  381. trace.event("lookupJavaClass", this, new Object[] { name, jc });
  382. }
  383. return jc;
  384. } catch (ClassNotFoundException e) {
  385. if (trace.isTraceEnabled()) {
  386. trace.error("Unable to find class '" + name + "' in repository", e);
  387. }
  388. return null;
  389. }
  390. }
  391. ClassPathManager.ClassFile file = null;
  392. try {
  393. file = classPath.find(UnresolvedType.forName(name));
  394. if (file == null) {
  395. return null;
  396. }
  397. ClassParser parser = new ClassParser(file.getInputStream(), file.getPath());
  398. JavaClass jc = parser.parse();
  399. return jc;
  400. } catch (IOException ioe) {
  401. if (trace.isTraceEnabled()) {
  402. trace.error("IOException whilst processing class",ioe);
  403. }
  404. return null;
  405. } finally {
  406. if (file != null) {
  407. file.close();
  408. }
  409. }
  410. }
  411. public BcelObjectType addSourceObjectType(JavaClass jc, boolean artificial) {
  412. return addSourceObjectType(jc.getClassName(), jc, artificial);
  413. }
  414. public BcelObjectType addSourceObjectType(String classname, JavaClass jc, boolean artificial) {
  415. BcelObjectType ret = null;
  416. if (!jc.getClassName().equals(classname)) {
  417. throw new RuntimeException(jc.getClassName() + "!=" + classname);
  418. }
  419. String signature = UnresolvedType.forName(jc.getClassName()).getSignature();
  420. ResolvedType resolvedTypeFromTypeMap = typeMap.get(signature);
  421. if (resolvedTypeFromTypeMap != null && !(resolvedTypeFromTypeMap instanceof ReferenceType)) {
  422. // what on earth is it then? See pr 112243
  423. StringBuffer exceptionText = new StringBuffer();
  424. exceptionText.append("Found invalid (not a ReferenceType) entry in the type map. ");
  425. exceptionText.append("Signature=[" + signature + "] Found=[" + resolvedTypeFromTypeMap + "] Class=[" + resolvedTypeFromTypeMap.getClass() + "]");
  426. throw new BCException(exceptionText.toString());
  427. }
  428. ReferenceType referenceTypeFromTypeMap = (ReferenceType) resolvedTypeFromTypeMap;
  429. if (referenceTypeFromTypeMap == null) {
  430. if (jc.isGeneric() && isInJava5Mode()) {
  431. ReferenceType rawType = ReferenceType.fromTypeX(UnresolvedType.forRawTypeName(jc.getClassName()), this);
  432. ret = buildBcelDelegate(rawType, jc, artificial, true);
  433. ReferenceType genericRefType = new ReferenceType(UnresolvedType.forGenericTypeSignature(signature,
  434. ret.getDeclaredGenericSignature()), this);
  435. rawType.setDelegate(ret);
  436. genericRefType.setDelegate(ret);
  437. rawType.setGenericType(genericRefType);
  438. typeMap.put(signature, rawType);
  439. } else {
  440. referenceTypeFromTypeMap = new ReferenceType(signature, this);
  441. ret = buildBcelDelegate(referenceTypeFromTypeMap, jc, artificial, true);
  442. typeMap.put(signature, referenceTypeFromTypeMap);
  443. }
  444. } else {
  445. ret = buildBcelDelegate(referenceTypeFromTypeMap, jc, artificial, true);
  446. }
  447. return ret;
  448. }
  449. public BcelObjectType addSourceObjectType(String classname, byte[] bytes, boolean artificial) {
  450. BcelObjectType retval = null;
  451. String signature = UnresolvedType.forName(classname).getSignature();
  452. ResolvedType resolvedTypeFromTypeMap = typeMap.get(signature);
  453. if (resolvedTypeFromTypeMap != null && !(resolvedTypeFromTypeMap instanceof ReferenceType)) {
  454. // what on earth is it then? See pr 112243
  455. StringBuffer exceptionText = new StringBuffer();
  456. exceptionText.append("Found invalid (not a ReferenceType) entry in the type map. ");
  457. exceptionText.append("Signature=[" + signature + "] Found=[" + resolvedTypeFromTypeMap + "] Class=[" + resolvedTypeFromTypeMap.getClass() + "]");
  458. throw new BCException(exceptionText.toString());
  459. }
  460. ReferenceType referenceTypeFromTypeMap = (ReferenceType) resolvedTypeFromTypeMap;
  461. if (referenceTypeFromTypeMap == null) {
  462. JavaClass jc = Utility.makeJavaClass(classname, bytes);
  463. if (jc.isGeneric() && isInJava5Mode()) {
  464. referenceTypeFromTypeMap = ReferenceType.fromTypeX(UnresolvedType.forRawTypeName(jc.getClassName()), this);
  465. retval = buildBcelDelegate(referenceTypeFromTypeMap, jc, artificial, true);
  466. ReferenceType genericRefType = new ReferenceType(UnresolvedType.forGenericTypeSignature(signature,
  467. retval.getDeclaredGenericSignature()), this);
  468. referenceTypeFromTypeMap.setDelegate(retval);
  469. genericRefType.setDelegate(retval);
  470. referenceTypeFromTypeMap.setGenericType(genericRefType);
  471. typeMap.put(signature, referenceTypeFromTypeMap);
  472. } else {
  473. referenceTypeFromTypeMap = new ReferenceType(signature, this);
  474. retval = buildBcelDelegate(referenceTypeFromTypeMap, jc, artificial, true);
  475. typeMap.put(signature, referenceTypeFromTypeMap);
  476. }
  477. } else {
  478. ReferenceTypeDelegate existingDelegate = referenceTypeFromTypeMap.getDelegate();
  479. if (!(existingDelegate instanceof BcelObjectType)) {
  480. throw new IllegalStateException("For " + classname + " should be BcelObjectType, but is " + existingDelegate.getClass());
  481. }
  482. retval = (BcelObjectType) existingDelegate;
  483. // Note1: If the type is already exposed to the weaver (retval.isExposedToWeaver()) then this is likely
  484. // to be a hotswap reweave so build a new delegate, don't accidentally use the old data.
  485. // Note2: Also seen when LTW and another agent precedes the AspectJ agent. Earlier in LTW
  486. // a type is resolved (and ends up in the typemap but not exposed to the weaver at that time)
  487. // then later LTW actually is attempted on this type. We end up here with different
  488. // bytes to the current delegate if the earlier agent has modified them. See PR488216
  489. // if (retval.isArtificial() || retval.isExposedToWeaver()) {
  490. retval = buildBcelDelegate(referenceTypeFromTypeMap, Utility.makeJavaClass(classname, bytes), artificial, true);
  491. // }
  492. }
  493. return retval;
  494. }
  495. void deleteSourceObjectType(UnresolvedType ty) {
  496. typeMap.remove(ty.getSignature());
  497. }
  498. public static Member makeFieldJoinPointSignature(LazyClassGen cg, FieldInstruction fi) {
  499. ConstantPool cpg = cg.getConstantPool();
  500. return MemberImpl.field(fi.getClassName(cpg),
  501. (fi.opcode == Constants.GETSTATIC || fi.opcode == Constants.PUTSTATIC) ? Modifier.STATIC : 0, fi.getName(cpg),
  502. fi.getSignature(cpg));
  503. }
  504. public Member makeJoinPointSignatureFromMethod(LazyMethodGen mg, MemberKind kind) {
  505. Member ret = mg.getMemberView();
  506. if (ret == null) {
  507. int mods = mg.getAccessFlags();
  508. if (mg.getEnclosingClass().isInterface()) {
  509. mods |= Modifier.INTERFACE;
  510. }
  511. return new ResolvedMemberImpl(kind, UnresolvedType.forName(mg.getClassName()), mods, fromBcel(mg.getReturnType()),
  512. mg.getName(), fromBcel(mg.getArgumentTypes()));
  513. } else {
  514. return ret;
  515. }
  516. }
  517. public Member makeJoinPointSignatureForMonitorEnter(LazyClassGen cg, InstructionHandle h) {
  518. return MemberImpl.monitorEnter();
  519. }
  520. public Member makeJoinPointSignatureForMonitorExit(LazyClassGen cg, InstructionHandle h) {
  521. return MemberImpl.monitorExit();
  522. }
  523. public Member makeJoinPointSignatureForArrayConstruction(LazyClassGen cg, InstructionHandle handle) {
  524. Instruction i = handle.getInstruction();
  525. ConstantPool cpg = cg.getConstantPool();
  526. Member retval = null;
  527. if (i.opcode == Constants.ANEWARRAY) {
  528. // ANEWARRAY arrayInstruction = (ANEWARRAY)i;
  529. Type ot = i.getType(cpg);
  530. UnresolvedType ut = fromBcel(ot);
  531. ut = UnresolvedType.makeArray(ut, 1);
  532. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", new ResolvedType[] { INT });
  533. } else if (i instanceof MULTIANEWARRAY) {
  534. MULTIANEWARRAY arrayInstruction = (MULTIANEWARRAY) i;
  535. UnresolvedType ut = null;
  536. short dimensions = arrayInstruction.getDimensions();
  537. ObjectType ot = arrayInstruction.getLoadClassType(cpg);
  538. if (ot != null) {
  539. ut = fromBcel(ot);
  540. ut = UnresolvedType.makeArray(ut, dimensions);
  541. } else {
  542. Type t = arrayInstruction.getType(cpg);
  543. ut = fromBcel(t);
  544. }
  545. ResolvedType[] parms = new ResolvedType[dimensions];
  546. for (int ii = 0; ii < dimensions; ii++) {
  547. parms[ii] = INT;
  548. }
  549. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", parms);
  550. } else if (i.opcode == Constants.NEWARRAY) {
  551. // NEWARRAY arrayInstruction = (NEWARRAY)i;
  552. Type ot = i.getType();
  553. UnresolvedType ut = fromBcel(ot);
  554. retval = MemberImpl.method(ut, Modifier.PUBLIC, UnresolvedType.VOID, "<init>", new ResolvedType[] { INT });
  555. } else {
  556. throw new BCException("Cannot create array construction signature for this non-array instruction:" + i);
  557. }
  558. return retval;
  559. }
  560. public Member makeJoinPointSignatureForMethodInvocation(LazyClassGen cg, InvokeInstruction ii) {
  561. ConstantPool cpg = cg.getConstantPool();
  562. String name = ii.getName(cpg);
  563. String declaring = ii.getClassName(cpg);
  564. UnresolvedType declaringType = null;
  565. String signature = ii.getSignature(cpg);
  566. // 307147
  567. if (name.startsWith("ajc$privMethod$")) {
  568. // The invoke is on a privileged accessor. These may be created for different
  569. // kinds of target, not necessarily just private methods. In bug 307147 it is
  570. // for a private method. This code is identifying the particular case in 307147
  571. try {
  572. declaringType = UnresolvedType.forName(declaring);
  573. String typeNameAsFoundInAccessorName = declaringType.getName().replace('.', '_');
  574. int indexInAccessorName = name.lastIndexOf(typeNameAsFoundInAccessorName);
  575. if (indexInAccessorName != -1) {
  576. String methodName = name.substring(indexInAccessorName+typeNameAsFoundInAccessorName.length()+1);
  577. ResolvedType resolvedDeclaringType = declaringType.resolve(this);
  578. ResolvedMember[] methods = resolvedDeclaringType.getDeclaredMethods();
  579. for (ResolvedMember method: methods) {
  580. if (method.getName().equals(methodName) && method.getSignature().equals(signature) && Modifier.isPrivate(method.getModifiers())) {
  581. return method;
  582. }
  583. }
  584. }
  585. } catch (Exception e) {
  586. // Remove this once confident above code isn't having unexpected side effects
  587. // Added 1.8.7
  588. e.printStackTrace();
  589. }
  590. }
  591. int modifier = (ii instanceof INVOKEINTERFACE) ? Modifier.INTERFACE
  592. : (ii.opcode == Constants.INVOKESTATIC) ? Modifier.STATIC : (ii.opcode == Constants.INVOKESPECIAL && !name
  593. .equals("<init>")) ? Modifier.PRIVATE : 0;
  594. // in Java 1.4 and after, static method call of super class within
  595. // subclass method appears
  596. // as declared by the subclass in the bytecode - but they are not
  597. // see #104212
  598. if (ii.opcode == Constants.INVOKESTATIC) {
  599. ResolvedType appearsDeclaredBy = resolve(declaring);
  600. // look for the method there
  601. for (Iterator<ResolvedMember> iterator = appearsDeclaredBy.getMethods(true, true); iterator.hasNext();) {
  602. ResolvedMember method = iterator.next();
  603. if (Modifier.isStatic(method.getModifiers())) {
  604. if (name.equals(method.getName()) && signature.equals(method.getSignature())) {
  605. // we found it
  606. declaringType = method.getDeclaringType();
  607. break;
  608. }
  609. }
  610. }
  611. }
  612. if (declaringType == null) {
  613. if (declaring.charAt(0) == '[') {
  614. declaringType = UnresolvedType.forSignature(declaring);
  615. } else {
  616. declaringType = UnresolvedType.forName(declaring);
  617. }
  618. }
  619. return MemberImpl.method(declaringType, modifier, name, signature);
  620. }
  621. @Override
  622. public String toString() {
  623. StringBuffer buf = new StringBuffer();
  624. buf.append("BcelWorld(");
  625. // buf.append(shadowMungerMap);
  626. buf.append(")");
  627. return buf.toString();
  628. }
  629. /**
  630. * Retrieve a bcel delegate for an aspect - this will return NULL if the delegate is an EclipseSourceType and not a
  631. * BcelObjectType - this happens quite often when incrementally compiling.
  632. */
  633. public static BcelObjectType getBcelObjectType(ResolvedType concreteAspect) {
  634. if (concreteAspect == null) {
  635. return null;
  636. }
  637. if (!(concreteAspect instanceof ReferenceType)) { // Might be Missing
  638. return null;
  639. }
  640. ReferenceTypeDelegate rtDelegate = ((ReferenceType) concreteAspect).getDelegate();
  641. if (rtDelegate instanceof BcelObjectType) {
  642. return (BcelObjectType) rtDelegate;
  643. } else {
  644. return null;
  645. }
  646. }
  647. public void tidyUp() {
  648. // At end of compile, close any open files so deletion of those archives
  649. // is possible
  650. classPath.closeArchives();
  651. typeMap.report();
  652. typeMap.demote(true);
  653. // ResolvedType.resetPrimitives();
  654. }
  655. // / The repository interface methods
  656. public JavaClass findClass(String className) {
  657. return lookupJavaClass(classPath, className);
  658. }
  659. public JavaClass loadClass(String className) throws ClassNotFoundException {
  660. return lookupJavaClass(classPath, className);
  661. }
  662. public void storeClass(JavaClass clazz) {
  663. // doesn't need to do anything
  664. }
  665. public void removeClass(JavaClass clazz) {
  666. throw new RuntimeException("Not implemented");
  667. }
  668. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  669. throw new RuntimeException("Not implemented");
  670. }
  671. public void clear() {
  672. delegate.clear();
  673. // throw new RuntimeException("Not implemented");
  674. }
  675. /**
  676. * 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
  677. * this method is intended to undo that... see pr85132
  678. */
  679. @Override
  680. public void validateType(UnresolvedType type) {
  681. ResolvedType result = typeMap.get(type.getSignature());
  682. if (result == null) {
  683. return; // We haven't heard of it yet
  684. }
  685. if (!result.isExposedToWeaver()) {
  686. return; // cant need resetting
  687. }
  688. result.ensureConsistent();
  689. // If we want to rebuild it 'from scratch' then:
  690. // ClassParser cp = new ClassParser(new
  691. // ByteArrayInputStream(newbytes),new String(cs));
  692. // try {
  693. // rt.setDelegate(makeBcelObjectType(rt,cp.parse(),true));
  694. // } catch (ClassFormatException e) {
  695. // e.printStackTrace();
  696. // } catch (IOException e) {
  697. // e.printStackTrace();
  698. // }
  699. }
  700. /**
  701. * Apply a single declare parents - return true if we change the type
  702. */
  703. private boolean applyDeclareParents(DeclareParents p, ResolvedType onType) {
  704. boolean didSomething = false;
  705. List<ResolvedType> newParents = p.findMatchingNewParents(onType, true);
  706. if (!newParents.isEmpty()) {
  707. didSomething = true;
  708. BcelObjectType classType = BcelWorld.getBcelObjectType(onType);
  709. // System.err.println("need to do declare parents for: " + onType);
  710. for (ResolvedType newParent : newParents) {
  711. // We set it here so that the imminent matching for ITDs can
  712. // succeed - we still haven't done the necessary changes to the class file
  713. // itself (like transform super calls) - that is done in
  714. // BcelTypeMunger.mungeNewParent()
  715. // classType.addParent(newParent);
  716. onType.addParent(newParent);
  717. ResolvedTypeMunger newParentMunger = new NewParentTypeMunger(newParent, p.getDeclaringType());
  718. newParentMunger.setSourceLocation(p.getSourceLocation());
  719. onType.addInterTypeMunger(new BcelTypeMunger(newParentMunger, getCrosscuttingMembersSet()
  720. .findAspectDeclaringParents(p)), false);
  721. }
  722. }
  723. return didSomething;
  724. }
  725. /**
  726. * Apply a declare @type - return true if we change the type
  727. */
  728. private boolean applyDeclareAtType(DeclareAnnotation decA, ResolvedType onType, boolean reportProblems) {
  729. boolean didSomething = false;
  730. if (decA.matches(onType)) {
  731. if (onType.hasAnnotation(decA.getAnnotation().getType())) {
  732. // already has it
  733. return false;
  734. }
  735. AnnotationAJ annoX = decA.getAnnotation();
  736. // check the annotation is suitable for the target
  737. boolean isOK = checkTargetOK(decA, onType, annoX);
  738. if (isOK) {
  739. didSomething = true;
  740. ResolvedTypeMunger newAnnotationTM = new AnnotationOnTypeMunger(annoX);
  741. newAnnotationTM.setSourceLocation(decA.getSourceLocation());
  742. onType.addInterTypeMunger(new BcelTypeMunger(newAnnotationTM, decA.getAspect().resolve(this)), false);
  743. decA.copyAnnotationTo(onType);
  744. }
  745. }
  746. return didSomething;
  747. }
  748. /**
  749. * Apply the specified declare @field construct to any matching fields in the specified type.
  750. * @param deca the declare annotation targeting fields
  751. * @param type the type to check for members matching the declare annotation
  752. * @return true if something matched and the type was modified
  753. */
  754. private boolean applyDeclareAtField(DeclareAnnotation deca, ResolvedType type) {
  755. boolean changedType = false;
  756. ResolvedMember[] fields = type.getDeclaredFields();
  757. for (ResolvedMember field: fields) {
  758. if (deca.matches(field, this)) {
  759. AnnotationAJ anno = deca.getAnnotation();
  760. if (!field.hasAnnotation(anno.getType())) {
  761. field.addAnnotation(anno);
  762. changedType=true;
  763. }
  764. }
  765. }
  766. return changedType;
  767. }
  768. /**
  769. * Checks for an @target() on the annotation and if found ensures it allows the annotation to be attached to the target type
  770. * that matched.
  771. */
  772. private boolean checkTargetOK(DeclareAnnotation decA, ResolvedType onType, AnnotationAJ annoX) {
  773. if (annoX.specifiesTarget()) {
  774. if ((onType.isAnnotation() && !annoX.allowedOnAnnotationType()) || (!annoX.allowedOnRegularType())) {
  775. return false;
  776. }
  777. }
  778. return true;
  779. }
  780. // Hmmm - very similar to the code in BcelWeaver.weaveParentTypeMungers -
  781. // this code
  782. // doesn't need to produce errors/warnings though as it won't really be
  783. // weaving.
  784. protected void weaveInterTypeDeclarations(ResolvedType onType) {
  785. List<DeclareParents> declareParentsList = getCrosscuttingMembersSet().getDeclareParents();
  786. if (onType.isRawType()) {
  787. onType = onType.getGenericType();
  788. }
  789. onType.clearInterTypeMungers();
  790. List<DeclareParents> decpToRepeat = new ArrayList<DeclareParents>();
  791. boolean aParentChangeOccurred = false;
  792. boolean anAnnotationChangeOccurred = false;
  793. // First pass - apply all decp mungers
  794. for (Iterator<DeclareParents> i = declareParentsList.iterator(); i.hasNext();) {
  795. DeclareParents decp = i.next();
  796. boolean typeChanged = applyDeclareParents(decp, onType);
  797. if (typeChanged) {
  798. aParentChangeOccurred = true;
  799. } else { // Perhaps it would have matched if a 'dec @type' had
  800. // modified the type
  801. if (!decp.getChild().isStarAnnotation()) {
  802. decpToRepeat.add(decp);
  803. }
  804. }
  805. }
  806. // Still first pass - apply all dec @type mungers
  807. for (DeclareAnnotation decA : getCrosscuttingMembersSet().getDeclareAnnotationOnTypes()) {
  808. boolean typeChanged = applyDeclareAtType(decA, onType, true);
  809. if (typeChanged) {
  810. anAnnotationChangeOccurred = true;
  811. }
  812. }
  813. // apply declare @field
  814. for (DeclareAnnotation deca: getCrosscuttingMembersSet().getDeclareAnnotationOnFields()) {
  815. if (applyDeclareAtField(deca,onType)) {
  816. anAnnotationChangeOccurred = true;
  817. }
  818. }
  819. while ((aParentChangeOccurred || anAnnotationChangeOccurred) && !decpToRepeat.isEmpty()) {
  820. anAnnotationChangeOccurred = aParentChangeOccurred = false;
  821. List<DeclareParents> decpToRepeatNextTime = new ArrayList<DeclareParents>();
  822. for (DeclareParents decp: decpToRepeat) {
  823. if (applyDeclareParents(decp, onType)) {
  824. aParentChangeOccurred = true;
  825. } else {
  826. decpToRepeatNextTime.add(decp);
  827. }
  828. }
  829. for (DeclareAnnotation deca: getCrosscuttingMembersSet().getDeclareAnnotationOnTypes()) {
  830. if (applyDeclareAtType(deca, onType, false)) {
  831. anAnnotationChangeOccurred = true;
  832. }
  833. }
  834. for (DeclareAnnotation deca: getCrosscuttingMembersSet().getDeclareAnnotationOnFields()) {
  835. if (applyDeclareAtField(deca, onType)) {
  836. anAnnotationChangeOccurred = true;
  837. }
  838. }
  839. decpToRepeat = decpToRepeatNextTime;
  840. }
  841. }
  842. @Override
  843. public IWeavingSupport getWeavingSupport() {
  844. return bcelWeavingSupport;
  845. }
  846. @Override
  847. public void reportCheckerMatch(Checker checker, Shadow shadow) {
  848. IMessage iMessage = new Message(checker.getMessage(shadow), shadow.toString(), checker.isError() ? IMessage.ERROR
  849. : IMessage.WARNING, shadow.getSourceLocation(), null, new ISourceLocation[] { checker.getSourceLocation() }, true,
  850. 0, -1, -1);
  851. getMessageHandler().handleMessage(iMessage);
  852. if (getCrossReferenceHandler() != null) {
  853. getCrossReferenceHandler()
  854. .addCrossReference(
  855. checker.getSourceLocation(),
  856. shadow.getSourceLocation(),
  857. (checker.isError() ? IRelationship.Kind.DECLARE_ERROR.getName() : IRelationship.Kind.DECLARE_WARNING
  858. .getName()), false);
  859. }
  860. if (getModel() != null) {
  861. AsmRelationshipProvider.addDeclareErrorOrWarningRelationship(getModelAsAsmManager(), shadow, checker);
  862. }
  863. }
  864. public AsmManager getModelAsAsmManager() {
  865. return (AsmManager) getModel(); // For now... always an AsmManager in a bcel environment
  866. }
  867. void raiseError(String message) {
  868. getMessageHandler().handleMessage(MessageUtil.error(message));
  869. }
  870. /**
  871. * These are aop.xml files that can be used to alter the aspects that actually apply from those passed in - and also their scope
  872. * of application to other files in the system.
  873. *
  874. * @param xmlFiles list of File objects representing any aop.xml files passed in to configure the build process
  875. */
  876. public void setXmlFiles(List<File> xmlFiles) {
  877. if (!isXmlConfiguredWorld && !xmlFiles.isEmpty()) {
  878. raiseError("xml configuration files only supported by the compiler when -xmlConfigured option specified");
  879. return;
  880. }
  881. if (!xmlFiles.isEmpty()) {
  882. xmlConfiguration = new WeavingXmlConfig(this, WeavingXmlConfig.MODE_COMPILE);
  883. }
  884. for (File xmlfile : xmlFiles) {
  885. try {
  886. Definition d = DocumentParser.parse(xmlfile.toURI().toURL());
  887. xmlConfiguration.add(d);
  888. } catch (MalformedURLException e) {
  889. raiseError("Unexpected problem processing XML config file '" + xmlfile.getName() + "' :" + e.getMessage());
  890. } catch (Exception e) {
  891. raiseError("Unexpected problem processing XML config file '" + xmlfile.getName() + "' :" + e.getMessage());
  892. }
  893. }
  894. }
  895. /**
  896. * Add a scoped aspects where the scoping was defined in an aop.xml file and this world is being used in a LTW configuration
  897. */
  898. public void addScopedAspect(String name, String scope) {
  899. this.isXmlConfiguredWorld = true;
  900. if (xmlConfiguration == null) {
  901. xmlConfiguration = new WeavingXmlConfig(this, WeavingXmlConfig.MODE_LTW);
  902. }
  903. xmlConfiguration.addScopedAspect(name, scope);
  904. }
  905. public void setXmlConfigured(boolean b) {
  906. this.isXmlConfiguredWorld = b;
  907. }
  908. @Override
  909. public boolean isXmlConfigured() {
  910. return isXmlConfiguredWorld && xmlConfiguration != null;
  911. }
  912. public WeavingXmlConfig getXmlConfiguration() {
  913. return xmlConfiguration;
  914. }
  915. @Override
  916. public boolean isAspectIncluded(ResolvedType aspectType) {
  917. if (!isXmlConfigured()) {
  918. return true;
  919. }
  920. return xmlConfiguration.specifiesInclusionOfAspect(aspectType.getName());
  921. }
  922. @Override
  923. public TypePattern getAspectScope(ResolvedType declaringType) {
  924. return xmlConfiguration.getScopeFor(declaringType.getName());
  925. }
  926. @Override
  927. public boolean hasUnsatisfiedDependency(ResolvedType aspectType) {
  928. String aspectName = aspectType.getName();
  929. if (aspectType.hasAnnotations()) {
  930. AnnotationAJ[] annos = aspectType.getAnnotations();
  931. for (AnnotationAJ anno: annos) {
  932. if (anno.getTypeName().equals("org.aspectj.lang.annotation.RequiredTypes")) {
  933. String values = anno.getStringFormOfValue("value"); // Example: "[A,org.foo.Bar]"
  934. if (values != null && values.length() > 2) {
  935. values = values.substring(1,values.length()-1);
  936. StringTokenizer tokenizer = new StringTokenizer(values,",");
  937. boolean anythingMissing = false;
  938. while (tokenizer.hasMoreElements()) {
  939. String requiredTypeName = tokenizer.nextToken();
  940. ResolvedType rt = resolve(UnresolvedType.forName(requiredTypeName));
  941. if (rt.isMissing()) {
  942. if (!getMessageHandler().isIgnoring(IMessage.INFO)) {
  943. getMessageHandler().handleMessage(
  944. MessageUtil.info("deactivating aspect '" + aspectName + "' as it requires type '"
  945. + requiredTypeName + "' which cannot be found on the classpath"));
  946. }
  947. anythingMissing = true;
  948. if (aspectRequiredTypes == null) {
  949. aspectRequiredTypes = new HashMap<String,String>();
  950. }
  951. // Record that it has an invalid type reference
  952. aspectRequiredTypes.put(aspectName,requiredTypeName);
  953. }
  954. }
  955. if (anythingMissing) {
  956. return true;
  957. }
  958. else {
  959. return false;
  960. }
  961. }
  962. else {
  963. // no value specified for annotation
  964. return false;
  965. }
  966. }
  967. }
  968. }
  969. if (aspectRequiredTypes == null) {
  970. // no aspects require anything, so there can be no unsatisfied dependencies
  971. return false;
  972. }
  973. if (!aspectRequiredTypesProcessed.contains(aspectName)) {
  974. String requiredTypeName = aspectRequiredTypes.get(aspectName);
  975. if (requiredTypeName==null) {
  976. aspectRequiredTypesProcessed.add(aspectName);
  977. return false;
  978. } else {
  979. ResolvedType rt = resolve(UnresolvedType.forName(requiredTypeName));
  980. if (!rt.isMissing()) {
  981. aspectRequiredTypesProcessed.add(aspectName);
  982. aspectRequiredTypes.remove(aspectName);
  983. return false;
  984. } else {
  985. if (!getMessageHandler().isIgnoring(IMessage.INFO)) {
  986. getMessageHandler().handleMessage(
  987. MessageUtil.info("deactivating aspect '" + aspectName + "' as it requires type '"
  988. + requiredTypeName + "' which cannot be found on the classpath"));
  989. }
  990. aspectRequiredTypesProcessed.add(aspectName);
  991. return true;
  992. }
  993. }
  994. }
  995. return aspectRequiredTypes.containsKey(aspectName);
  996. }
  997. private List<String> aspectRequiredTypesProcessed = new ArrayList<String>();
  998. private Map<String, String> aspectRequiredTypes = null;
  999. public void addAspectRequires(String aspectClassName, String requiredType) {
  1000. if (aspectRequiredTypes == null) {
  1001. aspectRequiredTypes = new HashMap<String, String>();
  1002. }
  1003. aspectRequiredTypes.put(aspectClassName,requiredType);
  1004. }
  1005. /**
  1006. * A WeavingXmlConfig is initially a collection of definitions from XML files - once the world is ready and weaving is running
  1007. * it will initialize and transform those definitions into an optimized set of values (eg. resolve type patterns and string
  1008. * names to real entities). It can then answer questions quickly: (1) is this aspect included in the weaving? (2) Is there a
  1009. * scope specified for this aspect and does it include type X?
  1010. *
  1011. */
  1012. static class WeavingXmlConfig {
  1013. final static int MODE_COMPILE = 1;
  1014. final static int MODE_LTW = 2;
  1015. private int mode;
  1016. private boolean initialized = false; // Lazily done
  1017. private List<Definition> definitions = new ArrayList<Definition>();
  1018. private List<String> resolvedIncludedAspects = new ArrayList<String>();
  1019. private Map<String, TypePattern> scopes = new HashMap<String, TypePattern>();
  1020. // these are not set for LTW mode (exclusion of these fast match patterns is handled before the weaver/world are used)
  1021. private List<String> includedFastMatchPatterns = Collections.emptyList();
  1022. private List<TypePattern> includedPatterns = Collections.emptyList();
  1023. private List<String> excludedFastMatchPatterns = Collections.emptyList();
  1024. private List<TypePattern> excludedPatterns = Collections.emptyList();
  1025. private BcelWorld world;
  1026. public WeavingXmlConfig(BcelWorld bcelWorld, int mode) {
  1027. this.world = bcelWorld;
  1028. this.mode = mode;
  1029. }
  1030. public void add(Definition d) {
  1031. definitions.add(d);
  1032. }
  1033. public void addScopedAspect(String aspectName, String scope) {
  1034. ensureInitialized();
  1035. resolvedIncludedAspects.add(aspectName);
  1036. try {
  1037. TypePattern scopePattern = new PatternParser(scope).parseTypePattern();
  1038. scopePattern.resolve(world);
  1039. scopes.put(aspectName, scopePattern);
  1040. if (!world.getMessageHandler().isIgnoring(IMessage.INFO)) {
  1041. world.getMessageHandler().handleMessage(
  1042. MessageUtil.info("Aspect '" + aspectName + "' is scoped to apply against types matching pattern '"
  1043. + scopePattern.toString() + "'"));
  1044. }
  1045. } catch (Exception e) {
  1046. world.getMessageHandler().handleMessage(
  1047. MessageUtil.error("Unable to parse scope as type pattern. Scope was '" + scope + "': " + e.getMessage()));
  1048. }
  1049. }
  1050. public void ensureInitialized() {
  1051. if (!initialized) {
  1052. try {
  1053. resolvedIncludedAspects = new ArrayList<String>();
  1054. // Process the definitions into something more optimal
  1055. for (Definition definition : definitions) {
  1056. List<String> aspectNames = definition.getAspectClassNames();
  1057. for (String name : aspectNames) {
  1058. resolvedIncludedAspects.add(name);
  1059. // TODO check for existence?
  1060. // ResolvedType resolvedAspect = resolve(UnresolvedType.forName(name));
  1061. // if (resolvedAspect.isMissing()) {
  1062. // // ERROR
  1063. // } else {
  1064. // resolvedIncludedAspects.add(resolvedAspect);
  1065. // }
  1066. String scope = definition.getScopeForAspect(name);
  1067. if (scope != null) {
  1068. // Resolve the type pattern
  1069. try {
  1070. TypePattern scopePattern = new PatternParser(scope).parseTypePattern();
  1071. scopePattern.resolve(world);
  1072. scopes.put(name, scopePattern);
  1073. if (!world.getMessageHandler().isIgnoring(IMessage.INFO)) {
  1074. world.getMessageHandler().handleMessage(
  1075. MessageUtil.info("Aspect '" + name
  1076. + "' is scoped to apply against types matching pattern '"
  1077. + scopePattern.toString() + "'"));
  1078. }
  1079. } catch (Exception e) {
  1080. // TODO definitions should remember which file they came from, for inclusion in this message
  1081. world.getMessageHandler().handleMessage(
  1082. MessageUtil.error("Unable to parse scope as type pattern. Scope was '" + scope + "': "
  1083. + e.getMessage()));
  1084. }
  1085. }
  1086. }
  1087. try {
  1088. List<String> includePatterns = definition.getIncludePatterns();
  1089. if (includePatterns.size() > 0) {
  1090. includedPatterns = new ArrayList<TypePattern>();
  1091. includedFastMatchPatterns = new ArrayList<String>();
  1092. }
  1093. for (String includePattern : includePatterns) {
  1094. if (includePattern.endsWith("..*")) {
  1095. // from 'blah.blah.blah..*' leave the 'blah.blah.blah.'
  1096. includedFastMatchPatterns.add(includePattern.substring(0, includePattern.length() - 2));
  1097. } else {
  1098. TypePattern includedPattern = new PatternParser(includePattern).parseTypePattern();
  1099. includedPatterns.add(includedPattern);
  1100. }
  1101. }
  1102. List<String> excludePatterns = definition.getExcludePatterns();
  1103. if (excludePatterns.size() > 0) {
  1104. excludedPatterns = new ArrayList<TypePattern>();
  1105. excludedFastMatchPatterns = new ArrayList<String>();
  1106. }
  1107. for (String excludePattern : excludePatterns) {
  1108. if (excludePattern.endsWith("..*")) {
  1109. // from 'blah.blah.blah..*' leave the 'blah.blah.blah.'
  1110. excludedFastMatchPatterns.add(excludePattern.substring(0, excludePattern.length() - 2));
  1111. } else {
  1112. TypePattern excludedPattern = new PatternParser(excludePattern).parseTypePattern();
  1113. excludedPatterns.add(excludedPattern);
  1114. }
  1115. }
  1116. } catch (ParserException pe) {
  1117. // TODO definitions should remember which file they came from, for inclusion in this message
  1118. world.getMessageHandler().handleMessage(
  1119. MessageUtil.error("Unable to parse type pattern: " + pe.getMessage()));
  1120. }
  1121. }
  1122. } finally {
  1123. initialized = true;
  1124. }
  1125. }
  1126. }
  1127. public boolean specifiesInclusionOfAspect(String name) {
  1128. ensureInitialized();
  1129. return resolvedIncludedAspects.contains(name);
  1130. }
  1131. public TypePattern getScopeFor(String name) {
  1132. return scopes.get(name);
  1133. }
  1134. // Can't quite follow the same rules for exclusion as used for loadtime weaving:
  1135. // "The set of types to be woven are those types matched by at least one weaver include element and not matched by any
  1136. // weaver
  1137. // exclude element. If there are no weaver include statements then all non-excluded types are included."
  1138. // Since if the weaver is seeing it during this kind of build, the type is implicitly included. So all we should check
  1139. // for is exclusion
  1140. public boolean excludesType(ResolvedType type) {
  1141. if (mode == MODE_LTW) {
  1142. return false;
  1143. }
  1144. String typename = type.getName();
  1145. boolean excluded = false;
  1146. for (String excludedPattern : excludedFastMatchPatterns) {
  1147. if (typename.startsWith(excludedPattern)) {
  1148. excluded = true;
  1149. break;
  1150. }
  1151. }
  1152. if (!excluded) {
  1153. for (TypePattern excludedPattern : excludedPatterns) {
  1154. if (excludedPattern.matchesStatically(type)) {
  1155. excluded = true;
  1156. break;
  1157. }
  1158. }
  1159. }
  1160. return excluded;
  1161. }
  1162. }
  1163. public TypeMap getTypeMap() {
  1164. return typeMap;
  1165. }
  1166. public boolean isLoadtimeWeaving() {
  1167. return false;
  1168. }
  1169. public void addTypeDelegateResolver(TypeDelegateResolver typeDelegateResolver) {
  1170. if (typeDelegateResolvers == null) {
  1171. typeDelegateResolvers = new ArrayList<TypeDelegateResolver>();
  1172. }
  1173. typeDelegateResolvers.add(typeDelegateResolver);
  1174. }
  1175. public void classWriteEvent(char[][] compoundName) {
  1176. typeMap.classWriteEvent(new String(CharOperation.concatWith(compoundName, '.')));
  1177. }
  1178. /**
  1179. * Force demote a type.
  1180. */
  1181. public void demote(ResolvedType type) {
  1182. typeMap.demote(type);
  1183. }
  1184. }