Path: blob/master/test/hotspot/jtreg/runtime/InvocationTests/shared/AbstractGenerator.java
40948 views
/*1* Copyright (c) 2009, 2019, 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*22*/2324package shared;2526import java.io.File;27import java.io.FileOutputStream;28import java.util.Arrays;29import java.util.Map;30import java.util.List;31import java.util.ArrayList;3233public abstract class AbstractGenerator {34protected final boolean dumpClasses;35protected final boolean executeTests;36private static int testNum = 0;3738protected AbstractGenerator(String[] args) {39List<String> params = new ArrayList<String>(Arrays.asList(args));4041if (params.contains("--help")) {42Utils.printHelp();43System.exit(0);44}4546dumpClasses = params.contains("--dump");47executeTests = !params.contains("--noexecute");4849params.remove("--dump");50params.remove("--noexecute");5152Utils.init(params);53}5455/*******************************************************************/56public static void writeToFile(File dir, Map<String, byte[]> classes) {57for (String name : classes.keySet()) {58try {59writeToFile(dir, name, classes.get(name));60} catch (Exception e) {61throw new RuntimeException(e);62}63}64}6566/*******************************************************************/67public static void writeToFile(File dir, String fullName, byte[] classBytecode) {68if (!dir.isDirectory()) {69throw new RuntimeException("Invalid parameter: dir doesn't point to an existing directory");70}7172File classFile =73new File(74dir.getPath() + File.separator75+ fullName.replaceAll("\\.", File.separator)76+ ".class"77);7879classFile.getParentFile().mkdirs();8081try {82FileOutputStream fos = new FileOutputStream(classFile);83try {84fos.write(classBytecode);85} finally {86fos.close();87}88} catch (Exception e) {89throw new RuntimeException(e);90}91}9293protected boolean exec(Map<String, byte[]> classes, String description, String calleeClassName, String classNameC, String[] callSites) throws ClassNotFoundException {94boolean isPassed = true;9596testNum++;9798String caseDescription = String.format("%4d| %s", testNum, description);99100// Create test executor for a single case101classes.put(102ExecutorGenerator.className103, new ExecutorGenerator(104caseDescription105, calleeClassName106, classNameC107).generateExecutor(callSites)108);109110// Dump generated set to disk, if needed111if (dumpClasses) {112File dir = new File("classes" + File.separator + String.format("%04d", testNum));113dir.mkdirs();114writeToFile(dir, classes);115}116117ByteArrayClassLoader loader = new ByteArrayClassLoader(classes);118119Class paramClass;120Class targetClass;121Checker checker;122123try {124paramClass = loader.loadClass(calleeClassName);125targetClass = loader.loadClass(classNameC);126127checker = getChecker(paramClass, targetClass);128} catch (Throwable e) {129String result = Checker.abbreviateResult(e.getClass().getName());130131System.out.printf(caseDescription);132133for (String site : callSites) {134System.out.printf(" %7s", result);135}136137System.out.println("");138139return true;140}141142if (executeTests) {143// Check runtime behavior144Caller caller = new Caller(loader, checker, paramClass, targetClass);145boolean printedCaseDes = false;146for (String site : callSites) {147String callResult = caller.call(site);148149if (!caller.isPassed()) {150isPassed = false;151if (!printedCaseDes) {152System.out.printf(caseDescription);153printedCaseDes = true;154}155System.out.printf(" %7s", callResult);156}157}158if (!caller.isPassed()) {159System.out.println(" | FAILED");160}161} else {162for (String site : callSites) {163String result = checker.check(loader.loadClass(site));164System.out.printf(" %7s", Checker.abbreviateResult(result));165}166}167168return isPassed;169}170171protected abstract Checker getChecker(Class paramClass, Class targetClass);172}173174175