TypeInsertFactory anfügen, convert UnifyPair->Pair implementieren

This commit is contained in:
JanUlrich 2017-05-18 13:17:52 +02:00
parent 2c999d0eb6
commit 4a52bc0e6e
12 changed files with 182 additions and 100 deletions

View File

@ -1,15 +1,19 @@
package de.dhbwstuttgart.core;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
@ -24,12 +28,18 @@ public class JavaTXCompiler {
private List<SourceFile> sourceFiles = new ArrayList<>();
public List<TypeInsertPoint> getTypeInserts(File forSourceFile){
return null;
public List<TypeInsertPoint> getTypeInserts(File forFile){
ResultSet result = typeInference();
for(SourceFile sf : sourceFiles){
if(sf.getFile().equals(forFile)){
return TypeInsertFactory.createTypeInsertPoints(sf, result);
}
}
throw new DebugException("Die Datei "+forFile+" wurde nicht geparst");
}
public ResultSet typeInference(){
ConstraintSet cons = new ConstraintSet();
ConstraintSet<Pair> cons = new ConstraintSet<>();
List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : sourceFiles){
allClasses.addAll(sf.getClasses());
@ -53,7 +63,21 @@ public class JavaTXCompiler {
System.out.println("RESULT: " + result);
results.addAll(result);
}
return new ResultSet(results);
return new ResultSet(UnifyTypeFactory.convert(results, generateTPHMap(cons)));
}
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints){
HashMap<String, TypePlaceholder> ret = new HashMap<>();
constraints.map((Pair p)->{
if(p.TA1 instanceof TypePlaceholder){
ret.put(((TypePlaceholder)p.TA1).getName(), (TypePlaceholder) p.TA1);
}
if(p.TA2 instanceof TypePlaceholder){
ret.put(((TypePlaceholder)p.TA2).getName(), (TypePlaceholder) p.TA2);
}
return null;
});
return ret;
}
public void parse(File sourceFile) throws IOException, ClassNotFoundException {

View File

@ -22,7 +22,7 @@ public class JavaTXParser {
Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(generateJavaLangNames()));
return generator.convert(tree);
return generator.convert(tree, sourceFile);
}
private List<String> generateJavaLangNames() throws IOException, ClassNotFoundException {

View File

@ -12,6 +12,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typecheck.*;
import java.io.File;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
@ -167,7 +168,7 @@ public class SyntaxTreeGenerator{
return ret;
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx, File parsedFile) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
this.setImports(ctx);
@ -181,7 +182,7 @@ public class SyntaxTreeGenerator{
}
classes.add(newClass);
}
return new SourceFile(this.pkgName, classes, this.imports);
return new SourceFile(parsedFile, this.pkgName, classes, this.imports);
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, GenericsRegistry generics) {

View File

@ -1,4 +1,5 @@
package de.dhbwstuttgart.syntaxtree;
import java.io.File;
import java.util.*;
@ -6,39 +7,26 @@ import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.security.x509.X509CertInfo;
public class SourceFile extends SyntaxTreeNode{
private String pkgName;
private List<ClassOrInterface> KlassenVektor = new ArrayList<>();
private List<JavaClassName> imports;
private final List<ClassOrInterface> KlassenVektor;
private final List<JavaClassName> imports;
private final File file;
/**
/**
* 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(File file, String pkgName, List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){
super(new NullToken());
this.KlassenVektor = classDefinitions;
if(pkgName != null){
this.pkgName = pkgName;
}
if(imports != null){
this.imports = imports;
}
}
public SourceFile(List<ClassOrInterface> classDefinitions){
this(null, classDefinitions, null);
}
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions){
this(pkgName, classDefinitions, null);
}
public SourceFile(List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){
this(null, classDefinitions, imports);
this.pkgName = pkgName;
this.imports = imports;
this.file = file;
}
public String getPkgName(){
@ -70,4 +58,8 @@ public class SourceFile extends SyntaxTreeNode{
public List<ClassOrInterface> getClasses() {
return KlassenVektor;
}
public File getFile() {
return file;
}
}

View File

@ -1,12 +1,14 @@
package de.dhbwstuttgart.syntaxtree.factory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
@ -133,61 +135,65 @@ public class UnifyTypeFactory {
* Convert from
* UnifyType -> ASTType
*/
/*
public static Pair convert(UnifyPair mp) {
Type tl = UnifyTypeFactory.convert(mp.getLhsType());
Type tr = UnifyTypeFactory.convert(mp.getRhsType());
public static Set<Set<Pair>> convert(Set<Set<UnifyPair>> unifyPairSet, Map<String,TypePlaceholder> tphs) {
return unifyPairSet.stream().map(
set -> set.stream().map(
unifyPair -> convert(unifyPair, tphs))
.collect(Collectors.toSet()))
.collect(Collectors.toSet());
}
public static Pair convert(UnifyPair mp, Map<String,TypePlaceholder> tphs) {
RefTypeOrTPHOrWildcardOrGeneric tl = UnifyTypeFactory.convert(mp.getLhsType(), tphs);
RefTypeOrTPHOrWildcardOrGeneric tr = UnifyTypeFactory.convert(mp.getRhsType(), tphs);
return new Pair(tl, tr, mp.getPairOp());
}
public static Type convert(ReferenceType t) {
//TODO: Hier kann man die GTVs extrahieren
if(t.getName().toString().equals(Void.VOID_NAME))return new Void( 0);
RefType ret = new RefType(t.getName(),0);
ret.set_ParaList(convert(t.getTypeParams()));
public static RefTypeOrTPHOrWildcardOrGeneric convert(ReferenceType t, Map<String,TypePlaceholder> tphs) {
if(JavaClassName.Void.equals(t.getName()))return new Void(new NullToken());
RefType ret = new RefType(JavaClassName.Void,convert(t.getTypeParams(), tphs),new NullToken());
return ret;
}
public static Type convert(FunNType t) {
RefType ret = new RefType(t.getName(),0);
ret.set_ParaList(convert(t.getTypeParams()));
public static RefTypeOrTPHOrWildcardOrGeneric convert(FunNType t, Map<String,TypePlaceholder> tphs) {
RefType ret = new RefType(new JavaClassName(t.getName()), convert(t.getTypeParams(), tphs), new NullToken());
return ret;
}
public static Type convert(SuperType t) {
RefType innerType = new RefType(t.getSuperedType().getName(),0);
return new SuperWildcardType(innerType);
public static RefTypeOrTPHOrWildcardOrGeneric convert(SuperType t, Map<String,TypePlaceholder> tphs) {
RefType innerType = new RefType(new JavaClassName(t.getSuperedType().getName()), new NullToken());
return new SuperWildcardType(innerType, new NullToken());
}
public static Type convert(ExtendsType t) {
RefType innerType = new RefType(t.getExtendedType().getName(),0);
return new ExtendsWildcardType(innerType);
public static RefTypeOrTPHOrWildcardOrGeneric convert(ExtendsType t, Map<String,TypePlaceholder> tphs) {
RefType innerType = new RefType(new JavaClassName(t.getExtendedType().getName()), new NullToken());
return new ExtendsWildcardType(innerType, new NullToken());
}
public static Type convert(PlaceholderType t) {
TypePlaceholder ret = TypePlaceholder.getInstance(t.getName());
public static RefTypeOrTPHOrWildcardOrGeneric convert(PlaceholderType t, Map<String,TypePlaceholder> tphs) {
TypePlaceholder ret = tphs.get(t.getName());
if(ret == null){ //Dieser TPH wurde vom Unifikationsalgorithmus erstellt
ret = TypePlaceholder.fresh(t.getName(), NULL_NODE);
ret = TypePlaceholder.fresh(new NullToken());
}
return ret;
}
public static Type convert(UnifyType t) {
if(t instanceof FunNType)return convert((FunNType) t);
if(t instanceof ReferenceType)return convert((ReferenceType) t);
if(t instanceof SuperType)return convert((SuperType) t);
if(t instanceof ExtendsType)return convert((ExtendsType) t);
if(t instanceof PlaceholderType)return convert((PlaceholderType) t);
public static RefTypeOrTPHOrWildcardOrGeneric convert(UnifyType t, Map<String,TypePlaceholder> tphs) {
if(t instanceof FunNType)return convert((FunNType) t, tphs);
if(t instanceof ReferenceType)return convert((ReferenceType) t, tphs);
if(t instanceof SuperType)return convert((SuperType) t, tphs);
if(t instanceof ExtendsType)return convert((ExtendsType) t, tphs);
if(t instanceof PlaceholderType)return convert((PlaceholderType) t, tphs);
throw new NotImplementedException("Der Typ "+t+" kann nicht umgewandelt werden");
}
private static List<Type> convert(TypeParams typeParams) {
List<Type> ret = new ArrayList<>();
private static List<RefTypeOrTPHOrWildcardOrGeneric> convert(TypeParams typeParams, Map<String,TypePlaceholder> tphs) {
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
for(UnifyType uT : typeParams){
Type toAdd = convert(uT);
RefTypeOrTPHOrWildcardOrGeneric toAdd = convert(uT, tphs);
ret.add(toAdd);
}
return ret;
}
*/
}

View File

@ -17,7 +17,7 @@ public class SuperWildcardType extends WildcardType{
* Author: Arne ¼dtke<br/>
* Standard Konstruktor ¼r eine SuperWildcard
*/
public SuperWildcardType(Token offset, RefType innerType)
public SuperWildcardType( RefType innerType, Token offset)
{
super(innerType, offset);
}

View File

@ -15,10 +15,7 @@ import org.antlr.v4.runtime.Token;
*/
public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
{
private static Hashtable<String, TypePlaceholder> m_TypePlaceholdersRegistry = new Hashtable<String, TypePlaceholder>();
private final String name;
private final String name;
@ -32,30 +29,6 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
super(offset);
this.name = name;
}
/**
* Statische Methode, um einen TypePlaceholder aus der Registry zu holen.
* <br>Author: ¯Â¿Â½rg ¯Â¿Â½uerle
* @param name Der Name des TypePlaceholders
* @return Der TypePlaceholder oder <code>null</code>, falls er nicht in der
* Registry existiert
public static TypePlaceholder getInstance(String name)
{
return m_TypePlaceholdersRegistry.get(name);
}
*/
/**
* Generiert einen neuen TPH mit einem bestimmten Namen.
* Wird benÃtigt, wenn aus Generischen Variablen TPH generiert werden.
* @param name
* @return
*/
public static TypePlaceholder fresh(String name, SyntaxTreeNode parent)
{
return null;
}
/**

View File

@ -0,0 +1,40 @@
package de.dhbwstuttgart.typedeployment;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class TypeInsertFactory {
public static List<TypeInsertPoint> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){
List<TypeInsertPoint> ret = new ArrayList<>();
for(ClassOrInterface cl : forSourcefile.getClasses()){
//Felder:
for(Field field : cl.getFieldDecl()){
if(field.getType() instanceof TypePlaceholder){
RefTypeOrTPHOrWildcardOrGeneric resolved = withResults.resolveType(field.getType());
String toInsert = getString(resolved) + " ";
ret.add(new TypeInsertPoint(field.getType().getOffset(), toInsert));
}
}
}
return ret;
}
private static String getString(RefTypeOrTPHOrWildcardOrGeneric resolved) {
if(resolved instanceof RefType){
return ((RefType) resolved).getName().toString();
}else if(resolved instanceof TypePlaceholder){
return ((TypePlaceholder) resolved).getName();
}else{
throw new NotImplementedException();
}
}
}

View File

@ -1,11 +1,36 @@
package de.dhbwstuttgart.typeinference;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.util.Set;
public class ResultSet {
public ResultSet(Set<Set<UnifyPair>> results){
Set<Set<Pair>> results;
public ResultSet(Set<Set<Pair>> results){
this.results = results;
}
public RefTypeOrTPHOrWildcardOrGeneric resolveType(RefTypeOrTPHOrWildcardOrGeneric type) {
/*//Probleme:
* Es müssen teilweise mehrere TPH eingesetzt werden
* Es werden alle eingesetzt, welch in der Kette stehen!
* TPHs müssen zu eindeutigen Namen aufgelöst werden
*/
final RefTypeOrTPHOrWildcardOrGeneric ret;
for(Set<Pair> pairs : results)for(Pair pair : pairs){
if(pair.OperatorEqual()){ //type ist vom Typ TypePlaceholder
if(pair.TA1.equals(type)){
return pair.TA2;
}else if(pair.TA2.equals(type)){
return pair.TA1;
}
}
}
return type;
}
}

