PackageCrawler und CompilationEnvironment implementieren

This commit is contained in:
Andreas Stadelmeier 2017-09-20 17:26:09 +02:00
parent 9b960329fc
commit b8a5795bb3
24 changed files with 149 additions and 117 deletions

View File

@ -2,6 +2,8 @@ package de.dhbwstuttgart.core;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
@ -71,13 +73,9 @@ public class JavaTXCompiler {
}
public SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
SourceFile ret = new JavaTXParser(environment).parse(sourceFile);
sourceFiles.add(ret);
return ret;
}
public SourceFile parse(String source) throws IOException, java.lang.ClassNotFoundException {
SourceFile ret = new JavaTXParser(environment).parse(source);
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile));
SourceFile ret = generator.convert(tree);
sourceFiles.add(ret);
return ret;
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.environment;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@ -17,10 +18,16 @@ import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext;
import de.dhbwstuttgart.parser.scope.GatherNames;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
/**
* Stellt die Java-Environment dar und speichert alle Binarys, Librarys und Sourcefiles im zu kompilierenden Projekt
* Sie erstellt anhand dieser Informationen die JavaClassNameRegistry
*
* TODO: Zur Initialisierung der CompilationEnvironment sollten alle SourceFiles mit ANTLR geparst werden und alle Klassen Generics und Typen herausgefunden werden
*/
public class CompilationEnvironment {
private final List<URL> librarys;
@ -41,4 +48,10 @@ public class CompilationEnvironment {
}
}
}
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
List<String> allNames = new ArrayList<>();
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
return new JavaClassRegistry(GatherNames.getNames(tree, new PackageCrawler(librarys)));
}
}

View File

@ -11,6 +11,8 @@ import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import de.dhbwstuttgart.parser.scope.JavaClassName;
/**
* Hilft beim Durchsuchen von Packages
* Benutzt die Reflections-Library (https://github.com/ronmamo/reflections)
@ -52,12 +54,11 @@ public class PackageCrawler {
return classes;
}
// Returns a list of JavaClassNames.
public List<JavaClassName> getClassNames(String packageName){
List<JavaClassName> nameList = new ArrayList();
public List<String> getClassNames(String packageName){
List<String> nameList = new ArrayList();
Set<Class<?>> classes = getClassesInPackage(packageName);
for(Class c : classes){
nameList.add(new JavaClassName(c.getName()));
nameList.add(c.getName());
}
return nameList;
}

View File

@ -1,10 +1,10 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.environment.JavaClassRegistry;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Lexer;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import org.antlr.v4.runtime.ANTLRInputStream;
@ -15,32 +15,23 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class JavaTXParser {
CompilationEnvironment environment;
public JavaTXParser(CompilationEnvironment env) {
environment = env;
}
public SourceFile parse(InputStream source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = source;//new FileInputStream(sourceFile);
public class JavaTXParser {
public static Java8Parser.CompilationUnitContext parse(File source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = new FileInputStream(source);
ANTLRInputStream input = new ANTLRInputStream(stream);
Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
JavaClassRegistry reg = new JavaClassRegistry(new ArrayList<>());
reg.addPackage("java.lang");
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(reg);
return parser.compilationUnit();
/*
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source));
return generator.convert(tree);
*/
}
public SourceFile parse(File file) throws IOException, java.lang.ClassNotFoundException {
return this.parse(new FileInputStream(file));
}
/* Für das Typsystem ist es notwendig, dass sich der Source in einer Datei befindet:
public SourceFile parse(String fileContent) throws IOException, java.lang.ClassNotFoundException {
return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8)));
}
*/
}

View File

