forked from JavaTX/JavaCompilerCore
merge
This commit is contained in:
commit
72fdf2dca8
@ -96,7 +96,7 @@ public class JavaTXCompiler {
|
|||||||
private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
|
private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
|
||||||
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
|
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
|
||||||
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile), new GenericsRegistry(null));
|
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile), new GenericsRegistry(null));
|
||||||
SourceFile ret = generator.convert(tree);
|
SourceFile ret = generator.convert(tree,environment.packageCrawler);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
|||||||
public class CompilationEnvironment {
|
public class CompilationEnvironment {
|
||||||
private final List<URL> librarys;
|
private final List<URL> librarys;
|
||||||
private final List<File> sourceFiles;
|
private final List<File> sourceFiles;
|
||||||
|
public final PackageCrawler packageCrawler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien
|
* Imitiert die Environment beim Aufruf des JavaCompilers auf einer Menge von java-Dateien
|
||||||
@ -49,12 +50,13 @@ public class CompilationEnvironment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.sourceFiles = sourceFiles;
|
this.sourceFiles = sourceFiles;
|
||||||
|
this.packageCrawler = new PackageCrawler(librarys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
|
public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException {
|
||||||
Map<String, Integer> allNames;
|
Map<String, Integer> allNames;
|
||||||
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
|
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
|
||||||
allNames = GatherNames.getNames(tree, new PackageCrawler(librarys));
|
allNames = GatherNames.getNames(tree, packageCrawler);
|
||||||
return new JavaClassRegistry(allNames);
|
return new JavaClassRegistry(allNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
|
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.environment.PackageCrawler;
|
||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import java.lang.ClassNotFoundException;
|
import java.lang.ClassNotFoundException;
|
||||||
|
|
||||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||||
import de.dhbwstuttgart.parser.antlr.Java8Parser;
|
import de.dhbwstuttgart.parser.antlr.Java8Parser;
|
||||||
|
import de.dhbwstuttgart.parser.scope.GatherNames;
|
||||||
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
||||||
@ -17,9 +19,8 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
|||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.sql.Ref;
|
import java.sql.Ref;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
import java.util.stream.Collectors;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
//import jdk.internal.dynalink.support.TypeConverterFactory;
|
//import jdk.internal.dynalink.support.TypeConverterFactory;
|
||||||
import org.antlr.v4.runtime.CommonToken;
|
import org.antlr.v4.runtime.CommonToken;
|
||||||
@ -30,7 +31,7 @@ public class SyntaxTreeGenerator{
|
|||||||
private JavaClassRegistry reg;
|
private JavaClassRegistry reg;
|
||||||
private final GenericsRegistry globalGenerics;
|
private final GenericsRegistry globalGenerics;
|
||||||
private String pkgName = "";
|
private String pkgName = "";
|
||||||
List<JavaClassName> imports = new ArrayList();
|
Set<JavaClassName> imports = new HashSet();
|
||||||
|
|
||||||
List<Statement> fieldInitializations = new ArrayList<>();
|
List<Statement> fieldInitializations = new ArrayList<>();
|
||||||
|
|
||||||
@ -70,7 +71,8 @@ public class SyntaxTreeGenerator{
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
|
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
|
||||||
List<JavaClassName> newImports = new ArrayList();
|
List<JavaClassName> newImports = new ArrayList();
|
||||||
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
||||||
@ -89,7 +91,7 @@ public class SyntaxTreeGenerator{
|
|||||||
}
|
}
|
||||||
this.imports.addAll(newImports);
|
this.imports.addAll(newImports);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
|
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
|
||||||
String typeName = convertTypeName(ctx.typeName());
|
String typeName = convertTypeName(ctx.typeName());
|
||||||
JavaClassName ret = reg.getName(typeName);
|
JavaClassName ret = reg.getName(typeName);
|
||||||
@ -107,7 +109,8 @@ public class SyntaxTreeGenerator{
|
|||||||
private List<JavaClassName> convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){
|
private List<JavaClassName> convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){
|
||||||
return reg.getAllFromPackage(ctx.typeName().getText());
|
return reg.getAllFromPackage(ctx.typeName().getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
private String getPackageFromClass(String cls){
|
private String getPackageFromClass(String cls){
|
||||||
String ret = "";
|
String ret = "";
|
||||||
String[] parts = cls.split("\\.");
|
String[] parts = cls.split("\\.");
|
||||||
@ -118,9 +121,10 @@ public class SyntaxTreeGenerator{
|
|||||||
return ret;
|
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<>();
|
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()){
|
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
|
||||||
ClassOrInterface newClass;
|
ClassOrInterface newClass;
|
||||||
if(typeDecl.classDeclaration() != null){
|
if(typeDecl.classDeclaration() != null){
|
||||||
|
@ -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<>();
|
Map<String, Integer> ret = new HashMap<>();
|
||||||
ret.putAll(packages.getClassNames("java.lang"));
|
|
||||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
||||||
|
ret.putAll(packages.getClassNames("java.lang"));
|
||||||
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
||||||
if(importDeclCtx.singleTypeImportDeclaration() != null){
|
if(importDeclCtx.singleTypeImportDeclaration() != null){
|
||||||
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());
|
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());
|
||||||
|
@ -13,13 +13,13 @@ public class SourceFile extends SyntaxTreeNode{
|
|||||||
private String pkgName;
|
private String pkgName;
|
||||||
|
|
||||||
public final List<ClassOrInterface> KlassenVektor;
|
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.
|
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
|
||||||
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
|
* 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());
|
super(new NullToken());
|
||||||
this.KlassenVektor = classDefinitions;
|
this.KlassenVektor = classDefinitions;
|
||||||
this.pkgName = pkgName;
|
this.pkgName = pkgName;
|
||||||
@ -31,7 +31,7 @@ public class SourceFile extends SyntaxTreeNode{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get imports (to test implementation)
|
// Get imports (to test implementation)
|
||||||
public List<JavaClassName> getImports(){
|
public Set<JavaClassName> getImports(){
|
||||||
return this.imports;
|
return this.imports;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
||||||
|
import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH;
|
||||||
import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType;
|
import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType;
|
||||||
import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH;
|
import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH;
|
||||||
import de.dhbwstuttgart.typeinference.result.ResultPair;
|
import de.dhbwstuttgart.typeinference.result.ResultPair;
|
||||||
@ -195,12 +196,21 @@ public class UnifyTypeFactory {
|
|||||||
RefTypeOrTPHOrWildcardOrGeneric tr = UnifyTypeFactory.convert(mp.getRhsType(), tphs);
|
RefTypeOrTPHOrWildcardOrGeneric tr = UnifyTypeFactory.convert(mp.getRhsType(), tphs);
|
||||||
if(tl instanceof TypePlaceholder){
|
if(tl instanceof TypePlaceholder){
|
||||||
if(tr instanceof TypePlaceholder) {
|
if(tr instanceof TypePlaceholder) {
|
||||||
|
<<<<<<< HEAD
|
||||||
if(mp.getPairOp().equals(PairOperator.EQUALSDOT))
|
if(mp.getPairOp().equals(PairOperator.EQUALSDOT))
|
||||||
//throw new DebugException("TPH =. TPH ist ein ungültiges Ergebnis");
|
//throw new DebugException("TPH =. TPH ist ein ungültiges Ergebnis");
|
||||||
//Einfach ignorieren TODO: Das hier muss ausgebessert werden:
|
//Einfach ignorieren TODO: Das hier muss ausgebessert werden:
|
||||||
return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, ASTFactory.createObjectType());
|
return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, ASTFactory.createObjectType());
|
||||||
else
|
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);
|
return new PairTPHsmallerTPH((TypePlaceholder)tl, (TypePlaceholder)tr);
|
||||||
|
}
|
||||||
}else if(tr instanceof RefType){
|
}else if(tr instanceof RefType){
|
||||||
return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, (RefType) tr);
|
return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder)tl, (RefType) tr);
|
||||||
}else if(tr instanceof WildcardType){
|
}else if(tr instanceof WildcardType){
|
||||||
|
@ -104,6 +104,11 @@ class TypeToInsertString implements ResultSetVisitor{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(PairTPHEqualTPH p) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(RefType resolved) {
|
public void visit(RefType resolved) {
|
||||||
insert = resolved.getName().toString();
|
insert = resolved.getName().toString();
|
||||||
|
@ -5,22 +5,22 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|||||||
/**
|
/**
|
||||||
* Paare, welche das Unifikationsergebnis darstellen
|
* Paare, welche das Unifikationsergebnis darstellen
|
||||||
*/
|
*/
|
||||||
public abstract class ResultPair {
|
public abstract class ResultPair<A extends RefTypeOrTPHOrWildcardOrGeneric,B extends RefTypeOrTPHOrWildcardOrGeneric> {
|
||||||
private final RefTypeOrTPHOrWildcardOrGeneric left;
|
private final A left;
|
||||||
private final RefTypeOrTPHOrWildcardOrGeneric right;
|
private final B right;
|
||||||
|
|
||||||
public abstract void accept(ResultSetVisitor visitor);
|
public abstract void accept(ResultSetVisitor visitor);
|
||||||
|
|
||||||
public ResultPair(RefTypeOrTPHOrWildcardOrGeneric left, RefTypeOrTPHOrWildcardOrGeneric right){
|
public ResultPair(A left, B right){
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RefTypeOrTPHOrWildcardOrGeneric getLeft() {
|
public A getLeft() {
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RefTypeOrTPHOrWildcardOrGeneric getRight() {
|
public B getRight() {
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,11 @@ class Resolver implements ResultSetVisitor {
|
|||||||
public ResolvedType resolve(TypePlaceholder tph){
|
public ResolvedType resolve(TypePlaceholder tph){
|
||||||
toResolve = tph;
|
toResolve = tph;
|
||||||
resolved = null;
|
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){
|
for(ResultPair resultPair : result.results){
|
||||||
resultPair.accept(this);
|
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
|
@Override
|
||||||
public void visit(RefType refType) {
|
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
|
@Override
|
||||||
public void visit(RefType refType) {
|
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:
|
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
|
Das wird verwendet, wenn alle relatedTPHs aus den Parametern eines RefTypes angefügt werden sollen
|
||||||
|
@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.type.*;
|
|||||||
public interface ResultSetVisitor {
|
public interface ResultSetVisitor {
|
||||||
void visit(PairTPHsmallerTPH p);
|
void visit(PairTPHsmallerTPH p);
|
||||||
void visit(PairTPHequalRefTypeOrWildcardType p);
|
void visit(PairTPHequalRefTypeOrWildcardType p);
|
||||||
|
void visit(PairTPHEqualTPH p);
|
||||||
|
|
||||||
void visit(RefType refType);
|
void visit(RefType refType);
|
||||||
|
|
||||||
@ -15,4 +16,5 @@ public interface ResultSetVisitor {
|
|||||||
void visit(TypePlaceholder typePlaceholder);
|
void visit(TypePlaceholder typePlaceholder);
|
||||||
|
|
||||||
void visit(ExtendsWildcardType extendsWildcardType);
|
void visit(ExtendsWildcardType extendsWildcardType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
public class DuMethod{
|
public class DuMethod{
|
||||||
|
|
||||||
method(a){
|
method(a,b){
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class JavaTXCompilerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void test() throws IOException, java.lang.ClassNotFoundException {
|
public void test() throws IOException, java.lang.ClassNotFoundException {
|
||||||
System.out.println(rootDirectory);
|
System.out.println(rootDirectory);
|
||||||
String fileName = "RecursiveMeth";
|
String fileName = "IfTest";
|
||||||
filesToTest.add(new File(rootDirectory+fileName+".jav"));
|
filesToTest.add(new File(rootDirectory+fileName+".jav"));
|
||||||
System.out.println(rootDirectory+fileName+".jav");
|
System.out.println(rootDirectory+fileName+".jav");
|
||||||
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
|
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
public class VoidMeth{
|
public class RecursiveMeth{
|
||||||
public Integer test(){
|
public Integer test(){
|
||||||
return this.test();
|
return this.test();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user