1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary check that members of abtract datum has protected access
  27  * @modules jdk.jdeps/com.sun.tools.classfile
  28  *          jdk.compiler/com.sun.tools.javac.util
  29  * @compile CheckReadResolveMethodTest.java
  30  * @run main CheckReadResolveMethodTest
  31  */
  32 
  33 import java.io.*;
  34 
  35 import com.sun.tools.classfile.*;
  36 
  37 public class CheckReadResolveMethodTest {
  38 
  39     // readResolve should be generated as a constructor is provided
  40     record Point1(int i, int j) implements Serializable {
  41         Point1(int i, int j) {
  42             this.i = i;
  43             this.j = j;
  44         }
  45     }
  46 
  47     // readResolve should be generated the record implements Serializable
  48     record Point2(int i, int j) implements Serializable {}
  49 
  50     // no readResolve should be generated as the record doesnt implement Serializable
  51     record Point3(int i, int j) {}
  52 
  53     // no readResolve should be generated as an implementation is already provided
  54     record Point4(int i, int j) {
  55         private Object readResolve() {
  56             return new Point4(i, j);
  57         }
  58     }
  59 
  60     // no readResolve should be generated as an implementation of readObject is already provided
  61     record Point5(int i, int j) {
  62         private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {}
  63     }
  64 
  65     public static void main(String args[]) throws Throwable {
  66         new CheckReadResolveMethodTest().run();
  67     }
  68 
  69     void run() throws Throwable {
  70         checkReadResolveMethod("Point1", true);
  71         checkReadResolveMethod("Point2", true);
  72         checkReadResolveMethod("Point3", false);
  73         checkReadResolveMethod("Point4", true);
  74         checkReadResolveMethod("Point5", false);
  75     }
  76 
  77     void checkReadResolveMethod(String className, boolean shouldBeThere) throws Throwable {
  78         File testClasses = new File(System.getProperty("test.classes"));
  79         File file = new File(testClasses,
  80                 CheckReadResolveMethodTest.class.getName() + "$" + className +".class");
  81         ClassFile classFile = ClassFile.read(file);
  82         boolean found = false;
  83         for (Method f : classFile.methods) {
  84             if (f.getName(classFile.constant_pool).equals("readResolve")) {
  85                 found = true;
  86             }
  87         }
  88         if (found != shouldBeThere) {
  89             throw new AssertionError("incorrect generation of readResolve method at class " + className);
  90         }
  91     }
  92 }