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

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