From eb8db0e0eb901afd5f4502ee4f7769a394b23422 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 14 Dec 2017 17:44:43 +0100 Subject: [PATCH] =?UTF-8?q?TPH=20=3D=20TPH=20ist=20jetzt=20auch=20im=20Res?= =?UTF-8?q?ultSet=20m=C3=B6glich.=20Anpassungen=20an=20der=20Generierung?= =?UTF-8?q?=20der=20Imports=20einer=20SourceFile;=20l=C3=A4uft=20noch=20ni?= =?UTF-8?q?cht=20fehlerfrei?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- .../environment/CompilationEnvironment.java | 4 +++- .../SyntaxTreeGenerator.java | 22 +++++++++++-------- .../parser/scope/GatherNames.java | 4 ++-- .../dhbwstuttgart/syntaxtree/SourceFile.java | 6 ++--- .../syntaxtree/factory/UnifyTypeFactory.java | 8 ++++--- .../typedeployment/TypeInsertFactory.java | 5 +++++ .../typeinference/result/ResultPair.java | 12 +++++----- .../typeinference/result/ResultSet.java | 20 +++++++++++++++++ .../result/ResultSetVisitor.java | 2 ++ 10 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 4ec0e2c34..80afb800a 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -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; } diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index 2f04423f1..7044da6b6 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -32,6 +32,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry; public class CompilationEnvironment { private final List librarys; private final List 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 allNames; CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); - allNames = GatherNames.getNames(tree, new PackageCrawler(librarys)); + allNames = GatherNames.getNames(tree, packageCrawler); return new JavaClassRegistry(allNames); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index ed9d82e26..bae6e9b92 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -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 imports = new ArrayList(); + Set imports = new HashSet(); List fieldInitializations = new ArrayList<>(); @@ -70,7 +71,8 @@ public class SyntaxTreeGenerator{ } return ret; } - + + /* public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException { List newImports = new ArrayList(); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ @@ -89,7 +91,7 @@ public class SyntaxTreeGenerator{ } this.imports.addAll(newImports); } - + private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{ String typeName = convertTypeName(ctx.typeName()); JavaClassName ret = reg.getName(typeName); @@ -107,7 +109,8 @@ public class SyntaxTreeGenerator{ private List convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){ 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 classes = new ArrayList<>(); - this.setImports(ctx); + Map 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){ diff --git a/src/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/de/dhbwstuttgart/parser/scope/GatherNames.java index 466d228be..cd508cdef 100644 --- a/src/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -69,10 +69,10 @@ public class GatherNames { } - private static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { + public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { Map 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()); diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 439a3a8c1..945ee5d8c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -13,13 +13,13 @@ public class SourceFile extends SyntaxTreeNode{ private String pkgName; public final List KlassenVektor; - public final List imports; + public final Set 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 classDefinitions, List imports){ + public SourceFile(String pkgName, List classDefinitions, Set 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 getImports(){ + public Set getImports(){ return this.imports; } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 325e453fd..66b750a40 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -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){ diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 290fd984b..29492c317 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -104,6 +104,11 @@ class TypeToInsertString implements ResultSetVisitor{ } + @Override + public void visit(PairTPHEqualTPH p) { + + } + @Override public void visit(RefType resolved) { insert = resolved.getName().toString(); diff --git a/src/de/dhbwstuttgart/typeinference/result/ResultPair.java b/src/de/dhbwstuttgart/typeinference/result/ResultPair.java index 4c7541980..4415a9397 100644 --- a/src/de/dhbwstuttgart/typeinference/result/ResultPair.java +++ b/src/de/dhbwstuttgart/typeinference/result/ResultPair.java @@ -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 { + 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; } } diff --git a/src/de/dhbwstuttgart/typeinference/result/ResultSet.java b/src/de/dhbwstuttgart/typeinference/result/ResultSet.java index 1d0bd32db..9c1982ce9 100644 --- a/src/de/dhbwstuttgart/typeinference/result/ResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/result/ResultSet.java @@ -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 diff --git a/src/de/dhbwstuttgart/typeinference/result/ResultSetVisitor.java b/src/de/dhbwstuttgart/typeinference/result/ResultSetVisitor.java index fbf241c1a..bed742dd6 100644 --- a/src/de/dhbwstuttgart/typeinference/result/ResultSetVisitor.java +++ b/src/de/dhbwstuttgart/typeinference/result/ResultSetVisitor.java @@ -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); + }