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.

BcelObjectType.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.PrintStream;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Collection;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.aspectj.apache.bcel.classfile.Field;
  21. import org.aspectj.apache.bcel.classfile.JavaClass;
  22. import org.aspectj.apache.bcel.classfile.Method;
  23. import org.aspectj.bridge.ISourceLocation;
  24. import org.aspectj.weaver.AjAttribute;
  25. import org.aspectj.weaver.BCException;
  26. import org.aspectj.weaver.ResolvedMember;
  27. import org.aspectj.weaver.ResolvedPointcutDefinition;
  28. import org.aspectj.weaver.ResolvedTypeX;
  29. import org.aspectj.weaver.TypeX;
  30. import org.aspectj.weaver.WeaverStateInfo;
  31. import org.aspectj.weaver.patterns.PerClause;
  32. // ??? exposed for testing
  33. public class BcelObjectType extends ResolvedTypeX.ConcreteName {
  34. private JavaClass javaClass;
  35. private boolean isObject = false; // set upon construction
  36. private LazyClassGen lazyClassGen = null; // set lazily if it's an aspect
  37. // lazy, for no particular reason I can discern
  38. private ResolvedTypeX[] interfaces = null;
  39. private ResolvedTypeX superClass = null;
  40. private ResolvedMember[] fields = null;
  41. private ResolvedMember[] methods = null;
  42. // strangely non-lazy
  43. private ResolvedPointcutDefinition[] pointcuts = null;
  44. private PerClause perClause = null;
  45. private WeaverStateInfo weaverState = null;
  46. private List typeMungers = Collections.EMPTY_LIST;
  47. private List declares = Collections.EMPTY_LIST;
  48. private ResolvedMember[] privilegedAccess = null;
  49. public Collection getTypeMungers() {
  50. return typeMungers;
  51. }
  52. public Collection getDeclares() {
  53. return declares;
  54. }
  55. public Collection getPrivilegedAccesses() {
  56. if (privilegedAccess == null) return Collections.EMPTY_LIST;
  57. return Arrays.asList(privilegedAccess);
  58. }
  59. // IMPORTANT! THIS DOESN'T do real work on the java class, just stores it away.
  60. BcelObjectType(ResolvedTypeX.Name resolvedTypeX, JavaClass javaClass, boolean exposedToWeaver) {
  61. super(resolvedTypeX, exposedToWeaver);
  62. this.javaClass = javaClass;
  63. if (resolvedTypeX.getSourceContext() == null) {
  64. resolvedTypeX.setSourceContext(new BcelSourceContext(this));
  65. }
  66. // this should only ever be java.lang.Object which is
  67. // the only class in Java-1.4 with no superclasses
  68. isObject = (javaClass.getSuperclassNameIndex() == 0);
  69. unpackAspectAttributes();
  70. }
  71. // repeat initialization
  72. public void setJavaClass(JavaClass newclass) {
  73. this.javaClass = newclass;
  74. resetState();
  75. }
  76. public int getModifiers() {
  77. return javaClass.getAccessFlags();
  78. }
  79. public ResolvedTypeX getSuperclass() {
  80. if (isObject) return null;
  81. if (superClass == null) {
  82. superClass = getResolvedTypeX().getWorld().resolve(TypeX.forName(javaClass.getSuperclassName()));
  83. }
  84. return superClass;
  85. }
  86. public ResolvedTypeX[] getDeclaredInterfaces() {
  87. if (interfaces == null) {
  88. String[] ifaceNames = javaClass.getInterfaceNames();
  89. interfaces = new ResolvedTypeX[ifaceNames.length];
  90. for (int i = 0, len = ifaceNames.length; i < len; i++) {
  91. interfaces[i] = getResolvedTypeX().getWorld().resolve(TypeX.forName(ifaceNames[i]));
  92. }
  93. }
  94. return interfaces;
  95. }
  96. public ResolvedMember[] getDeclaredMethods() {
  97. if (methods == null) {
  98. Method[] ms = javaClass.getMethods();
  99. ResolvedMember[] ret = new ResolvedMember[ms.length];
  100. for (int i = ms.length - 1; i >= 0; i--) {
  101. ret[i] = new BcelMethod(this, ms[i]);
  102. }
  103. methods = ret;
  104. }
  105. return methods;
  106. }
  107. public ResolvedMember[] getDeclaredFields() {
  108. if (fields == null) {
  109. Field[] fs = javaClass.getFields();
  110. ResolvedMember[] ret = new ResolvedMember[fs.length];
  111. for (int i = 0, len = fs.length; i < len; i++) {
  112. ret[i] = new BcelField(this, fs[i]);
  113. }
  114. fields = ret;
  115. }
  116. return fields;
  117. }
  118. // ----
  119. // fun based on the aj attributes
  120. public ResolvedMember[] getDeclaredPointcuts() {
  121. return pointcuts;
  122. }
  123. //??? method only used for testing
  124. public void addPointcutDefinition(ResolvedPointcutDefinition d) {
  125. int len = pointcuts.length;
  126. ResolvedPointcutDefinition[] ret = new ResolvedPointcutDefinition[len+1];
  127. System.arraycopy(pointcuts, 0, ret, 0, len);
  128. ret[len] = d;
  129. pointcuts = ret;
  130. }
  131. public boolean isAspect() {
  132. return perClause != null;
  133. }
  134. private void unpackAspectAttributes() {
  135. List pointcuts = new ArrayList();
  136. typeMungers = new ArrayList();
  137. declares = new ArrayList();
  138. List l = BcelAttributes.readAjAttributes(javaClass.getAttributes(), getResolvedTypeX().getSourceContext());
  139. for (Iterator iter = l.iterator(); iter.hasNext();) {
  140. AjAttribute a = (AjAttribute) iter.next();
  141. //System.err.println("unpacking: " + this + " and " + a);
  142. if (a instanceof AjAttribute.Aspect) {
  143. perClause = ((AjAttribute.Aspect)a).reify(this.getResolvedTypeX());
  144. } else if (a instanceof AjAttribute.PointcutDeclarationAttribute) {
  145. pointcuts.add(((AjAttribute.PointcutDeclarationAttribute)a).reify());
  146. } else if (a instanceof AjAttribute.WeaverState) {
  147. weaverState = ((AjAttribute.WeaverState)a).reify();
  148. } else if (a instanceof AjAttribute.TypeMunger) {
  149. typeMungers.add(((AjAttribute.TypeMunger)a).reify(getResolvedTypeX().getWorld(), getResolvedTypeX()));
  150. } else if (a instanceof AjAttribute.DeclareAttribute) {
  151. declares.add(((AjAttribute.DeclareAttribute)a).getDeclare());
  152. } else if (a instanceof AjAttribute.PrivilegedAttribute) {
  153. privilegedAccess = ((AjAttribute.PrivilegedAttribute)a).getAccessedMembers();
  154. } else if (a instanceof AjAttribute.SourceContextAttribute) {
  155. if (getResolvedTypeX().getSourceContext() instanceof BcelSourceContext) {
  156. ((BcelSourceContext)getResolvedTypeX().getSourceContext()).addAttributeInfo((AjAttribute.SourceContextAttribute)a);
  157. }
  158. } else {
  159. throw new BCException("bad attribute " + a);
  160. }
  161. }
  162. this.pointcuts = (ResolvedPointcutDefinition[])
  163. pointcuts.toArray(new ResolvedPointcutDefinition[pointcuts.size()]);
  164. // this.typeMungers = (BcelTypeMunger[])
  165. // typeMungers.toArray(new BcelTypeMunger[typeMungers.size()]);
  166. // this.declares = (Declare[])
  167. // declares.toArray(new Declare[declares.size()]);
  168. }
  169. public PerClause getPerClause() {
  170. return perClause;
  171. }
  172. JavaClass getJavaClass() {
  173. return javaClass;
  174. }
  175. public void resetState() {
  176. this.interfaces = null;
  177. this.superClass = null;
  178. this.fields = null;
  179. this.methods = null;
  180. this.pointcuts = null;
  181. this.perClause = null;
  182. this.weaverState = null;
  183. this.lazyClassGen = null;
  184. isObject = (javaClass.getSuperclassNameIndex() == 0);
  185. unpackAspectAttributes();
  186. }
  187. public void finishedWith() {
  188. // memory usage experiments....
  189. // this.interfaces = null;
  190. // this.superClass = null;
  191. // this.fields = null;
  192. // this.methods = null;
  193. // this.pointcuts = null;
  194. // this.perClause = null;
  195. // this.weaverState = null;
  196. // this.lazyClassGen = null;
  197. // this next line frees up memory, but need to understand incremental implications
  198. // before leaving it in.
  199. // getResolvedTypeX().setSourceContext(null);
  200. }
  201. public WeaverStateInfo getWeaverState() {
  202. return weaverState;
  203. }
  204. void setWeaverState(WeaverStateInfo weaverState) {
  205. this.weaverState = weaverState;
  206. }
  207. public void printWackyStuff(PrintStream out) {
  208. if (typeMungers.size() > 0) {
  209. out.println(" TypeMungers: " + typeMungers);
  210. }
  211. if (declares.size() > 0) {
  212. out.println(" declares: " + declares);
  213. }
  214. }
  215. /**
  216. * Return the lazyClassGen associated with this type. For aspect types, this
  217. * value will be cached, since it is used to inline advice. For non-aspect
  218. * types, this lazyClassGen is always newly constructed.
  219. */
  220. public LazyClassGen getLazyClassGen() {
  221. LazyClassGen ret = lazyClassGen;
  222. if (ret == null) {
  223. //System.err.println("creating lazy class gen for: " + this);
  224. ret = new LazyClassGen(this);
  225. //ret.print(System.err);
  226. //System.err.println("made LCG from : " + this.getJavaClass().getSuperclassName() );
  227. if (isAspect()) {
  228. lazyClassGen = ret;
  229. }
  230. }
  231. return ret;
  232. }
  233. public boolean isInterface() {
  234. return javaClass.isInterface();
  235. }
  236. public boolean isSynthetic() {
  237. return getResolvedTypeX().isSynthetic();
  238. }
  239. public ISourceLocation getSourceLocation() {
  240. return getResolvedTypeX().getSourceContext().makeSourceLocation(0); //FIXME, we can do better than this
  241. }
  242. public void addParent(ResolvedTypeX newParent) {
  243. if (newParent.isClass()) {
  244. superClass = newParent;
  245. } else {
  246. ResolvedTypeX[] oldInterfaceNames = getDeclaredInterfaces();
  247. int len = oldInterfaceNames.length;
  248. ResolvedTypeX[] newInterfaceNames = new ResolvedTypeX[len+1];
  249. System.arraycopy(oldInterfaceNames, 0, newInterfaceNames, 0, len);
  250. newInterfaceNames[len] = newParent;
  251. interfaces = newInterfaceNames;
  252. }
  253. //System.err.println("javaClass: " + Arrays.asList(javaClass.getInterfaceNames()) + " super " + javaClass.getSuperclassName());
  254. //if (lazyClassGen != null) lazyClassGen.print();
  255. }
  256. }