Change Parser FileInput to InputStream

This commit is contained in:
JanUlrich 2017-07-05 17:50:38 +02:00
parent 87d2edaaa6
commit 0ad97251ca
4 changed files with 12 additions and 24 deletions

View File

@ -13,8 +13,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JavaTXParser { public class JavaTXParser {
public SourceFile parse(File sourceFile) throws IOException, ClassNotFoundException { public SourceFile parse(InputStream source) throws IOException, ClassNotFoundException {
InputStream stream = new FileInputStream(sourceFile); InputStream stream = source;//new FileInputStream(sourceFile);
ANTLRInputStream input = new ANTLRInputStream(stream); ANTLRInputStream input = new ANTLRInputStream(stream);
Java8Lexer lexer = new Java8Lexer(input); Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
@ -22,7 +22,7 @@ public class JavaTXParser {
Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(generateJavaLangNames())); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(generateJavaLangNames()));
return generator.convert(tree, sourceFile); return generator.convert(tree);
} }
private List<String> generateJavaLangNames() throws IOException, ClassNotFoundException { private List<String> generateJavaLangNames() throws IOException, ClassNotFoundException {
@ -36,4 +36,8 @@ public class JavaTXParser {
return ret; return ret;
} }
public SourceFile parse(File file) throws IOException, ClassNotFoundException {
return this.parse(new FileInputStream(file));
}
} }

View File

@ -170,7 +170,7 @@ public class SyntaxTreeGenerator{
return ret; return ret;
} }
public SourceFile convert(Java8Parser.CompilationUnitContext ctx, File parsedFile) throws ClassNotFoundException{ public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>(); List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx); this.getNames(ctx);
this.setImports(ctx); this.setImports(ctx);
@ -184,7 +184,7 @@ public class SyntaxTreeGenerator{
} }
classes.add(newClass); classes.add(newClass);
} }
return new SourceFile(parsedFile, this.pkgName, classes, this.imports); return new SourceFile(this.pkgName, classes, this.imports);
} }
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, GenericsRegistry generics) { public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, GenericsRegistry generics) {

View File

@ -15,18 +15,16 @@ public class SourceFile extends SyntaxTreeNode{
private final List<ClassOrInterface> KlassenVektor; private final List<ClassOrInterface> KlassenVektor;
private final List<JavaClassName> imports; private final List<JavaClassName> imports;
private final File file;
/** /**
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei. * Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar. * SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
*/ */
public SourceFile(File file, String pkgName, List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){ public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){
super(new NullToken()); super(new NullToken());
this.KlassenVektor = classDefinitions; this.KlassenVektor = classDefinitions;
this.pkgName = pkgName; this.pkgName = pkgName;
this.imports = imports; this.imports = imports;
this.file = file;
} }
public String getPkgName(){ public String getPkgName(){
@ -59,10 +57,6 @@ public class SourceFile extends SyntaxTreeNode{
return KlassenVektor; return KlassenVektor;
} }
public File getFile() {
return file;
}
@Override @Override
public void accept(ASTVisitor visitor) { public void accept(ASTVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -43,9 +43,9 @@ 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"));
for(File f : filesToTest){ for(File f : filesToTest){
this.parse(f); SourceFile sf = this.parse(f);
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
List<TypeInsert> result = this.getTypeInserts(f); List<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, this.typeInference());
String content = readFile(f.getPath(), StandardCharsets.UTF_8); String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){ for(TypeInsert tip : result){
System.out.println(tip.insert(content)); System.out.println(tip.insert(content));
@ -54,16 +54,6 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
} }
public List<TypeInsert> getTypeInserts(File forFile){
ResultSet result = typeInference();
for(SourceFile sf : sourceFiles){
if(sf.getFile().equals(forFile)){
return TypeInsertFactory.createTypeInsertPoints(sf, result);
}
}
throw new DebugException("Die Datei "+forFile+" wurde nicht geparst");
}
static String readFile(String path, Charset encoding) static String readFile(String path, Charset encoding)
throws IOException throws IOException
{ {