1 /* 2 * Copyright (c) 2016, 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 package jdk.test.lib.jittester; 25 26 import jdk.test.lib.jittester.visitors.JavaCodeVisitor; 27 28 import java.io.FileWriter; 29 import java.io.IOException; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 32 import java.util.function.BiFunction; 33 34 /** 35 * Generates class files from java source code 36 */ 37 class JavaCodeGenerator implements BiFunction<IRNode, IRNode, String> { 38 private final Path testbase = Paths.get(ProductionParams.testbaseDir.value(), "java_tests"); 39 40 private String generateJavaCode(IRNode mainClass, IRNode privateClasses) { 41 StringBuilder code = new StringBuilder(); 42 JavaCodeVisitor vis = new JavaCodeVisitor(); 43 44 code.append(Automatic.getJtregHeader(mainClass.getName(), true)); 45 if (privateClasses != null) { 46 code.append(privateClasses.accept(vis)); 47 } 48 code.append(mainClass.accept(vis)); 49 50 return code.toString(); 51 } 52 53 public Path getTestbase() { 54 return testbase; 55 } 56 57 @Override 58 public String apply(IRNode mainClass, IRNode privateClasses) { 59 String code = generateJavaCode(mainClass, privateClasses); 60 Automatic.ensureExisting(testbase); 61 Path fileName = testbase.resolve(mainClass.getName() + ".java"); 62 try (FileWriter file = new FileWriter(fileName.toFile())) { 63 file.write(code); 64 return fileName.toString(); 65 } catch (IOException ex) { 66 ex.printStackTrace(); 67 } 68 return ""; 69 } 70 }