changeset 55893:d8ffec159b1f records-and-sealed

API record methods added by the compiler should have origin MANDATED
author vromero
date Tue, 23 Apr 2019 18:18:20 -0400
parents 7b014520e960
children d96a597f272f
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java test/langtools/tools/javac/processing/model/element/TestRecord.java
diffstat 3 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Apr 18 16:48:09 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Tue Apr 23 18:18:20 2019 -0400
@@ -1135,7 +1135,7 @@
             if (lookupMethod(tree.sym, names.toString, List.nil()) == null) {
                 // public String toString() { return ???; }
                 JCMethodDecl toString = make.
-                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD),
+                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD | Flags.MANDATED),
                               names.toString,
                               make.Type(syms.stringType),
                               List.nil(),
@@ -1149,7 +1149,7 @@
             if (lookupMethod(tree.sym, names.hashCode, List.nil()) == null) {
                 // public int hashCode() { return ???; }
                 JCMethodDecl hashCode = make.
-                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD | Flags.FINAL),
+                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD | Flags.FINAL | Flags.MANDATED),
                               names.hashCode,
                               make.Type(syms.intType),
                               List.nil(),
@@ -1163,7 +1163,7 @@
             if (lookupMethod(tree.sym, names.equals, List.of(syms.objectType)) == null) {
                 // public boolean equals(Object o) { return ???; }
                 JCMethodDecl equals = make.
-                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD | Flags.FINAL),
+                    MethodDef(make.Modifiers(Flags.PUBLIC | Flags.RECORD | Flags.FINAL | Flags.MANDATED),
                               names.equals,
                               make.Type(syms.booleanType),
                               List.nil(),
@@ -1181,7 +1181,7 @@
                     lookupMethod(tree.sym, names.readObject, List.nil()) == null) {
                     // private Object readResolve() { return ???; }
                     JCMethodDecl readResolve = make.
-                        MethodDef(make.Modifiers(Flags.PRIVATE | Flags.RECORD | Flags.FINAL),
+                        MethodDef(make.Modifiers(Flags.PRIVATE | Flags.RECORD | Flags.FINAL | Flags.MANDATED),
                                   names.readResolve,
                                   make.Type(syms.objectType),
                                   List.nil(),
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java	Thu Apr 18 16:48:09 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java	Tue Apr 23 18:18:20 2019 -0400
@@ -463,6 +463,8 @@
         Symbol sym = cast(Symbol.class, e);
         if ((sym.flags() & Flags.GENERATEDCONSTR) != 0)
             return Origin.MANDATED;
+        if ((sym.flags() & (Flags.RECORD | Flags.MANDATED)) != 0)
+            return Origin.MANDATED;
         //TypeElement.getEnclosedElements does not return synthetic elements,
         //and most synthetic elements are not read from the classfile anyway:
         return Origin.EXPLICIT;
--- a/test/langtools/tools/javac/processing/model/element/TestRecord.java	Thu Apr 18 16:48:09 2019 -0700
+++ b/test/langtools/tools/javac/processing/model/element/TestRecord.java	Tue Apr 23 18:18:20 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -61,7 +61,7 @@
        return true;
     }
 
-    static record PersonalBest(Duration marathonTime) {
+    static record PersonalBest(Duration marathonTime) implements Serializable {
         private static final Duration MIN_QUAL_TIME = Duration.ofHours(3);
         public boolean bostonQualified() {
             return marathonTime.compareTo(MIN_QUAL_TIME) <= 0;
@@ -72,7 +72,7 @@
      * Verify that a record modeled as an element behaves as expected
      * under 6 and latest specific visitors.
      */
-    private static void testRecord(Element element) {
+    private static void testRecord(Element element, Elements elements) {
         ElementVisitor visitor6 = new ElementKindVisitor6<Void, Void>() {};
 
         try {
@@ -87,6 +87,23 @@
             @Override
             public Object visitTypeAsRecord(TypeElement e,
                                             Void p) {
+                System.out.println("printing record " + e);
+                List<? extends Element> enclosedElements = e.getEnclosedElements();
+                for (Element elem : enclosedElements) {
+                    System.out.println("name " + elem.getSimpleName());
+                    System.out.println("origin " + elements.getOrigin(elem));
+                    switch (elem.getSimpleName().toString()) {
+                        case "marathonTime": case "toString":
+                        case "<init>": case "hashCode":
+                        case "equals": case "readResolve":
+                            if (elements.getOrigin(elem) != Elements.Origin.MANDATED) {
+                                throw new RuntimeException("MANDATED origin expected");
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                }
                 return e; // a non-null value
             }
         };
@@ -107,7 +124,7 @@
            System.out.println("Name: " + element.getSimpleName() +
                               "\tKind: " + element.getKind());
            if (element.getKind() == ElementKind.RECORD) {
-               testRecord(element);
+               testRecord(element, elements);
                recordCount++;
            }
            return super.visitType(element, p);