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.

ReflectionShadow.java 15KB

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