diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 4ec0e2c3..80afb800 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 2f04423f..7044da6b 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 ed9d82e2..bae6e9b9 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 466d228b..cd508cde 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 439a3a8c..945ee5d8 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 e99713fb..e0a8ddbf 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,21 @@ public class UnifyTypeFactory { RefTypeOrTPHOrWildcardOrGeneric tr = UnifyTypeFactory.convert(mp.getRhsType(), tphs); if(tl instanceof TypePlaceholder){ if(tr instanceof TypePlaceholder) { +<<<<<<< HEAD if(mp.getPairOp().equals(PairOperator.EQUALSDOT)) //throw new DebugException("TPH =. TPH ist ein ungültiges Ergebnis"); //Einfach ignorieren TODO: Das hier muss ausgebessert werden: return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, ASTFactory.createObjectType()); else +======= + 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{ +>>>>>>> eb8db0e0eb901afd5f4502ee4f7769a394b23422 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 290fd984..29492c31 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 4c754198..4415a939 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 1d0bd32d..9c1982ce 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 fbf241c1..bed742dd 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); + } diff --git a/test/bytecode/DuMethod.jav b/test/bytecode/DuMethod.jav index b56f6c55..12666893 100644 --- a/test/bytecode/DuMethod.jav +++ b/test/bytecode/DuMethod.jav @@ -1,6 +1,6 @@ public class DuMethod{ - method(a){ + method(a,b){ return a; } diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index 35c2ea3f..66e60fe1 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -29,7 +29,7 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, java.lang.ClassNotFoundException { System.out.println(rootDirectory); - String fileName = "RecursiveMeth"; + String fileName = "IfTest"; filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); diff --git a/test/bytecode/RecursiveMeth.jav b/test/bytecode/RecursiveMeth.jav index fd2851b0..be35a43c 100644 --- a/test/bytecode/RecursiveMeth.jav +++ b/test/bytecode/RecursiveMeth.jav @@ -1,4 +1,4 @@ -public class VoidMeth{ +public class RecursiveMeth{ public Integer test(){ return this.test(); }