Fix loading packages from same package

This commit is contained in:
JanUlrich 2020-01-06 22:04:29 +01:00
parent abdff6c8d2
commit f63346649b
8 changed files with 95 additions and 8 deletions

View File

@ -90,9 +90,10 @@ public class JavaTXCompiler {
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
List<ClassOrInterface> 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<ClassOrInterface> 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<ClassOrInterface> 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<Pair> cons = getConstraints();
@ -464,9 +467,11 @@ public class JavaTXCompiler {
public List<ResultSet> typeInference() throws ClassNotFoundException, IOException {
List<ClassOrInterface> 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<Pair> cons = getConstraints();

View File

@ -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<URL> 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;
}

View File

@ -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");
}
}

View File

@ -0,0 +1,8 @@
import java.lang.Integer;
import java.util.Vector;
public class Gen{
Vector<Integer> m(Vector<Integer> v){
return v;
}
}

View File

@ -0,0 +1,3 @@
public class Test{
}

View File

@ -0,0 +1,10 @@
package de.test;
import de.test.mathStrucVector;
class mathStrucVectorUse {
public static void main(String[] args) {
new mathStrucVector().main();
}
}

View File

@ -0,0 +1,8 @@
class mathStrucVectorUse {
public static void main(String[] args) {
new mathStrucVector().main();
}
}

View File

@ -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