View File

@ -3,17 +3,16 @@ import java.io.Serializable;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class Pair implements Serializable
{
public RefTypeOrTPHOrWildcardOrGeneric TA1;
public RefTypeOrTPHOrWildcardOrGeneric TA2;
public final RefTypeOrTPHOrWildcardOrGeneric TA1;
public final RefTypeOrTPHOrWildcardOrGeneric TA2;
private PairOperator eOperator = PairOperator.SMALLER;
Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2 )
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2 )
{
this.TA1 = TA1;
this.TA2 = TA2;
@ -22,7 +21,7 @@ public class Pair implements Serializable
eOperator = PairOperator.SMALLER;
}
Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
{
// Konstruktor
this(TA1,TA2);

View File

@ -8,7 +8,7 @@ import java.util.List;
* A pair which contains two types and an operator, e.q. (Integer <. a).
* @author Florian Steurer
*/
public class UnifyPair {
public class UnifyPair {
/**
* The type on the left hand side of the pair.
@ -88,12 +88,14 @@ public class UnifyPair {
return "(" + lhs + " " + pairOp + " " + rhs + ")";
}
/*
public List<? extends PlaceholderType> getInvolvedPlaceholderTypes() {
ArrayList<PlaceholderType> ret = new ArrayList<>();
ret.addAll(lhs.getInvolvedPlaceholderTypes());
ret.addAll(rhs.getInvolvedPlaceholderTypes());
return ret;
}
*/
}

View File

@ -2,10 +2,18 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.*;
@ -21,6 +29,18 @@ public class JavaTXCompilerTest {
//compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
//compiler.parse(new File(rootDirectory+"Lambda.jav"));
compiler.typeInference();
List<TypeInsertPoint> result = compiler.getTypeInserts(new File(rootDirectory+"Methods.jav"));
String content = readFile(rootDirectory+"Methods.jav", StandardCharsets.UTF_8);
for(TypeInsertPoint tip : result){
System.out.println(tip.insert(content));
}
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}