diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 13d752cbe..658f0c658 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -90,9 +90,10 @@ public class JavaTXCompiler { public ConstraintSet getConstraints() throws ClassNotFoundException, IOException { List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); - for (SourceFile sf : sourceFiles.values()) { + for (File f : this.sourceFiles.keySet()) { + SourceFile sf = sourceFiles.get(f); allClasses.addAll(sf.getClasses()); - } + } List importedClasses = new ArrayList<>(); //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC for (File forSourceFile : sourceFiles.keySet()){ @@ -296,9 +297,11 @@ public class JavaTXCompiler { throws ClassNotFoundException, IOException { List allClasses = new ArrayList<>();// environment.getAllAvailableClasses(); // Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC - for (SourceFile sf : this.sourceFiles.values()) { + for (File f : this.sourceFiles.keySet()) { + SourceFile sf = sourceFiles.get(f); allClasses.addAll(getAvailableClasses(sf)); allClasses.addAll(sf.getClasses()); + allClasses.addAll(CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).collect(Collectors.toList())); } final ConstraintSet cons = getConstraints(); @@ -464,9 +467,11 @@ public class JavaTXCompiler { public List typeInference() throws ClassNotFoundException, IOException { List allClasses = new ArrayList<>();// environment.getAllAvailableClasses(); // Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC - for (SourceFile sf : this.sourceFiles.values()) { + for (File f : this.sourceFiles.keySet()) { + SourceFile sf = sourceFiles.get(f); allClasses.addAll(getAvailableClasses(sf)); allClasses.addAll(sf.getClasses()); + allClasses.addAll(CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).collect(Collectors.toList())); } final ConstraintSet cons = getConstraints(); diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 7433d6619..682e1eeab 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -84,7 +84,9 @@ public class CompilationEnvironment { String packageName = getPackageName(JavaTXParser.parse(forSourceFile)); //Set classLoader to include default package for this specific source file File dir = new File(forSourceFile.getParent()); - String path = "file://" + dir.toString().substring(0, dir.toString().length() - packageName.length() + 1); + String dirPath = dir.toString() + "/"; + if(packageName.length()>0)dirPath = dirPath.substring(0,dirPath.length() - packageName.length()); + String path = "file://" + dirPath; ArrayList defaultPath = Lists.newArrayList(new URL(path)); classLoader = new URLClassLoader(defaultPath.toArray(new URL[0]), classLoader); //Gather all names in the default package for this source file (classes that are imported by default) @@ -92,14 +94,15 @@ public class CompilationEnvironment { if(files != null)for (File classFile : files) { String className = classFile.getName().substring(0,classFile.getName().length()-6); ret.add(classLoader.loadClass(packageName + className)); - } + } return ret; } private static String getPackageName(CompilationUnitContext forTree){ String packageName = ""; - for(TerminalNode subPackage : forTree.packageDeclaration().Identifier()){ - packageName += subPackage.toString() + "."; + if(forTree.packageDeclaration() != null && forTree.packageDeclaration().Identifier() != null) + for(TerminalNode subPackage : forTree.packageDeclaration().Identifier()){ + packageName += subPackage.toString() + "."; } return packageName; } diff --git a/src/test/java/packages/LoadDefaultPackageClassesTest.java b/src/test/java/packages/LoadDefaultPackageClassesTest.java new file mode 100644 index 000000000..25af87ef0 --- /dev/null +++ b/src/test/java/packages/LoadDefaultPackageClassesTest.java @@ -0,0 +1,39 @@ +package packages; + +import com.google.common.collect.Lists; +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.environment.CompilationEnvironment; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; + +public class LoadDefaultPackageClassesTest extends TestCase { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/"; + + + public LoadDefaultPackageClassesTest() throws ClassNotFoundException, IOException { + /* + Generate ToImport class in rootDirectory and in output-Directory + */ + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Gen.jav")); + compiler.typeInference(); + compiler.generateBytecode(); + File f = new File(rootDirectory + "Gen.class"); + assertTrue(f.exists()); + } + + public void testLoadGenClass() throws IOException, ClassNotFoundException { + CompilationEnvironment.loadDefaultPackageClasses(new File( rootDirectory + "Test.jav"), ClassLoader.getSystemClassLoader()); + + } + public void testURLClassLoader() throws IOException, ClassNotFoundException { + URLClassLoader cl = new URLClassLoader(new URL[]{new URL("file://"+rootDirectory)}, ClassLoader.getSystemClassLoader()); + cl.loadClass("Gen"); + } + +} diff --git a/src/test/resources/javFiles/packageTest/Gen.jav b/src/test/resources/javFiles/packageTest/Gen.jav new file mode 100644 index 000000000..3b58b188d --- /dev/null +++ b/src/test/resources/javFiles/packageTest/Gen.jav @@ -0,0 +1,8 @@ +import java.lang.Integer; +import java.util.Vector; + +public class Gen{ + Vector m(Vector v){ + return v; + } +} diff --git a/src/test/resources/javFiles/packageTest/Test.jav b/src/test/resources/javFiles/packageTest/Test.jav new file mode 100644 index 000000000..2f6429a1c --- /dev/null +++ b/src/test/resources/javFiles/packageTest/Test.jav @@ -0,0 +1,3 @@ + +public class Test{ +} diff --git a/src/test/resources/javFiles/packageTest/de/test/output/de/test/mathStrucVectoruse.java b/src/test/resources/javFiles/packageTest/de/test/output/de/test/mathStrucVectoruse.java new file mode 100644 index 000000000..54b9e45b8 --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/output/de/test/mathStrucVectoruse.java @@ -0,0 +1,10 @@ +package de.test; +import de.test.mathStrucVector; + + +class mathStrucVectorUse { + + public static void main(String[] args) { + new mathStrucVector().main(); + } +} diff --git a/src/test/resources/javFiles/packageTest/de/test/output/mathStrucVectoruse.java b/src/test/resources/javFiles/packageTest/de/test/output/mathStrucVectoruse.java new file mode 100644 index 000000000..9a9b2e753 --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/output/mathStrucVectoruse.java @@ -0,0 +1,8 @@ + + +class mathStrucVectorUse { + + public static void main(String[] args) { + new mathStrucVector().main(); + } +} diff --git a/vorgehen.md b/vorgehen.md index 0854c1526..a83ca18f2 100644 --- a/vorgehen.md +++ b/vorgehen.md @@ -32,6 +32,17 @@ * damit lässt sich ein andere ort zur Ausgabe der Class-files bestimmen # Tasks +* Es steht nur noch ein Task aus: +## Klassen aus Default Package laden +* Das kann für jede SourceFile anders sein +* Momentan funktioniert der Compiler sowieso nur mit einer SourceFile! +* an allen Stellen, an denen die Imports der SourceFile beachtet werden, müssen jetzt auch alle Klassen aus dem gleichen Ordner beachtet werden + +### Stand +* CompilationEnvironment wurde erweitert und kann alle Klassen aus einem Ordner laden +* Allerdings funktionieren manche Tests noch nicht +* Die Bytecode-Tests funktionieren nicht, da sich kaputte Klassen im Bytecode-Jav-Ordner befinden + ## Class files in richtigen Ordner legen ## * Wenn Pfad übergeben, dann in Pfad + packageName * Ohne Pfad, direkt neben die Source File legen