Fix loading packages from same package
This commit is contained in:
parent
abdff6c8d2
commit
f63346649b
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
39
src/test/java/packages/LoadDefaultPackageClassesTest.java
Normal file
39
src/test/java/packages/LoadDefaultPackageClassesTest.java
Normal 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");
|
||||
}
|
||||
|
||||
}
|
8
src/test/resources/javFiles/packageTest/Gen.jav
Normal file
8
src/test/resources/javFiles/packageTest/Gen.jav
Normal file
@ -0,0 +1,8 @@
|
||||
import java.lang.Integer;
|
||||
import java.util.Vector;
|
||||
|
||||
public class Gen{
|
||||
Vector<Integer> m(Vector<Integer> v){
|
||||
return v;
|
||||
}
|
||||
}
|
3
src/test/resources/javFiles/packageTest/Test.jav
Normal file
3
src/test/resources/javFiles/packageTest/Test.jav
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
public class Test{
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package de.test;
|
||||
import de.test.mathStrucVector;
|
||||
|
||||
|
||||
class mathStrucVectorUse {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new mathStrucVector().main();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
class mathStrucVectorUse {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new mathStrucVector().main();
|
||||
}
|
||||
}
|
11
vorgehen.md
11
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
|
||||
|
Loading…
Reference in New Issue
Block a user