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

changeset 1217
d2508136751c
parent 1215
161230ec7c73
child 1221
c2234816495f
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Mar 02 12:57:47 2012 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Mar 02 12:58:35 2012 +0000
     1.3 @@ -1835,7 +1835,7 @@
     1.4                     steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
     1.5                     sym.kind >= ERRONEOUS) {
     1.6                  currentResolutionContext.step = steps.head;
     1.7 -                sym = findConstructor(pos, env, site, argtypes, typeargtypes,
     1.8 +                sym = findDiamond(env, site, argtypes, typeargtypes,
     1.9                          steps.head.isBoxingRequired(),
    1.10                          env.info.varArgs = steps.head.isVarargsRequired());
    1.11                  currentResolutionContext.resolutionCache.put(steps.head, sym);
    1.12 @@ -1867,6 +1867,43 @@
    1.13          }
    1.14      }
    1.15  
    1.16 +    /** This method scans all the constructor symbol in a given class scope -
    1.17 +     *  assuming that the original scope contains a constructor of the kind:
    1.18 +     *  Foo(X x, Y y), where X,Y are class type-variables declared in Foo,
    1.19 +     *  a method check is executed against the modified constructor type:
    1.20 +     *  <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond
    1.21 +     *  inference. The inferred return type of the synthetic constructor IS
    1.22 +     *  the inferred type for the diamond operator.
    1.23 +     */
    1.24 +    private Symbol findDiamond(Env<AttrContext> env,
    1.25 +                              Type site,
    1.26 +                              List<Type> argtypes,
    1.27 +                              List<Type> typeargtypes,
    1.28 +                              boolean allowBoxing,
    1.29 +                              boolean useVarargs) {
    1.30 +        Symbol bestSoFar = methodNotFound;
    1.31 +        for (Scope.Entry e = site.tsym.members().lookup(names.init);
    1.32 +             e.scope != null;
    1.33 +             e = e.next()) {
    1.34 +            //- System.out.println(" e " + e.sym);
    1.35 +            if (e.sym.kind == MTH &&
    1.36 +                (e.sym.flags_field & SYNTHETIC) == 0) {
    1.37 +                    List<Type> oldParams = e.sym.type.tag == FORALL ?
    1.38 +                            ((ForAll)e.sym.type).tvars :
    1.39 +                            List.<Type>nil();
    1.40 +                    Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
    1.41 +                            types.createMethodTypeWithReturn(e.sym.type.asMethodType(), site));
    1.42 +                    bestSoFar = selectBest(env, site, argtypes, typeargtypes,
    1.43 +                            new MethodSymbol(e.sym.flags(), names.init, constrType, site.tsym),
    1.44 +                            bestSoFar,
    1.45 +                            allowBoxing,
    1.46 +                            useVarargs,
    1.47 +                            false);
    1.48 +            }
    1.49 +        }
    1.50 +        return bestSoFar;
    1.51 +    }
    1.52 +
    1.53      /** Resolve constructor.
    1.54       *  @param pos       The position to use for error reporting.
    1.55       *  @param env       The environment current at the constructor invocation.

mercurial