@ -1,6 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
public class GenericContext {
public final String parentMethod;

View File

@ -1,9 +1,9 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.environment.JavaClassRegistry;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.*;

View File

@ -1,11 +1,11 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.environment.JavaClassRegistry;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import java.lang.ClassNotFoundException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.*;
@ -37,59 +37,6 @@ public class SyntaxTreeGenerator{
this.reg = reg;
}
public void setPackageName(Java8Parser.CompilationUnitContext ctx){
if(ctx.packageDeclaration() != null){
for(TerminalNode t : ctx.packageDeclaration().Identifier()){
this.pkgName = this.pkgName + "." + t.toString();
}
this.pkgName = this.pkgName.substring(1);
}
}
public void getNames(Java8Parser.CompilationUnitContext ctx){
if(this.pkgName == "") this.setPackageName(ctx);
String nameString = "";
for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
if(typeDecl.interfaceDeclaration() != null){
if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){
if(this.pkgName != ""){
nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
//this.reg.add(tp.Identifier().toString());
}
}
*/
this.reg.add(nameString);
}
}
else{
if(typeDecl.classDeclaration().normalClassDeclaration() != null){
if(this.pkgName != ""){
nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
this.reg.add(tp.Identifier().toString());
}
}
*/
this.reg.add(nameString);
}
}
}
}
public JavaClassRegistry getReg(){
return this.reg;
@ -140,8 +87,6 @@ public class SyntaxTreeGenerator{
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
String typeName = convertTypeName(ctx.typeName());
Thread.currentThread().getContextClassLoader().loadClass(typeName);
reg.add(typeName);
JavaClassName ret = reg.getName(typeName);
return ret;
}
@ -170,7 +115,6 @@ public class SyntaxTreeGenerator{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
this.setImports(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;

View File

@ -1,12 +1,12 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.environment.GenericTypeName;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.environment.JavaClassRegistry;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;

View File

@ -0,0 +1,91 @@
package de.dhbwstuttgart.parser.scope;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.tree.TerminalNode;
import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
public class GatherNames {
private static String getPackageName(Java8Parser.CompilationUnitContext ctx){
String pkgName = "";
if(ctx.packageDeclaration() != null){
for(TerminalNode t : ctx.packageDeclaration().Identifier()){
pkgName = pkgName + "." + t.toString();
}
pkgName = pkgName.substring(1);
}
return pkgName;
}
public static List<String> getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{
List<String> ret = new ArrayList<>();
String pkgName = getPackageName(ctx);
String nameString = "";
for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
if(typeDecl.interfaceDeclaration() != null){
if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){
if(pkgName != ""){
nameString = pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
//this.reg.add(tp.Identifier().toString());
}
}
*/
ret.add(nameString);
}
}
else{
if(typeDecl.classDeclaration().normalClassDeclaration() != null){
if(pkgName != ""){
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
this.reg.add(tp.Identifier().toString());
}
}
*/
ret.add(nameString);
}
}
}
ret.addAll(getImports(ctx, packages));
return ret;
}
private static List<String> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
List<String> ret = new ArrayList();
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){
ret.add(importDeclCtx.singleTypeImportDeclaration().typeName().getText());
}
else if(importDeclCtx.typeImportOnDemandDeclaration() != null){
ret.addAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText()));
}
else if(importDeclCtx.singleStaticImportDeclaration() != null){
ret.add(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText());
}
else{
ret.addAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText()));
}
}
return ret;
}
}

View File

@ -1,7 +1,6 @@
package de.dhbwstuttgart.environment;
package de.dhbwstuttgart.parser.scope;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
//import jdk.nashorn.internal.ir.Node;
public class GenericTypeName extends JavaClassName {
private final static String DELIMITER = "%";

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.environment;
package de.dhbwstuttgart.parser.scope;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.environment;
package de.dhbwstuttgart.parser.scope;
import java.util.*;

View File

@ -1,5 +0,0 @@
package de.dhbwstuttgart.parser.scope;
public class sad {
}

View File

@ -1,7 +1,7 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;

View File

@ -1,6 +1,6 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.environment.GenericTypeName;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;

View File

@ -2,8 +2,8 @@ package de.dhbwstuttgart.syntaxtree;
import java.io.File;
import java.util.*;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
//import sun.security.x509.X509CertInfo;

View File

@ -5,11 +5,11 @@ import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.environment.GenericTypeName;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.environment.JavaClassRegistry;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.*;

View File

@ -3,9 +3,9 @@ package de.dhbwstuttgart.syntaxtree.factory;
import java.util.*;
import java.util.stream.Collectors;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;

View File

@ -1,6 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;

View File

@ -1,8 +1,8 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import java.util.List;

View File

@ -1,7 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.environment.GenericTypeName;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typeinference.result.ResultSetVisitor;
import org.antlr.v4.runtime.Token;

View File

@ -1,6 +1,6 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typeinference.result.ResultSetVisitor;
import org.antlr.v4.runtime.Token;

View File

@ -2,7 +2,7 @@ package de.dhbwstuttgart.syntaxtree.type;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
public class Void extends RefType

View File

@ -1,7 +1,7 @@
package de.dhbwstuttgart.typeinference.typeAlgo;
import de.dhbwstuttgart.environment.JavaClassName;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.Statement;