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.3KB

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