< prev index next >

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

Print this page
*** 188,10 ***
--- 188,14 ---
       * This is required when a capturing local class is created from a lambda (in which
       * case the captured symbols should be replaced with the translated lambda symbols).
       */
      Map<Symbol, Symbol> lambdaTranslationMap = null;
  
+     /** A hash table mapping local classes to a set of outer this fields
+      */
+     public Map<ClassSymbol, Set<JCExpression>> initializerOuterThis = new WeakHashMap<>();
+ 
      /** A navigator class for assembling a mapping from local class symbols
       *  to class definition trees.
       *  There is only one case; all other cases simply traverse down the tree.
       */
      class ClassMap extends TreeScanner {

*** 904,11 ***
          for (List<ClassSymbol> l = accessConstrTags; l.nonEmpty(); l = l.tail) {
              ClassSymbol c = l.head;
              if (isTranslatedClassAvailable(c))
                  continue;
              // Create class definition tree.
!             JCClassDecl cdec = makeEmptyClass(STATIC | SYNTHETIC,
                      c.outermostClass(), c.flatname, false);
              swapAccessConstructorTag(c, cdec.sym);
              translated.append(cdec);
          }
      }
--- 908,11 ---
          for (List<ClassSymbol> l = accessConstrTags; l.nonEmpty(); l = l.tail) {
              ClassSymbol c = l.head;
              if (isTranslatedClassAvailable(c))
                  continue;
              // Create class definition tree.
!             JCClassDecl cdec = makeEmptyClass(STATIC | SYNTHETIC | IDENTITY_TYPE,
                      c.outermostClass(), c.flatname, false);
              swapAccessConstructorTag(c, cdec.sym);
              translated.append(cdec);
          }
      }

*** 1388,11 ***
              Name flatname = names.fromString("" + topClass.getQualifiedName() +
                                              target.syntheticNameChar() +
                                              i);
              ClassSymbol ctag = chk.getCompiled(topModle, flatname);
              if (ctag == null)
!                 ctag = makeEmptyClass(STATIC | SYNTHETIC, topClass).sym;
              else if (!ctag.isAnonymous())
                  continue;
              // keep a record of all tags, to verify that all are generated as required
              accessConstrTags = accessConstrTags.prepend(ctag);
              return ctag;
--- 1392,11 ---
              Name flatname = names.fromString("" + topClass.getQualifiedName() +
                                              target.syntheticNameChar() +
                                              i);
              ClassSymbol ctag = chk.getCompiled(topModle, flatname);
              if (ctag == null)
!                 ctag = makeEmptyClass(STATIC | SYNTHETIC | IDENTITY_TYPE, topClass).sym;
              else if (!ctag.isAnonymous())
                  continue;
              // keep a record of all tags, to verify that all are generated as required
              accessConstrTags = accessConstrTags.prepend(ctag);
              return ctag;

*** 1632,11 ***
      /** Definition for this$n field.
       *  @param pos        The source code position of the definition.
       *  @param owner      The class in which the definition goes.
       */
      JCVariableDecl outerThisDef(int pos, ClassSymbol owner) {
!         VarSymbol outerThis = makeOuterThisVarSymbol(owner, FINAL | SYNTHETIC);
          return makeOuterThisVarDecl(pos, outerThis);
      }
  
      /** Return a list of trees that load the free variables in given list,
       *  in reverse order.
--- 1636,11 ---
      /** Definition for this$n field.
       *  @param pos        The source code position of the definition.
       *  @param owner      The class in which the definition goes.
       */
      JCVariableDecl outerThisDef(int pos, ClassSymbol owner) {
!         VarSymbol outerThis = makeOuterThisVarSymbol(owner, FINAL | SYNTHETIC | (owner.isValueClass() ? STRICT : 0));
          return makeOuterThisVarDecl(pos, outerThis);
      }
  
      /** Return a list of trees that load the free variables in given list,
       *  in reverse order.

*** 1965,11 ***
          Scope s = clazz.members();
          for (Symbol sym : s.getSymbols(NON_RECURSIVE))
              if (sym.kind == TYP &&
                  sym.name == names.empty &&
                  (sym.flags() & INTERFACE) == 0) return (ClassSymbol) sym;
!         return makeEmptyClass(STATIC | SYNTHETIC, clazz).sym;
      }
  
      /** Create an attributed tree of the form left.name(). */
      private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
          Assert.checkNonNull(left.type);
--- 1969,11 ---
          Scope s = clazz.members();
          for (Symbol sym : s.getSymbols(NON_RECURSIVE))
              if (sym.kind == TYP &&
                  sym.name == names.empty &&
                  (sym.flags() & INTERFACE) == 0) return (ClassSymbol) sym;
