changeset 48436:f942ae050606 datum

generate getters for fields of abstract records
author vromero
date Fri, 08 Dec 2017 17:33:56 -0500
parents e3b7341fd0d4
children 0bfcdc878c1b
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java test/langtools/tools/javac/datum/Pos01.java
diffstat 3 files changed, 44 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Dec 08 16:32:38 2017 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Dec 08 17:33:56 2017 -0500
@@ -2239,7 +2239,7 @@
             (types.supertype(currentClass.type).tsym.flags() & ENUM) == 0)
             visitEnumDef(tree);
 
-        if ((tree.mods.flags & (RECORD | ABSTRACT)) == RECORD) {
+        if ((tree.mods.flags & RECORD) != 0) {
             visitRecordDef(tree);
         }
 
@@ -2315,31 +2315,31 @@
     List<JCTree> accessors(JCClassDecl tree) {
         ListBuffer<JCTree> buffer = new ListBuffer<>();
         tree.defs.stream()
-                    .filter(t -> t.hasTag(VARDEF))
-                    .map(t -> (JCVariableDecl)t)
-                    .filter(vd -> vd.sym.accessors.nonEmpty())
-                    .forEach(vd -> {
-                        for (Pair<Accessors.Kind, MethodSymbol> accessor : vd.sym.accessors) {
-                            MethodSymbol accessorSym = accessor.snd;
-                            if ((accessorSym.flags() & Flags.MANDATED) != 0) {
-                                make_at(tree.pos());
-                                switch (accessor.fst) {
-                                    case GET:
-                                        buffer.add(make.MethodDef(accessorSym, make.Block(0,
-                                                List.of(make.Return(make.Ident(vd.sym))))));
-                                        break;
-                                    case SET:
-                                        buffer.add(make.MethodDef(accessorSym, make.Block(0,
-                                                List.of(make.Exec(
-                                                        make.Assign(make.Ident(vd.sym), make.Ident(accessorSym.params.head))
-                                                                .setType(vd.sym.type))))));
-                                        break;
-                                    default:
-                                        Assert.error("Cannot get here!");
-                                }
+                .filter(t -> t.hasTag(VARDEF))
+                .map(t -> (JCVariableDecl)t)
+                .filter(vd -> vd.sym.accessors.nonEmpty())
+                .forEach(vd -> {
+                    for (Pair<Accessors.Kind, MethodSymbol> accessor : vd.sym.accessors) {
+                        MethodSymbol accessorSym = accessor.snd;
+                        if ((accessorSym.flags() & Flags.MANDATED) != 0) {
+                            make_at(tree.pos());
+                            switch (accessor.fst) {
+                                case GET:
+                                    buffer.add(make.MethodDef(accessorSym, make.Block(0,
+                                            List.of(make.Return(make.Ident(vd.sym))))));
+                                    break;
+                                case SET:
+                                    buffer.add(make.MethodDef(accessorSym, make.Block(0,
+                                            List.of(make.Exec(
+                                                    make.Assign(make.Ident(vd.sym), make.Ident(accessorSym.params.head))
+                                                            .setType(vd.sym.type))))));
+                                    break;
+                                default:
+                                    Assert.error("Cannot get here!");
                             }
                         }
-                    });
+                    }
+                });
         return buffer.toList();
     }
 
@@ -2509,6 +2509,7 @@
     private EnumSet<GenerationSwitch> generationSwitchSet = EnumSet.noneOf(GenerationSwitch.class);
 
     private void visitRecordDef(JCClassDecl tree) {
+        boolean isAbstract = (tree.mods.flags & ABSTRACT) != 0;
         make_at(tree.pos());
         List<VarSymbol> vars = types.recordVars(tree.type);
         Pool.MethodHandle[] getterMethHandles = new Pool.MethodHandle[vars.size()];
@@ -2522,17 +2523,20 @@
         }
 
         tree.defs = tree.defs.appendList(accessors(tree));
-        tree.defs = tree.defs.appendList(List.of(
-                generationSwitchSet.contains(GenerationSwitch.OLD_EQUALS) ?
-                        recordOldEquals(tree, vars):
-                        recordEquals(tree, getterMethHandles),
-                generationSwitchSet.contains(GenerationSwitch.OLD_TOSTRING) ?
-                        recordOldToString(tree, vars):
-                        recordToString(tree, vars, getterMethHandles),
-                generationSwitchSet.contains(GenerationSwitch.OLD_HASCODE) ?
-                        recordOldHashCode(tree, vars):
-                        recordHashCode(tree, getterMethHandles)
-        ));
+
+        if (!isAbstract) {
+            tree.defs = tree.defs.appendList(List.of(
+                    generationSwitchSet.contains(GenerationSwitch.OLD_EQUALS) ?
+                            recordOldEquals(tree, vars):
+                            recordEquals(tree, getterMethHandles),
+                    generationSwitchSet.contains(GenerationSwitch.OLD_TOSTRING) ?
+                            recordOldToString(tree, vars):
+                            recordToString(tree, vars, getterMethHandles),
+                    generationSwitchSet.contains(GenerationSwitch.OLD_HASCODE) ?
+                            recordOldHashCode(tree, vars):
+                            recordHashCode(tree, getterMethHandles)
+            ));
+        }
     }
 
     JCTree recordToString(JCClassDecl tree, List<VarSymbol> vars, Pool.MethodHandle[] getterMethHandles) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Fri Dec 08 16:32:38 2017 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Fri Dec 08 17:33:56 2017 -0500
@@ -996,8 +996,11 @@
             List<JCTree> defsToEnter = (tree.sym.flags_field & RECORD) != 0 ?
                     tree.defs.diff(List.convert(JCTree.class, TreeInfo.recordFields(tree))) : tree.defs;
             memberEnter.memberEnter(defsToEnter, env);
-            if ((tree.mods.flags & (RECORD | ABSTRACT)) == RECORD) {
-                addRecordMembersIfNeeded(tree, env);
+            if ((tree.mods.flags & RECORD) != 0) {
+                if ((tree.mods.flags & (RECORD | ABSTRACT)) == RECORD) {
+                    addRecordMembersIfNeeded(tree, env);
+                }
+                addAccessorsIfNeeded(tree, env);
             }
 
             if (tree.sym.isAnnotationType()) {
@@ -1075,10 +1078,7 @@
          *  to the symbol table.
          */
         private void addRecordMembersIfNeeded(JCClassDecl tree, Env<AttrContext> env) {
-
             if (lookupMethod(tree.sym, names.toString, List.nil()) == null) {
-                JCExpression toStringType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass));
-
                 // public String toString() { return ???; }
                 JCMethodDecl toString = make.
                     MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD),
@@ -1121,8 +1121,6 @@
                               null);
                 memberEnter.memberEnter(equals, env);
             }
-
-            addAccessorsIfNeeded(tree, env);
         }
 
     }
--- a/test/langtools/tools/javac/datum/Pos01.java	Fri Dec 08 16:32:38 2017 -0500
+++ b/test/langtools/tools/javac/datum/Pos01.java	Fri Dec 08 17:33:56 2017 -0500
@@ -30,10 +30,7 @@
  */
 public class Pos01 {
 
-    static int assertCount;
-
     static void assertTrue(boolean cond) {
-        assertCount++;
         if (!cond) {
             throw new AssertionError();
         }