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.

StandardShadow.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.reflect;
  13. import java.lang.reflect.Constructor;
  14. import java.lang.reflect.Field;
  15. import java.lang.reflect.Method;
  16. import java.lang.reflect.Modifier;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.aspectj.bridge.ISourceLocation;
  20. import org.aspectj.weaver.Member;
  21. import org.aspectj.weaver.ResolvedMember;
  22. import org.aspectj.weaver.ResolvedMemberImpl;
  23. import org.aspectj.weaver.ResolvedType;
  24. import org.aspectj.weaver.Shadow;
  25. import org.aspectj.weaver.UnresolvedType;
  26. import org.aspectj.weaver.World;
  27. import org.aspectj.weaver.ast.Var;
  28. import org.aspectj.weaver.tools.MatchingContext;
  29. /**
  30. * @author colyer
  31. *
  32. */
  33. public class StandardShadow extends Shadow {
  34. private final World world;
  35. private final ResolvedType enclosingType;
  36. private final ResolvedMember enclosingMember;
  37. private final MatchingContext matchContext;
  38. private Var thisVar = null;
  39. private Var targetVar = null;
  40. private Var[] argsVars = null;
  41. private Var atThisVar = null;
  42. private Var atTargetVar = null;
  43. private Map atArgsVars = new HashMap();
  44. private Map withinAnnotationVar = new HashMap();
  45. private Map withinCodeAnnotationVar = new HashMap();
  46. private Map annotationVar = new HashMap();
  47. private AnnotationFinder annotationFinder;
  48. public static Shadow makeExecutionShadow(World inWorld, java.lang.reflect.Member forMethod, MatchingContext withContext) {
  49. Kind kind = (forMethod instanceof Method) ? Shadow.MethodExecution : Shadow.ConstructorExecution;
  50. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forMethod, inWorld);
  51. ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  52. return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
  53. }
  54. public static Shadow makeExecutionShadow(World inWorld, ResolvedMember forMethod, MatchingContext withContext) {
  55. Kind kind = forMethod.getName().equals("<init>") ? Shadow.ConstructorExecution : Shadow.MethodExecution;
  56. // Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forMethod, inWorld);
  57. // ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  58. return new StandardShadow(inWorld, kind, forMethod, null, (ResolvedType) forMethod.getDeclaringType(), null, withContext);
  59. }
  60. public static Shadow makeAdviceExecutionShadow(World inWorld, java.lang.reflect.Method forMethod, MatchingContext withContext) {
  61. Kind kind = Shadow.AdviceExecution;
  62. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedAdviceMember(forMethod, inWorld);
  63. ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  64. return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
  65. }
  66. public static Shadow makeCallShadow(World inWorld, ResolvedMember aMember, ResolvedMember withinCode,
  67. MatchingContext withContext) {
  68. Shadow enclosingShadow = makeExecutionShadow(inWorld, withinCode, withContext);
  69. // Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(aMember, inWorld);
  70. // ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(withinCode, inWorld);
  71. // ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  72. Kind kind = !aMember.getName().equals("<init>") ? Shadow.MethodCall : Shadow.ConstructorCall;
  73. return new StandardShadow(inWorld, kind, aMember, enclosingShadow, (ResolvedType) withinCode.getDeclaringType(),
  74. withinCode, withContext);
  75. }
  76. public static Shadow makeCallShadow(World inWorld, java.lang.reflect.Member aMember, Class thisClass,
  77. MatchingContext withContext) {
  78. Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, thisClass, withContext);
  79. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(aMember, inWorld);
  80. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(thisClass, inWorld);
  81. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  82. Kind kind = aMember instanceof Method ? Shadow.MethodCall : Shadow.ConstructorCall;
  83. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  84. }
  85. public static Shadow makeStaticInitializationShadow(World inWorld, Class forType, MatchingContext withContext) {
  86. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(forType, inWorld);
  87. ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  88. Kind kind = Shadow.StaticInitialization;
  89. return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
  90. }
  91. public static Shadow makeStaticInitializationShadow(World inWorld, ResolvedType forType, MatchingContext withContext) {
  92. ResolvedMember[] members = forType.getDeclaredMethods();
  93. int clinit = -1;
  94. for (int i = 0; i < members.length && clinit == -1; i++) {
  95. if (members[i].getName().equals("<clinit>")) {
  96. clinit = i;
  97. }
  98. }
  99. // Member signature = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(forType, inWorld);
  100. Kind kind = Shadow.StaticInitialization;
  101. if (clinit == -1) {
  102. Member clinitMember = new ResolvedMemberImpl(org.aspectj.weaver.Member.STATIC_INITIALIZATION, forType, Modifier.STATIC,
  103. UnresolvedType.VOID, "<clinit>", UnresolvedType.NONE, UnresolvedType.NONE);
  104. return new StandardShadow(inWorld, kind, clinitMember, null, forType, null, withContext);
  105. } else {
  106. return new StandardShadow(inWorld, kind, members[clinit], null, forType, null, withContext);
  107. }
  108. }
  109. public static Shadow makePreInitializationShadow(World inWorld, Constructor forConstructor, MatchingContext withContext) {
  110. Kind kind = Shadow.PreInitialization;
  111. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forConstructor, inWorld);
  112. ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  113. return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
  114. }
  115. public static Shadow makeInitializationShadow(World inWorld, Constructor forConstructor, MatchingContext withContext) {
  116. Kind kind = Shadow.Initialization;
  117. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forConstructor, inWorld);
  118. ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
  119. return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
  120. }
  121. public static Shadow makeHandlerShadow(World inWorld, Class exceptionType, Class withinType, MatchingContext withContext) {
  122. Kind kind = Shadow.ExceptionHandler;
  123. Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, withinType, withContext);
  124. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createHandlerMember(exceptionType, withinType, inWorld);
  125. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(withinType, inWorld);
  126. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  127. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  128. }
  129. public static Shadow makeHandlerShadow(World inWorld, Class exceptionType, java.lang.reflect.Member withinCode,
  130. MatchingContext withContext) {
  131. Kind kind = Shadow.ExceptionHandler;
  132. Shadow enclosingShadow = makeExecutionShadow(inWorld, withinCode, withContext);
  133. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createHandlerMember(exceptionType,
  134. withinCode.getDeclaringClass(), inWorld);
  135. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(withinCode, inWorld);
  136. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  137. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  138. }
  139. public static Shadow makeFieldGetShadow(World inWorld, Field forField, Class callerType, MatchingContext withContext) {
  140. Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, callerType, withContext);
  141. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
  142. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(callerType, inWorld);
  143. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  144. Kind kind = Shadow.FieldGet;
  145. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  146. }
  147. public static Shadow makeFieldGetShadow(World inWorld, Field forField, java.lang.reflect.Member inMember,
  148. MatchingContext withContext) {
  149. Shadow enclosingShadow = makeExecutionShadow(inWorld, inMember, withContext);
  150. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
  151. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(inMember, inWorld);
  152. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  153. Kind kind = Shadow.FieldGet;
  154. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  155. }
  156. public static Shadow makeFieldSetShadow(World inWorld, Field forField, Class callerType, MatchingContext withContext) {
  157. Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, callerType, withContext);
  158. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
  159. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(callerType, inWorld);
  160. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  161. Kind kind = Shadow.FieldSet;
  162. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  163. }
  164. public static Shadow makeFieldSetShadow(World inWorld, Field forField, java.lang.reflect.Member inMember,
  165. MatchingContext withContext) {
  166. Shadow enclosingShadow = makeExecutionShadow(inWorld, inMember, withContext);
  167. Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
  168. ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(inMember, inWorld);
  169. ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
  170. Kind kind = Shadow.FieldSet;
  171. return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
  172. }
  173. public StandardShadow(World world, Kind kind, Member signature, Shadow enclosingShadow, ResolvedType enclosingType,
  174. ResolvedMember enclosingMember, MatchingContext withContext) {
  175. super(kind, signature, enclosingShadow);
  176. this.world = world;
  177. this.enclosingType = enclosingType;
  178. this.enclosingMember = enclosingMember;
  179. this.matchContext = withContext;
  180. if (world instanceof IReflectionWorld) {
  181. this.annotationFinder = ((IReflectionWorld) world).getAnnotationFinder();
  182. }
  183. }
  184. /*
  185. * (non-Javadoc)
  186. *
  187. * @see org.aspectj.weaver.Shadow#getIWorld()
  188. */
  189. public World getIWorld() {
  190. return world;
  191. }
  192. /*
  193. * (non-Javadoc)
  194. *
  195. * @see org.aspectj.weaver.Shadow#getThisVar()
  196. */
  197. public Var getThisVar() {
  198. if (thisVar == null && hasThis()) {
  199. thisVar = ReflectionVar.createThisVar(getThisType().resolve(world), this.annotationFinder);
  200. }
  201. return thisVar;
  202. }
  203. /*
  204. * (non-Javadoc)
  205. *
  206. * @see org.aspectj.weaver.Shadow#getTargetVar()
  207. */
  208. public Var getTargetVar() {
  209. if (targetVar == null && hasTarget()) {
  210. targetVar = ReflectionVar.createTargetVar(getThisType().resolve(world), this.annotationFinder);
  211. }
  212. return targetVar;
  213. }
  214. /*
  215. * (non-Javadoc)
  216. *
  217. * @see org.aspectj.weaver.Shadow#getEnclosingType()
  218. */
  219. public UnresolvedType getEnclosingType() {
  220. return this.enclosingType;
  221. }
  222. /*
  223. * (non-Javadoc)
  224. *
  225. * @see org.aspectj.weaver.Shadow#getArgVar(int)
  226. */
  227. public Var getArgVar(int i) {
  228. if (argsVars == null) {
  229. this.argsVars = new Var[this.getArgCount()];
  230. for (int j = 0; j < this.argsVars.length; j++) {
  231. this.argsVars[j] = ReflectionVar.createArgsVar(getArgType(j).resolve(world), j, this.annotationFinder);
  232. }
  233. }
  234. if (i < argsVars.length) {
  235. return argsVars[i];
  236. } else {
  237. return null;
  238. }
  239. }
  240. /*
  241. * (non-Javadoc)
  242. *
  243. * @see org.aspectj.weaver.Shadow#getThisJoinPointVar()
  244. */
  245. public Var getThisJoinPointVar() {
  246. // TODO Auto-generated method stub
  247. return null;
  248. }
  249. public Var getThisJoinPointStaticPartVar() {
  250. return null;
  251. }
  252. public Var getThisEnclosingJoinPointStaticPartVar() {
  253. return null;
  254. }
  255. public Var getThisAspectInstanceVar(ResolvedType aspectType) {
  256. return null;
  257. }
  258. public Var getKindedAnnotationVar(UnresolvedType forAnnotationType) {
  259. ResolvedType annType = forAnnotationType.resolve(world);
  260. if (annotationVar.get(annType) == null) {
  261. Var v = ReflectionVar.createAtAnnotationVar(annType, this.annotationFinder);
  262. annotationVar.put(annType, v);
  263. }
  264. return (Var) annotationVar.get(annType);
  265. }
  266. /*
  267. * (non-Javadoc)
  268. *
  269. * @see org.aspectj.weaver.Shadow#getWithinAnnotationVar(org.aspectj.weaver.UnresolvedType)
  270. */
  271. public Var getWithinAnnotationVar(UnresolvedType forAnnotationType) {
  272. ResolvedType annType = forAnnotationType.resolve(world);
  273. if (withinAnnotationVar.get(annType) == null) {
  274. Var v = ReflectionVar.createWithinAnnotationVar(annType, this.annotationFinder);
  275. withinAnnotationVar.put(annType, v);
  276. }
  277. return (Var) withinAnnotationVar.get(annType);
  278. }
  279. /*
  280. * (non-Javadoc)
  281. *
  282. * @see org.aspectj.weaver.Shadow#getWithinCodeAnnotationVar(org.aspectj.weaver.UnresolvedType)
  283. */
  284. public Var getWithinCodeAnnotationVar(UnresolvedType forAnnotationType) {
  285. ResolvedType annType = forAnnotationType.resolve(world);
  286. if (withinCodeAnnotationVar.get(annType) == null) {
  287. Var v = ReflectionVar.createWithinCodeAnnotationVar(annType, this.annotationFinder);
  288. withinCodeAnnotationVar.put(annType, v);
  289. }
  290. return (Var) withinCodeAnnotationVar.get(annType);
  291. }
  292. /*
  293. * (non-Javadoc)
  294. *
  295. * @see org.aspectj.weaver.Shadow#getThisAnnotationVar(org.aspectj.weaver.UnresolvedType)
  296. */
  297. public Var getThisAnnotationVar(UnresolvedType forAnnotationType) {
  298. if (atThisVar == null) {
  299. atThisVar = ReflectionVar.createThisAnnotationVar(forAnnotationType.resolve(world), this.annotationFinder);
  300. }
  301. return atThisVar;
  302. }
  303. /*
  304. * (non-Javadoc)
  305. *
  306. * @see org.aspectj.weaver.Shadow#getTargetAnnotationVar(org.aspectj.weaver.UnresolvedType)
  307. */
  308. public Var getTargetAnnotationVar(UnresolvedType forAnnotationType) {
  309. if (atTargetVar == null) {
  310. atTargetVar = ReflectionVar.createTargetAnnotationVar(forAnnotationType.resolve(world), this.annotationFinder);
  311. }
  312. return atTargetVar;
  313. }
  314. /*
  315. * (non-Javadoc)
  316. *
  317. * @see org.aspectj.weaver.Shadow#getArgAnnotationVar(int, org.aspectj.weaver.UnresolvedType)
  318. */
  319. public Var getArgAnnotationVar(int i, UnresolvedType forAnnotationType) {
  320. ResolvedType annType = forAnnotationType.resolve(world);
  321. if (atArgsVars.get(annType) == null) {
  322. Var[] vars = new Var[getArgCount()];
  323. atArgsVars.put(annType, vars);
  324. }
  325. Var[] vars = (Var[]) atArgsVars.get(annType);
  326. if (i > (vars.length - 1))
  327. return null;
  328. if (vars[i] == null) {
  329. vars[i] = ReflectionVar.createArgsAnnotationVar(annType, i, this.annotationFinder);
  330. }
  331. return vars[i];
  332. }
  333. /*
  334. * (non-Javadoc)
  335. *
  336. * @see org.aspectj.weaver.Shadow#getEnclosingCodeSignature()
  337. */
  338. public Member getEnclosingCodeSignature() {
  339. // XXX this code is copied from BcelShadow with one minor change...
  340. if (getKind().isEnclosingKind()) {
  341. return getSignature();
  342. } else if (getKind() == Shadow.PreInitialization) {
  343. // PreInit doesn't enclose code but its signature
  344. // is correctly the signature of the ctor.
  345. return getSignature();
  346. } else if (enclosingShadow == null) {
  347. return this.enclosingMember;
  348. } else {
  349. return enclosingShadow.getSignature();
  350. }
  351. }
  352. /*
  353. * (non-Javadoc)
  354. *
  355. * @see org.aspectj.weaver.Shadow#getSourceLocation()
  356. */
  357. public ISourceLocation getSourceLocation() {
  358. return null;
  359. }
  360. public MatchingContext getMatchingContext() {
  361. return this.matchContext;
  362. }
  363. }