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.

InterTypeDeclaration.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.ajdt.internal.compiler.ast;
  13. import java.lang.reflect.Modifier;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  17. import org.aspectj.ajdt.internal.compiler.lookup.EclipseTypeMunger;
  18. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeScope;
  19. import org.aspectj.ajdt.internal.core.builder.EclipseSourceContext;
  20. import org.aspectj.weaver.*;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.ClassFile;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.*;
  25. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  26. /**
  27. * Base type for all inter-type declarations including methods, fields and constructors.
  28. *
  29. * @author Jim Hugunin
  30. */
  31. public abstract class InterTypeDeclaration extends AjMethodDeclaration {
  32. public TypeReference onType;
  33. protected ReferenceBinding onTypeBinding;
  34. protected ResolvedTypeMunger munger;
  35. protected int declaredModifiers;
  36. protected char[] declaredSelector;
  37. // XXXAJ5 - When the compiler is changed, these will exist somewhere in it...
  38. private final static short ACC_ANNOTATION = 0x2000;
  39. private final static short ACC_ENUM = 0x4000;
  40. public InterTypeDeclaration(CompilationResult result, TypeReference onType) {
  41. super(result);
  42. this.onType = onType;
  43. modifiers = AccPublic | AccStatic;
  44. }
  45. public void setDeclaredModifiers(int modifiers) {
  46. this.declaredModifiers = modifiers;
  47. }
  48. public void setSelector(char[] selector) {
  49. declaredSelector = selector;
  50. this.selector = CharOperation.concat(selector, Integer.toHexString(sourceStart).toCharArray());
  51. }
  52. /**
  53. * Checks that the target for the ITD is not an annotation. If it is, an error message
  54. * is signaled. We return true if it is annotation so the caller knows to stop processing.
  55. * kind is 'constructor', 'field', 'method'
  56. */
  57. public boolean isTargetAnnotation(ClassScope classScope,String kind) {
  58. if ((onTypeBinding.getAccessFlags() & ACC_ANNOTATION)!=0) {
  59. classScope.problemReporter().signalError(sourceStart,sourceEnd,
  60. "can't make inter-type "+kind+" declarations on annotation types.");
  61. ignoreFurtherInvestigation = true;
  62. return true;
  63. }
  64. return false;
  65. }
  66. /**
  67. * Checks that the target for the ITD is not an enum. If it is, an error message
  68. * is signaled. We return true if it is enum so the caller knows to stop processing.
  69. */
  70. public boolean isTargetEnum(ClassScope classScope,String kind) {
  71. if ((onTypeBinding.getAccessFlags() & ACC_ENUM)!=0) {
  72. classScope.problemReporter().signalError(sourceStart,sourceEnd,
  73. "can't make inter-type "+kind+" declarations on enum types.");
  74. ignoreFurtherInvestigation = true;
  75. return true;
  76. }
  77. return false;
  78. }
  79. public void resolve(ClassScope upperScope) {
  80. if (ignoreFurtherInvestigation) return;
  81. ClassScope newParent = new InterTypeScope(upperScope, onTypeBinding);
  82. scope.parent = newParent;
  83. this.scope.isStatic = Modifier.isStatic(declaredModifiers);
  84. fixSuperCallsForInterfaceContext(upperScope);
  85. if (ignoreFurtherInvestigation) return;
  86. super.resolve(newParent);
  87. fixSuperCallsInBody();
  88. }
  89. private void fixSuperCallsForInterfaceContext(ClassScope scope) {
  90. if (onTypeBinding.isInterface()) {
  91. InterSuperFixerVisitor v =
  92. new InterSuperFixerVisitor(this,
  93. EclipseFactory.fromScopeLookupEnvironment(scope), scope);
  94. this.traverse(v, scope);
  95. }
  96. }
  97. /**
  98. * Called from AspectDeclarations.buildInterTypeAndPerClause
  99. */
  100. public abstract EclipseTypeMunger build(ClassScope classScope);
  101. public void fixSuperCallsInBody() {
  102. SuperFixerVisitor v = new SuperFixerVisitor(this, onTypeBinding);
  103. this.traverse(v, (ClassScope)null);
  104. munger.setSuperMethodsCalled(v.superMethodsCalled);
  105. }
  106. protected void resolveOnType(ClassScope classScope) {
  107. checkSpec();
  108. onTypeBinding = (ReferenceBinding)onType.getTypeBindingPublic(classScope);
  109. if (!onTypeBinding.isValidBinding()) {
  110. classScope.problemReporter().invalidType(onType, onTypeBinding);
  111. ignoreFurtherInvestigation = true;
  112. }
  113. }
  114. protected void checkSpec() {
  115. if (Modifier.isProtected(declaredModifiers)) {
  116. scope.problemReporter().signalError(sourceStart, sourceEnd,
  117. "protected inter-type declarations are not allowed");
  118. ignoreFurtherInvestigation = true;
  119. }
  120. }
  121. protected List makeEffectiveSignatureAttribute(
  122. ResolvedMember sig,
  123. Shadow.Kind kind,
  124. boolean weaveBody)
  125. {
  126. List l = new ArrayList(1);
  127. l.add(new EclipseAttributeAdapter(
  128. new AjAttribute.EffectiveSignatureAttribute(sig, kind, weaveBody)));
  129. return l;
  130. }
  131. protected void setMunger(ResolvedTypeMunger munger) {
  132. munger.getSignature().setPosition(sourceStart, sourceEnd);
  133. munger.getSignature().setSourceContext(new EclipseSourceContext(compilationResult));
  134. this.munger = munger;
  135. }
  136. protected int generateInfoAttributes(ClassFile classFile) {
  137. List l;;
  138. Shadow.Kind kind = getShadowKindForBody();
  139. if (kind != null) {
  140. l = makeEffectiveSignatureAttribute(munger.getSignature(), kind, true);
  141. } else {
  142. l = new ArrayList(0);
  143. }
  144. addDeclarationStartLineAttribute(l,classFile);
  145. return classFile.generateMethodInfoAttribute(binding, false, l);
  146. }
  147. protected abstract Shadow.Kind getShadowKindForBody();
  148. public ResolvedMember getSignature() {
  149. if (munger==null) return null; // Can be null in an erroneous program I think
  150. return munger.getSignature();
  151. }
  152. public char[] getDeclaredSelector() {
  153. return declaredSelector;
  154. }
  155. }