8026936: Initialize LamdbaToMethod lazily and as required

Wed, 23 Oct 2013 15:45:18 -0700

author
ksrini
date
Wed, 23 Oct 2013 15:45:18 -0700
changeset 2166
31fe30e2deac
parent 2165
864dafc5ab7a
child 2167
d2fa3f7e964e

8026936: Initialize LamdbaToMethod lazily and as required
Reviewed-by: jjg, rfield
Contributed-by: jan.lahoda@oracle.com

src/share/classes/com/sun/tools/javac/main/JavaCompiler.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Wed Oct 23 07:50:04 2013 +0200
     1.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Wed Oct 23 15:45:18 2013 -0700
     1.3 @@ -271,10 +271,6 @@
     1.4       */
     1.5      protected TransTypes transTypes;
     1.6  
     1.7 -    /** The lambda translator.
     1.8 -     */
     1.9 -    protected LambdaToMethod lambdaToMethod;
    1.10 -
    1.11      /** The syntactic sugar desweetener.
    1.12       */
    1.13      protected Lower lower;
    1.14 @@ -388,8 +384,6 @@
    1.15  
    1.16          options = Options.instance(context);
    1.17  
    1.18 -        lambdaToMethod = LambdaToMethod.instance(context);
    1.19 -
    1.20          verbose       = options.isSet(VERBOSE);
    1.21          sourceOutput  = options.isSet(PRINTSOURCE); // used to be -s
    1.22          stubOutput    = options.isSet("-stubs");
    1.23 @@ -1393,6 +1387,7 @@
    1.24           */
    1.25          class ScanNested extends TreeScanner {
    1.26              Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>();
    1.27 +            protected boolean hasLambdas;
    1.28              @Override
    1.29              public void visitClassDef(JCClassDecl node) {
    1.30                  Type st = types.supertype(node.sym.type);
    1.31 @@ -1402,7 +1397,18 @@
    1.32                      Env<AttrContext> stEnv = enter.getEnv(c);
    1.33                      if (stEnv != null && env != stEnv) {
    1.34                          if (dependencies.add(stEnv)) {
    1.35 -                            scan(stEnv.tree);
    1.36 +                            boolean prevHasLambdas = hasLambdas;
    1.37 +                            try {
    1.38 +                                scan(stEnv.tree);
    1.39 +                            } finally {
    1.40 +                                /*
    1.41 +                                 * ignore any updates to hasLambdas made during
    1.42 +                                 * the nested scan, this ensures an initalized
    1.43 +                                 * LambdaToMethod is available only to those
    1.44 +                                 * classes that contain lambdas
    1.45 +                                 */
    1.46 +                                hasLambdas = prevHasLambdas;
    1.47 +                            }
    1.48                          }
    1.49                          envForSuperTypeFound = true;
    1.50                      }
    1.51 @@ -1410,6 +1416,16 @@
    1.52                  }
    1.53                  super.visitClassDef(node);
    1.54              }
    1.55 +            @Override
    1.56 +            public void visitLambda(JCLambda tree) {
    1.57 +                hasLambdas = true;
    1.58 +                super.visitLambda(tree);
    1.59 +            }
    1.60 +            @Override
    1.61 +            public void visitReference(JCMemberReference tree) {
    1.62 +                hasLambdas = true;
    1.63 +                super.visitReference(tree);
    1.64 +            }
    1.65          }
    1.66          ScanNested scanner = new ScanNested();
    1.67          scanner.scan(env.tree);
    1.68 @@ -1468,11 +1484,11 @@
    1.69              env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
    1.70              compileStates.put(env, CompileState.TRANSTYPES);
    1.71  
    1.72 -            if (source.allowLambda()) {
    1.73 +            if (source.allowLambda() && scanner.hasLambdas) {
    1.74                  if (shouldStop(CompileState.UNLAMBDA))
    1.75                      return;
    1.76  
    1.77 -                env.tree = lambdaToMethod.translateTopLevelClass(env, env.tree, localMake);
    1.78 +                env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
    1.79                  compileStates.put(env, CompileState.UNLAMBDA);
    1.80              }
    1.81  

mercurial