1 /*
2 * Copyright (c) 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 import java.lang.annotation.Annotation;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.io.File;
29 import java.nio.file.Path;
30 import java.util.Arrays;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import jdk.test.lib.cds.CDSAppTester;
35 import jdk.test.lib.process.OutputAnalyzer;
36 import jdk.test.lib.Platform;
37
38 import org.junit.Test;
39
40 // This class is for running the ../../../../../../jdk/java/lang/invoke/MethodHandles*java tests
41 // using CDSAppTester
42 public class JDKMethodHandlesTestRunner {
43 private static final String classDir = System.getProperty("test.classes");
44 private static final String mainClass = "TestMHApp";
45 private static final String javaClassPath = System.getProperty("java.class.path");
46 private static final String ps = System.getProperty("path.separator");
47 private static final String testPackageName = "test.java.lang.invoke";
48
49 public static void test(String testClassName) throws Exception {
50 String appJar = JarBuilder.build("MH", new File(classDir), null);
51 String classList = testClassName + ".list";
52 String archiveName = testClassName + ".jsa";
53 // Disable VerifyDpendencies when running with debug build because
54 // the test requires a lot more time to execute with the option enabled.
55 String verifyOpt =
56 Platform.isDebugBuild() ? "-XX:-VerifyDependencies" : "-showversion";
57
58 String junitJar = Path.of(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
59
60 String jars = appJar + ps + junitJar;
61
62 CDSAppTester tester = new CDSAppTester(testClassName) {
63 @Override
64 public String classpath(RunMode runMode) {
65 return jars;
66 }
67
68 @Override
69 public String[] vmArgs(RunMode runMode) {
70 if (runMode.isProductionRun()) {
71 return new String[] {
72 "-Xlog:class+load,cds=debug",
73 verifyOpt,
74 };
75 } else {
76 return new String[] {
77 verifyOpt,
78 };
79 }
80 }
81
82 @Override
83 public String[] appCommandLine(RunMode runMode) {
84 return new String[] {
85 mainClass,
86 testPackageName + "." + testClassName,
87 };
88 }
89
90 @Override
91 public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
92 out.shouldHaveExitValue(0);
93 if (runMode.isProductionRun()) {
94 out.shouldMatch(".class.load.* test.java.lang.invoke." + testClassName +
95 "[$][$]Lambda.*/0x.*source:.*shared.*objects.*file");
96 }
97 }
98 };
99
100 String workflow = System.getProperty("cds.app.tester.workflow");
101 tester.run(workflow);
102 }
103 }
104
105 class TestMHApp {
106 public static void main(String args[]) throws Exception {
107 try {
108 Class<?> testClass = Class.forName(args[0]);
109 System.out.println(testClass);
110 Object obj = testClass.newInstance();
111 final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(testClass.getDeclaredMethods()));
112 for (final Method method : allMethods) {
113 method.setAccessible(true);
114 Annotation[] annotations = null;
115 try {
116 annotations = method.getDeclaredAnnotations();
117 } catch (Throwable th) {
118 System.out.println("skipping method");
119 continue;
120 }
121 boolean isTest = false;
122 for (Annotation annotation : annotations) {
123 String annotationString = annotation.toString();
124 System.out.println(" annotation: " + annotationString);
125 if (annotationString.startsWith("@org.junit.Test")) {
126 isTest = true;
127 }
128 }
129 if (isTest) {
130 System.out.println(" invoking method: " + method.getName());
131 try {
132 method.invoke(obj);
133 } catch (IllegalAccessException iae) {
134 System.out.println("Got IllegalAccessException!!!");
135 System.out.println(iae.getCause());
136 } catch (InvocationTargetException ite) {
137 System.out.println("Got InvocationTargetException!!!");
138 throw ite;
139 }
140 }
141 }
142 } catch (ClassNotFoundException cnfe) {
143 System.out.println("Class not found: " + args[0]);
144 } catch (java.lang.IllegalAccessError iae) {
145 System.out.println("Skipping test: " + args[0]);
146 }
147 }
148 }