Merge mit bigRefactoring

This commit is contained in:
JanUlrich 2018-06-13 23:54:35 +02:00
commit fe72936737
18 changed files with 253 additions and 54 deletions

View File

@ -7,6 +7,8 @@ import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.TypePrinter;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.*;
@ -24,12 +26,16 @@ public class FCGenerator {
return toFC(availableClasses).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet());
}
public static Set<Pair> toFC(Collection<ClassOrInterface> availableClasses) throws ClassNotFoundException {
HashSet<Pair> pairs = new HashSet<>();
public static Collection<Pair> toFC(Collection<ClassOrInterface> availableClasses) throws ClassNotFoundException {
HashMap<String, Pair> pairs = new HashMap<>();
TypePrinter printer = new TypePrinter();
for(ClassOrInterface cly : availableClasses){
pairs.addAll(getSuperTypes(cly, availableClasses));
for(Pair p : getSuperTypes(cly, availableClasses)){
String hash = p.TA1.acceptTV(printer)+";"+p.TA2.acceptTV(printer);
pairs.put(hash, p);
}
}
return pairs;
return pairs.values();
}
/**

View File

@ -427,6 +427,9 @@ public class SyntaxTreeGenerator{
GenericsRegistry ret = new GenericsRegistry(this.globalGenerics);
ret.putAll(generics);
if(ctx == null || ctx.typeParameterList() == null)return ret;
for(Java8Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
ret.put(tp.Identifier().getText(), new GenericContext(parentClass, parentMethod));
}
for(Java8Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
}

View File

@ -22,18 +22,20 @@ import java.util.List;
public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
String name;
if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().Identifier().getText();
}
Java8Parser.TypeArgumentsContext arguments;
if(unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().Identifier().getText();
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
}else{// if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
name = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().getText();
arguments = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
}
/**
* Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten
* Beispiel: Typ<String>.InnererTyp<Integer>
*/
String name = unannClassOrInterfaceTypeContext.getText();
if(name.contains("<")){
name = name.split("<")[0]; //Der Typ ist alles vor den ersten Argumenten
}
return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics);
}
@ -75,7 +77,6 @@ public class TypeGenerator {
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
generics.put(name, new GenericContext(parentClass, parentMethod));
return ret;
}
@ -90,7 +91,7 @@ public class TypeGenerator {
return ret;
}
if(typeBoundContext.classOrInterfaceType() != null){
ret.add(convert(typeBoundContext.classOrInterfaceType()));
ret.add(convert(typeBoundContext.classOrInterfaceType(), reg, generics));
if(typeBoundContext.additionalBound() != null)
for(Java8Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()){
ret.add(convert(addCtx.interfaceType()));
@ -101,8 +102,9 @@ public class TypeGenerator {
}
}
private static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext) {
throw new NotImplementedException();
private static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
Java8Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = classOrInterfaceTypeContext.classType_lfno_classOrInterfaceType();
return convertTypeName(ctx.Identifier().toString(), ctx.typeArguments(),classOrInterfaceTypeContext.getStart(), reg, generics);
}
private static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.InterfaceTypeContext interfaceTypeContext) {
@ -112,10 +114,7 @@ public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ReferenceTypeContext referenceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
if(referenceTypeContext.classOrInterfaceType() != null){
if(referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType()!= null){
Java8Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType();
//return convertTypeName(ctx.Identifier().toString(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics);
if(ctx.typeArguments() != null)throw new NotImplementedException();
return convertTypeName(referenceTypeContext.getText(), null,referenceTypeContext.getStart(), reg, generics);
return convert(referenceTypeContext.classOrInterfaceType(), reg, generics);//return convertTypeName(referenceTypeContext.getText(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics);
}else{
throw new NotImplementedException();
}

View File

@ -1,11 +1,13 @@
package de.dhbwstuttgart.sat.asp.model;
import de.dhbwstuttgart.bytecode.signature.Signature;
public enum ASPRule {
ASP_PAIR_EQUALS_NAME("equals"),
ASP_PAIR_SMALLER_NAME("smaller"),
ASP_PAIR_SMALLER_DOT_NAME("smallerDot"),
ASP_PARAMLIST_NAME("param"),
ASP_PARAMLIST_END_POINTER("null"),
ASP_FC_PARAMLIST_NAME("paramFC"),
ASP_TYPE("type"),
ASP_FCTYPE("typeFC"),
ASP_TYPE_VAR("typeVar"), ASP_ODER("oder"),

View File

@ -210,7 +210,7 @@ public class ASPParser extends UnifyResultBaseListener {
private List<String> getParams(String pointer) {
List<String> params = new ArrayList<>();
while(pointer != null){
if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params;
//if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params;
if(!parameterLists.containsKey(pointer))
throw new DebugException("Fehler in Ergebnisparsen");
//TODO: Fehler in ASP. Die adapt Regel muss erkennen, wenn die Parameterliste auf der linken Seite kürzer ist und diese Rechtzeitig beenden

View File

@ -0,0 +1,17 @@
grammar ASPResult;
answer : 'ANSWER' (resultSetRule '.')*;
resultSetRule : NAME parameterList;
parameterList : '(' value (',' value)* ')';
value : NAME
| resultSetRule ;
NAME : [a-zA-Z0-9_]+;
WS : [ \t\r\n\u000C]+ -> skip
;
LINE_COMMENT
: '%' ~[\r\n]* -> skip
;

View File

@ -93,7 +93,7 @@ public class ASPFactory implements TypeVisitor<String>{
boolean isFCType = false;
protected void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes);
Collection<Pair> fc = FCGenerator.toFC(classes);
isFCType = true;
for(Pair fcp : fc){
convertPair(fcp);
@ -132,28 +132,18 @@ public class ASPFactory implements TypeVisitor<String>{
return new ASPStatement(stmt);
}
protected String convertParameterlist(List<String> pointers){
//TODO: Hier an die neue Datenstruktur anpassen
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
protected void convertParameterlist(String pointer, List<String> pointers){
Iterator<String> it = pointers.iterator();
String p = pointer;
if(!it.hasNext()){
return ASPRule.ASP_PARAMLIST_END_POINTER.toString();
}
Integer i = 1;
String ruleName = ASPRule.ASP_PARAMLIST_NAME.toString();
if(isFCType) ruleName = ASPRule.ASP_FC_PARAMLIST_NAME.toString();
while (it.hasNext()){
ASPStatement stmt;
String type = it.next();
String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName());
if(it.hasNext()){
stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type, nextP);
}else{
stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type,
ASPRule.ASP_PARAMLIST_END_POINTER.toString());
}
p = nextP;
stmt = makeStatement(ruleName, pointer, type, i.toString());
writer.add(stmt);
i++;
}
return pointer;
}
@Override
@ -165,7 +155,7 @@ public class ASPFactory implements TypeVisitor<String>{
}
String typeName = ASPStringConverter.toConstant(refType.getName());
String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString();
convertParameterlist(params);
convertParameterlist(pointer, params);
ASPStatement stmt = makeStatement(ruleName, pointer, typeName, Integer.toString(params.size()));
writer.add(stmt);
return pointer;
@ -195,7 +185,7 @@ public class ASPFactory implements TypeVisitor<String>{
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
String typeName = ASPStringConverter.toConstant(genericRefType.getParsedName());
String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString();
ASPStatement stmt = makeStatement(ruleName, pointer, typeName, ASPRule.ASP_PARAMLIST_END_POINTER.toString());
ASPStatement stmt = makeStatement(ruleName, pointer, typeName, ASPRule.ASP_LIST_ENDPOINTER.toString());
writer.add(stmt);
return pointer;
}

View File

@ -70,7 +70,7 @@ public class ASPGencayFactory implements TypeVisitor<String> {
}
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes);
Collection<Pair> fc = FCGenerator.toFC(classes);
isFCType = true;
for(Pair fcp : fc){
generateTheta((RefType) fcp.TA1);

View File

@ -66,10 +66,10 @@ public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(ParameterList formalParameters) {
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
Iterator<FormalParameter> it = formalParameters.getFormalparalist().iterator();
if(it.hasNext()){
while(it.hasNext()){
it.next().accept(this);
}
}
}

View File

@ -12,6 +12,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -33,6 +34,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters,
RefType superClass, Boolean isInterface, List<RefType> implementedInterfaces, Token offset){
super(offset);
if(isInterface && !Modifier.isInterface(modifiers))modifiers += Modifier.INTERFACE;
this.modifiers = modifiers;
this.name = name;
this.fields = fielddecl;

View File

@ -37,7 +37,7 @@ public class ASTFactory {
methoden.add(createMethod(method, jreClass));
}
List<Field> felder = new ArrayList<>();
for(java.lang.reflect.Field field : jreClass.getFields()){
for(java.lang.reflect.Field field : jreClass.getDeclaredFields()){
felder.add(createField(field, name));
}
int modifier = jreClass.getModifiers();
@ -89,8 +89,7 @@ public class ASTFactory {
}
private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) {
//TODO: Hier auch GenericRefType generieren:
return new Field(field.getName(), createType(field.getType(), jreClass, null), field.getModifiers(), new NullToken());
return new Field(field.getName(), createType(field.getGenericType(), jreClass, null), field.getModifiers(), new NullToken());
}
//private static RefType createType(Class classType) {

View File

@ -0,0 +1,30 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.type.*;
public class TypePrinter implements TypeVisitor<String> {
@Override
public String visit(RefType refType) {
return refType.toString();
}
@Override
public String visit(SuperWildcardType superWildcardType) {
return "? super " + superWildcardType.getInnerType().acceptTV(this);
}
@Override
public String visit(TypePlaceholder typePlaceholder) {
return "TPH " + typePlaceholder.getName();
}
@Override
public String visit(ExtendsWildcardType extendsWildcardType) {
return "? extends " + extendsWildcardType.getInnerType().acceptTV(this);
}
@Override
public String visit(GenericRefType genericRefType) {
return genericRefType.getParsedName();
}
}

View File

@ -59,13 +59,11 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
}
@Override
public void visit(ParameterList params) {
for(FormalParameter param : params){
public void visit(FormalParameter param) {
if(param.getType() instanceof TypePlaceholder)
inserts.add(TypeInsertFactory.createInsertPoints(
param.getType(), param.getType().getOffset(), cl, method, results));
}
super.visit(params);
super.visit(param);
}
@Override

View File

@ -116,6 +116,16 @@ public class UnifyWithoutWildcards {
assert resultSet.results.size() == 1;
}
@Test
public void fc() throws ClassNotFoundException {
Collection<ClassOrInterface> fc = new ArrayList<>();
fc.add(ASTFactory.createClass(MatrixTest.class));
fc.add(ASTFactory.createClass(Vector.class));
String content = "";
content = ASPFactory.generateASP(new ConstraintSet<>(), fc);
System.out.println(content);
}
public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
content = ASPFactory.generateASP(toTest, fc);
@ -184,4 +194,6 @@ public class UnifyWithoutWildcards {
private class Test1<A> extends Object{}
private class Test2 extends Object{}
private class MatrixTest extends Vector<Vector<Integer>>{}
}

View File

@ -0,0 +1,125 @@
package asp.withWildcards;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class InputGenerator {
public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
@Test
public void finiteClosure() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"fc.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Lambda.jav"));
}
@Test
public void lambda2() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Lambda2.jav"));
}
@Test
public void lambda3() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Lambda3.jav"));
}
@Test
public void mathStruc() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"mathStruc.jav"));
}
@Test
public void generics() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Generics.jav"));
}
@Test
public void genericsMethodCall() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"MethodCallGenerics.jav"));
//TODO: Hier sollte der Rückgabetyp der Methode String sein
}
@Test
public void faculty() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Faculty.jav"));
}
@Test
public void facultyTyped() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"FacultyTyped.jav"));
}
@Test
public void matrix() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Matrix.jav"));
}
@Test
public void packageTests() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Package.jav"));
}
@Test
public void vector() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Vector.jav"));
}
@Test
public void lambdaRunnable() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"LambdaRunnable.jav"));
}
@Test
public void expressions() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"Expressions.jav"));
}
@Test
public void addLong() throws IOException, ClassNotFoundException, InterruptedException {
execute(new File(rootDirectory+"AddLong.jav"));
}
private static class TestResultSet{
}
public void execute(File fileToTest) throws IOException, ClassNotFoundException, InterruptedException {
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
//List<ResultSet> results = compiler.aspTypeInference();
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for(SourceFile sf : compiler.sourceFiles.values()) {
allClasses.addAll(compiler.getAvailableClasses(sf));
allClasses.addAll(sf.getClasses());
}
final ConstraintSet<Pair> cons = compiler.getConstraints();
String content = "";
content = ASPFactory.generateASP(cons, allClasses);
System.out.println(content);
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@ -1,6 +1,9 @@
package astfactory;
//import javafx.collections.ObservableList;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import org.junit.Test;
import java.lang.reflect.ParameterizedType;
@ -19,4 +22,13 @@ public class ASTFactoryTest<A> extends HashMap<String, A>{
System.out.println(Arrays.toString(arguments));
System.out.println(((TypeVariable)arguments[1]).getGenericDeclaration());
}
public static class GenericFieldTest<G>{
public G testField;
}
@Test
public void genericFieldTest(){
ClassOrInterface test = ASTFactory.createClass(GenericFieldTest.class);
assertTrue(test.getFieldDecl().get(0).getType() instanceof GenericRefType);
}
}

View File

@ -42,7 +42,7 @@ public class GeneralParserTest{
filenames.add("StructuralTypes.jav");
*/
// filenames.add("ExtendsTest.jav");
filenames.add("OpratorTest.jav");
filenames.add("PackageNameTest.jav");
try{
new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + s)).collect(Collectors.toList()));
}catch(Exception exc){

View File

@ -1,5 +1,9 @@
import java.lang.Integer;
class ImportTest{
import java.lang.Comparable;
class PackageNameTest{
java.lang.Integer test(a){return a;}
Comparable<Integer> test2(a){return a;}
}