1 /* 2 * Copyright (c) 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 8333748 26 * @summary javap should not fail if reserved access flag bits are set to 1 27 * @library /tools/lib 28 * @modules jdk.jdeps/com.sun.tools.javap 29 * @run junit UndefinedAccessFlagTest 30 */ 31 32 import org.junit.jupiter.params.ParameterizedTest; 33 import org.junit.jupiter.params.provider.EnumSource; 34 import toolbox.JavapTask; 35 import toolbox.Task; 36 import toolbox.ToolBox; 37 38 import java.lang.classfile.AccessFlags; 39 import java.lang.classfile.ClassModel; 40 import java.lang.classfile.FieldModel; 41 import java.lang.classfile.MethodModel; 42 import java.lang.classfile.attribute.InnerClassInfo; 43 import java.lang.classfile.attribute.InnerClassesAttribute; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 47 import static java.lang.classfile.ClassFile.*; 48 import static org.junit.jupiter.api.Assertions.assertTrue; 49 50 public class UndefinedAccessFlagTest { 51 52 final ToolBox toolBox = new ToolBox(); 53 54 enum TestLocation { 55 NONE(false), CLASS, FIELD, METHOD, INNER_CLASS(false); 56 57 final boolean fails; 58 TestLocation() { this(true); } 59 TestLocation(boolean fails) { this.fails = fails; } 60 } 61 62 @ParameterizedTest 63 @EnumSource(TestLocation.class) 64 void test(TestLocation location) throws Throwable { 65 var cf = of(); 66 ClassModel cm; 67 try (var is = UndefinedAccessFlagTest.class.getResourceAsStream( 68 "/UndefinedAccessFlagTest$SampleInnerClass.class" 69 )) { 70 cm = cf.parse(is.readAllBytes()); 71 } 72 var bytes = cf.transformClass(cm, (cb, ce) -> { 73 switch (ce) { 74 case AccessFlags flags when location == TestLocation.CLASS -> cb 75 .withFlags(flags.flagsMask() | ACC_PRIVATE); 76 case FieldModel f when location == TestLocation.FIELD -> cb 77 .transformField(f, (fb, fe) -> { 78 if (fe instanceof AccessFlags flags) { 79 fb.withFlags(flags.flagsMask() | ACC_SYNCHRONIZED); 80 } else { 81 fb.with(fe); 82 } 83 }); 84 case MethodModel m when location == TestLocation.METHOD -> cb 85 .transformMethod(m, (mb, me) -> { 86 if (me instanceof AccessFlags flags) { 87 mb.withFlags(flags.flagsMask() | ACC_INTERFACE); 88 } else { 89 mb.with(me); 90 } 91 }); 92 case InnerClassesAttribute attr when location == TestLocation.INNER_CLASS -> cb 93 .with(InnerClassesAttribute.of(attr.classes().stream() 94 .map(ic -> InnerClassInfo.of(ic.innerClass(), ic.outerClass(), ic.innerName(), ic.flagsMask() | 0x0050)) 95 .toList())); 96 default -> cb.with(ce); 97 } 98 }); 99 100 Files.write(Path.of("transformed.class"), bytes); 101 102 var lines = new JavapTask(toolBox) 103 .classes("transformed.class") 104 .options("-c", "-p", "-v") 105 .run(location.fails ? Task.Expect.FAIL : Task.Expect.SUCCESS) 106 .writeAll() 107 .getOutputLines(Task.OutputKind.DIRECT); 108 109 // No termination when access flag error happens 110 assertTrue(lines.stream().anyMatch(l -> l.contains("java.lang.String field;"))); 111 assertTrue(lines.stream().anyMatch(l -> l.contains("UndefinedAccessFlagTest$SampleInnerClass();"))); 112 assertTrue(lines.stream().anyMatch(l -> l.contains("void method();"))); 113 assertTrue(lines.stream().anyMatch(l -> l.contains("SampleInnerClass=class UndefinedAccessFlagTest$SampleInnerClass of class UndefinedAccessFlagTest"))); 114 115 // Remove non-error lines 116 assertTrue(lines.removeIf(st -> !st.startsWith("Error:"))); 117 // Desired locations has errors 118 assertTrue(location == TestLocation.NONE || !lines.isEmpty()); 119 // Access Flag errors only 120 assertTrue(lines.stream().allMatch(l -> l.contains("Access Flags:")), () -> String.join("\n", lines)); 121 } 122 123 static class SampleInnerClass { 124 String field; 125 void method() {} 126 } 127 }