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.

AjAttribute.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.EOFException;
  17. import java.io.IOException;
  18. import org.aspectj.bridge.MessageUtil;
  19. import org.aspectj.bridge.Version;
  20. import org.aspectj.util.FileUtil;
  21. import org.aspectj.weaver.patterns.Declare;
  22. import org.aspectj.weaver.patterns.IScope;
  23. import org.aspectj.weaver.patterns.PerClause;
  24. import org.aspectj.weaver.patterns.Pointcut;
  25. /**
  26. * These attributes are written to and read from .class files (see the JVM spec).
  27. *
  28. * <p>Each member or type can have a number of AjAttributes. Each
  29. * such attribute is in 1-1 correspondence with an Unknown bcel attribute.
  30. * Creating one of these does NOTHING to the underlying thing, so if you really
  31. * want to add an attribute to a particular thing, well, you'd better actually do that.
  32. *
  33. * @author Erik Hilsdale
  34. * @author Jim Hugunin
  35. */
  36. public abstract class AjAttribute {
  37. public static final String AttributePrefix = "org.aspectj.weaver";
  38. protected abstract void write(DataOutputStream s) throws IOException;
  39. public abstract String getNameString();
  40. public char[] getNameChars() {
  41. return getNameString().toCharArray();
  42. }
  43. /**
  44. * Just writes the contents
  45. */
  46. public byte[] getBytes() {
  47. try {
  48. ByteArrayOutputStream b0 = new ByteArrayOutputStream();
  49. DataOutputStream s0 = new DataOutputStream(b0);
  50. write(s0);
  51. return b0.toByteArray();
  52. } catch (IOException e) {
  53. // shouldn't happen with ByteArrayOutputStreams
  54. throw new RuntimeException("sanity check");
  55. }
  56. }
  57. /**
  58. * Writes the full attribute, i.e. name_index, length, and contents
  59. */
  60. public byte[] getAllBytes(short nameIndex) {
  61. try {
  62. byte[] bytes = getBytes();
  63. ByteArrayOutputStream b0 = new ByteArrayOutputStream();
  64. DataOutputStream s0 = new DataOutputStream(b0);
  65. s0.writeShort(nameIndex);
  66. s0.writeInt(bytes.length);
  67. s0.write(bytes);
  68. return b0.toByteArray();
  69. } catch (IOException e) {
  70. // shouldn't happen with ByteArrayOutputStreams
  71. throw new RuntimeException("sanity check");
  72. }
  73. }
  74. public static AjAttribute read(AjAttribute.WeaverVersionInfo v, String name, byte[] bytes, ISourceContext context,World w) {
  75. try {
  76. if (bytes == null) bytes = new byte[0];
  77. VersionedDataInputStream s = new VersionedDataInputStream(new ByteArrayInputStream(bytes));
  78. s.setVersion(v);
  79. if (name.equals(Aspect.AttributeName)) {
  80. return new Aspect(PerClause.readPerClause(s, context));
  81. } else if (name.equals(MethodDeclarationLineNumberAttribute.AttributeName)) {
  82. return MethodDeclarationLineNumberAttribute.read(s);
  83. } else if (name.equals(WeaverState.AttributeName)) {
  84. return new WeaverState(WeaverStateInfo.read(s, context));
  85. } else if (name.equals(WeaverVersionInfo.AttributeName)) {
  86. return WeaverVersionInfo.read(s);
  87. } else if (name.equals(AdviceAttribute.AttributeName)) {
  88. AdviceAttribute aa = AdviceAttribute.read(s, context);
  89. aa.getPointcut().check(context,w);
  90. return aa;
  91. } else if (name.equals(PointcutDeclarationAttribute.AttributeName)) {
  92. PointcutDeclarationAttribute pda = new PointcutDeclarationAttribute(ResolvedPointcutDefinition.read(s, context));
  93. pda.pointcutDef.getPointcut().check(context,w);
  94. return pda;
  95. } else if (name.equals(TypeMunger.AttributeName)) {
  96. return new TypeMunger(ResolvedTypeMunger.read(s, context));
  97. } else if (name.equals(AjSynthetic.AttributeName)) {
  98. return new AjSynthetic();
  99. } else if (name.equals(DeclareAttribute.AttributeName)) {
  100. return new DeclareAttribute(Declare.read(s, context));
  101. } else if (name.equals(PrivilegedAttribute.AttributeName)) {
  102. return PrivilegedAttribute.read(s, context);
  103. } else if (name.equals(SourceContextAttribute.AttributeName)) {
  104. return SourceContextAttribute.read(s);
  105. } else if (name.equals(EffectiveSignatureAttribute.AttributeName)) {
  106. return EffectiveSignatureAttribute.read(s, context);
  107. } else {
  108. // We have to tell the user about this...
  109. if (w == null || w.getMessageHandler()==null) throw new BCException("unknown attribute" + name);
  110. w.getMessageHandler().handleMessage(MessageUtil.warn("unknown attribute encountered "+name));
  111. return null;
  112. }
  113. } catch (IOException e) {
  114. throw new BCException("malformed " + name + " attribute " + e);
  115. }
  116. }
  117. //----
  118. /** Synthetic members should have NO advice put on them or on their contents.
  119. * This attribute is currently unused as we consider all members starting
  120. * with NameMangler.PREFIX to automatically be synthetic. As we use this we might
  121. * find that we want multiple
  122. * kinds of synthetic. In particular, if we want to treat the call to a synthetic getter
  123. * (say, of an introduced field) as a field reference itself, then a method might want
  124. * a particular kind of AjSynthetic attribute that also includes a signature of what
  125. * it stands for.
  126. */
  127. public static class AjSynthetic extends AjAttribute {
  128. public static final String AttributeName = "org.aspectj.weaver.AjSynthetic";
  129. public String getNameString() {
  130. return AttributeName;
  131. }
  132. // private ResolvedTypeMunger munger;
  133. public AjSynthetic() {}
  134. public void write(DataOutputStream s) throws IOException {}
  135. }
  136. public static class TypeMunger extends AjAttribute {
  137. public static final String AttributeName = "org.aspectj.weaver.TypeMunger";
  138. public String getNameString() {
  139. return AttributeName;
  140. }
  141. private ResolvedTypeMunger munger;
  142. public TypeMunger(ResolvedTypeMunger munger) {
  143. this.munger = munger;
  144. }
  145. public void write(DataOutputStream s) throws IOException {
  146. munger.write(s);
  147. }
  148. public ConcreteTypeMunger reify(World world, ResolvedType aspectType) {
  149. return world.concreteTypeMunger(munger, aspectType);
  150. }
  151. }
  152. public static class WeaverState extends AjAttribute {
  153. public static final String AttributeName = "org.aspectj.weaver.WeaverState";
  154. public String getNameString() {
  155. return AttributeName;
  156. }
  157. private WeaverStateInfo kind;
  158. public WeaverState(WeaverStateInfo kind) {
  159. this.kind = kind;
  160. }
  161. public void write(DataOutputStream s) throws IOException {
  162. kind.write(s);
  163. }
  164. public WeaverStateInfo reify() {
  165. return kind;
  166. }
  167. }
  168. public static class WeaverVersionInfo extends AjAttribute {
  169. public static final String AttributeName = "org.aspectj.weaver.WeaverVersion";
  170. // If you change the format of an AspectJ class file, you have two options:
  171. // - changing the minor version means you have not added anything that prevents
  172. // previous versions of the weaver from operating (e.g. MethodDeclarationLineNumber attribute)
  173. // - changing the major version means you have added something that prevents previous
  174. // versions of the weaver from operating correctly.
  175. //
  176. // The user will get a warning for any org.aspectj.weaver attributes the weaver does
  177. // not recognize.
  178. // When we don't know ... (i.e. pre 1.2.1)
  179. public static short WEAVER_VERSION_MAJOR_UNKNOWN = 0;
  180. public static short WEAVER_VERSION_MINOR_UNKNOWN = 0;
  181. // These are the weaver major/minor numbers for AspectJ 1.2.1
  182. public static short WEAVER_VERSION_MAJOR_AJ121 = 1;
  183. public static short WEAVER_VERSION_MINOR_AJ121 = 0;
  184. // These are the weaver major/minor numbers for AspectJ 1.5.0
  185. public static short WEAVER_VERSION_MAJOR_AJ150M4 = 3;
  186. public static short WEAVER_VERSION_MAJOR_AJ150 = 2;
  187. public static short WEAVER_VERSION_MINOR_AJ150 = 0;
  188. // These are the weaver major/minor versions for *this* weaver
  189. private static short CURRENT_VERSION_MAJOR = WEAVER_VERSION_MAJOR_AJ150M4;
  190. private static short CURRENT_VERSION_MINOR = WEAVER_VERSION_MINOR_AJ150;
  191. public static final WeaverVersionInfo UNKNOWN =
  192. new WeaverVersionInfo(WEAVER_VERSION_MAJOR_UNKNOWN,WEAVER_VERSION_MINOR_UNKNOWN);
  193. // These are the versions read in from a particular class file.
  194. private short major_version;
  195. private short minor_version;
  196. private long buildstamp = Version.NOTIME;
  197. public String getNameString() {
  198. return AttributeName;
  199. }
  200. // Default ctor uses the current version numbers
  201. public WeaverVersionInfo() {
  202. this.major_version = CURRENT_VERSION_MAJOR;
  203. this.minor_version = CURRENT_VERSION_MINOR;
  204. }
  205. public WeaverVersionInfo(short major,short minor) {
  206. major_version = major;
  207. minor_version = minor;
  208. }
  209. public void write(DataOutputStream s) throws IOException {
  210. s.writeShort(CURRENT_VERSION_MAJOR);
  211. s.writeShort(CURRENT_VERSION_MINOR);
  212. s.writeLong(Version.getTime()); // build used to construct the class...
  213. }
  214. public static WeaverVersionInfo read(VersionedDataInputStream s) throws IOException {
  215. short major = s.readShort();
  216. short minor = s.readShort();
  217. WeaverVersionInfo wvi = new WeaverVersionInfo(major,minor);
  218. if (s.getMajorVersion()>=WEAVER_VERSION_MAJOR_AJ150M4) {
  219. long stamp = 0;
  220. try {
  221. stamp = s.readLong();
  222. wvi.setBuildstamp(stamp);
  223. } catch (EOFException eof) {
  224. // didnt find that build stamp - its not the end of the world
  225. }
  226. }
  227. return wvi;
  228. }
  229. public short getMajorVersion() {
  230. return major_version;
  231. }
  232. public short getMinorVersion() {
  233. return minor_version;
  234. }
  235. public static short getCurrentWeaverMajorVersion() {
  236. return CURRENT_VERSION_MAJOR;
  237. }
  238. public static short getCurrentWeaverMinorVersion() {
  239. return CURRENT_VERSION_MINOR;
  240. }
  241. public void setBuildstamp(long stamp) {
  242. this.buildstamp = stamp;
  243. }
  244. public long getBuildstamp() {
  245. return buildstamp;
  246. }
  247. public String toString() {
  248. return major_version+"."+minor_version;
  249. }
  250. public static String toCurrentVersionString() {
  251. return CURRENT_VERSION_MAJOR+"."+CURRENT_VERSION_MINOR;
  252. }
  253. }
  254. public static class SourceContextAttribute extends AjAttribute {
  255. public static final String AttributeName = "org.aspectj.weaver.SourceContext";
  256. public String getNameString() {
  257. return AttributeName;
  258. }
  259. private String sourceFileName;
  260. private int[] lineBreaks;
  261. public SourceContextAttribute(String sourceFileName, int[] lineBreaks) {
  262. this.sourceFileName = sourceFileName;
  263. this.lineBreaks = lineBreaks;
  264. }
  265. public void write(DataOutputStream s) throws IOException {
  266. s.writeUTF(sourceFileName);
  267. FileUtil.writeIntArray(lineBreaks, s);
  268. }
  269. public static SourceContextAttribute read(VersionedDataInputStream s) throws IOException {
  270. return new SourceContextAttribute(s.readUTF(), FileUtil.readIntArray(s));
  271. }
  272. public int[] getLineBreaks() {
  273. return lineBreaks;
  274. }
  275. public String getSourceFileName() {
  276. return sourceFileName;
  277. }
  278. }
  279. public static class MethodDeclarationLineNumberAttribute extends AjAttribute {
  280. public static final String AttributeName = "org.aspectj.weaver.MethodDeclarationLineNumber";
  281. public String getNameString() {
  282. return AttributeName;
  283. }
  284. private int lineNumber;
  285. // AV: added in 1.5 M3 thus handling cases where we don't have that information
  286. private int offset;
  287. public MethodDeclarationLineNumberAttribute(int line, int offset) {
  288. this.lineNumber = line;
  289. this.offset = offset;
  290. }
  291. public int getLineNumber() { return lineNumber; }
  292. public int getOffset() { return offset; }
  293. public void write(DataOutputStream s) throws IOException {
  294. s.writeInt(lineNumber);
  295. s.writeInt(offset);
  296. }
  297. public static MethodDeclarationLineNumberAttribute read(VersionedDataInputStream s) throws IOException {
  298. int line = s.readInt();
  299. int offset = 0;
  300. if (s.available()>0) {
  301. offset = s.readInt();
  302. }
  303. return new MethodDeclarationLineNumberAttribute(line, offset);
  304. }
  305. public String toString() {
  306. return AttributeName + ": " + lineNumber + ":" + offset;
  307. }
  308. }
  309. public static class PointcutDeclarationAttribute extends AjAttribute {
  310. public static final String AttributeName = "org.aspectj.weaver.PointcutDeclaration";
  311. public String getNameString() {
  312. return AttributeName;
  313. }
  314. private ResolvedPointcutDefinition pointcutDef;
  315. public PointcutDeclarationAttribute(ResolvedPointcutDefinition pointcutDef) {
  316. this.pointcutDef = pointcutDef;
  317. }
  318. public void write(DataOutputStream s) throws IOException {
  319. pointcutDef.write(s);
  320. }
  321. public ResolvedPointcutDefinition reify() {
  322. return pointcutDef;
  323. }
  324. }
  325. public static class DeclareAttribute extends AjAttribute {
  326. public static final String AttributeName = "org.aspectj.weaver.Declare";
  327. public String getNameString() {
  328. return AttributeName;
  329. }
  330. private Declare declare;
  331. public DeclareAttribute(Declare declare) {
  332. this.declare = declare;
  333. }
  334. public void write(DataOutputStream s) throws IOException {
  335. declare.write(s);
  336. }
  337. public Declare getDeclare() {
  338. return declare;
  339. }
  340. }
  341. public static class AdviceAttribute extends AjAttribute {
  342. public static final String AttributeName = "org.aspectj.weaver.Advice";
  343. public String getNameString() {
  344. return AttributeName;
  345. }
  346. private AdviceKind kind;
  347. private Pointcut pointcut;
  348. private int extraParameterFlags;
  349. private int start;
  350. private int end;
  351. private ISourceContext sourceContext;
  352. // these are only used by around advice
  353. private boolean proceedInInners;
  354. private ResolvedMember[] proceedCallSignatures; // size == # of proceed calls in body
  355. private boolean[] formalsUnchangedToProceed; // size == formals.size
  356. private UnresolvedType[] declaredExceptions;
  357. /**
  358. * @param lexicalPosition must be greater than the lexicalPosition
  359. * of any advice declared before this one in an aspect, otherwise,
  360. * it can be any value.
  361. */
  362. public AdviceAttribute(AdviceKind kind, Pointcut pointcut, int extraArgumentFlags,
  363. int start, int end, ISourceContext sourceContext) {
  364. this.kind = kind;
  365. this.pointcut = pointcut;
  366. this.extraParameterFlags = extraArgumentFlags;
  367. this.start = start;
  368. this.end = end;
  369. this.sourceContext = sourceContext;
  370. //XXX put this back when testing works better (or fails better)
  371. //if (kind == AdviceKind.Around) throw new IllegalArgumentException("not for around");
  372. }
  373. public AdviceAttribute(AdviceKind kind, Pointcut pointcut, int extraArgumentFlags,
  374. int start, int end, ISourceContext sourceContext,
  375. boolean proceedInInners, ResolvedMember[] proceedCallSignatures,
  376. boolean[] formalsUnchangedToProceed, UnresolvedType[] declaredExceptions) {
  377. this.kind = kind;
  378. this.pointcut = pointcut;
  379. this.extraParameterFlags = extraArgumentFlags;
  380. this.start = start;
  381. this.end = end;
  382. this.sourceContext = sourceContext;
  383. if (kind != AdviceKind.Around) throw new IllegalArgumentException("only for around");
  384. this.proceedInInners = proceedInInners;
  385. this.proceedCallSignatures = proceedCallSignatures;
  386. this.formalsUnchangedToProceed = formalsUnchangedToProceed;
  387. this.declaredExceptions = declaredExceptions;
  388. }
  389. public static AdviceAttribute read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  390. AdviceKind kind = AdviceKind.read(s);
  391. if (kind == AdviceKind.Around) {
  392. return new AdviceAttribute(
  393. kind,
  394. Pointcut.read(s, context),
  395. s.readByte(),
  396. s.readInt(), s.readInt(), context,
  397. s.readBoolean(),
  398. ResolvedMemberImpl.readResolvedMemberArray(s, context),
  399. FileUtil.readBooleanArray(s),
  400. UnresolvedType.readArray(s));
  401. } else {
  402. return new AdviceAttribute(
  403. kind,
  404. Pointcut.read(s, context),
  405. s.readByte(),
  406. s.readInt(), s.readInt(), context);
  407. }
  408. }
  409. public void write(DataOutputStream s) throws IOException {
  410. kind.write(s);
  411. pointcut.write(s);
  412. s.writeByte(extraParameterFlags);
  413. s.writeInt(start);
  414. s.writeInt(end);
  415. if (kind == AdviceKind.Around) {
  416. s.writeBoolean(proceedInInners);
  417. ResolvedMemberImpl.writeArray(proceedCallSignatures, s);
  418. FileUtil.writeBooleanArray(formalsUnchangedToProceed, s);
  419. UnresolvedType.writeArray(declaredExceptions, s);
  420. }
  421. }
  422. public Advice reify(Member signature, World world) {
  423. return world.createAdviceMunger(this, pointcut, signature);
  424. }
  425. public String toString() {
  426. return "AdviceAttribute(" + kind + ", " + pointcut + ", " +
  427. extraParameterFlags + ", " + start+")";
  428. }
  429. public int getExtraParameterFlags() {
  430. return extraParameterFlags;
  431. }
  432. public AdviceKind getKind() {
  433. return kind;
  434. }
  435. public Pointcut getPointcut() {
  436. return pointcut;
  437. }
  438. public UnresolvedType[] getDeclaredExceptions() {
  439. return declaredExceptions;
  440. }
  441. public boolean[] getFormalsUnchangedToProceed() {
  442. return formalsUnchangedToProceed;
  443. }
  444. public ResolvedMember[] getProceedCallSignatures() {
  445. return proceedCallSignatures;
  446. }
  447. public boolean isProceedInInners() {
  448. return proceedInInners;
  449. }
  450. public int getEnd() {
  451. return end;
  452. }
  453. public ISourceContext getSourceContext() {
  454. return sourceContext;
  455. }
  456. public int getStart() {
  457. return start;
  458. }
  459. }
  460. public static class Aspect extends AjAttribute {
  461. public static final String AttributeName = "org.aspectj.weaver.Aspect";
  462. public String getNameString() {
  463. return AttributeName;
  464. }
  465. private PerClause perClause;
  466. private IScope resolutionScope;
  467. public Aspect(PerClause perClause) {
  468. this.perClause = perClause;
  469. }
  470. public PerClause reify(ResolvedType inAspect) {
  471. //XXXperClause.concretize(inAspect);
  472. return perClause;
  473. }
  474. public PerClause reifyFromAtAspectJ(ResolvedType inAspect) {
  475. perClause.resolve(resolutionScope);
  476. return perClause;
  477. }
  478. public void write(DataOutputStream s) throws IOException {
  479. perClause.write(s);
  480. }
  481. public void setResolutionScope(IScope binding) {
  482. this.resolutionScope = binding;
  483. }
  484. }
  485. public static class PrivilegedAttribute extends AjAttribute {
  486. public static final String AttributeName = "org.aspectj.weaver.Privileged";
  487. public String getNameString() {
  488. return AttributeName;
  489. }
  490. private ResolvedMember[] accessedMembers;
  491. public PrivilegedAttribute(ResolvedMember[] accessedMembers) {
  492. this.accessedMembers = accessedMembers;
  493. }
  494. public void write(DataOutputStream s) throws IOException {
  495. ResolvedMemberImpl.writeArray(accessedMembers, s);
  496. }
  497. public ResolvedMember[] getAccessedMembers() {
  498. return accessedMembers;
  499. }
  500. public static PrivilegedAttribute read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  501. return new PrivilegedAttribute(ResolvedMemberImpl.readResolvedMemberArray(s, context));
  502. }
  503. }
  504. public static class EffectiveSignatureAttribute extends AjAttribute {
  505. public static final String AttributeName = "org.aspectj.weaver.EffectiveSignature";
  506. public String getNameString() {
  507. return AttributeName;
  508. }
  509. private ResolvedMember effectiveSignature;
  510. private Shadow.Kind shadowKind;
  511. private boolean weaveBody;
  512. public EffectiveSignatureAttribute(ResolvedMember effectiveSignature, Shadow.Kind shadowKind, boolean weaveBody) {
  513. this.effectiveSignature = effectiveSignature;
  514. this.shadowKind = shadowKind;
  515. this.weaveBody = weaveBody;
  516. }
  517. public void write(DataOutputStream s) throws IOException {
  518. effectiveSignature.write(s);
  519. shadowKind.write(s);
  520. s.writeBoolean(weaveBody);
  521. }
  522. public static EffectiveSignatureAttribute read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  523. return new EffectiveSignatureAttribute(
  524. ResolvedMemberImpl.readResolvedMember(s, context),
  525. Shadow.Kind.read(s),
  526. s.readBoolean());
  527. }
  528. public ResolvedMember getEffectiveSignature() {
  529. return effectiveSignature;
  530. }
  531. public String toString() {
  532. return "EffectiveSignatureAttribute(" + effectiveSignature + ", " + shadowKind + ")";
  533. }
  534. public Shadow.Kind getShadowKind() {
  535. return shadowKind;
  536. }
  537. public boolean isWeaveBody() {
  538. return weaveBody;
  539. }
  540. }
  541. }