Fehler beheben. PackageCrawler lauffähig

This commit is contained in:
JanUlrich 2017-09-20 18:20:52 +02:00
parent b8a5795bb3
commit 8ce017c2f2
10 changed files with 33 additions and 128 deletions

View File

@ -3,16 +3,14 @@ package de.dhbwstuttgart.core;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class ConsoleInterface { public class ConsoleInterface {
private static final String directory = System.getProperty("user.dir"); private static final String directory = System.getProperty("user.dir");
public static void main(String[] args) throws IOException, ClassNotFoundException { public static void main(String[] args) throws IOException, ClassNotFoundException {
List<File> input = Arrays.asList(args).stream().map((s -> new File(s))).collect(Collectors.toList());
JavaTXCompiler compiler = new JavaTXCompiler(); JavaTXCompiler compiler = new JavaTXCompiler(input);
for(String arg : Arrays.asList(args)){
compiler.parse(new File(arg));
}
compiler.typeInference(); compiler.typeInference();
} }
} }

View File

@ -24,21 +24,27 @@ import java.util.stream.Collectors;
public class JavaTXCompiler { public class JavaTXCompiler {
CompilationEnvironment environment; final CompilationEnvironment environment;
public final Map<File, SourceFile> sourceFiles = new HashMap<>();
protected List<SourceFile> sourceFiles = new ArrayList<>();
/*
public JavaTXCompiler(List<File> sourceFiles){
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFile));
} }
*/
public JavaTXCompiler(List<File> sources) throws IOException, ClassNotFoundException {
environment = new CompilationEnvironment(sources);
for(File s : sources){
sourceFiles.put(s,parse(s));
}
}
public List<ResultSet> typeInference(){ public List<ResultSet> typeInference(){
List<ClassOrInterface> allClasses = new ArrayList<>(); List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : sourceFiles){ for(SourceFile sf : sourceFiles.values()){
allClasses.addAll(sf.getClasses()); allClasses.addAll(sf.getClasses());
} }
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
final ConstraintSet<Pair> cons = new TYPE(sourceFiles).getConstraints(); final ConstraintSet<Pair> cons = new TYPE(sourceFiles.values()).getConstraints();
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons); ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
TypeUnify unify = new TypeUnify(); TypeUnify unify = new TypeUnify();
@ -72,11 +78,10 @@ public class JavaTXCompiler {
return ret; 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); CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile)); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile));
SourceFile ret = generator.convert(tree); SourceFile ret = generator.convert(tree);
sourceFiles.add(ret);
return ret; return ret;
} }

View File

@ -34,13 +34,13 @@ public class CompilationEnvironment {
/** /**
* Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien * 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<EFBFBD>lt automatisch die Java Standard Library
* @param sourceFiles die zu kompilierenden Dateien * @param sourceFiles die zu kompilierenden Dateien
*/ */
public CompilationEnvironment(List<File> sourceFiles) { public CompilationEnvironment(List<File> sourceFiles) {
String bootClassPath = System.getProperty("sun.boot.class.path"); String bootClassPath = System.getProperty("sun.boot.class.path");
librarys = new ArrayList<>(); librarys = new ArrayList<>();
for(String path : bootClassPath.split(";")) { for(String path : bootClassPath.split(File.pathSeparator)) {
try { try {
librarys.add(new URL("file:"+path)); librarys.add(new URL("file:"+path));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
@ -52,6 +52,7 @@ public class CompilationEnvironment {
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException { public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
List<String> allNames = new ArrayList<>(); List<String> allNames = new ArrayList<>();
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); 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);
} }
} }

View File

@ -12,7 +12,6 @@ import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typecheck.*;
import java.io.File; import java.io.File;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;

View File

@ -72,6 +72,7 @@ public class GatherNames {
private static List<String> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { private static List<String> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
List<String> ret = new ArrayList(); List<String> ret = new ArrayList();
ret.addAll(packages.getClassNames("java.lang"));
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){ if(importDeclCtx.singleTypeImportDeclaration() != null){
ret.add(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); ret.add(importDeclCtx.singleTypeImportDeclaration().typeName().getText());

View File

@ -12,9 +12,9 @@ import java.util.*;
public class TYPE { public class TYPE {
private final List<SourceFile> sfs; private final Collection<SourceFile> sfs;
public TYPE(List<SourceFile> sourceFiles){ public TYPE(Collection<SourceFile> sourceFiles){
sfs = sourceFiles; sfs = sourceFiles;
} }

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class JavaTXCompilerTest extends JavaTXCompiler { public class JavaTXCompilerTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final List<File> filesToTest = new ArrayList<>(); private static final List<File> filesToTest = new ArrayList<>();
@ -31,9 +31,6 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
@Test @Test
public void test() throws IOException, java.lang.ClassNotFoundException { 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); if(fileToTest != null)filesToTest.add(fileToTest);
//filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.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+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav"));
for(File f : filesToTest){ JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
SourceFile sf = this.parse(f); compiler.typeInference();
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
for(ResultSet resultSet : this.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<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);
String content = readFile(f.getPath(), StandardCharsets.UTF_8); String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){ for(TypeInsert tip : result){