< prev index next >

test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java

Print this page

  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  */

260      * of acceptable flags for nested inner classes.
261      *
262      * @return generated list of test cases
263      */
264     protected List<TestCase> generateTestCases() {
265         setProperties();
266         List<TestCase> list = new ArrayList<>();
267 
268         List<List<Modifier>> outerMods = getAllCombinations(outerAccessModifiers, outerOtherModifiers);
269         List<List<Modifier>> innerMods = getAllCombinations(innerAccessModifiers, innerOtherModifiers);
270 
271         for (List<Modifier> outerMod : outerMods) {
272             if (isForbiddenWithoutStaticInOuterMods && !outerMod.contains(Modifier.STATIC)) {
273                 continue;
274             }
275             StringBuilder sb = new StringBuilder();
276             sb.append("public class InnerClassesSrc {")
277                     .append(toString(outerMod)).append(' ')
278                     .append(outerClassType).append(' ')
279                     .append(prefix).append(' ').append('\n');


280             int count = 0;
281             Map<String, Set<String>> class2Flags = new HashMap<>();
282             List<String> syntheticClasses = new ArrayList<>();
283             for (List<Modifier> innerMod : innerMods) {
284                 ++count;
285                 String privateConstructor = "";
286                 if (hasSyntheticClass && !innerMod.contains(Modifier.ABSTRACT)) {
287                     privateConstructor = "private A" + count + "() {}";
288                     syntheticClasses.add("new A" + count + "();");
289                 }


290                 sb.append(toString(innerMod)).append(' ');
291                 sb.append(String.format("%s A%d {%s}\n", innerClassType, count, privateConstructor));
292                 Set<String> flags = getFlags(innerClassType, innerMod);
293                 class2Flags.put("A" + count, flags);
294             }
295             if (hasSyntheticClass) {
296                 // Source to generate synthetic classes
297                 sb.append(syntheticClasses.stream().collect(Collectors.joining(" ", "{", "}")));
298                 class2Flags.put("1", new HashSet<>(Arrays.asList("ACC_STATIC", "ACC_SYNTHETIC")));
299             }
300             sb.append(suffix).append("\n}");
301             getAdditionalFlags(class2Flags, outerClassType, outerMod.toArray(new Modifier[outerMod.size()]));
302             list.add(new TestCase(sb.toString(), class2Flags));
303         }
304         return list;
305     }
306 
307     /**
308      * Methods returns flags which must have type.
309      *
310      * @param type class, interface, enum or annotation
311      * @param mods modifiers
312      * @return set of access flags
313      */
314     protected Set<String> getFlags(ClassType type, List<Modifier> mods) {
315         Set<String> flags = mods.stream()
316                 .map(Modifier::getString)
317                 .filter(str -> !str.isEmpty())
318                 .map(str -> "ACC_" + str.toUpperCase())
319                 .collect(Collectors.toSet());
320         type.addSpecificFlags(flags);
321         return flags;
322     }
323 
324     protected List<String> getCompileOptions() {
325         return Collections.emptyList();

326     }
327 
328     private List<List<Modifier>> getAllCombinations(Modifier[] accessModifiers, Modifier[] otherModifiers) {
329         List<List<Modifier>> list = new ArrayList<>();
330         for (Modifier access : accessModifiers) {
331             for (int i = 0; i < otherModifiers.length; ++i) {
332                 Modifier mod1 = otherModifiers[i];
333                 for (int j = i + 1; j < otherModifiers.length; ++j) {
334                     Modifier mod2 = otherModifiers[j];
335                     if (isForbidden(mod1, mod2)) {
336                         continue;
337                     }
338                     list.add(Arrays.asList(access, mod1, mod2));
339                 }
340                 if (mod1 == Modifier.EMPTY) {
341                     list.add(Collections.singletonList(access));
342                 }
343             }
344         }
345         return list;

422         private final String classType;
423 
424         ClassType(String clazz) {
425             this.classType = clazz;
426         }
427 
428         public abstract void addSpecificFlags(Set<String> flags);
429 
430         public String toString() {
431             return classType;
432         }
433 
434         public void addFlags(Set<String> set) {
435         }
436     }
437 
438     public enum Modifier {
439         PUBLIC("public"), PRIVATE("private"),
440         PROTECTED("protected"), DEFAULT("default"),
441         FINAL("final"), ABSTRACT("abstract"),
442         STATIC("static"), EMPTY("");

443 
444         private final String str;
445 
446         Modifier(String str) {
447             this.str = str;
448         }
449 
450         public String getString() {
451             return str;
452         }
453     }
454 }

  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  */

