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 18KB

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