1 /*
  2  * Copyright (c) 2014, 2024, 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  * @bug 8042251
 27  * @summary Testing InnerClasses_attribute of inner classes in local class.
 28  * @library /tools/lib /tools/javac/lib ../lib
 29  * @enablePreview
 30  * @modules java.base/jdk.internal.classfile.impl
 31  *          jdk.compiler/com.sun.tools.javac.api
 32  *          jdk.compiler/com.sun.tools.javac.main
 33  * @enablePreview
 34  * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase
 35  * @build InnerClassesTestBase
 36  * @run main InnerClassesInLocalClassTest
 37  */
 38 
 39 import java.util.*;
 40 
 41 public class InnerClassesInLocalClassTest extends InnerClassesTestBase {
 42 
 43     private final static Modifier[] LOCAL_CLASS_MODIFIERS =
 44             new Modifier[]{Modifier.EMPTY, Modifier.ABSTRACT, Modifier.FINAL};
 45     private final static String CLASS_TEMPLATE =
 46             "public %CLASS% OuterClass {\n" +
 47             "%SOURCE%\n" +
 48             "}";
 49 
 50     private final List<Data> innerClassesData;
 51 
 52     public InnerClassesInLocalClassTest() {
 53         innerClassesData = new ArrayList<>();
 54         for (Modifier outerModifier : LOCAL_CLASS_MODIFIERS) {
 55             StringBuilder sb = new StringBuilder();
 56             sb.append(outerModifier.getString()).append(' ');
 57             sb.append("class Local { int f; "); // impose identity to make testing predictable.
 58             Map<String, Set<String>> class2Flags = new HashMap<>();
 59             for (int i = 0; i < LOCAL_CLASS_MODIFIERS.length; ++i) {
 60                 Modifier innerModifier = LOCAL_CLASS_MODIFIERS[i];
 61                 sb.append(innerModifier.getString()).append(' ')
 62                         .append("class").append(' ')
 63                         .append('A').append(i).append("{}\n");
 64                 class2Flags.put("A" + i, getFlags(innerModifier));
 65             }
 66             sb.append("};");
 67             class2Flags.put("1Local", getFlags(outerModifier));
 68             innerClassesData.add(new Data(sb.toString(), class2Flags));
 69         }
 70     }
 71 
 72     public static void main(String[] args) throws TestFailedException {
 73         InnerClassesTestBase test = new InnerClassesInLocalClassTest();
 74         test.test("OuterClass$1Local", "1Local");
 75     }
 76 
 77     @Override
 78     public void setProperties() {
 79     }
 80 
 81     @Override
 82     public List<TestCase> generateTestCases() {
 83         List<TestCase> testCases = new ArrayList<>();
 84         testCases.addAll(localClassInClassMethod());
 85         testCases.addAll(localClassInInterfaceMethod());
 86         return testCases;
 87     }
 88 
 89     private List<TestCase> localClassInClassMethod() {
 90         List<TestCase> list = new ArrayList<>();
 91         String template = CLASS_TEMPLATE.replace("%CLASS%", "class");
 92         list.addAll(lambda(template));
 93         list.addAll(constructor(template));
 94         list.addAll(method(template,
 95                 new Modifier[]{Modifier.EMPTY, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC},
 96                 new Modifier[]{Modifier.EMPTY, Modifier.FINAL, Modifier.STATIC}));
 97         list.addAll(staticAndInstanceInitializer(template));
 98         return list;
 99     }
100 
101     private List<TestCase> localClassInInterfaceMethod() {
102         String template = CLASS_TEMPLATE.replace("%CLASS%", "interface");
103         return method(template,
104                 new Modifier[]{Modifier.EMPTY, Modifier.PUBLIC},
105                 new Modifier[]{Modifier.DEFAULT, Modifier.STATIC});
106     }
107 
108     private List<TestCase> generate(String template, String prefix, String suffix) {
109         List<TestCase> list = new ArrayList<>();
110         for (Data data : innerClassesData) {
111             list.add(new TestCase(template.replace("%SOURCE%",
112                     prefix + data.sources + suffix),
113                     data.class2Flags));
114         }
115         return list;
116     }
117 
118     private List<TestCase> lambda(String template) {
119         return generate(template, "Runnable run = () -> {", "};");
120     }
121 
122     private List<TestCase> constructor(String template) {
123         List<TestCase> list = new ArrayList<>();
124         for (Modifier modifier :
125                 new Modifier[]{Modifier.EMPTY, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC}) {
126             list.addAll(generate(template, modifier.getString() + " OuterClass() {", "}"));
127         }
128         return list;
129     }
130 
131     private List<TestCase> method(String template, Modifier[] mods, Modifier[] otherMods) {
132         List<TestCase> list = new ArrayList<>();
133         for (Modifier modifier : mods) {
134             for (Modifier otherMod : otherMods) {
135                 list.addAll(generate(template,
136                         String.format("%s %s void method() {",
137                                 modifier.getString(),
138                                 otherMod.getString()),
139                         "}"));
140             }
141         }
142         return list;
143     }
144 
145     private List<TestCase> staticAndInstanceInitializer(String template) {
146         List<TestCase> list = new ArrayList<>();
147         for (Modifier modifier : new Modifier[]{Modifier.EMPTY, Modifier.STATIC}) {
148             list.addAll(generate(template, modifier.getString() + "{", "}"));
149         }
150         return list;
151     }
152 
153     private Set<String> getFlags(Modifier modifier) {
154         HashSet<String> set = new HashSet<>();
155         set.add("ACC_IDENTITY");
156         if (modifier != Modifier.EMPTY) {
157             set.add("ACC_" + modifier.getString().toUpperCase());
158         }
159         return set;
160     }
161 
162     /**
163      * Class represents part of sources which is inserted in other code.
164      */
165     private static class Data {
166         public final String sources;
167         public final Map<String, Set<String>> class2Flags;
168 
169         public Data(String sources, Map<String, Set<String>> class2Flags) {
170             this.sources = sources;
171             this.class2Flags = class2Flags;
172         }
173     }
174 }