< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 2014, 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  */

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())

357                 .collect(Collectors.joining(" "));
358     }
359 
360     /**
361      * Method is called in generateTestCases().
362      * If you need to add additional access flags, you should override this method.
363      *
364      *
365      * @param class2Flags map with flags
366      * @param type class, interface, enum or @annotation
367      * @param mods modifiers
368      */
369     public void getAdditionalFlags(Map<String, Set<String>> class2Flags, ClassType type, Modifier...mods) {
370         class2Flags.values().forEach(type::addFlags);
371     }
372 
373     public enum ClassType {
374         CLASS("class") {
375             @Override
376             public void addSpecificFlags(Set<String> flags) {

377             }
378         },
379         INTERFACE("interface") {
380             @Override
381             public void addFlags(Set<String> flags) {
382                 flags.add("ACC_STATIC");
383                 flags.add("ACC_PUBLIC");
384             }
385 
386             @Override
387             public void addSpecificFlags(Set<String> flags) {
388                 flags.add("ACC_INTERFACE");
389                 flags.add("ACC_ABSTRACT");
390                 flags.add("ACC_STATIC");
391             }
392         },
393         ANNOTATION("@interface") {
394             @Override
395             public void addFlags(Set<String> flags) {
396                 flags.add("ACC_STATIC");
397                 flags.add("ACC_PUBLIC");
398             }
399 
400             @Override
401             public void addSpecificFlags(Set<String> flags) {
402                 flags.add("ACC_INTERFACE");
403                 flags.add("ACC_ABSTRACT");
404                 flags.add("ACC_STATIC");
405                 flags.add("ACC_ANNOTATION");
406             }
407         },
408         ENUM("enum") {
409             @Override
410             public void addSpecificFlags(Set<String> flags) {
411                 flags.add("ACC_ENUM");
412                 flags.add("ACC_FINAL");
413                 flags.add("ACC_STATIC");

414             }
415         },
416         OTHER("") {
417             @Override
418             public void addSpecificFlags(Set<String> flags) {

419             }
420         };
421 
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, 2022, 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_IDENTITY", "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())

361                 .collect(Collectors.joining(" "));
362     }
363 
364     /**
365      * Method is called in generateTestCases().
366      * If you need to add additional access flags, you should override this method.
367      *
368      *
369      * @param class2Flags map with flags
370      * @param type class, interface, enum or @annotation
371      * @param mods modifiers
372      */
373     public void getAdditionalFlags(Map<String, Set<String>> class2Flags, ClassType type, Modifier...mods) {
374         class2Flags.values().forEach(type::addFlags);
375     }
376 
377     public enum ClassType {
378         CLASS("class") {
379             @Override
380             public void addSpecificFlags(Set<String> flags) {
381                 flags.add("ACC_IDENTITY");
382             }
383         },
384         INTERFACE("interface") {
385             @Override
386             public void addFlags(Set<String> flags) {
387                 flags.add("ACC_STATIC");
388                 flags.add("ACC_PUBLIC");
389             }
390 
391             @Override
392             public void addSpecificFlags(Set<String> flags) {
393                 flags.add("ACC_INTERFACE");
394                 flags.add("ACC_ABSTRACT");
395                 flags.add("ACC_STATIC");
396             }
397         },
398         ANNOTATION("@interface") {
399             @Override
400             public void addFlags(Set<String> flags) {
401                 flags.add("ACC_STATIC");
402                 flags.add("ACC_PUBLIC");
403             }
404 
405             @Override
406             public void addSpecificFlags(Set<String> flags) {
407                 flags.add("ACC_INTERFACE");
408                 flags.add("ACC_ABSTRACT");
409                 flags.add("ACC_STATIC");
410                 flags.add("ACC_ANNOTATION");
411             }
412         },
413         ENUM("enum") {
414             @Override
415             public void addSpecificFlags(Set<String> flags) {
416                 flags.add("ACC_ENUM");
417                 flags.add("ACC_FINAL");
418                 flags.add("ACC_STATIC");
419                 flags.add("ACC_IDENTITY");
420             }
421         },
422         OTHER("") {
423             @Override
424             public void addSpecificFlags(Set<String> flags) {
425                 flags.add("ACC_IDENTITY");
426             }
427         };
428 
429         private final String classType;
430 
431         ClassType(String clazz) {
432             this.classType = clazz;
433         }
434 
435         public abstract void addSpecificFlags(Set<String> flags);
436 
437         public String toString() {
438             return classType;
439         }
440 
441         public void addFlags(Set<String> set) {
442         }
443     }
444 
445     public enum Modifier {
446         PUBLIC("public"), PRIVATE("private"),
447         PROTECTED("protected"), DEFAULT("default"),
448         FINAL("final"), ABSTRACT("abstract"),
449         STATIC("static"), EMPTY(""),
450         IDENTITY("identity");
451 
452         private final String str;
453 
454         Modifier(String str) {
455             this.str = str;
456         }
457 
458         public String getString() {
459             return str;
460         }
461     }
462 }
< prev index next >