langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
changeset 34757 85ceb9850b1d
parent 34756 d31f11c4cc75
child 34995 e4e19f521ee7
equal deleted inserted replaced
34756:d31f11c4cc75 34757:85ceb9850b1d
   595             return false;
   595             return false;
   596         }
   596         }
   597     }
   597     }
   598 
   598 
   599     public Type removeWildcards(Type site) {
   599     public Type removeWildcards(Type site) {
   600         Type capturedSite = capture(site);
   600         if (site.getTypeArguments().stream().anyMatch(t -> t.hasTag(WILDCARD))) {
   601         if (capturedSite != site) {
   601             //compute non-wildcard parameterization - JLS 9.9
   602             Type formalInterface = site.tsym.type;
   602             List<Type> actuals = site.getTypeArguments();
   603             ListBuffer<Type> typeargs = new ListBuffer<>();
   603             List<Type> formals = site.tsym.type.getTypeArguments();
   604             List<Type> actualTypeargs = site.getTypeArguments();
   604             ListBuffer<Type> targs = new ListBuffer<>();
   605             List<Type> capturedTypeargs = capturedSite.getTypeArguments();
   605             for (Type formal : formals) {
   606             //simply replace the wildcards with its bound
   606                 Type actual = actuals.head;
   607             for (Type t : formalInterface.getTypeArguments()) {
   607                 Type bound = formal.getUpperBound();
   608                 if (actualTypeargs.head.hasTag(WILDCARD)) {
   608                 if (actuals.head.hasTag(WILDCARD)) {
   609                     WildcardType wt = (WildcardType)actualTypeargs.head;
   609                     WildcardType wt = (WildcardType)actual;
   610                     Type bound;
   610                     //check that bound does not contain other formals
   611                     switch (wt.kind) {
   611                     if (bound.containsAny(formals)) {
   612                         case EXTENDS:
   612                         targs.add(wt.type);
   613                         case UNBOUND:
   613                     } else {
   614                             CapturedType capVar = (CapturedType)capturedTypeargs.head;
   614                         //compute new type-argument based on declared bound and wildcard bound
   615                             //use declared bound if it doesn't depend on formal type-args
   615                         switch (wt.kind) {
   616                             bound = capVar.bound.containsAny(capturedSite.getTypeArguments()) ?
   616                             case UNBOUND:
   617                                     wt.type : capVar.bound;
   617                                 targs.add(bound);
   618                             break;
   618                                 break;
   619                         default:
   619                             case EXTENDS:
   620                             bound = wt.type;
   620                                 targs.add(glb(bound, wt.type));
       
   621                                 break;
       
   622                             case SUPER:
       
   623                                 targs.add(wt.type);
       
   624                                 break;
       
   625                             default:
       
   626                                 Assert.error("Cannot get here!");
       
   627                         }
   621                     }
   628                     }
   622                     typeargs.append(bound);
       
   623                 } else {
   629                 } else {
   624                     typeargs.append(actualTypeargs.head);
   630                     //not a wildcard - the new type argument remains unchanged
   625                 }
   631                     targs.add(actual);
   626                 actualTypeargs = actualTypeargs.tail;
   632                 }
   627                 capturedTypeargs = capturedTypeargs.tail;
   633                 actuals = actuals.tail;
   628             }
   634             }
   629             return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
   635             return subst(site.tsym.type, formals, targs.toList());
   630         } else {
   636         } else {
   631             return site;
   637             return site;
   632         }
   638         }
   633     }
   639     }
   634 
   640