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.

InterTypeFieldBinding.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.lookup;
  13. import org.aspectj.weaver.AjcMemberMaker;
  14. import org.aspectj.weaver.ResolvedTypeMunger;
  15. import org.aspectj.weaver.UnresolvedType;
  16. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InvocationSite;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Scope;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SyntheticMethodBinding;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  25. public class InterTypeFieldBinding extends FieldBinding {
  26. public ReferenceBinding targetType;
  27. public SyntheticMethodBinding reader;
  28. public SyntheticMethodBinding writer;
  29. public AbstractMethodDeclaration sourceMethod;
  30. public InterTypeFieldBinding(EclipseFactory world, ResolvedTypeMunger munger, UnresolvedType withinType,
  31. AbstractMethodDeclaration sourceMethod) {
  32. super(world.makeFieldBinding(munger.getSignature(), munger.getTypeVariableAliases()), null);
  33. this.sourceMethod = sourceMethod;
  34. targetType = (ReferenceBinding) world.makeTypeBinding(munger.getSignature().getDeclaringType());
  35. this.declaringClass = (ReferenceBinding) world.makeTypeBinding(withinType);
  36. // We called the super() with null, we must now do the last step that will have been skipped because of this, see the
  37. // supers() final line:
  38. // OPTIMIZE dont makeFieldBinding twice, HORRIBLE
  39. setAnnotations(world.makeFieldBinding(munger.getSignature(), munger.getTypeVariableAliases()).getAnnotations(), false);
  40. reader = new SimpleSyntheticAccessMethodBinding(world.makeMethodBinding(AjcMemberMaker.interFieldGetDispatcher(munger
  41. .getSignature(), withinType)));
  42. writer = new SimpleSyntheticAccessMethodBinding(world.makeMethodBinding(AjcMemberMaker.interFieldSetDispatcher(munger
  43. .getSignature(), withinType)));
  44. }
  45. public boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
  46. scope.compilationUnitScope().recordTypeReference(declaringClass);
  47. // System.err.println("canBeSeenBy: " + this + ", " + isPublic());
  48. if (isPublic())
  49. return true;
  50. SourceTypeBinding invocationType = scope.invocationType();
  51. // System.out.println("receiver: " + receiverType + ", " + invocationType);
  52. ReferenceBinding declaringType = declaringClass;
  53. if (invocationType == null) // static import call
  54. return !isPrivate() && scope.getCurrentPackage() == receiverType.getPackage();
  55. // FIXME asc what about parameterized types and private ITD generic fields on interfaces?
  56. // Don't work with a raw type, work with the generic type
  57. if (declaringClass.isRawType())
  58. declaringType = ((RawTypeBinding) declaringClass).type;
  59. if (invocationType == declaringType)
  60. return true;
  61. // if (invocationType.isPrivileged) {
  62. // System.out.println("privileged access to: " + this);
  63. // return true;
  64. // }
  65. if (isProtected()) {
  66. throw new RuntimeException("unimplemented");
  67. }
  68. // XXX make sure this walks correctly
  69. if (isPrivate()) {
  70. // answer true if the receiverType is the declaringClass
  71. // AND the invocationType and the declaringClass have a common enclosingType
  72. // see pr149071 - it has caused me to comment out this block below - what
  73. // is it trying to achieve? Possibly it should be using the scope.parentScope (the class scope of
  74. // where the reference is being made) rather than the receiver type
  75. // Is the receiverType an innertype of the declaring type?
  76. // boolean receiverTypeIsSameOrInsideDeclaringType = receiverType == declaringType;
  77. // ReferenceBinding typeToCheckNext = receiverType.enclosingType();
  78. // while (!receiverTypeIsSameOrInsideDeclaringType && typeToCheckNext!=null) {
  79. // if (typeToCheckNext==declaringType) receiverTypeIsSameOrInsideDeclaringType=true;
  80. // }
  81. // if (!receiverTypeIsSameOrInsideDeclaringType) return false;
  82. // the code above replaces this line: (pr118698)
  83. // if (receiverType != declaringType) return false;
  84. if (invocationType != declaringType) {
  85. ReferenceBinding outerInvocationType = invocationType;
  86. ReferenceBinding temp = outerInvocationType.enclosingType();
  87. while (temp != null) {
  88. outerInvocationType = temp;
  89. temp = temp.enclosingType();
  90. }
  91. ReferenceBinding outerDeclaringClass = declaringType;
  92. temp = outerDeclaringClass.enclosingType();
  93. while (temp != null) {
  94. outerDeclaringClass = temp;
  95. temp = temp.enclosingType();
  96. }
  97. if (outerInvocationType != outerDeclaringClass)
  98. return false;
  99. }
  100. return true;
  101. }
  102. // isDefault()
  103. if (invocationType.fPackage == declaringClass.fPackage)
  104. return true;
  105. return false;
  106. }
  107. public SyntheticMethodBinding getAccessMethod(boolean isReadAccess) {
  108. if (isReadAccess)
  109. return reader;
  110. else
  111. return writer;
  112. }
  113. public boolean alwaysNeedsAccessMethod(boolean isReadAccess) {
  114. return true;
  115. }
  116. public ReferenceBinding getTargetType() {
  117. return targetType;
  118. }
  119. // overrides ITD'd method in FieldBinding...
  120. public ReferenceBinding getOwningClass() {
  121. return targetType;
  122. }
  123. }