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.

DeclareDeclaration.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.ast;
  13. //import java.util.List;
  14. import java.util.Collection;
  15. import java.util.Iterator;
  16. import org.aspectj.ajdt.internal.compiler.lookup.EclipseScope;
  17. import org.aspectj.org.eclipse.jdt.core.Flags;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.ClassFile;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.parser.Parser;
  26. import org.aspectj.weaver.AjAttribute;
  27. import org.aspectj.weaver.UnresolvedType;
  28. import org.aspectj.weaver.patterns.Declare;
  29. import org.aspectj.weaver.patterns.DeclareAnnotation;
  30. import org.aspectj.weaver.patterns.DeclareErrorOrWarning;
  31. import org.aspectj.weaver.patterns.DeclareParents;
  32. import org.aspectj.weaver.patterns.DeclarePrecedence;
  33. import org.aspectj.weaver.patterns.DeclareSoft;
  34. import org.aspectj.weaver.patterns.FormalBinding;
  35. public class DeclareDeclaration extends AjMethodDeclaration {
  36. public Declare declareDecl;
  37. /**
  38. * Constructor for IntraTypeDeclaration.
  39. */
  40. public DeclareDeclaration(CompilationResult result, Declare symbolicDeclare) {
  41. super(result);
  42. this.declareDecl = symbolicDeclare;
  43. if (declareDecl != null) {
  44. // AMC added init of declarationSourceXXX fields which are used
  45. // in AsmBuilder for processing of MethodDeclaration locations.
  46. declarationSourceStart = sourceStart = declareDecl.getStart();
  47. declarationSourceEnd = sourceEnd = declareDecl.getEnd();
  48. }
  49. // ??? we might need to set parameters to be empty
  50. this.returnType = TypeReference.baseTypeReference(T_void, 0, null);
  51. }
  52. public void addAtAspectJAnnotations() {
  53. Annotation annotation = null;
  54. if (declareDecl instanceof DeclareAnnotation) {
  55. DeclareAnnotation da = (DeclareAnnotation) declareDecl;
  56. String patternString = da.getPatternAsString();
  57. String annString = da.getAnnotationString();
  58. String kind = da.getKind().toString();
  59. annotation = AtAspectJAnnotationFactory.createDeclareAnnAnnotation(patternString, annString, kind,
  60. declarationSourceStart);
  61. } else if (declareDecl instanceof DeclareErrorOrWarning) {
  62. DeclareErrorOrWarning dd = (DeclareErrorOrWarning) declareDecl;
  63. annotation = AtAspectJAnnotationFactory.createDeclareErrorOrWarningAnnotation(dd.getPointcut().toString(),
  64. dd.getMessage(), dd.isError(), declarationSourceStart);
  65. } else if (declareDecl instanceof DeclareParents) {
  66. DeclareParents dp = (DeclareParents) declareDecl;
  67. String childPattern = dp.getChild().toString();
  68. Collection parentPatterns = dp.getParents().getExactTypes();
  69. StringBuilder parents = new StringBuilder();
  70. for (Iterator iter = parentPatterns.iterator(); iter.hasNext();) {
  71. UnresolvedType urt = ((UnresolvedType) iter.next());
  72. parents.append(urt.getName());
  73. if (iter.hasNext()) {
  74. parents.append(", ");
  75. }
  76. }
  77. annotation = AtAspectJAnnotationFactory.createDeclareParentsAnnotation(childPattern, parents.toString(),
  78. dp.isExtends(), declarationSourceStart);
  79. } else if (declareDecl instanceof DeclarePrecedence) {
  80. DeclarePrecedence dp = (DeclarePrecedence) declareDecl;
  81. String precedenceList = dp.getPatterns().toString();
  82. annotation = AtAspectJAnnotationFactory.createDeclarePrecedenceAnnotation(precedenceList, declarationSourceStart);
  83. } else if (declareDecl instanceof DeclareSoft) {
  84. DeclareSoft ds = (DeclareSoft) declareDecl;
  85. annotation = AtAspectJAnnotationFactory.createDeclareSoftAnnotation(ds.getPointcut().toString(), ds.getException()
  86. .getExactType().getName(), declarationSourceStart);
  87. }
  88. if (annotation != null) {
  89. AtAspectJAnnotationFactory.addAnnotation(this, annotation, this.scope);
  90. }
  91. }
  92. /**
  93. * A declare declaration exists in a classfile only as an attibute on the class. Unlike advice and inter-type declarations, it
  94. * has no corresponding method. **AMC** changed the above policy in the case of declare annotation, which uses a corresponding
  95. * method as the anchor for the declared annotation
  96. */
  97. public void generateCode(ClassScope classScope, ClassFile classFile) {
  98. if (shouldBeSynthetic()) {
  99. this.binding.modifiers |= Flags.AccSynthetic;
  100. }
  101. classFile.extraAttributes.add(new EclipseAttributeAdapter(new AjAttribute.DeclareAttribute(declareDecl)));
  102. if (shouldDelegateCodeGeneration()) {
  103. super.generateCode(classScope, classFile);
  104. }
  105. return;
  106. }
  107. protected boolean shouldDelegateCodeGeneration() {
  108. return true;
  109. }
  110. protected boolean shouldBeSynthetic() {
  111. return true;
  112. }
  113. public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
  114. // do nothing
  115. }
  116. public void resolveStatements(ClassScope upperScope) {
  117. // do nothing
  118. }
  119. // public boolean finishResolveTypes(SourceTypeBinding sourceTypeBinding) {
  120. // // there's nothing for our super to resolve usefully
  121. // //if (!super.finishResolveTypes(sourceTypeBinding)) return false;
  122. // // if (declare == null) return true;
  123. // //
  124. // // EclipseScope scope = new EclipseScope(new FormalBinding[0], this.scope);
  125. // //
  126. // // declare.resolve(scope);
  127. // // return true;
  128. // }
  129. public Declare build(ClassScope classScope) {
  130. if (declareDecl == null) {
  131. return null;
  132. }
  133. EclipseScope scope = new EclipseScope(new FormalBinding[0], classScope);
  134. declareDecl.resolve(scope);
  135. return declareDecl;
  136. }
  137. public StringBuffer print(int tab, StringBuffer output) {
  138. printIndent(tab, output);
  139. if (declareDecl == null) {
  140. output.append("<declare>");
  141. } else {
  142. output.append(declareDecl.toString());
  143. }
  144. return output;
  145. }
  146. /**
  147. * We need the ajc$declare method that is created to represent this declare to be marked as synthetic
  148. */
  149. protected int generateInfoAttributes(ClassFile classFile) {
  150. return super.generateInfoAttributes(classFile, true);
  151. }
  152. public void postParse(TypeDeclaration typeDec) {
  153. super.postParse(typeDec);
  154. int declareSequenceNumberInType = ((AspectDeclaration) typeDec).declareCounter++;
  155. // FIXME asc the name should perhaps include the hashcode of the pattern (type/sig) for binary compatibility reasons!
  156. StringBuilder sb = new StringBuilder();
  157. sb.append("ajc$declare");
  158. // Declares can choose to provide a piece of the name - to enable
  159. // them to be easily distinguised at weave time (e.g. see declare annotation)
  160. if (declareDecl != null) {
  161. String suffix = declareDecl.getNameSuffix();
  162. if (suffix.length() != 0) {
  163. sb.append("_");
  164. sb.append(suffix);
  165. }
  166. }
  167. sb.append("_");
  168. sb.append(declareSequenceNumberInType);
  169. this.selector = sb.toString().toCharArray();
  170. }
  171. }