TPH = TPH ist jetzt auch im ResultSet möglich. Anpassungen an der Generierung der Imports einer SourceFile; läuft noch nicht fehlerfrei

This commit is contained in:
JanUlrich 2017-12-14 17:44:43 +01:00
parent 4616f82b09
commit eb8db0e0eb
10 changed files with 60 additions and 25 deletions

View File

@ -96,7 +96,7 @@ public class JavaTXCompiler {
private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile), new GenericsRegistry(null));
SourceFile ret = generator.convert(tree);
SourceFile ret = generator.convert(tree,environment.packageCrawler);
return ret;
}

View File

@ -32,6 +32,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
public class CompilationEnvironment {
private final List<URL> librarys;
private final List<File> sourceFiles;
public final PackageCrawler packageCrawler;
/**
* Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien
@ -49,12 +50,13 @@ public class CompilationEnvironment {
}
}
this.sourceFiles = sourceFiles;
this.packageCrawler = new PackageCrawler(librarys);
}
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
Map<String, Integer> allNames;
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
allNames = GatherNames.getNames(tree, new PackageCrawler(librarys));
allNames = GatherNames.getNames(tree, packageCrawler);
return new JavaClassRegistry(allNames);
}

View File

@ -1,10 +1,12 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import java.lang.ClassNotFoundException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GatherNames;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
@ -17,9 +19,8 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.lang.reflect.Modifier;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
//import jdk.internal.dynalink.support.TypeConverterFactory;
import org.antlr.v4.runtime.CommonToken;
@ -30,7 +31,7 @@ public class SyntaxTreeGenerator{
private JavaClassRegistry reg;
private final GenericsRegistry globalGenerics;
private String pkgName = "";
List<JavaClassName> imports = new ArrayList();
Set<JavaClassName> imports = new HashSet();
List<Statement> fieldInitializations = new ArrayList<>();
@ -71,6 +72,7 @@ public class SyntaxTreeGenerator{
return ret;
}
/*
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
List<JavaClassName> newImports = new ArrayList();
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
@ -108,6 +110,7 @@ public class SyntaxTreeGenerator{
return reg.getAllFromPackage(ctx.typeName().getText());
}
*/
private String getPackageFromClass(String cls){
String ret = "";
String[] parts = cls.split("\\.");
@ -118,9 +121,10 @@ public class SyntaxTreeGenerator{
return ret;
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>();
this.setImports(ctx);
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;
if(typeDecl.classDeclaration() != null){

View File

@ -69,10 +69,10 @@ public class GatherNames {
}
private static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
public static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>();
ret.putAll(packages.getClassNames("java.lang"));
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
ret.putAll(packages.getClassNames("java.lang"));
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());

View File

@ -13,13 +13,13 @@ public class SourceFile extends SyntaxTreeNode{
private String pkgName;
public final List<ClassOrInterface> KlassenVektor;
public final List<JavaClassName> imports;
public final Set<JavaClassName> imports;
/**
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
*/
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, Set<JavaClassName> imports){
super(new NullToken());
this.KlassenVektor = classDefinitions;
this.pkgName = pkgName;
@ -31,7 +31,7 @@ public class SourceFile extends SyntaxTreeNode{
}
// Get imports (to test implementation)
public List<JavaClassName> getImports(){
public Set<JavaClassName> getImports(){
return this.imports;
}

View File

@ -14,6 +14,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH;
import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType;
import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH;
import de.dhbwstuttgart.typeinference.result.ResultPair;
@ -195,12 +196,13 @@ public class UnifyTypeFactory {
RefTypeOrTPHOrWildcardOrGeneric tr = UnifyTypeFactory.convert(mp.getRhsType(), tphs);
if(tl instanceof TypePlaceholder){
if(tr instanceof TypePlaceholder) {
if(mp.getPairOp().equals(PairOperator.EQUALSDOT))
throw new DebugException("TPH =. TPH ist ein ungültiges Ergebnis");
if(mp.getPairOp().equals(PairOperator.EQUALSDOT)) {
return new PairTPHEqualTPH((TypePlaceholder)tl, (TypePlaceholder)tr);
//Einfach ignorieren TODO: Das hier muss ausgebessert werden:
//return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, ASTFactory.createObjectType());
else
}else{
return new PairTPHsmallerTPH((TypePlaceholder)tl, (TypePlaceholder)tr);
}
}else if(tr instanceof RefType){
return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, (RefType) tr);
}else if(tr instanceof WildcardType){

View File

@ -104,6 +104,11 @@ class TypeToInsertString implements ResultSetVisitor{
}
@Override
public void visit(PairTPHEqualTPH p) {
}
@Override
public void visit(RefType resolved) {
insert = resolved.getName().toString();

View File

@ -5,22 +5,22 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
/**
* Paare, welche das Unifikationsergebnis darstellen
*/
public abstract class ResultPair {
private final RefTypeOrTPHOrWildcardOrGeneric left;
private final RefTypeOrTPHOrWildcardOrGeneric right;
public abstract class ResultPair<A extends RefTypeOrTPHOrWildcardOrGeneric,B extends RefTypeOrTPHOrWildcardOrGeneric> {
private final A left;
private final B right;
public abstract void accept(ResultSetVisitor visitor);
public ResultPair(RefTypeOrTPHOrWildcardOrGeneric left, RefTypeOrTPHOrWildcardOrGeneric right){
public ResultPair(A left, B right){
this.left = left;
this.right = right;
}
public RefTypeOrTPHOrWildcardOrGeneric getLeft() {
public A getLeft() {
return left;
}
public RefTypeOrTPHOrWildcardOrGeneric getRight() {
public B getRight() {
return right;
}
}

View File

@ -42,6 +42,11 @@ class Resolver implements ResultSetVisitor {
public ResolvedType resolve(TypePlaceholder tph){
toResolve = tph;
resolved = null;
for(ResultPair resultPair : result.results){
if(resultPair instanceof PairTPHEqualTPH && ((PairTPHEqualTPH) resultPair).getLeft().equals(toResolve)){
return resolve(((PairTPHEqualTPH) resultPair).getRight());
}
}
for(ResultPair resultPair : result.results){
resultPair.accept(this);
}
@ -72,6 +77,11 @@ class Resolver implements ResultSetVisitor {
}
}
@Override
public void visit(PairTPHEqualTPH p) {
//Do nothing. Dieser Fall wird in der resolve-Methode abgefangen
}
@Override
public void visit(RefType refType) {
@ -139,6 +149,11 @@ class TPHResolver implements ResultSetVisitor {
}
}
@Override
public void visit(PairTPHEqualTPH p) {
//ignorieren. Wird vom Resolver behandelt
}
@Override
public void visit(RefType refType) {
@ -208,6 +223,11 @@ class RelatedTypeWalker implements ResultSetVisitor {
}
}
@Override
public void visit(PairTPHEqualTPH p) {
//Kann ignoriert werden. Diese Fälle werden vom Resolver behandelt
}
/*
Die folgenden Funktionen fügen alle TPHs an die relatedTPHs an, denen sie begegnen:
Das wird verwendet, wenn alle relatedTPHs aus den Parametern eines RefTypes angefügt werden sollen

View File

@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.type.*;
public interface ResultSetVisitor {
void visit(PairTPHsmallerTPH p);
void visit(PairTPHequalRefTypeOrWildcardType p);
void visit(PairTPHEqualTPH p);
void visit(RefType refType);
@ -15,4 +16,5 @@ public interface ResultSetVisitor {
void visit(TypePlaceholder typePlaceholder);
void visit(ExtendsWildcardType extendsWildcardType);
}