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

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