diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index 5091af5e..04f408af 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -88,7 +89,7 @@ public class BytecodeGen implements ASTVisitor { private Collection listOfResultSets; private ResultSet resultSet; private SourceFile sf; - private String path; + private File path; private Optional fieldInitializations; @@ -122,7 +123,7 @@ public class BytecodeGen implements ASTVisitor { private final ClassLoader classLoader; public BytecodeGen(HashMap classFiles, Collection listOfResultSets, List simplifyResultsForAllSourceFiles, SourceFile sf, - String path, ClassLoader classLoader) { + File path, ClassLoader classLoader) { this.classFiles = classFiles; this.listOfResultSets = listOfResultSets; this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 1c32748d..102438f8 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -57,7 +57,7 @@ public class BytecodeGenMethod implements StatementVisitor { private HashMap genericsAndBounds; public boolean isBinaryExp = false; private String superClass; - private String path; + private File path; private SourceFile sf; private IStatement statement = null; private boolean isReturnStmt = false; @@ -85,7 +85,7 @@ public class BytecodeGenMethod implements StatementVisitor { public BytecodeGenMethod(JavaClassName className, String superClass, ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, - SourceFile sf, String path, Block block, int constructorPos, ClassLoader classLoader) { + SourceFile sf, File path, Block block, int constructorPos, ClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -111,7 +111,7 @@ public class BytecodeGenMethod implements StatementVisitor { public BytecodeGenMethod(JavaClassName className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, - HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,String path, ClassLoader classLoader) { + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,File path, ClassLoader classLoader) { this.className = className; this.superClass = superClass; @@ -134,7 +134,7 @@ public class BytecodeGenMethod implements StatementVisitor { } public BytecodeGenMethod(JavaClassName className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList usedVars, ResultSet resultSet, MethodVisitor mv, - int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, String path, int lamCounter, SourceFile sf,HashMap genericsAndBoundsMethod, + int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, File path, int lamCounter, SourceFile sf,HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, ClassLoader classLoader) { this.className = className; this.cw = cw; @@ -816,7 +816,7 @@ public class BytecodeGenMethod implements StatementVisitor { // mDesc = helper.generateBCForFunN(methCallType,typesOfParams); }else { try { - cLoader2 = new DirectoryClassLoader(new File(path), classLoader); + cLoader2 = new DirectoryClassLoader(path, classLoader); java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods(); System.out.println("Methods of " + receiverName + " "); for(int i = 0; i classFiles, SourceFile sf, + public void generateBytecodForFile(File path, HashMap classFiles, SourceFile sf, List typeinferenceResult) throws IOException { try { List genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); @@ -780,13 +780,23 @@ public class JavaTXCompiler { } public void generateBytecode() throws ClassNotFoundException, IOException, BytecodeGeneratorError { - generateBytecode(null); + generateBytecode((File) null); } /** * @param path - can be null, then class file output is in the same directory as the parsed source files */ public void generateBytecode(String path) throws ClassNotFoundException, IOException, BytecodeGeneratorError { + if(path != null) + generateBytecode(new File(path)); + else + generateBytecode(); + } + + /** + * @param path - can be null, then class file output is in the same directory as the parsed source files + */ + public void generateBytecode(File path) throws ClassNotFoundException, IOException, BytecodeGeneratorError { List typeinferenceResult = this.typeInference(); List simplifyResultsForAllSourceFiles = getGeneratedGenericResultsForAllSourceFiles( typeinferenceResult); @@ -799,16 +809,16 @@ public class JavaTXCompiler { * @param simplifyResultsForAllSourceFiles * @throws IOException */ - public void generateBytecode(String outputPath, List typeinferenceResult, + public void generateBytecode(File outputPath, List typeinferenceResult, List simplifyResultsForAllSourceFiles) throws IOException { for (File f : sourceFiles.keySet()) { HashMap classFiles = new HashMap<>(); SourceFile sf = sourceFiles.get(f); - String path; + File path; if(outputPath == null){ - path = f.getParent(); //Set path to path of the parsed .jav file + path = f.getParentFile(); //Set path to path of the parsed .jav file }else{ - path = outputPath + sf.getPkgName().replace(".","/"); //add package path to root path + path = new File(outputPath ,sf.getPkgName().replace(".","/")); //add package path to root path } BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles, sf, path, classLoader); @@ -817,15 +827,15 @@ public class JavaTXCompiler { } } - private void writeClassFile(HashMap classFiles, String path) throws IOException { + private void writeClassFile(HashMap classFiles, File path) throws IOException { FileOutputStream output; for (JavaClassName name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); System.out.println("generating " + name + ".class file ..."); // output = new FileOutputStream(new File(System.getProperty("user.dir") + // "/testBytecode/generatedBC/" +name+".class")); - File outputFile = new File(path + File.separator + name.getClassName() + ".class"); - outputFile.getParentFile().mkdirs(); + File outputFile = new File(path, name.getClassName() + ".class"); + outputFile.getAbsoluteFile().getParentFile().mkdirs(); output = new FileOutputStream(outputFile); output.write(bytecode); output.close(); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index ca879cfd..746d1e85 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -74,7 +74,7 @@ public class CompilationEnvironment { List ret = new ArrayList<>(); String packageName = getPackageName(JavaTXParser.parse(forSourceFile)); //Set classLoader to include default package for this specific source file - File dir = new File(forSourceFile.getParent()); + File dir = new File(forSourceFile.getAbsoluteFile().getParent()); String dirPath = dir.toString() + "/"; if(packageName.length()>0)dirPath = dirPath.substring(0,dirPath.length() - packageName.length()); String path = dirPath; diff --git a/src/test/java/bytecode/OLTest.java b/src/test/java/bytecode/OLTest.java index d9de9f25..5b98c7ff 100644 --- a/src/test/java/bytecode/OLTest.java +++ b/src/test/java/bytecode/OLTest.java @@ -35,7 +35,7 @@ public class OLTest { pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; List typeinferenceResult = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); - compiler.generateBytecode(pathToClassFile,typeinferenceResult,simplifyResultsForAllSourceFiles); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("OL"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); diff --git a/src/test/java/packages/Bytecode.java b/src/test/java/packages/Bytecode.java index 3fabed05..7b5bb958 100644 --- a/src/test/java/packages/Bytecode.java +++ b/src/test/java/packages/Bytecode.java @@ -21,7 +21,7 @@ public class Bytecode extends TestCase { if(f.exists() && !f.isDirectory()) { f.delete(); } - compiler.generateBytecode(null); + compiler.generateBytecode(); f = new File(rootDirectory + "de/test/TestClass.class"); assertTrue(f.exists()); diff --git a/src/test/java/packages/ConsoleInterfaceTest.java b/src/test/java/packages/ConsoleInterfaceTest.java index 32fffe0d..c92bc8a9 100644 --- a/src/test/java/packages/ConsoleInterfaceTest.java +++ b/src/test/java/packages/ConsoleInterfaceTest.java @@ -39,6 +39,38 @@ public class ConsoleInterfaceTest extends TestCase { assertTrue(f.exists()); } + @Test + public void testCpNotEndsWithSlash() throws Exception { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav")); + compiler.typeInference(); + compiler.generateBytecode(rootDirectory + "output/"); + File f = new File(rootDirectory + "output/de/test/ToImport.class"); + assertTrue(f.exists()); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output" , rootDirectory + "de/test/ImportTest.jav"}); + + f = new File(rootDirectory + "de/test/ImportTest.class"); + assertTrue(f.exists()); + } + + @Test + public void testOutputDirNotEndsWithSlash() throws Exception { + File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + + ConsoleInterface.main(new String[]{"-d", rootDirectory + "de/test/output" ,rootDirectory + "de/test/TestClass.jav"}); + + f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); + assertTrue(f.exists()); + } + @Test public void testCompileSingleJavFileWithClassPath() throws Exception { JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav")); diff --git a/src/test/java/packages/ImportTest.java b/src/test/java/packages/ImportTest.java index 92d3f5bb..dc459629 100644 --- a/src/test/java/packages/ImportTest.java +++ b/src/test/java/packages/ImportTest.java @@ -27,7 +27,7 @@ public class ImportTest extends TestCase { compiler = new JavaTXCompiler(new File(rootDirectory+"ToImport.jav")); compiler.typeInference(); - compiler.generateBytecode(null); + compiler.generateBytecode(); f = new File(rootDirectory + "ToImport.class"); assertTrue(f.exists()); @@ -69,7 +69,7 @@ public class ImportTest extends TestCase { if(f.exists() && !f.isDirectory()) { f.delete(); } - compiler.generateBytecode(null); + compiler.generateBytecode(); f = new File(rootDirectory + "ImportTest.class"); assertTrue(f.exists()); } @@ -85,7 +85,7 @@ public class ImportTest extends TestCase { if(f.exists() && !f.isDirectory()) { f.delete(); } - compiler.generateBytecode(null); + compiler.generateBytecode(); f = new File(rootDirectory + "ImportTest2.class"); assertTrue(f.exists()); } @@ -99,7 +99,7 @@ public class ImportTest extends TestCase { if(f.exists() && !f.isDirectory()) { f.delete(); } - compiler.generateBytecode(null); + compiler.generateBytecode(); f = new File(rootDirectory + "ImportTestDefault.class"); assertTrue(f.exists()); } diff --git a/src/test/resources/bytecode/javFiles/VectorAdd.jav b/src/test/resources/bytecode/javFiles/VectorAdd.jav index bb14aa21..252c322c 100644 --- a/src/test/resources/bytecode/javFiles/VectorAdd.jav +++ b/src/test/resources/bytecode/javFiles/VectorAdd.jav @@ -37,5 +37,5 @@ public class VectorAdd { // var y; // x.add(y); // z = x; -} +// } }