Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/ClassfileGeneratorTest.java
41155 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package vm.mlvm.share;2425import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.util.List;29import java.util.LinkedList;3031import vm.mlvm.share.ClassfileGenerator;32import vm.mlvm.share.CustomClassLoaders;33import vm.mlvm.share.Env;34import vm.mlvm.share.MlvmTest;35import vm.share.options.Option;3637public class ClassfileGeneratorTest extends MlvmTest {3839public static final String CLASS_NAME = "Dummy";4041@Option(name = "generator", default_value = "", description = "Class name of the generator. Must inherit from vm.mlvm.share.ClassfileGenerator")42private String generatorClassNameOpt;4344private Class<? extends ClassfileGenerator> generatorClass;4546public ClassfileGeneratorTest() {47}4849public ClassfileGeneratorTest(Class<? extends ClassfileGenerator> genClass) {50generatorClass = genClass;51}5253@Override54public boolean run() throws Throwable {55if (generatorClass == null) {56generatorClass = Class.forName(generatorClassNameOpt).asSubclass(ClassfileGenerator.class);57}5859Env.traceVerbose("Generating class");60ClassfileGenerator gen = generatorClass.newInstance();6162gen.setClassName(null, CLASS_NAME);63ClassfileGenerator.Klass k = gen.generateBytecodes()[0];64k.writeClass(".");65ClassLoader cl = CustomClassLoaders.makeClassBytesLoader(k.getBytes(), k.getClassName());6667Env.traceNormal("Loading class " + k.getClassName());68Class<?> dummyClass = cl.loadClass(k.getClassName());6970MethodType mt = MethodType.fromMethodDescriptorString(k.getMainMethodSignature(), getClass().getClassLoader());71MethodHandle m = MethodHandles.lookup().findStatic(dummyClass, k.getMainMethodName(), mt);7273Env.traceVerbose("Main method: " + m);7475// Generate default parameter values76List<Object> arguments = new LinkedList<>();77for(Class<?> t : mt.wrap().parameterArray()) {78Object arg;79if (t.isArray()) {80arg = java.lang.reflect.Array.newInstance(t.getComponentType(), 0);81} else {82arg = t.newInstance();83}84arguments.add(arg);85}8687Env.traceNormal("Invoking method " + m);88m.invokeWithArguments(arguments);8990return true;91}9293public static void main(String[] args) {94MlvmTest.launch(args);95}9697}9899100