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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.util.LangUtil;
  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 (NullPointerException npe) {
  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(LangUtil.is15VMOrGreater());
  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.newInstance();
  163. } catch (InstantiationException ie) {
  164. } catch (IllegalAccessException iae) {
  165. }
  166. // fall through if exceptions
  167. }
  168. return Collections.synchronizedMap(new HashMap());
  169. }
  170. private static Class makeConcurrentMapClass() {
  171. String betterChoices[] = { "java.util.concurrent.ConcurrentHashMap",
  172. "edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap",
  173. "EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap" };
  174. for (int i = 0; i < betterChoices.length; i++) {
  175. try {
  176. return Class.forName(betterChoices[i]);
  177. } catch (ClassNotFoundException cnfe) {
  178. // try the next one
  179. } catch (SecurityException se) {
  180. // you get one of these if you dare to try to load an undefined class in a
  181. // package starting with java like java.util.concurrent
  182. }
  183. }
  184. return null;
  185. }
  186. @Override
  187. public boolean isRunMinimalMemory() {
  188. if (isRunMinimalMemorySet()) {
  189. return super.isRunMinimalMemory();
  190. }
  191. return false;
  192. }
  193. // One type is completed at a time, if multiple need doing then they
  194. // are queued up
  195. private boolean typeCompletionInProgress = false;
  196. private List/* ResolvedType */typesForCompletion = new ArrayList();
  197. @Override
  198. protected void completeBinaryType(ResolvedType ret) {
  199. if (isLocallyDefined(ret.getName())) {
  200. if (typeCompletionInProgress) {
  201. typesForCompletion.add(ret);
  202. } else {
  203. try {
  204. typeCompletionInProgress = true;
  205. completeHierarchyForType(ret);
  206. } finally {
  207. typeCompletionInProgress = false;
  208. }
  209. while (typesForCompletion.size() != 0) {
  210. ResolvedType rt = (ResolvedType) typesForCompletion.get(0);
  211. completeHierarchyForType(rt);
  212. typesForCompletion.remove(0);
  213. }
  214. }
  215. } else {
  216. if (!ret.needsModifiableDelegate()) {
  217. ret = completeNonLocalType(ret);
  218. }
  219. }
  220. }
  221. private void completeHierarchyForType(ResolvedType ret) {
  222. getLint().typeNotExposedToWeaver.setSuppressed(true);
  223. weaveInterTypeDeclarations(ret);
  224. getLint().typeNotExposedToWeaver.setSuppressed(false);
  225. }
  226. protected boolean needsCompletion() {
  227. return true;
  228. }
  229. @Override
  230. public boolean isLocallyDefined(String classname) {
  231. return weavingContext.isLocallyDefined(classname);
  232. }
  233. protected ResolvedType completeNonLocalType(ResolvedType ret) {
  234. if (ret.isMissing()) {
  235. return ret; // who knows ?!?
  236. }
  237. ResolvedType toResolve = ret;
  238. if (ret.isParameterizedType() || ret.isGenericType()) {
  239. toResolve = toResolve.getGenericType();
  240. }
  241. ReferenceTypeDelegate rtd = resolveReflectionTypeDelegate((ReferenceType) toResolve, getClassLoader());
  242. ((ReferenceType) ret).setDelegate(rtd);
  243. return ret;
  244. }
  245. @Override
  246. public void storeClass(JavaClass clazz) {
  247. ensureRepositorySetup();
  248. delegate.storeClass(clazz);
  249. }
  250. @Override
  251. public void accept(IVisitor visitor) {
  252. visitor.visitObject("Class loader:");
  253. visitor.visitObject(classLoaderString);
  254. visitor.visitObject("Class loader parent:");
  255. visitor.visitObject(classLoaderParentString);
  256. super.accept(visitor);
  257. }
  258. public boolean isLoadtimeWeaving() {
  259. return true;
  260. }
  261. }