< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java

Print this page
*** 51,11 ***
  import com.sun.tools.javac.util.*;
  import com.sun.tools.javac.util.DefinedBy.Api;
  import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
- import com.sun.tools.javac.util.JCDiagnostic.Warning;
  
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.EnumSet;
  import java.util.HashSet;
--- 51,10 ---

*** 63,11 ***
  import java.util.LinkedHashMap;
  import java.util.Map;
  import java.util.Set;
  import java.util.function.BiFunction;
  import java.util.function.BiPredicate;
- import java.util.function.Consumer;
  import java.util.function.Function;
  import java.util.function.Predicate;
  import java.util.stream.Stream;
  import java.util.stream.StreamSupport;
  
--- 62,10 ---

*** 416,47 ***
          if (env.info.visitingServiceImplementation &&
              env.toplevel.modle == sym.packge().modle) {
              return true;
          }
  
!         switch ((short)(sym.flags() & AccessFlags)) {
!         case PRIVATE:
!             return
!                 (env.enclClass.sym == sym.owner // fast special case
!                  ||
!                  env.enclClass.sym.outermostClass() ==
!                  sym.owner.outermostClass())
!                 &&
!                 sym.isInheritedIn(site.tsym, types);
!         case 0:
!             return
!                 (env.toplevel.packge == sym.owner.owner // fast special case
!                  ||
!                  env.toplevel.packge == sym.packge())
!                 &&
!                 isAccessible(env, site, checkInner)
!                 &&
!                 sym.isInheritedIn(site.tsym, types)
!                 &&
!                 notOverriddenIn(site, sym);
!         case PROTECTED:
!             return
!                 (env.toplevel.packge == sym.owner.owner // fast special case
!                  ||
!                  env.toplevel.packge == sym.packge()
!                  ||
!                  isProtectedAccessible(sym, env.enclClass.sym, site)
!                  ||
!                  // OK to select instance method or field from 'super' or type name
!                  // (but type names should be disallowed elsewhere!)
!                  env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
!                 &&
!                 isAccessible(env, site, checkInner)
!                 &&
!                 notOverriddenIn(site, sym);
!         default: // this case includes erroneous combinations as well
!             return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
          }
      }
      //where
      /* `sym' is accessible only if not overridden by
       * another symbol which is a member of `site'
--- 414,64 ---
          if (env.info.visitingServiceImplementation &&
              env.toplevel.modle == sym.packge().modle) {
              return true;
          }
  
!         ClassSymbol enclosingCsym = env.enclClass.sym;
!         if (sym.kind == MTH || sym.kind == VAR) {
!             /* If any primitive class types are involved, ask the same question in the reference universe,
!                where the hierarchy is navigable
!             */
!             if (site.isPrimitiveClass())
!                 site = site.referenceProjection();
!         } else if (sym.kind == TYP) {
!             // A type is accessible in a reference projection if it was
!             // accessible in the value projection.
!             if (site.isReferenceProjection())
!                 site = site.asValueType();
!         }
!         try {
!             switch ((short)(sym.flags() & AccessFlags)) {
!                 case PRIVATE:
!                     return
!                             (env.enclClass.sym == sym.owner // fast special case
!                                     ||
!                                     env.enclClass.sym.outermostClass() ==
!                                             sym.owner.outermostClass())
!                                     &&
!                                     sym.isInheritedIn(site.tsym, types);
!                 case 0:
!                     return
!                             (env.toplevel.packge == sym.owner.owner // fast special case
!                                     ||
!                                     env.toplevel.packge == sym.packge())
!                                     &&
!                                     isAccessible(env, site, checkInner)
!                                     &&
!                                     sym.isInheritedIn(site.tsym, types)
!                                     &&
!                                     notOverriddenIn(site, sym);
!                 case PROTECTED:
!                     return
!                             (env.toplevel.packge == sym.owner.owner // fast special case
+                                     ||
+                                     env.toplevel.packge == sym.packge()
+                                     ||
+                                     isProtectedAccessible(sym, env.enclClass.sym, site)
+                                     ||
+                                     // OK to select instance method or field from 'super' or type name
+                                     // (but type names should be disallowed elsewhere!)
+                                     env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
+                                     &&
+                                     isAccessible(env, site, checkInner)
+                                     &&
+                                     notOverriddenIn(site, sym);
+                 default: // this case includes erroneous combinations as well
+                     return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
+             }
+         } finally {
+             env.enclClass.sym = enclosingCsym;
          }
      }
      //where
      /* `sym' is accessible only if not overridden by
       * another symbol which is a member of `site'

*** 465,15 ***
       * cannot be overridden (e.g. MH.invokeExact(Object[])).
       */
      private boolean notOverriddenIn(Type site, Symbol sym) {
          if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
              return true;
!         else {
!             Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
!             return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
!                     !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
!         }
      }
      //where
          /** Is given protected symbol accessible if it is selected from given site
           *  and the selection takes place in given class?
           *  @param sym     The symbol with protected access
--- 480,20 ---
       * cannot be overridden (e.g. MH.invokeExact(Object[])).
       */
      private boolean notOverriddenIn(Type site, Symbol sym) {
          if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
              return true;
! 
!         /* If any primitive class types are involved, ask the same question in the reference universe,
!            where the hierarchy is navigable
!         */
!         if (site.isPrimitiveClass())
+             site = site.referenceProjection();
+ 
+         Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
+         return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
+                 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
      }
      //where
          /** Is given protected symbol accessible if it is selected from given site
           *  and the selection takes place in given class?
           *  @param sym     The symbol with protected access

*** 1684,16 ***
                  TypeSymbol m2Owner = (TypeSymbol)m2.owner;
                  // the two owners can never be the same if the target methods are compiled from source,
                  // but we need to protect against cases where the methods are defined in some classfile
                  // and make sure we issue an ambiguity error accordingly (by skipping the logic below).
                  if (m1Owner != m2Owner) {
!                     if (types.asSuper(m1Owner.type, m2Owner) != null &&
                          ((m1.owner.flags_field & INTERFACE) == 0 ||
                           (m2.owner.flags_field & INTERFACE) != 0) &&
                          m1.overrides(m2, m1Owner, types, false))
                          return m1;
!                     if (types.asSuper(m2Owner.type, m1Owner) != null &&
                          ((m2.owner.flags_field & INTERFACE) == 0 ||
                           (m1.owner.flags_field & INTERFACE) != 0) &&
                          m2.overrides(m1, m2Owner, types, false))
                          return m2;
                  }
--- 1704,16 ---
                  TypeSymbol m2Owner = (TypeSymbol)m2.owner;
                  // the two owners can never be the same if the target methods are compiled from source,
                  // but we need to protect against cases where the methods are defined in some classfile
                  // and make sure we issue an ambiguity error accordingly (by skipping the logic below).
                  if (m1Owner != m2Owner) {
!                     if (types.asSuper(m1Owner.type.referenceProjectionOrSelf(), m2Owner) != null &&
                          ((m1.owner.flags_field & INTERFACE) == 0 ||
                           (m2.owner.flags_field & INTERFACE) != 0) &&
                          m1.overrides(m2, m1Owner, types, false))
                          return m1;
!                     if (types.asSuper(m2Owner.type.referenceProjectionOrSelf(), m1Owner) != null &&
                          ((m2.owner.flags_field & INTERFACE) == 0 ||
                           (m1.owner.flags_field & INTERFACE) != 0) &&
                          m2.overrides(m1, m2Owner, types, false))
                          return m2;
                  }

*** 2291,10 ***
--- 2311,26 ---
       */
      Symbol findMemberType(Env<AttrContext> env,
                            Type site,
                            Name name,
                            TypeSymbol c) {
+         return findMemberTypeInternal(env,site, name, c);
+     }
+ 
+     /** Find qualified member type.
+      *  @param env       The current environment.
+      *  @param site      The original type from where the selection takes
+      *                   place.
+      *  @param name      The type's name.
+      *  @param c         The class to search for the member type. This is
+      *                   always a superclass or implemented interface of
+      *                   site's class.
+      */
+     Symbol findMemberTypeInternal(Env<AttrContext> env,
+                           Type site,
+                           Name name,
+                           TypeSymbol c) {
          Symbol sym = findImmediateMemberType(env, site, name, c);
  
          if (sym != typeNotFound)
              return sym;
  

*** 2339,10 ***
--- 2375,18 ---
      /** Find an unqualified type symbol.
       *  @param env       The current environment.
       *  @param name      The type's name.
       */
      Symbol findType(Env<AttrContext> env, Name name) {
+         return findTypeInternal(env, name);
+     }
+ 
+     /** Find an unqualified type symbol.
+      *  @param env       The current environment.
+      *  @param name      The type's name.
+      */
+     Symbol findTypeInternal(Env<AttrContext> env, Name name) {
          if (name == names.empty)
              return typeNotFound; // do not allow inadvertent "lookup" of anonymous types
          Symbol bestSoFar = typeNotFound;
          Symbol sym;
          boolean staticOnly = false;

*** 3556,11 ***
          @Override
          ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
              if (TreeInfo.isStaticSelector(referenceTree.expr, names)) {
                  if (argtypes.nonEmpty() &&
                          (argtypes.head.hasTag(NONE) ||
!                         types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), originalSite))) {
                      return new UnboundMethodReferenceLookupHelper(referenceTree, name,
                              originalSite, argtypes, typeargtypes, maxPhase);
                  } else {
                      return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
                          @Override
--- 3600,11 ---
          @Override
          ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
              if (TreeInfo.isStaticSelector(referenceTree.expr, names)) {
                  if (argtypes.nonEmpty() &&
                          (argtypes.head.hasTag(NONE) ||
!                         types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head.referenceProjectionOrSelf()), originalSite))) {
                      return new UnboundMethodReferenceLookupHelper(referenceTree, name,
                              originalSite, argtypes, typeargtypes, maxPhase);
                  } else {
                      return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
                          @Override

*** 3609,11 ***
  
          UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
                  List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
              super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
              if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
!                 Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
                  this.site = types.skipTypeVars(asSuperSite, true);
              }
          }
  
          @Override
