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.

LTWWorld.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  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. * Ron Bodkin Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.ltw;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.aspectj.apache.bcel.classfile.JavaClass;
  20. import org.aspectj.bridge.IMessageHandler;
  21. import org.aspectj.weaver.Dump.IVisitor;
  22. import org.aspectj.weaver.ICrossReferenceHandler;
  23. import org.aspectj.weaver.ReferenceType;
  24. import org.aspectj.weaver.ReferenceTypeDelegate;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.bcel.BcelWorld;
  27. import org.aspectj.weaver.loadtime.IWeavingContext;
  28. import org.aspectj.weaver.reflect.AnnotationFinder;
  29. import org.aspectj.weaver.reflect.IReflectionWorld;
  30. import org.aspectj.weaver.reflect.ReflectionBasedReferenceTypeDelegateFactory;
  31. import org.aspectj.weaver.reflect.ReflectionWorld;
  32. /**
  33. * @author adrian
  34. * @author Ron Bodkin
  35. *
  36. * For use in LT weaving
  37. *
  38. * Backed by both a BcelWorld and a ReflectionWorld
  39. *
  40. * Needs a callback when a woven class is defined This is the trigger for us to ditch the class from Bcel and cache it in
  41. * the reflective world instead.
  42. *
  43. * Create by passing in a classloader, message handler
  44. */
  45. public class LTWWorld extends BcelWorld implements IReflectionWorld {
  46. private AnnotationFinder annotationFinder;
  47. private IWeavingContext weavingContext;
  48. private String classLoaderString;
  49. private String classLoaderParentString;
  50. protected final static Class concurrentMapClass;
  51. private static final boolean ShareBootstrapTypes = false;
  52. protected static Map/* <String, WeakReference<ReflectionBasedReferenceTypeDelegate>> */bootstrapTypes;
  53. static {
  54. if (ShareBootstrapTypes) {
  55. concurrentMapClass = makeConcurrentMapClass();
  56. bootstrapTypes = makeConcurrentMap();
  57. } else {
  58. concurrentMapClass = null;
  59. }
  60. }
  61. /**
  62. * Build a World from a ClassLoader, for LTW support
  63. */
  64. public LTWWorld(ClassLoader loader, IWeavingContext weavingContext, IMessageHandler handler, ICrossReferenceHandler xrefHandler) {
  65. super(loader, handler, xrefHandler);
  66. this.weavingContext = weavingContext;
  67. try {
  68. classLoaderString = loader.toString();
  69. } catch (Throwable t) {
  70. // Possibly some state in the loader isn't initialized but is used in the toString()
  71. classLoaderString = loader.getClass().getName()+":"+Integer.toString(System.identityHashCode(loader));
  72. }
  73. classLoaderParentString = (loader.getParent() == null ? "<NullParent>" : loader.getParent().toString());
  74. setBehaveInJava5Way(true);
  75. annotationFinder = ReflectionWorld.makeAnnotationFinderIfAny(loader, this);
  76. }
  77. public ClassLoader getClassLoader() {
  78. return weavingContext.getClassLoader();
  79. }
  80. // TEST
  81. // this is probably easier: just mark anything loaded while loading aspects as not
  82. // expendible... it also fixes a possible bug whereby non-rewoven aspects are deemed expendible
  83. // <exclude within="org.foo.aspects..*"/>
  84. // protected boolean isExpendable(ResolvedType type) {
  85. // return ((type != null) && !loadingAspects && !type.isAspect() && (!type
  86. // .isPrimitiveType()));
  87. // }
  88. /**
  89. * Override
  90. */
  91. @Override
  92. protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) {
  93. // use reflection delegates for all bootstrap types
  94. ReferenceTypeDelegate bootstrapLoaderDelegate = resolveIfBootstrapDelegate(ty);
  95. if (bootstrapLoaderDelegate != null) {
  96. return bootstrapLoaderDelegate;
  97. }
  98. return super.resolveDelegate(ty);
  99. }
  100. protected ReferenceTypeDelegate resolveIfBootstrapDelegate(ReferenceType ty) {
  101. // first check for anything available in the bootstrap loader: these types are just defined from that without allowing
  102. // nondelegation
  103. // if (!ShareBootstrapTypes) return null;
  104. // String name = ty.getName();
  105. // Reference bootRef = (Reference) bootstrapTypes.get(name);
  106. // if (bootRef != null) {
  107. // ReferenceTypeDelegate rtd = (ReferenceTypeDelegate) bootRef.get();
  108. // if (rtd != null) {
  109. // return rtd;
  110. // }
  111. // }
  112. //
  113. // char fc = name.charAt(0);
  114. // if (fc == 'j' || fc == 'c' || fc == 'o' || fc == 's') { // cheaper than imminent string startsWith tests
  115. // if (name.startsWith("java") || name.startsWith("com.sun.") || name.startsWith("org.w3c") ||
  116. // name.startsWith("sun.") || name.startsWith("org.omg")) {
  117. // ReferenceTypeDelegate bootstrapLoaderDelegate = resolveReflectionTypeDelegate(ty, null);
  118. // if (bootstrapLoaderDelegate != null) {
  119. // // it's always fine to load these bytes: there's no weaving into them
  120. // // and since the class isn't initialized, all we are doing at this point is loading the bytes
  121. // // processedRefTypes.put(ty, this); // has no effect - and probably too aggressive if we did store
  122. // // these in the type map
  123. //
  124. // // should we share these, like we do the BCEL delegates?
  125. // bootstrapTypes.put(ty.getName(), new WeakReference(bootstrapLoaderDelegate));
  126. // }
  127. // return bootstrapLoaderDelegate;
  128. // }
  129. // }
  130. return null;
  131. }
  132. /**
  133. * Helper method to resolve the delegate from the reflection delegate factory.
  134. */
  135. private ReferenceTypeDelegate resolveReflectionTypeDelegate(ReferenceType ty, ClassLoader resolutionLoader) {
  136. ReferenceTypeDelegate res = ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, resolutionLoader);
  137. return res;
  138. }
  139. /**
  140. * Remove this class from the typeMap. Call back to be made from a publishing class loader The class loader should, ideally,
  141. * make this call on each not yet working
  142. *
  143. * @param clazz
  144. */
  145. public void loadedClass(Class clazz) {
  146. }
  147. private static final long serialVersionUID = 1;
  148. public AnnotationFinder getAnnotationFinder() {
  149. return this.annotationFinder;
  150. }
  151. /*
  152. * (non-Javadoc)
  153. *
  154. * @see org.aspectj.weaver.reflect.IReflectionWorld#resolve(java.lang.Class)
  155. */
  156. public ResolvedType resolve(Class aClass) {
  157. return ReflectionWorld.resolve(this, aClass);
  158. }
  159. private static Map<?, ?> makeConcurrentMap() {
  160. if (concurrentMapClass != null) {
  161. try {
  162. return (Map) concurrentMapClass.getDeclaredConstructor().newInstance();
  163. } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ignored) {}
  164. // fall through if exceptions
  165. }
  166. return Collections.synchronizedMap(new HashMap<>());
  167. }
  168. private static Class<?> makeConcurrentMapClass() {
  169. String betterChoices[] = { "java.util.concurrent.ConcurrentHashMap",
  170. "edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap",
  171. "EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap" };
  172. for (String betterChoice : betterChoices) {
  173. try {
  174. return Class.forName(betterChoice);
  175. } catch (ClassNotFoundException cnfe) {
  176. // try the next one
  177. } catch (SecurityException se) {
  178. // you get one of these if you dare to try to load an undefined class in a
  179. // package starting with java like java.util.concurrent
  180. }
  181. }
  182. return null;
  183. }
  184. @Override
  185. public boolean isRunMinimalMemory() {
  186. if (isRunMinimalMemorySet()) {
  187. return super.isRunMinimalMemory();
  188. }
  189. return false;
  190. }
  191. // One type is completed at a time, if multiple need doing then they
  192. // are queued up
  193. private boolean typeCompletionInProgress = false;
  194. private List<ResolvedType> typesForCompletion = new ArrayList<>();
  195. @Override
  196. protected void completeBinaryType(ResolvedType ret) {
  197. if (isLocallyDefined(ret.getName())) {
  198. if (typeCompletionInProgress) {
  199. typesForCompletion.add(ret);
  200. } else {
  201. try {
  202. typeCompletionInProgress = true;
  203. completeHierarchyForType(ret);
  204. } finally {
  205. typeCompletionInProgress = false;
  206. }
  207. while (typesForCompletion.size() != 0) {
  208. ResolvedType rt = (ResolvedType) typesForCompletion.get(0);
  209. completeHierarchyForType(rt);
  210. typesForCompletion.remove(0);
  211. }
  212. }
  213. } else {
  214. if (!ret.needsModifiableDelegate()) {
  215. ret = completeNonLocalType(ret);
  216. }
  217. }
  218. }
  219. private void completeHierarchyForType(ResolvedType ret) {
  220. getLint().typeNotExposedToWeaver.setSuppressed(true);
  221. weaveInterTypeDeclarations(ret);
  222. getLint().typeNotExposedToWeaver.setSuppressed(false);
  223. }
  224. protected boolean needsCompletion() {
  225. return true;
  226. }
  227. @Override
  228. public boolean isLocallyDefined(String classname) {
  229. return weavingContext.isLocallyDefined(classname);
  230. }
  231. protected ResolvedType completeNonLocalType(ResolvedType ret) {
  232. if (ret.isMissing()) {
  233. return ret; // who knows ?!?
  234. }
  235. ResolvedType toResolve = ret;
  236. if (ret.isParameterizedType() || ret.isGenericType()) {
  237. toResolve = toResolve.getGenericType();
  238. }
  239. ReferenceTypeDelegate rtd = resolveReflectionTypeDelegate((ReferenceType) toResolve, getClassLoader());
  240. ((ReferenceType) ret).setDelegate(rtd);
  241. return ret;
  242. }
  243. @Override
  244. public void storeClass(JavaClass clazz) {
  245. ensureRepositorySetup();
  246. delegate.storeClass(clazz);
  247. }
  248. @Override
  249. public void accept(IVisitor visitor) {
  250. visitor.visitObject("Class loader:");
  251. visitor.visitObject(classLoaderString);
  252. visitor.visitObject("Class loader parent:");
  253. visitor.visitObject(classLoaderParentString);
  254. super.accept(visitor);
  255. }
  256. public boolean isLoadtimeWeaving() {
  257. return true;
  258. }
  259. }