1 /*
   2  * Copyright (c) 2019, 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 MethodParametersForCanonicalConstructorTest.java
  30  * @run main MethodParametersForCanonicalConstructorTest
  31  */
  32 
  33 import java.io.*;
  34 
  35 import com.sun.tools.classfile.*;
  36 
  37 public class MethodParametersForCanonicalConstructorTest {
  38 
  39     record R1(int i, int j) {}
  40 
  41     record R2(int i, int j) {
  42         public R2 {}
  43     }
  44 
  45     record R3(int i, int j) {
  46         public R3(int i, int j) {}
  47     }
  48 
  49     public static void main(String args[]) throws Throwable {
  50         new MethodParametersForCanonicalConstructorTest().run();
  51     }
  52 
  53     void run() throws Throwable {
  54         checkCanonical("R1", "i", "j");
  55         checkCanonical("R2", "i", "j");
  56         checkCanonical("R3", "i", "j");
  57     }
  58 
  59     void checkCanonical(String className, String... expectedParamNames) throws Throwable {
  60         if (expectedParamNames == null) {
  61             return;
  62         }
  63         File testClasses = new File(System.getProperty("test.classes"));
  64         File file = new File(testClasses,
  65                 MethodParametersForCanonicalConstructorTest.class.getName() + "$" + className +".class");
  66         ClassFile classFile = ClassFile.read(file);
  67         boolean found = false;
  68         for (Method m : classFile.methods) {
  69             if (m.getName(classFile.constant_pool).equals("<init>")) {
  70                 for (Attribute attribute : m.attributes) {
  71                     if (attribute instanceof MethodParameters_attribute) {
  72                         found = true;
  73                         MethodParameters_attribute mpa = (MethodParameters_attribute)attribute;
  74                         if (mpa.method_parameter_table_length != expectedParamNames.length) {
  75                             throw new AssertionError("unexpected method parameter table lenght");
  76                         }
  77                         int i = 0;
  78                         for (MethodParameters_attribute.Entry entry : mpa.method_parameter_table) {
  79                             if (!classFile.constant_pool.getUTF8Value(entry.name_index).equals(expectedParamNames[i])) {
  80                                 throw new AssertionError("unexpected name at position " + i);
  81                             }
  82                             i++;
  83                         }
  84                     }
  85                 }
  86             }
  87         }
  88         if (!found) {
  89             throw new AssertionError("attribute MethodParameters not found");
  90         }
  91     }
  92 }