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