diff --git a/src/de/dhbwstuttgart/core/ConsoleInterface.java b/src/de/dhbwstuttgart/core/ConsoleInterface.java index c86afbe3..77a172bf 100755 --- a/src/de/dhbwstuttgart/core/ConsoleInterface.java +++ b/src/de/dhbwstuttgart/core/ConsoleInterface.java @@ -3,16 +3,14 @@ package de.dhbwstuttgart.core; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.stream.Collectors; public class ConsoleInterface { private static final String directory = System.getProperty("user.dir"); public static void main(String[] args) throws IOException, ClassNotFoundException { - - JavaTXCompiler compiler = new JavaTXCompiler(); - for(String arg : Arrays.asList(args)){ - compiler.parse(new File(arg)); - } + List input = Arrays.asList(args).stream().map((s -> new File(s))).collect(Collectors.toList()); + JavaTXCompiler compiler = new JavaTXCompiler(input); compiler.typeInference(); } } diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 8a67fa26..10371436 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -24,21 +24,27 @@ import java.util.stream.Collectors; public class JavaTXCompiler { - CompilationEnvironment environment; - - protected List sourceFiles = new ArrayList<>(); -/* - public JavaTXCompiler(List sourceFiles){ + final CompilationEnvironment environment; + public final Map sourceFiles = new HashMap<>(); + public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { + this(Arrays.asList(sourceFile)); } -*/ + + public JavaTXCompiler(List sources) throws IOException, ClassNotFoundException { + environment = new CompilationEnvironment(sources); + for(File s : sources){ + sourceFiles.put(s,parse(s)); + } + } + public List typeInference(){ List allClasses = new ArrayList<>(); - for(SourceFile sf : sourceFiles){ + for(SourceFile sf : sourceFiles.values()){ allClasses.addAll(sf.getClasses()); } FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); - final ConstraintSet cons = new TYPE(sourceFiles).getConstraints(); + final ConstraintSet cons = new TYPE(sourceFiles.values()).getConstraints(); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); @@ -72,11 +78,10 @@ public class JavaTXCompiler { return ret; } - public SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException { + private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException { CompilationUnitContext tree = JavaTXParser.parse(sourceFile); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile)); SourceFile ret = generator.convert(tree); - sourceFiles.add(ret); return ret; } diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index 416e702b..929b9117 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -34,13 +34,13 @@ public class CompilationEnvironment { /** * Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien - * Die Environment enthält automatisch die Java Standard Library + * Die Environment enth�lt automatisch die Java Standard Library * @param sourceFiles die zu kompilierenden Dateien */ public CompilationEnvironment(List sourceFiles) { String bootClassPath = System.getProperty("sun.boot.class.path"); librarys = new ArrayList<>(); - for(String path : bootClassPath.split(";")) { + for(String path : bootClassPath.split(File.pathSeparator)) { try { librarys.add(new URL("file:"+path)); } catch (MalformedURLException e) { @@ -52,6 +52,7 @@ public class CompilationEnvironment { public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException { List allNames = new ArrayList<>(); CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); - return new JavaClassRegistry(GatherNames.getNames(tree, new PackageCrawler(librarys))); + allNames = GatherNames.getNames(tree, new PackageCrawler(librarys)); + return new JavaClassRegistry(allNames); } } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index ce2dddb0..3980369d 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -12,7 +12,6 @@ import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typecheck.*; import java.io.File; import java.lang.reflect.Modifier; diff --git a/src/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/de/dhbwstuttgart/parser/scope/GatherNames.java index e488ff08..919dc386 100644 --- a/src/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -72,6 +72,7 @@ public class GatherNames { private static List getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { List ret = new ArrayList(); + ret.addAll(packages.getClassNames("java.lang")); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ if(importDeclCtx.singleTypeImportDeclaration() != null){ ret.add(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index a67ef8b9..ad9f454b 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -12,9 +12,9 @@ import java.util.*; public class TYPE { - private final List sfs; + private final Collection sfs; - public TYPE(List sourceFiles){ + public TYPE(Collection sourceFiles){ sfs = sourceFiles; } diff --git a/test/parser/FeatherWeightJavaTest.java b/test/parser/FeatherWeightJavaTest.java deleted file mode 100644 index 84113e0c..00000000 --- a/test/parser/FeatherWeightJavaTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package parser; - -import de.dhbwstuttgart.parser.JavaTXParser; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.Field; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Modifier; - -public class FeatherWeightJavaTest { - private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/"; - - @Test - public void test() throws IOException, ClassNotFoundException { - - JavaTXParser parser = new JavaTXParser(); - SourceFile f = parser.parse(new File(rootDirectory + "Methods.jav")); - - String pkgName = f.getPkgName(); - System.out.println("package: " + pkgName); - System.out.println("classes:"); - for(ClassOrInterface c : f.getClasses()){ - int mod = c.getModifiers(); - System.out.println(Modifier.toString(mod)); - System.out.println(c.getClassName().toString()); - System.out.println("{"); - for(Field field : c.getFieldDecl()){ - System.out.println(field.getName()); - if(field instanceof Method){ - System.out.println(((Method) field).getParameterList().getFormalparalist().toString()); - } - } - System.out.println("}"); - } - } -} diff --git a/test/parser/FieldTest.java b/test/parser/FieldTest.java deleted file mode 100644 index 134943e0..00000000 --- a/test/parser/FieldTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package parser; - -import de.dhbwstuttgart.parser.JavaTXParser; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.Field; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Modifier; - -public class FieldTest { - private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/"; - - @Test - public void test() throws IOException, ClassNotFoundException, ClassNotFoundException { - - JavaTXParser parser = new JavaTXParser(); - SourceFile f = parser.parse(new File(rootDirectory + "FieldVarTest.jav")); - - String pkgName = f.getPkgName(); - System.out.println("package: " + pkgName); - System.out.println("classes:"); - for(ClassOrInterface c : f.getClasses()){ - int mod = c.getModifiers(); - System.out.println(Modifier.toString(mod)); - System.out.println(c.getClassName().toString()); - System.out.println("{"); - for(Field field : c.getFieldDecl()){ - System.out.println(field.getName()); - } - System.out.println("}"); - } - } -} diff --git a/test/parser/RunParserTest.java b/test/parser/RunParserTest.java deleted file mode 100644 index f1c63434..00000000 --- a/test/parser/RunParserTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package parser; - -import de.dhbwstuttgart.parser.JavaTXParser; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; -import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.*; - -public class RunParserTest { - - private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/"; - - @Test - public void testMain() throws Exception { - String[] args = new String[1]; - args[0] = rootDirectory+"WhileTest.jav"; - SourceFile sf = new JavaTXParser().parse(new File(args[0])); - System.out.println(ASTPrinter.print(sf)); - } -} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 389cd6b7..0fccae5c 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -20,7 +20,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -public class JavaTXCompilerTest extends JavaTXCompiler { +public class JavaTXCompilerTest { public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; private static final List filesToTest = new ArrayList<>(); @@ -31,9 +31,6 @@ public class JavaTXCompilerTest extends JavaTXCompiler { @Test public void test() throws IOException, java.lang.ClassNotFoundException { - for(URL url : ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs()){ - System.out.println(url); - } if(fileToTest != null)filesToTest.add(fileToTest); //filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); @@ -46,10 +43,13 @@ public class JavaTXCompilerTest extends JavaTXCompiler { //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); - for(File f : filesToTest){ - SourceFile sf = this.parse(f); - System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); - for(ResultSet resultSet : this.typeInference()){ + JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + compiler.typeInference(); + + for(File f : compiler.sourceFiles.keySet()){ + SourceFile sf = compiler.sourceFiles.get(f); + System.out.println(ASTTypePrinter.print(sf)); + for(ResultSet resultSet : compiler.typeInference()){ Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){