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.

SuperFixerVisitor.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 java.util.HashSet;
  15. import java.util.Set;
  16. import org.aspectj.ajdt.internal.compiler.lookup.AjLookupEnvironment;
  17. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  18. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeMethodBinding;
  19. import org.aspectj.asm.internal.CharOperation;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MessageSend;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ThisReference;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodScope;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  36. import org.aspectj.weaver.NameMangler;
  37. import org.aspectj.weaver.ResolvedMember;
  38. /**
  39. * Takes a method that already has the three extra parameters thisJoinPointStaticPart, thisJoinPoint and
  40. * thisEnclosingJoinPointStaticPart
  41. */
  42. public class SuperFixerVisitor extends ASTVisitor {
  43. Set superMethodsCalled = new HashSet();
  44. AbstractMethodDeclaration method;
  45. ReferenceBinding targetClass;
  46. private int depthCounter = 0; // Keeps track of whether we are inside any nested local type declarations
  47. SuperFixerVisitor(AbstractMethodDeclaration method, ReferenceBinding targetClass) {
  48. this.method = method;
  49. this.targetClass = targetClass;
  50. }
  51. private static final char[] ctor = "<init>".toCharArray();
  52. public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
  53. if (localTypeDeclaration.binding instanceof LocalTypeBinding) {
  54. if (((LocalTypeBinding)localTypeDeclaration.binding).isAnonymousType()) {
  55. localTypeDeclaration.binding.modifiers |=Modifier.PUBLIC;
  56. MethodBinding[] bindings = localTypeDeclaration.binding.methods;
  57. if (bindings!=null) {
  58. for (int i=0,max=bindings.length;i<max;i++) {
  59. if (CharOperation.equals(bindings[i].selector,ctor)) {
  60. bindings[i].modifiers|=Modifier.PUBLIC;
  61. }
  62. }
  63. }
  64. // localTypeDeclaration.modifiers|=Modifier.PUBLIC;
  65. }
  66. }
  67. depthCounter++;
  68. return super.visit(localTypeDeclaration, scope);
  69. }
  70. public void endVisit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
  71. depthCounter--;
  72. }
  73. public void endVisit(MessageSend call, BlockScope scope) {
  74. // System.out.println("endVisit: " + call);
  75. // an error has already occurred
  76. if (call.binding/*codegenBinding*/ == null)
  77. return;
  78. MethodBinding superBinding = call.binding/*codegenBinding*/;
  79. if (superBinding instanceof ProblemMethodBinding) {
  80. return;
  81. }
  82. // InterTypeMethodBindings are always statically bound, so there's no
  83. // need to treat super calls specially here
  84. if (superBinding instanceof InterTypeMethodBinding) {
  85. return;
  86. // InterTypeMethodBinding m = (InterTypeMethodBinding)superBinding;
  87. // if (m.postDispatchMethod != null) {
  88. // call.binding = m.postDispatchMethod;
  89. // }
  90. // return;
  91. }
  92. if (superBinding instanceof ParameterizedMethodBinding) {
  93. superBinding = ((ParameterizedMethodBinding) superBinding).original();
  94. }
  95. EclipseFactory factory = ((AjLookupEnvironment) method.scope.environment()).factory;
  96. if (depthCounter != 0 && targetClass.isInterface()) {// pr198196 - when calling MarkerInterface.super.XXX()
  97. if (call.isSuperAccess() && !call.binding.isStatic()) {
  98. MethodScope currentMethodScope = scope.methodScope();
  99. SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
  100. FieldBinding field = sourceType.addSyntheticFieldForInnerclass(targetClass);
  101. call.receiver = new KnownFieldReference(field, call.receiver.sourceStart, call.receiver.sourceEnd);
  102. } else {
  103. return;
  104. }
  105. } else if (depthCounter == 0) { // Allow case testSuperItds_pr198196_2/3
  106. char[] accessName;
  107. if (call.isSuperAccess() && !call.binding.isStatic()) {
  108. call.receiver = new ThisReference(call.receiver.sourceStart, call.receiver.sourceEnd);
  109. accessName = NameMangler.superDispatchMethod(factory.fromBinding(targetClass), new String(superBinding.selector))
  110. .toCharArray();
  111. } else if (call.receiver.isThis() && call.binding.isProtected() && !call.binding.isStatic()) {
  112. // XXX this is a hack that violates some binary compatibility rules
  113. ReferenceBinding superBindingDeclaringClass = superBinding.declaringClass;
  114. if (superBindingDeclaringClass.isParameterizedType()) {
  115. superBindingDeclaringClass = ((ParameterizedTypeBinding) superBindingDeclaringClass).type;
  116. }
  117. if (superBindingDeclaringClass.equals(targetClass)) {
  118. accessName = NameMangler.protectedDispatchMethod(factory.fromBinding(targetClass),
  119. new String(superBinding.selector)).toCharArray();
  120. } else {
  121. accessName = NameMangler.superDispatchMethod(factory.fromBinding(targetClass),
  122. new String(superBinding.selector)).toCharArray();
  123. }
  124. } else {
  125. return;
  126. }
  127. // ??? do we want these to be unique
  128. MethodBinding superAccessBinding = new MethodBinding(ClassFileConstants.AccPublic, accessName, superBinding.returnType,
  129. superBinding.parameters, superBinding.thrownExceptions, targetClass);
  130. AstUtil.replaceMethodBinding(call, superAccessBinding);
  131. } else {
  132. return;
  133. }
  134. ResolvedMember targetMember = null;
  135. if (superBinding.declaringClass.isParameterizedType()) { // pr206911
  136. targetMember = factory.makeResolvedMember(superBinding, ((ParameterizedTypeBinding) superBinding.declaringClass)
  137. .genericType());
  138. } else {
  139. targetMember = factory.makeResolvedMember(superBinding);
  140. }
  141. superMethodsCalled.add(targetMember);
  142. }
  143. }