This commit is contained in:
Fayez Abu Alia 2017-12-14 21:20:57 +01:00
commit 72fdf2dca8
13 changed files with 68 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<>();
@ -70,7 +71,8 @@ public class SyntaxTreeGenerator{
}
return ret;
}
/*
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
List<JavaClassName> 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<JavaClassName> 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<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,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){

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);
}

View File

@ -1,6 +1,6 @@
public class DuMethod{
method(a){
method(a,b){
return a;
}

View File

@ -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);

View File

@ -1,4 +1,4 @@
public class VoidMeth{
public class RecursiveMeth{
public Integer test(){
return this.test();
}