Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ResolvedTypeMunger.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.EOFException;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Set;
  26. import org.aspectj.bridge.ISourceLocation;
  27. import org.aspectj.bridge.SourceLocation;
  28. import org.aspectj.util.TypeSafeEnum;
  29. /** This is an abstraction over method/field introduction. It might not have the chops
  30. * to handle other inter-type declarations. This is the thing that is used on the
  31. * eclipse side and serialized into a ConcreteTypeMunger.
  32. */
  33. public abstract class ResolvedTypeMunger {
  34. protected Kind kind;
  35. protected ResolvedMember signature;
  36. // This list records the occurences (in order) of any names specified in the <>
  37. // for a target type for the ITD. So for example, for List<C,B,A> this list
  38. // will be C,B,A - the list is used later to map other occurrences of C,B,A
  39. // across the intertype declaration to the right type variables in the generic
  40. // type upon which the itd is being made.
  41. // might need serializing the class file for binary weaving.
  42. protected List /*String*/ typeVariableToGenericTypeVariableIndex;
  43. private Set /* resolvedMembers */ superMethodsCalled = Collections.EMPTY_SET;
  44. private ISourceLocation location; // Lost during serialize/deserialize !
  45. public ResolvedTypeMunger(Kind kind, ResolvedMember signature) {
  46. this.kind = kind;
  47. this.signature = signature;
  48. UnresolvedType declaringType = signature != null ? signature.getDeclaringType() : null;
  49. if (declaringType != null) {
  50. if (declaringType.isRawType()) throw new IllegalStateException("Use generic type, not raw type");
  51. if (declaringType.isParameterizedType()) throw new IllegalStateException("Use generic type, not parameterized type");
  52. }
  53. }
  54. public void setSourceLocation(ISourceLocation isl) {
  55. location = isl;
  56. }
  57. public ISourceLocation getSourceLocation() {
  58. return location;
  59. }
  60. // ----
  61. // fromType is guaranteed to be a non-abstract aspect
  62. public ConcreteTypeMunger concretize(World world, ResolvedType aspectType) {
  63. ConcreteTypeMunger munger = world.concreteTypeMunger(this, aspectType);
  64. return munger;
  65. }
  66. public boolean matches(ResolvedType matchType, ResolvedType aspectType) {
  67. ResolvedType onType = matchType.getWorld().resolve(signature.getDeclaringType());
  68. if (onType.isRawType()) onType = onType.getGenericType();
  69. //System.err.println("matching: " + this + " to " + matchType + " onType = " + onType);
  70. if (matchType.equals(onType)) {
  71. if (!onType.isExposedToWeaver()) {
  72. // if the onType is an interface, and it already has the member we are about
  73. // to munge, then this is ok...
  74. boolean ok = (onType.isInterface() && (onType.lookupMemberWithSupersAndITDs(getSignature()) != null));
  75. if (!ok && onType.getWeaverState() == null) {
  76. if (matchType.getWorld().getLint().typeNotExposedToWeaver.isEnabled()) {
  77. matchType.getWorld().getLint().typeNotExposedToWeaver.signal(
  78. matchType.getName(), signature.getSourceLocation());
  79. }
  80. }
  81. }
  82. return true;
  83. }
  84. //System.err.println("NO MATCH DIRECT");
  85. if (onType.isInterface()) {
  86. return matchType.isTopmostImplementor(onType);
  87. } else {
  88. return false;
  89. }
  90. }
  91. // ----
  92. public String toString() {
  93. return "ResolvedTypeMunger(" + getKind() + ", " + getSignature() +")";
  94. //.superMethodsCalled + ")";
  95. }
  96. // ----
  97. public static ResolvedTypeMunger read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  98. Kind kind = Kind.read(s);
  99. if (kind == Field) {
  100. return NewFieldTypeMunger.readField(s, context);
  101. } else if (kind == Method) {
  102. return NewMethodTypeMunger.readMethod(s, context);
  103. } else if (kind == Constructor) {
  104. return NewConstructorTypeMunger.readConstructor(s, context);
  105. } else {
  106. throw new RuntimeException("unimplemented");
  107. }
  108. }
  109. protected static Set readSuperMethodsCalled(VersionedDataInputStream s) throws IOException {
  110. Set ret = new HashSet();
  111. int n = s.readInt();
  112. if (n<0) throw new BCException("Problem deserializing type munger");
  113. for (int i=0; i < n; i++) {
  114. ret.add(ResolvedMemberImpl.readResolvedMember(s, null));
  115. }
  116. return ret;
  117. }
  118. protected void writeSuperMethodsCalled(DataOutputStream s) throws IOException {
  119. if (superMethodsCalled == null || superMethodsCalled.size()==0) {
  120. s.writeInt(0);
  121. return;
  122. }
  123. List ret = new ArrayList(superMethodsCalled);
  124. Collections.sort(ret);
  125. int n = ret.size();
  126. s.writeInt(n);
  127. for (Iterator i = ret.iterator(); i.hasNext(); ) {
  128. ResolvedMember m = (ResolvedMember)i.next();
  129. m.write(s);
  130. }
  131. }
  132. protected static ISourceLocation readSourceLocation(VersionedDataInputStream s) throws IOException {
  133. // Location persistence for type mungers was added after 1.2.1 was shipped...
  134. if (s.getMajorVersion()<AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150) return null;
  135. SourceLocation ret = null;
  136. ObjectInputStream ois = null;
  137. try {
  138. // This logic copes with the location missing from the attribute - an EOFException will
  139. // occur on the next line and we ignore it.
  140. ois = new ObjectInputStream(s);
  141. Boolean validLocation = (Boolean)ois.readObject();
  142. if (validLocation.booleanValue()) {
  143. File f = (File) ois.readObject();
  144. Integer ii = (Integer)ois.readObject();
  145. Integer offset = (Integer)ois.readObject();
  146. ret = new SourceLocation(f,ii.intValue());
  147. ret.setOffset(offset.intValue());
  148. }
  149. } catch (EOFException eof) {
  150. return null; // This exception occurs if processing an 'old style' file where the
  151. // type munger attributes don't include the source location.
  152. } catch (IOException ioe) {
  153. // Something went wrong, maybe this is an 'old style' file that doesnt attach locations to mungers?
  154. // (but I thought that was just an EOFException?)
  155. ioe.printStackTrace();
  156. return null;
  157. } catch (ClassNotFoundException e) {
  158. } finally {
  159. if (ois!=null) ois.close();
  160. }
  161. return ret;
  162. }
  163. protected void writeSourceLocation(DataOutputStream s) throws IOException {
  164. ObjectOutputStream oos = new ObjectOutputStream(s);
  165. // oos.writeObject(location);
  166. oos.writeObject(new Boolean(location!=null));
  167. if (location !=null) {
  168. oos.writeObject(location.getSourceFile());
  169. oos.writeObject(new Integer(location.getLine()));
  170. oos.writeObject(new Integer(location.getOffset()));
  171. }
  172. oos.flush();
  173. oos.close();
  174. }
  175. public abstract void write(DataOutputStream s) throws IOException;
  176. public Kind getKind() {
  177. return kind;
  178. }
  179. public static class Kind extends TypeSafeEnum {
  180. /* private */ Kind(String name, int key) {
  181. super(name, key);
  182. }
  183. public static Kind read(DataInputStream s) throws IOException {
  184. int key = s.readByte();
  185. switch(key) {
  186. case 1: return Field;
  187. case 2: return Method;
  188. case 5: return Constructor;
  189. }
  190. throw new BCException("bad kind: " + key);
  191. }
  192. }
  193. // ---- fields
  194. public static final Kind Field = new Kind("Field", 1);
  195. public static final Kind Method = new Kind("Method", 2);
  196. public static final Kind Constructor = new Kind("Constructor", 5);
  197. // not serialized, only created during concretization of aspects
  198. public static final Kind PerObjectInterface = new Kind("PerObjectInterface", 3);
  199. public static final Kind PrivilegedAccess = new Kind("PrivilegedAccess", 4);
  200. public static final Kind Parent = new Kind("Parent", 6);
  201. public static final Kind PerTypeWithinInterface = new Kind("PerTypeWithinInterface",7); // PTWIMPL not serialized, used during concretization of aspects
  202. public static final Kind AnnotationOnType = new Kind("AnnotationOnType",8); // not serialized
  203. public static final String SUPER_DISPATCH_NAME = "superDispatch";
  204. public void setSuperMethodsCalled(Set c) {
  205. this.superMethodsCalled = c;
  206. }
  207. public Set getSuperMethodsCalled() {
  208. return superMethodsCalled;
  209. }
  210. public ResolvedMember getSignature() {
  211. return signature;
  212. }
  213. // ----
  214. public ResolvedMember getMatchingSyntheticMember(Member member, ResolvedType aspectType) {
  215. if ((getSignature() != null) && getSignature().isPublic() && member.equals(getSignature())) {
  216. return getSignature();
  217. }
  218. return null;
  219. }
  220. public boolean changesPublicSignature() {
  221. return kind == Field || kind == Method || kind == Constructor;
  222. }
  223. public boolean needsAccessToTopmostImplementor() {
  224. if (kind == Field) {
  225. return true;
  226. } else if (kind == Method) {
  227. return !signature.isAbstract();
  228. } else {
  229. return false;
  230. }
  231. }
  232. }