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.

ReflectionVar.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Member;
  14. import org.aspectj.weaver.ResolvedType;
  15. import org.aspectj.weaver.ast.Var;
  16. /**
  17. * A variable at a reflection shadow, used by the residual tests.
  18. */
  19. public final class ReflectionVar extends Var {
  20. static final int THIS_VAR = 0;
  21. static final int TARGET_VAR = 1;
  22. static final int ARGS_VAR = 2;
  23. static final int AT_THIS_VAR = 3;
  24. static final int AT_TARGET_VAR = 4;
  25. static final int AT_ARGS_VAR = 5;
  26. static final int AT_WITHIN_VAR = 6;
  27. static final int AT_WITHINCODE_VAR = 7;
  28. static final int AT_ANNOTATION_VAR = 8;
  29. private AnnotationFinder annotationFinder = null;
  30. // static {
  31. // try {
  32. // Class java15AnnotationFinder = Class.forName("org.aspectj.weaver.reflect.Java15AnnotationFinder");
  33. // annotationFinder = (AnnotationFinder) java15AnnotationFinder.newInstance();
  34. // } catch(ClassNotFoundException ex) {
  35. // // must be on 1.4 or earlier
  36. // } catch(IllegalAccessException ex) {
  37. // // not so good
  38. // throw new RuntimeException("AspectJ internal error",ex);
  39. // } catch(InstantiationException ex) {
  40. // throw new RuntimeException("AspectJ internal error",ex);
  41. // }
  42. // }
  43. private int argsIndex = 0;
  44. private int varType;
  45. public static ReflectionVar createThisVar(ResolvedType type,AnnotationFinder finder) {
  46. ReflectionVar ret = new ReflectionVar(type,finder);
  47. ret.varType = THIS_VAR;
  48. return ret;
  49. }
  50. public static ReflectionVar createTargetVar(ResolvedType type, AnnotationFinder finder) {
  51. ReflectionVar ret = new ReflectionVar(type,finder);
  52. ret.varType = TARGET_VAR;
  53. return ret;
  54. }
  55. public static ReflectionVar createArgsVar(ResolvedType type, int index, AnnotationFinder finder) {
  56. ReflectionVar ret = new ReflectionVar(type,finder);
  57. ret.varType = ARGS_VAR;
  58. ret.argsIndex = index;
  59. return ret;
  60. }
  61. public static ReflectionVar createThisAnnotationVar(ResolvedType type, AnnotationFinder finder) {
  62. ReflectionVar ret = new ReflectionVar(type,finder);
  63. ret.varType = AT_THIS_VAR;
  64. return ret;
  65. }
  66. public static ReflectionVar createTargetAnnotationVar(ResolvedType type, AnnotationFinder finder) {
  67. ReflectionVar ret = new ReflectionVar(type,finder);
  68. ret.varType = AT_TARGET_VAR;
  69. return ret;
  70. }
  71. public static ReflectionVar createArgsAnnotationVar(ResolvedType type, int index, AnnotationFinder finder) {
  72. ReflectionVar ret = new ReflectionVar(type,finder);
  73. ret.varType = AT_ARGS_VAR;
  74. ret.argsIndex = index;
  75. return ret;
  76. }
  77. public static ReflectionVar createWithinAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
  78. ReflectionVar ret = new ReflectionVar(annType,finder);
  79. ret.varType = AT_WITHIN_VAR;
  80. return ret;
  81. }
  82. public static ReflectionVar createWithinCodeAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
  83. ReflectionVar ret = new ReflectionVar(annType,finder);
  84. ret.varType = AT_WITHINCODE_VAR;
  85. return ret;
  86. }
  87. public static ReflectionVar createAtAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
  88. ReflectionVar ret = new ReflectionVar(annType,finder);
  89. ret.varType = AT_ANNOTATION_VAR;
  90. return ret;
  91. }
  92. private ReflectionVar(ResolvedType type,AnnotationFinder finder) {
  93. super(type);
  94. this.annotationFinder = finder;
  95. }
  96. public Object getBindingAtJoinPoint(Object thisObject, Object targetObject, Object[] args) {
  97. return getBindingAtJoinPoint(thisObject,targetObject,args,null,null,null);
  98. }
  99. /**
  100. * At a join point with the given this, target, and args, return the object to which this
  101. * var is bound.
  102. * @param thisObject
  103. * @param targetObject
  104. * @param args
  105. * @return
  106. */
  107. public Object getBindingAtJoinPoint(
  108. Object thisObject,
  109. Object targetObject,
  110. Object[] args,
  111. Member subject,
  112. Member withinCode,
  113. Class withinType) {
  114. switch( this.varType) {
  115. case THIS_VAR: return thisObject;
  116. case TARGET_VAR: return targetObject;
  117. case ARGS_VAR:
  118. if (this.argsIndex > (args.length - 1)) return null;
  119. return args[argsIndex];
  120. case AT_THIS_VAR:
  121. if (annotationFinder != null) {
  122. return annotationFinder.getAnnotation(getType(), thisObject);
  123. } else return null;
  124. case AT_TARGET_VAR:
  125. if (annotationFinder != null) {
  126. return annotationFinder.getAnnotation(getType(), targetObject);
  127. } else return null;
  128. case AT_ARGS_VAR:
  129. if (this.argsIndex > (args.length - 1)) return null;
  130. if (annotationFinder != null) {
  131. return annotationFinder.getAnnotation(getType(), args[argsIndex]);
  132. } else return null;
  133. case AT_WITHIN_VAR:
  134. if (annotationFinder != null) {
  135. return annotationFinder.getAnnotationFromClass(getType(), withinType);
  136. } else return null;
  137. case AT_WITHINCODE_VAR:
  138. if (annotationFinder != null) {
  139. return annotationFinder.getAnnotationFromMember(getType(), withinCode);
  140. } else return null;
  141. case AT_ANNOTATION_VAR:
  142. if (annotationFinder != null) {
  143. return annotationFinder.getAnnotationFromMember(getType(), subject);
  144. } else return null;
  145. }
  146. return null;
  147. }
  148. }