--- 3653,11 ---
  
          UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
                  List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
              super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
              if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
!                 Type asSuperSite = types.asSuper(argtypes.head.referenceProjectionOrSelf(), site.tsym);
                  this.site = types.skipTypeVars(asSuperSite, true);
              }
          }
  
          @Override

*** 3668,11 ***
  
          ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
                  List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
              super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
              if (site.isRaw()) {
!                 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym, site.getMetadata());
                  needsInference = true;
              }
          }
  
          @Override
--- 3712,11 ---
  
          ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
                  List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
              super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
              if (site.isRaw()) {
!                 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym, site.getMetadata(), site.getFlavor());
                  needsInference = true;
              }
          }
  
          @Override

*** 3758,11 ***
              //this might be a default super call if one of the superinterfaces is 'c'
              for (Type t : pruneInterfaces(env.enclClass.type)) {
                  if (t.tsym == c) {
                      env.info.defaultSuperCallSite = t;
                      return new VarSymbol(0, names._super,
!                             types.asSuper(env.enclClass.type, c), env.enclClass.sym);
                  }
              }
              //find a direct super type that is a subtype of 'c'
              for (Type i : types.directSupertypes(env.enclClass.type)) {
                  if (i.tsym.isSubClass(c, types) && i.tsym != c) {
--- 3802,11 ---
              //this might be a default super call if one of the superinterfaces is 'c'
              for (Type t : pruneInterfaces(env.enclClass.type)) {
                  if (t.tsym == c) {
                      env.info.defaultSuperCallSite = t;
                      return new VarSymbol(0, names._super,
!                             types.asSuper(env.enclClass.type.referenceProjectionOrSelf(), c), env.enclClass.sym);
                  }
              }
              //find a direct super type that is a subtype of 'c'
              for (Type i : types.directSupertypes(env.enclClass.type)) {
                  if (i.tsym.isSubClass(c, types) && i.tsym != c) {
< prev index next >