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.

InterTypeMethodDeclaration.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.ajdt.internal.compiler.ast;
  13. import java.lang.reflect.Modifier;
  14. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  15. import org.aspectj.ajdt.internal.compiler.lookup.EclipseTypeMunger;
  16. import org.aspectj.ajdt.internal.compiler.problem.AjProblemReporter;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.ClassFile;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Argument;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.codegen.CodeStream;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.codegen.Opcodes;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FlowInfo;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.parser.Parser;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit;
  37. import org.aspectj.weaver.AjAttribute;
  38. import org.aspectj.weaver.AjcMemberMaker;
  39. import org.aspectj.weaver.Constants;
  40. import org.aspectj.weaver.NameMangler;
  41. import org.aspectj.weaver.NewMethodTypeMunger;
  42. import org.aspectj.weaver.ResolvedMember;
  43. import org.aspectj.weaver.ResolvedMemberImpl;
  44. import org.aspectj.weaver.ResolvedType;
  45. import org.aspectj.weaver.Shadow;
  46. import org.aspectj.weaver.UnresolvedType;
  47. /**
  48. * An inter-type method declaration.
  49. *
  50. * @author Jim Hugunin
  51. */
  52. public class InterTypeMethodDeclaration extends InterTypeDeclaration {
  53. public InterTypeMethodDeclaration(CompilationResult result, TypeReference onType) {
  54. super(result, onType);
  55. }
  56. public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
  57. if (ignoreFurtherInvestigation)
  58. return;
  59. if (!Modifier.isAbstract(declaredModifiers)) {
  60. parser.parse(this, unit);
  61. }
  62. }
  63. protected char[] getPrefix() {
  64. return (NameMangler.ITD_PREFIX + "interMethod$").toCharArray();
  65. }
  66. public boolean isFinal() {
  67. return (declaredModifiers & ClassFileConstants.AccFinal) != 0;
  68. }
  69. public void analyseCode(ClassScope currentScope, InitializationFlowContext flowContext, FlowInfo flowInfo) {
  70. if (Modifier.isAbstract(declaredModifiers))
  71. return;
  72. super.analyseCode(currentScope, flowContext, flowInfo);
  73. }
  74. public void resolve(ClassScope upperScope) {
  75. if (munger == null)
  76. ignoreFurtherInvestigation = true;
  77. if (binding == null)
  78. ignoreFurtherInvestigation = true;
  79. if (ignoreFurtherInvestigation)
  80. return;
  81. if (!Modifier.isStatic(declaredModifiers)) {
  82. this.arguments = AstUtil.insert(AstUtil.makeFinalArgument("ajc$this_".toCharArray(), onTypeBinding), this.arguments);
  83. binding.parameters = AstUtil.insert(onTypeBinding, binding.parameters);
  84. }
  85. super.resolve(upperScope);
  86. }
  87. public void resolveStatements() {
  88. checkAndSetModifiersForMethod();
  89. if ((modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0) {
  90. if ((declaredModifiers & ClassFileConstants.AccAbstract) == 0)
  91. scope.problemReporter().methodNeedBody(this);
  92. } else {
  93. // the method HAS a body --> abstract native modifiers are forbiden
  94. if (((declaredModifiers & ClassFileConstants.AccAbstract) != 0))
  95. scope.problemReporter().methodNeedingNoBody(this);
  96. }
  97. // XXX AMC we need to do this, but I'm not 100% comfortable as I don't
  98. // know why the return type is wrong in this case. Also, we don't seem to need
  99. // to do it for args...
  100. if (munger.getSignature().getReturnType().isRawType()) {
  101. if (!binding.returnType.isRawType()) {
  102. EclipseFactory world = EclipseFactory.fromScopeLookupEnvironment(scope);
  103. binding.returnType = world.makeTypeBinding(munger.getSignature().getReturnType());
  104. }
  105. }
  106. // check @Override annotation - based on MethodDeclaration.resolveStatements() @Override processing
  107. checkOverride: {
  108. if (this.binding == null)
  109. break checkOverride;
  110. if (this.scope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5)
  111. break checkOverride;
  112. boolean hasOverrideAnnotation = (this.binding.tagBits & TagBits.AnnotationOverride) != 0;
  113. // Need to verify
  114. if (hasOverrideAnnotation) {
  115. // Work out the real method binding that we can use for comparison
  116. EclipseFactory world = EclipseFactory.fromScopeLookupEnvironment(scope);
  117. MethodBinding realthing = world.makeMethodBinding(munger.getSignature(), munger.getTypeVariableAliases());
  118. boolean reportError = true;
  119. // Go up the hierarchy, looking for something we override
  120. ReferenceBinding supertype = onTypeBinding.superclass();
  121. while (supertype != null && reportError) {
  122. MethodBinding[] possibles = supertype.getMethods(declaredSelector);
  123. for (int i = 0; i < possibles.length; i++) {
  124. MethodBinding mb = possibles[i];
  125. boolean couldBeMatch = true;
  126. if (mb.parameters.length != realthing.parameters.length)
  127. couldBeMatch = false;
  128. else {
  129. for (int j = 0; j < mb.parameters.length && couldBeMatch; j++) {
  130. if (!mb.parameters[j].equals(realthing.parameters[j]))
  131. couldBeMatch = false;
  132. }
  133. }
  134. // return types compatible? (allow for covariance)
  135. if (couldBeMatch && !returnType.resolvedType.isCompatibleWith(mb.returnType))
  136. couldBeMatch = false;
  137. if (couldBeMatch)
  138. reportError = false;
  139. }
  140. supertype = supertype.superclass(); // superclass of object is null
  141. }
  142. // If we couldn't find something we override, report the error
  143. if (reportError)
  144. ((AjProblemReporter) this.scope.problemReporter()).itdMethodMustOverride(this, realthing);
  145. }
  146. }
  147. if (!Modifier.isAbstract(declaredModifiers))
  148. super.resolveStatements();
  149. if (Modifier.isStatic(declaredModifiers)) {
  150. // Check the target for ITD is not an interface
  151. if (onTypeBinding.isInterface()) {
  152. scope.problemReporter().signalError(sourceStart, sourceEnd, "methods in interfaces cannot be declared static");
  153. }
  154. }
  155. }
  156. public EclipseTypeMunger build(ClassScope classScope) {
  157. EclipseFactory factory = EclipseFactory.fromScopeLookupEnvironment(classScope);
  158. resolveOnType(classScope);
  159. if (ignoreFurtherInvestigation)
  160. return null;
  161. binding = classScope.referenceContext.binding.resolveTypesFor(binding);
  162. if (binding == null) {
  163. // if binding is null, we failed to find a type used in the method params, this error
  164. // has already been reported.
  165. this.ignoreFurtherInvestigation = true;
  166. // return null;
  167. throw new AbortCompilationUnit(compilationResult, null);
  168. }
  169. if (isTargetAnnotation(classScope, "method"))
  170. return null; // Error message output in isTargetAnnotation
  171. if (isTargetEnum(classScope, "method"))
  172. return null; // Error message output in isTargetEnum
  173. if (interTypeScope == null)
  174. return null; // We encountered a problem building the scope, don't continue - error already reported
  175. // This signature represents what we want consumers of the targetted type to 'see'
  176. // must use the factory method to build it since there may be typevariables from the binding
  177. // referred to in the parameters/returntype
  178. ResolvedMemberImpl sig = factory.makeResolvedMemberForITD(binding, onTypeBinding, interTypeScope.getRecoveryAliases());
  179. sig.resetName(new String(declaredSelector));
  180. int resetModifiers = declaredModifiers;
  181. if (binding.isVarargs())
  182. resetModifiers = resetModifiers | Constants.ACC_VARARGS;
  183. sig.resetModifiers(resetModifiers);
  184. NewMethodTypeMunger myMunger = new NewMethodTypeMunger(sig, null, typeVariableAliases);
  185. setMunger(myMunger);
  186. ResolvedType aspectType = factory.fromEclipse(classScope.referenceContext.binding);
  187. ResolvedMember me = myMunger.getInterMethodBody(aspectType);
  188. this.selector = binding.selector = me.getName().toCharArray();
  189. return new EclipseTypeMunger(factory, myMunger, aspectType, this);
  190. }
  191. private AjAttribute makeAttribute() {
  192. return new AjAttribute.TypeMunger(munger);
  193. }
  194. public void generateCode(ClassScope classScope, ClassFile classFile) {
  195. if (ignoreFurtherInvestigation) {
  196. // System.err.println("no code for " + this);
  197. return;
  198. }
  199. classFile.extraAttributes.add(new EclipseAttributeAdapter(makeAttribute()));
  200. if (!Modifier.isAbstract(declaredModifiers)) {
  201. super.generateCode(classScope, classFile); // this makes the interMethodBody
  202. }
  203. // annotations on the ITD declaration get put on this method
  204. generateDispatchMethod(classScope, classFile);
  205. }
  206. public void generateDispatchMethod(ClassScope classScope, ClassFile classFile) {
  207. EclipseFactory world = EclipseFactory.fromScopeLookupEnvironment(classScope);
  208. UnresolvedType aspectType = world.fromBinding(classScope.referenceContext.binding);
  209. ResolvedMember signature = munger.getSignature();
  210. ResolvedMember dispatchMember = AjcMemberMaker.interMethodDispatcher(signature, aspectType);
  211. MethodBinding dispatchBinding = world.makeMethodBinding(dispatchMember, munger.getTypeVariableAliases(), munger
  212. .getSignature().getDeclaringType());
  213. MethodBinding introducedMethod = world.makeMethodBinding(AjcMemberMaker.interMethod(signature, aspectType, onTypeBinding
  214. .isInterface()), munger.getTypeVariableAliases());
  215. classFile.generateMethodInfoHeader(dispatchBinding);
  216. int methodAttributeOffset = classFile.contentsOffset;
  217. // Watch out! We are passing in 'binding' here (instead of dispatchBinding) so that
  218. // the dispatch binding attributes will include the annotations from the 'binding'.
  219. // There is a chance that something else on the binding (e.g. throws clause) might
  220. // damage the attributes generated for the dispatch binding.
  221. int attributeNumber = classFile.generateMethodInfoAttributes(binding, makeEffectiveSignatureAttribute(signature,
  222. Shadow.MethodCall, false));
  223. int codeAttributeOffset = classFile.contentsOffset;
  224. classFile.generateCodeAttributeHeader();
  225. CodeStream codeStream = classFile.codeStream;
  226. codeStream.reset(this, classFile);
  227. codeStream.initializeMaxLocals(dispatchBinding);
  228. Argument[] itdArgs = this.arguments;
  229. if (itdArgs != null) {
  230. for (int a = 0; a < itdArgs.length; a++) {
  231. LocalVariableBinding lvb = itdArgs[a].binding;
  232. LocalVariableBinding lvbCopy = new LocalVariableBinding(lvb.name, lvb.type, lvb.modifiers, true);
  233. codeStream.record(lvbCopy);
  234. lvbCopy.recordInitializationStartPC(0);
  235. lvbCopy.resolvedPosition = lvb.resolvedPosition;
  236. }
  237. }
  238. MethodBinding methodBinding = introducedMethod;
  239. TypeBinding[] parameters = methodBinding.parameters;
  240. int length = parameters.length;
  241. int resolvedPosition;
  242. if (methodBinding.isStatic())
  243. resolvedPosition = 0;
  244. else {
  245. codeStream.aload_0();
  246. resolvedPosition = 1;
  247. }
  248. for (int i = 0; i < length; i++) {
  249. codeStream.load(parameters[i], resolvedPosition);
  250. if ((parameters[i] == TypeBinding.DOUBLE) || (parameters[i] == TypeBinding.LONG))
  251. resolvedPosition += 2;
  252. else
  253. resolvedPosition++;
  254. }
  255. // TypeBinding type;
  256. if (methodBinding.isStatic())
  257. codeStream.invoke(Opcodes.OPC_invokestatic,methodBinding,null);
  258. else {
  259. if (methodBinding.declaringClass.isInterface()) {
  260. codeStream.invoke(Opcodes.OPC_invokeinterface, methodBinding, null);
  261. } else {
  262. codeStream.invoke(Opcodes.OPC_invokevirtual, methodBinding, null);
  263. }
  264. }
  265. AstUtil.generateReturn(dispatchBinding.returnType, codeStream);
  266. // tag the local variables as used throughout the method
  267. if (itdArgs != null && codeStream.locals != null) {
  268. for (int a = 0; a < itdArgs.length; a++) {
  269. if (codeStream.locals[a] != null) {
  270. codeStream.locals[a].recordInitializationEndPC(codeStream.position);
  271. }
  272. }
  273. }
  274. classFile.completeCodeAttribute(codeAttributeOffset);
  275. attributeNumber++;
  276. classFile.completeMethodInfo(binding,methodAttributeOffset, attributeNumber);
  277. }
  278. protected Shadow.Kind getShadowKindForBody() {
  279. return Shadow.MethodExecution;
  280. }
  281. // XXX this code is copied from MethodScope, with a few adjustments for ITDs...
  282. private void checkAndSetModifiersForMethod() {
  283. // for reported problems, we want the user to see the declared selector
  284. char[] realSelector = this.selector;
  285. this.selector = declaredSelector;
  286. final ReferenceBinding declaringClass = this.binding.declaringClass;
  287. if ((declaredModifiers & ExtraCompilerModifiers.AccAlternateModifierProblem) != 0)
  288. scope.problemReporter().duplicateModifierForMethod(onTypeBinding, this);
  289. // after this point, tests on the 16 bits reserved.
  290. int realModifiers = declaredModifiers & ExtraCompilerModifiers.AccJustFlag;
  291. // check for abnormal modifiers
  292. int unexpectedModifiers = ~(ClassFileConstants.AccPublic | ClassFileConstants.AccPrivate | ClassFileConstants.AccProtected
  293. | ClassFileConstants.AccAbstract | ClassFileConstants.AccStatic | ClassFileConstants.AccFinal
  294. | ClassFileConstants.AccSynchronized | ClassFileConstants.AccNative | ClassFileConstants.AccStrictfp);
  295. if ((realModifiers & unexpectedModifiers) != 0) {
  296. scope.problemReporter().illegalModifierForMethod(this);
  297. declaredModifiers &= ~ExtraCompilerModifiers.AccJustFlag | ~unexpectedModifiers;
  298. }
  299. // check for incompatible modifiers in the visibility bits, isolate the visibility bits
  300. int accessorBits = realModifiers
  301. & (ClassFileConstants.AccPublic | ClassFileConstants.AccProtected | ClassFileConstants.AccPrivate);
  302. if ((accessorBits & (accessorBits - 1)) != 0) {
  303. scope.problemReporter().illegalVisibilityModifierCombinationForMethod(onTypeBinding, this);
  304. // need to keep the less restrictive so disable Protected/Private as necessary
  305. if ((accessorBits & ClassFileConstants.AccPublic) != 0) {
  306. if ((accessorBits & ClassFileConstants.AccProtected) != 0)
  307. declaredModifiers &= ~ClassFileConstants.AccProtected;
  308. if ((accessorBits & ClassFileConstants.AccPrivate) != 0)
  309. declaredModifiers &= ~ClassFileConstants.AccPrivate;
  310. } else if ((accessorBits & ClassFileConstants.AccProtected) != 0 && (accessorBits & ClassFileConstants.AccPrivate) != 0) {
  311. declaredModifiers &= ~ClassFileConstants.AccPrivate;
  312. }
  313. }
  314. // check for modifiers incompatible with abstract modifier
  315. if ((declaredModifiers & ClassFileConstants.AccAbstract) != 0) {
  316. int incompatibleWithAbstract = ClassFileConstants.AccStatic | ClassFileConstants.AccFinal
  317. | ClassFileConstants.AccSynchronized | ClassFileConstants.AccNative | ClassFileConstants.AccStrictfp;
  318. if ((declaredModifiers & incompatibleWithAbstract) != 0)
  319. scope.problemReporter().illegalAbstractModifierCombinationForMethod(onTypeBinding, this);
  320. if (!onTypeBinding.isAbstract())
  321. scope.problemReporter().abstractMethodInAbstractClass((SourceTypeBinding) onTypeBinding, this);
  322. }
  323. /*
  324. * DISABLED for backward compatibility with javac (if enabled should also mark private methods as final) // methods from a
  325. * final class are final : 8.4.3.3 if (methodBinding.declaringClass.isFinal()) modifiers |= AccFinal;
  326. */
  327. // native methods cannot also be tagged as strictfp
  328. if ((declaredModifiers & ClassFileConstants.AccNative) != 0 && (declaredModifiers & ClassFileConstants.AccStrictfp) != 0)
  329. scope.problemReporter().nativeMethodsCannotBeStrictfp(onTypeBinding, this);
  330. // static members are only authorized in a static member or top level type
  331. if (((realModifiers & ClassFileConstants.AccStatic) != 0) && declaringClass.isNestedType() && !declaringClass.isStatic())
  332. scope.problemReporter().unexpectedStaticModifierForMethod(onTypeBinding, this);
  333. // restore the true selector now that any problems have been reported
  334. this.selector = realSelector;
  335. }
  336. }