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.

ShadowMunger.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Collection;
  16. import java.util.HashSet;
  17. import java.util.Map;
  18. import java.util.Set;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.bridge.ISourceLocation;
  21. import org.aspectj.bridge.MessageUtil;
  22. import org.aspectj.bridge.SourceLocation;
  23. import org.aspectj.util.FuzzyBoolean;
  24. import org.aspectj.util.PartialOrder;
  25. import org.aspectj.weaver.patterns.PerClause;
  26. import org.aspectj.weaver.patterns.Pointcut;
  27. import org.aspectj.weaver.patterns.TypePattern;
  28. /**
  29. * For every shadow munger, nothing can be done with it until it is concretized. Then...
  30. *
  31. * (Then we call fast match.)
  32. *
  33. * For every shadow munger, for every shadow, first match is called, then (if match returned true) the shadow munger is specialized
  34. * for the shadow, which may modify state. Then implement is called.
  35. */
  36. public abstract class ShadowMunger implements PartialOrder.PartialComparable, IHasPosition {
  37. public static final ShadowMunger[] NONE = new ShadowMunger[0];
  38. private static int VERSION_1 = 1; // ShadowMunger version for serialization
  39. protected static final int ShadowMungerAdvice = 1;
  40. protected static final int ShadowMungerDeow = 2;
  41. public String handle = null;
  42. private int shadowMungerKind;
  43. protected int start, end;
  44. protected ISourceContext sourceContext;
  45. private ISourceLocation sourceLocation;
  46. private ISourceLocation binarySourceLocation;
  47. private File binaryFile;
  48. private ResolvedType declaringType;
  49. private boolean isBinary;
  50. private boolean checkedIsBinary;
  51. protected Pointcut pointcut;
  52. protected ShadowMunger() {
  53. }
  54. public ShadowMunger(Pointcut pointcut, int start, int end, ISourceContext sourceContext, int shadowMungerKind) {
  55. this.shadowMungerKind = shadowMungerKind;
  56. this.pointcut = pointcut;
  57. this.start = start;
  58. this.end = end;
  59. this.sourceContext = sourceContext;
  60. }
  61. /**
  62. * All overriding methods should call super
  63. */
  64. public boolean match(Shadow shadow, World world) {
  65. if (world.isXmlConfigured() && world.isAspectIncluded(declaringType)) {
  66. TypePattern scoped = world.getAspectScope(declaringType);
  67. if (scoped != null) {
  68. // Check the 'cached' exclusion map
  69. Set<ResolvedType> excludedTypes = world.getExclusionMap().get(declaringType);
  70. ResolvedType type = shadow.getEnclosingType().resolve(world);
  71. if (excludedTypes != null && excludedTypes.contains(type)) {
  72. return false;
  73. }
  74. boolean b = scoped.matches(type, TypePattern.STATIC).alwaysTrue();
  75. if (!b) {
  76. if (!world.getMessageHandler().isIgnoring(IMessage.INFO)) {
  77. world.getMessageHandler().handleMessage(
  78. MessageUtil.info("Type '" + type.getName() + "' not woven by aspect '" + declaringType.getName()
  79. + "' due to scope exclusion in XML definition"));
  80. }
  81. if (excludedTypes == null) {
  82. excludedTypes = new HashSet<ResolvedType>();
  83. excludedTypes.add(type);
  84. world.getExclusionMap().put(declaringType, excludedTypes);
  85. } else {
  86. excludedTypes.add(type);
  87. }
  88. return false;
  89. }
  90. }
  91. }
  92. if (world.areInfoMessagesEnabled() && world.isTimingEnabled()) {
  93. long starttime = System.nanoTime();
  94. FuzzyBoolean isMatch = pointcut.match(shadow);
  95. long endtime = System.nanoTime();
  96. world.record(pointcut, endtime - starttime);
  97. return isMatch.maybeTrue();
  98. } else {
  99. FuzzyBoolean isMatch = pointcut.match(shadow);
  100. return isMatch.maybeTrue();
  101. }
  102. }
  103. public int fallbackCompareTo(Object other) {
  104. return toString().compareTo(toString());
  105. }
  106. public int getEnd() {
  107. return end;
  108. }
  109. public int getStart() {
  110. return start;
  111. }
  112. public ISourceLocation getSourceLocation() {
  113. if (sourceLocation == null) {
  114. if (sourceContext != null) {
  115. sourceLocation = sourceContext.makeSourceLocation(this);
  116. }
  117. }
  118. if (isBinary()) {
  119. if (binarySourceLocation == null) {
  120. binarySourceLocation = getBinarySourceLocation(sourceLocation);
  121. }
  122. return binarySourceLocation;
  123. }
  124. return sourceLocation;
  125. }
  126. public Pointcut getPointcut() {
  127. return pointcut;
  128. }
  129. // pointcut may be updated during rewriting...
  130. public void setPointcut(Pointcut pointcut) {
  131. this.pointcut = pointcut;
  132. }
  133. /**
  134. * Invoked when the shadow munger of a resolved type are processed.
  135. *
  136. * @param aType
  137. */
  138. public void setDeclaringType(ResolvedType aType) {
  139. declaringType = aType;
  140. }
  141. public ResolvedType getDeclaringType() {
  142. return declaringType;
  143. }
  144. public abstract ResolvedType getConcreteAspect();
  145. /**
  146. * Returns the binarySourceLocation for the given sourcelocation. This isn't cached because it's used when faulting in the
  147. * binary nodes and is called with ISourceLocations for all advice, pointcuts and deows contained within the
  148. * resolvedDeclaringAspect.
  149. */
  150. public ISourceLocation getBinarySourceLocation(ISourceLocation sl) {
  151. if (sl == null) {
  152. return null;
  153. }
  154. String sourceFileName = null;
  155. if (getDeclaringType() instanceof ReferenceType) {
  156. String s = ((ReferenceType) getDeclaringType()).getDelegate().getSourcefilename();
  157. int i = s.lastIndexOf('/');
  158. if (i != -1) {
  159. sourceFileName = s.substring(i + 1);
  160. } else {
  161. sourceFileName = s;
  162. }
  163. }
  164. ISourceLocation sLoc = new SourceLocation(getBinaryFile(), sl.getLine(), sl.getEndLine(),
  165. ((sl.getColumn() == 0) ? ISourceLocation.NO_COLUMN : sl.getColumn()), sl.getContext(), sourceFileName);
  166. return sLoc;
  167. }
  168. /**
  169. * Returns the File with pathname to the class file, for example either:<br>
  170. * C:\temp \ajcSandbox\workspace\ajcTest16957.tmp\simple.jar!pkg\BinaryAspect.class if the class file is in a jar file, or <br>
  171. * C:\temp\ajcSandbox\workspace\ajcTest16957.tmp!pkg\BinaryAspect.class if the class file is in a directory
  172. */
  173. private File getBinaryFile() {
  174. if (binaryFile == null) {
  175. String binaryPath = getDeclaringType().getBinaryPath();
  176. if (binaryPath == null) {
  177. // Looks like an aspect that has been picked up from the classpath (likely an abstract one
  178. // being extended). As it didn't come in via inpath or aspectpath the binarypath has not
  179. // yet been constructed.
  180. // We can't discover where the file came from now, that info has been lost. So just
  181. // use "classpath" for now - until we discover we need to get this right.
  182. binaryPath = "classpath";
  183. getDeclaringType().setBinaryPath(binaryPath);
  184. // ReferenceTypeDelegate delegate = ((ReferenceType) getDeclaringType()).getDelegate();
  185. // if (delegate instanceof BcelObjectType) {
  186. // grab javaclass... but it doesnt know the originating file
  187. // }
  188. }
  189. if (!binaryPath.contains("!")) {
  190. File f = getDeclaringType().getSourceLocation().getSourceFile();
  191. // Replace the source file suffix with .class
  192. int i = f.getPath().lastIndexOf('.');
  193. String path = null;
  194. if (i != -1) {
  195. path = f.getPath().substring(0, i) + ".class";
  196. } else {
  197. path = f.getPath() + ".class";
  198. }
  199. binaryFile = new File(binaryPath + "!" + path);
  200. } else {
  201. binaryFile = new File(binaryPath);
  202. }
  203. }
  204. return binaryFile;
  205. }
  206. /**
  207. * Returns whether or not this shadow munger came from a binary aspect - keep a record of whether or not we've checked if we're
  208. * binary otherwise we keep calculating the same thing many times
  209. */
  210. public boolean isBinary() {
  211. if (!checkedIsBinary) {
  212. ResolvedType rt = getDeclaringType();
  213. if (rt != null) {
  214. isBinary = ((rt.getBinaryPath() == null) ? false : true);
  215. }
  216. checkedIsBinary = true;
  217. }
  218. return isBinary;
  219. }
  220. public abstract ShadowMunger concretize(ResolvedType fromType, World world, PerClause clause);
  221. public abstract void specializeOn(Shadow shadow);
  222. /**
  223. * Implement this munger at the specified shadow, returning a boolean to indicate success.
  224. *
  225. * @param shadow the shadow where this munger should be applied
  226. * @return true if the implement was successful
  227. */
  228. public abstract boolean implementOn(Shadow shadow);
  229. public abstract ShadowMunger parameterizeWith(ResolvedType declaringType, Map<String, UnresolvedType> typeVariableMap);
  230. /**
  231. * @return a Collection of ResolvedTypes for all checked exceptions that might be thrown by this munger
  232. */
  233. public abstract Collection<ResolvedType> getThrownExceptions();
  234. /**
  235. * Does the munger have to check that its exception are accepted by the shadow ? It is not the case for annotation style around
  236. * advice, for example: that can throw Throwable, even if the advised method does not throw any exceptions.
  237. *
  238. * @return true if munger has to check that its exceptions can be thrown based on the shadow
  239. */
  240. public abstract boolean mustCheckExceptions();
  241. public void write(CompressingDataOutputStream stream) throws IOException {
  242. stream.writeInt(VERSION_1);
  243. stream.writeInt(shadowMungerKind); // determines real subclass
  244. stream.writeInt(start);
  245. stream.writeInt(end);
  246. PersistenceSupport.write(stream, sourceContext);
  247. PersistenceSupport.write(stream, sourceLocation);
  248. PersistenceSupport.write(stream, binarySourceLocation);
  249. PersistenceSupport.write(stream, binaryFile);
  250. declaringType.write(stream);
  251. stream.writeBoolean(isBinary);
  252. stream.writeBoolean(checkedIsBinary);
  253. pointcut.write(stream);
  254. }
  255. //
  256. // public static ShadowMunger read(VersionedDataInputStream stream, World world) throws IOException {
  257. // stream.readInt();
  258. // int kind = stream.readInt();
  259. // ShadowMunger newShadowMunger = null;
  260. // switch (kind) {
  261. // case ShadowMungerAdvice:
  262. // // world.getWeavingSupport().createAdviceMunger(attribute, pointcut, signature)
  263. // case ShadowMungerDeow:
  264. // newShadowMunger = Checker.read(stream, world);
  265. // default:
  266. // throw new IllegalStateException("Unexpected type of shadow munger found on deserialization: " + kind);
  267. // }
  268. // newShadowMunger.binaryFile = null;
  269. // }
  270. public boolean bindsProceedingJoinPoint() {
  271. return false;
  272. }
  273. public boolean isAroundAdvice() {
  274. return false;
  275. }
  276. }