!         return makeEmptyClass(STATIC | SYNTHETIC | IDENTITY_TYPE, clazz).sym;
      }
  
      /** Create an attributed tree of the form left.name(). */
      private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
          Assert.checkNonNull(left.type);

*** 2017,11 ***
      /**Used to create an auxiliary class to hold $assertionsDisabled for interfaces.
       */
      private ClassSymbol assertionsDisabledClass() {
          if (assertionsDisabledClassCache != null) return assertionsDisabledClassCache;
  
!         assertionsDisabledClassCache = makeEmptyClass(STATIC | SYNTHETIC, outermostClassDef.sym).sym;
  
          return assertionsDisabledClassCache;
      }
  
      // This code is not particularly robust if the user has
--- 2021,11 ---
      /**Used to create an auxiliary class to hold $assertionsDisabled for interfaces.
       */
      private ClassSymbol assertionsDisabledClass() {
          if (assertionsDisabledClassCache != null) return assertionsDisabledClassCache;
  
!         assertionsDisabledClassCache = makeEmptyClass(STATIC | SYNTHETIC | IDENTITY_TYPE, outermostClassDef.sym).sym;
  
          return assertionsDisabledClassCache;
      }
  
      // This code is not particularly robust if the user has

*** 2882,23 ***
              ListBuffer<VarSymbol> fields = new ListBuffer<>();
              for (Symbol sym : currentClass.getEnclosedElements()) {
                  if (sym.kind == Kinds.Kind.VAR && ((sym.flags() & RECORD) != 0))
                      fields.append((VarSymbol) sym);
              }
              for (VarSymbol field: fields) {
                  if ((field.flags_field & Flags.UNINITIALIZED_FIELD) != 0) {
                      VarSymbol param = tree.params.stream().filter(p -> p.name == field.name).findFirst().get().sym;
                      make.at(tree.pos);
!                     tree.body.stats = tree.body.stats.append(
!                             make.Exec(
!                                     make.Assign(
!                                             make.Select(make.This(field.owner.erasure(types)), field),
-                                             make.Ident(param)).setType(field.erasure(types))));
-                     // we don't need the flag at the field anymore
                      field.flags_field &= ~Flags.UNINITIALIZED_FIELD;
                  }
              }
          }
          result = tree;
      }
      //where
          private Map<Symbol, Symbol> makeTranslationMap(JCMethodDecl tree) {
--- 2886,29 ---
              ListBuffer<VarSymbol> fields = new ListBuffer<>();
              for (Symbol sym : currentClass.getEnclosedElements()) {
                  if (sym.kind == Kinds.Kind.VAR && ((sym.flags() & RECORD) != 0))
                      fields.append((VarSymbol) sym);
              }
+             ListBuffer<JCStatement> initializers = new ListBuffer<>();
              for (VarSymbol field: fields) {
                  if ((field.flags_field & Flags.UNINITIALIZED_FIELD) != 0) {
                      VarSymbol param = tree.params.stream().filter(p -> p.name == field.name).findFirst().get().sym;
                      make.at(tree.pos);
!                     initializers.add(make.Exec(
!                             make.Assign(
!                                     make.Select(make.This(field.owner.erasure(types)), field),
!                                     make.Ident(param)).setType(field.erasure(types))));
                      field.flags_field &= ~Flags.UNINITIALIZED_FIELD;
                  }
              }
+             if (initializers.nonEmpty()) {
+                 if (tree.sym.owner.isValueClass()) {
+                     TreeInfo.mapSuperCalls(tree.body, supercall -> make.Block(0, initializers.toList().append(supercall)));
+                 } else {
+                     tree.body.stats = tree.body.stats.appendList(initializers);
+                 }
+             }
          }
          result = tree;
      }
      //where
          private Map<Symbol, Symbol> makeTranslationMap(JCMethodDecl tree) {

*** 2956,10 ***
--- 2966,21 ---
                  // local class
                  thisArg = makeThis(tree.pos(), c.type.getEnclosingType().tsym);
              } else {
                  // nested class
                  thisArg = makeOwnerThis(tree.pos(), c, false);
+                 if (currentMethodSym != null &&
+                         ((currentMethodSym.flags_field & (STATIC | BLOCK)) == BLOCK) &&
+                         currentMethodSym.owner.isValueClass()) {
+                     // instance initializer in a value class
+                     Set<JCExpression> outerThisSet = initializerOuterThis.get(currentClass);
+                     if (outerThisSet == null) {
+                         outerThisSet = new HashSet<>();
+                     }
+                     outerThisSet.add(thisArg);
+                     initializerOuterThis.put(currentClass, outerThisSet);
+                 }
              }
              tree.args = tree.args.prepend(thisArg);
          }
          tree.encl = null;
  
< prev index next >