260      * of acceptable flags for nested inner classes.
261      *
262      * @return generated list of test cases
263      */
264     protected List<TestCase> generateTestCases() {
265         setProperties();
266         List<TestCase> list = new ArrayList<>();
267 
268         List<List<Modifier>> outerMods = getAllCombinations(outerAccessModifiers, outerOtherModifiers);
269         List<List<Modifier>> innerMods = getAllCombinations(innerAccessModifiers, innerOtherModifiers);
270 
271         for (List<Modifier> outerMod : outerMods) {
272             if (isForbiddenWithoutStaticInOuterMods && !outerMod.contains(Modifier.STATIC)) {
273                 continue;
274             }
275             StringBuilder sb = new StringBuilder();
276             sb.append("public class InnerClassesSrc {")
277                     .append(toString(outerMod)).append(' ')
278                     .append(outerClassType).append(' ')
279                     .append(prefix).append(' ').append('\n');
280             if (outerClassType == ClassType.CLASS && outerMod.contains(Modifier.ABSTRACT))
281                 sb.append("int f;\n"); // impose identity to make testing predicatable
282             int count = 0;
283             Map<String, Set<String>> class2Flags = new HashMap<>();
284             List<String> syntheticClasses = new ArrayList<>();
285             for (List<Modifier> innerMod : innerMods) {
286                 ++count;
287                 String privateConstructor = "";
288                 if (hasSyntheticClass && !innerMod.contains(Modifier.ABSTRACT)) {
289                     privateConstructor = "private A" + count + "() {}";
290                     syntheticClasses.add("new A" + count + "();");
291                 }
292                 String instField = innerClassType == ClassType.CLASS && innerMod.contains(Modifier.ABSTRACT) ?
293                                                 "int f; " : ""; // impose identity to make testing predicatable
294                 sb.append(toString(innerMod)).append(' ');
295                 sb.append(String.format("%s A%d { %s %s}\n", innerClassType, count, instField, privateConstructor));
296                 Set<String> flags = getFlags(innerClassType, innerMod);
297                 class2Flags.put("A" + count, flags);
298             }
299             if (hasSyntheticClass) {
300                 // Source to generate synthetic classes
301                 sb.append(syntheticClasses.stream().collect(Collectors.joining(" ", "{", "}")));
302                 class2Flags.put("1", new HashSet<>(Arrays.asList("ACC_STATIC", "ACC_SYNTHETIC")));
303             }
304             sb.append(suffix).append("\n}");
305             getAdditionalFlags(class2Flags, outerClassType, outerMod.toArray(new Modifier[outerMod.size()]));
306             list.add(new TestCase(sb.toString(), class2Flags));
307         }
308         return list;
309     }
310 
311     /**
312      * Methods returns flags which must have type.
313      *
314      * @param type class, interface, enum or annotation
315      * @param mods modifiers
316      * @return set of access flags
317      */
318     protected Set<String> getFlags(ClassType type, List<Modifier> mods) {
319         Set<String> flags = mods.stream()
320                 .map(Modifier::getString)
321                 .filter(str -> !str.isEmpty())
322                 .map(str -> "ACC_" + str.toUpperCase())
323                 .collect(Collectors.toSet());
324         type.addSpecificFlags(flags);
325         return flags;
326     }
327 
328     protected List<String> getCompileOptions() {
329         // Use a release before value classes for now.
330         return List.of("--release", "25");
331     }
332 
333     private List<List<Modifier>> getAllCombinations(Modifier[] accessModifiers, Modifier[] otherModifiers) {
334         List<List<Modifier>> list = new ArrayList<>();
335         for (Modifier access : accessModifiers) {
336             for (int i = 0; i < otherModifiers.length; ++i) {
337                 Modifier mod1 = otherModifiers[i];
338                 for (int j = i + 1; j < otherModifiers.length; ++j) {
339                     Modifier mod2 = otherModifiers[j];
340                     if (isForbidden(mod1, mod2)) {
341                         continue;
342                     }
343                     list.add(Arrays.asList(access, mod1, mod2));
344                 }
345                 if (mod1 == Modifier.EMPTY) {
346                     list.add(Collections.singletonList(access));
347                 }
348             }
349         }
350         return list;

427         private final String classType;
428 
429         ClassType(String clazz) {
430             this.classType = clazz;
431         }
432 
433         public abstract void addSpecificFlags(Set<String> flags);
434 
435         public String toString() {
436             return classType;
437         }
438 
439         public void addFlags(Set<String> set) {
440         }
441     }
442 
443     public enum Modifier {
444         PUBLIC("public"), PRIVATE("private"),
445         PROTECTED("protected"), DEFAULT("default"),
446         FINAL("final"), ABSTRACT("abstract"),
447         STATIC("static"), EMPTY(""),
448         IDENTITY("identity");
449 
450         private final String str;
451 
452         Modifier(String str) {
453             this.str = str;
454         }
455 
456         public String getString() {
457             return str;
458         }
459     }
460 }
< prev index next >