From 592de5310e631861e5195777b88417e0f6f69f79 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Wed, 18 Mar 2020 17:36:10 +0100 Subject: [PATCH] modified: ../src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java modified: ../src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java modified: ../src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java modified: ../src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: ../src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: ../src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 41 +++++++++++-------- .../syntaxtree/ClassOrInterface.java | 18 ++++++++ .../dhbwstuttgart/syntaxtree/SourceFile.java | 7 ++++ .../typeinference/typeAlgo/TYPEStmt.java | 14 ++++--- .../typeinference/unify/TypeUnifyTask.java | 10 ++++- .../typeinference/unify/model/UnifyPair.java | 2 + 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 458585e1..157c6b82 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -110,6 +110,8 @@ public class JavaTXCompiler { public ConstraintSet getConstraints() throws ClassNotFoundException, IOException { List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); List importedClasses = new ArrayList<>(); + ClassOrInterface objectClass = ASTFactory.createClass( + classLoader.loadClass(new JavaClassName("java.lang.Object").toString())); //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC for (File forSourceFile : sourceFiles.keySet()){ for (JavaClassName name : sourceFiles.get(forSourceFile).getImports()) { @@ -125,16 +127,29 @@ public class JavaTXCompiler { } for (File f : this.sourceFiles.keySet()) { SourceFile sf = sourceFiles.get(f); + sf = new SourceFile(sf.getPkgName(), + sf.KlassenVektor.stream() + .map(cl -> new ClassOrInterface(cl)) + .collect(Collectors.toCollection(ArrayList::new)), + sf.imports); + //sf enthaelt neues Source-File, neue Klassen-Objekte und neue + //ArrayListen-Objekte fuer Fields, Construktoren und Methoden + //Alle anderen Objekte werden nur kopiert. sf.KlassenVektor.forEach(cl -> { ClassOrInterface superclass = null; - Optional optSuperclass = - importedClasses.stream().filter(x -> x.getClassName().equals( - cl.getSuperClass().getName())).findFirst(); - if (optSuperclass.isPresent()) { - superclass = optSuperclass.get(); - } + if (cl.getSuperClass().getName().equals(new JavaClassName("java.lang.Object"))) { + superclass = objectClass; + } else { - //throw new ClassNotFoundException(""); + Optional optSuperclass = + importedClasses.stream().filter(x -> x.getClassName().equals( + cl.getSuperClass().getName())).findFirst(); + if (optSuperclass.isPresent()) { + superclass = optSuperclass.get(); + } + else { + //throw new ClassNotFoundException(""); + } } Iterator paraIt= cl.getSuperClass().getParaList().iterator(); Iterator tvarVarIt = superclass.getGenerics().iterator(); @@ -152,22 +167,12 @@ public class JavaTXCompiler { .stream() .map(fp -> new FormalParameter(fp.getName(), fp.getType().acceptTV(new TypeExchanger(gtvs)), fp.getOffset())) .collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset()); - /* - Iterator genericsIt = m.getGenerics().iterator(); - List newGenericsList = new ArrayList<>(); - Set keys = gtvs.keySet(); - while (genericsIt.hasNext()) { - GenericTypeVar gtv = genericsIt.next(); - if (!keys.contains(gtv.getName())) { - newGenericsList.add(gtv); - } - } - */ cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block, //new GenericDeclarationList(newGenericsList, ((GenericDeclarationList)m.getGenerics()).getOffset()), (GenericDeclarationList)m.getGenerics(), m.getOffset())); } + }); allClasses.addAll(sf.getClasses()); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index bbd184f0..87f11980 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -34,6 +34,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private List implementedInterfaces; private List constructors; + public ClassOrInterface(int modifiers, JavaClassName name, List fielddecl, Optional fieldInitializations, List methods, List constructors, GenericDeclarationList genericClassParameters, RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset){ super(offset); @@ -50,6 +51,23 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ this.constructors = constructors; } + /* erzeugt fuer Fields, Konstruktoren und Methoden neue ArrayList-Objekte + * alle anderen Datenobjekte werden nur kopiert. + */ + public ClassOrInterface(ClassOrInterface cl){ + super(cl.getOffset()); + this.modifiers = cl.modifiers; + this.name = cl.name; + this.fields = new ArrayList<>(cl.fields); + this.fieldInitializations= cl.fieldInitializations; + this.genericClassParameters = cl.genericClassParameters; + this.superClass = cl.superClass; + this.isInterface = cl.isInterface; + this.implementedInterfaces = cl.implementedInterfaces; + this.methods = new ArrayList<>(cl.methods); + this.constructors = new ArrayList<>(cl.constructors); + } + // Gets class name public JavaClassName getClassName(){ return this.name; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java index 945ee5d8..4a22147b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -26,6 +26,13 @@ public class SourceFile extends SyntaxTreeNode{ this.imports = imports; } + public SourceFile(SourceFile sf) { + super(new NullToken()); + this.KlassenVektor = new ArrayList<>(sf.KlassenVektor); + this.imports = new HashSet<>(sf.imports); + } + + public String getPkgName(){ return this.pkgName; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index bd054a7c..684077e4 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -574,11 +574,12 @@ public class TYPEStmt implements StatementVisitor{ RefTypeOrTPHOrWildcardOrGeneric retType = assumption.getReceiverType(resolver); methodConstraint.add(new Pair(forMethod.receiver.getType(), retType, - PairOperator.SMALLERDOT)); + PairOperator.EQUALSDOT));//PL 2020-03-17 SMALLERDOT in EQUALSDOT umgewandelt, weil alle geerbten Methoden in den jeweilen Klassen enthalten sind. - //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt - methodConstraint.add(new Pair(forMethod.receiverType, retType, - PairOperator.EQUALSDOT)); + //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ANFANG + //methodConstraint.add(new Pair(forMethod.receiverType, retType, + // PairOperator.EQUALSDOT)); + //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); @@ -595,8 +596,9 @@ public class TYPEStmt implements StatementVisitor{ RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i); ret.add(new Pair(argType, assType, PairOperator.SMALLERDOT)); - //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt - ret.add(new Pair(foMethod.argTypes.get(i), assType, PairOperator.EQUALSDOT)); + //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ANFANG + // ret.add(new Pair(foMethod.argTypes.get(i), assType, PairOperator.EQUALSDOT)); + //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE } return ret; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 56e0eca2..76db7a2a 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -1813,7 +1813,10 @@ public class TypeUnifyTask extends RecursiveTask>> { oderConstraintsOutput.remove(ret); //Set retFlat = new HashSet<>(); //ret.stream().forEach(x -> retFlat.addAll(x)); - ret.stream().forEach(x -> x.stream().forEach(y -> y.addSubstitutions(x))); + ret.stream().forEach(x -> x.stream().forEach(y -> { Set x_new = new HashSet<>(x); //PL 2020-03-18: y selbst darf nicht in die Substitutionen + x_new.remove(y); + y.addSubstitutions(x_new); + })); result.get(8).add(ret); first = false; } @@ -1836,7 +1839,10 @@ public class TypeUnifyTask extends RecursiveTask>> { // System.out.println("M"); //Set retFlat = new HashSet<>(); //ret.stream().forEach(x -> retFlat.addAll(x)); - ret.stream().forEach(x -> x.stream().forEach(y -> y.addSubstitutions(x))); + ret.stream().forEach(x -> x.stream().forEach(y -> { Set x_new = new HashSet<>(x); //PL 2020-03-18: y selbst darf nicht in die Substitutionen + x_new.remove(y); + y.addSubstitutions(x_new); + })); result.get(8).add(ret); first = false; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index fbe3a099..06c464dc 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -195,11 +195,13 @@ public class UnifyPair { if (other.getBasePair() != basePair || (other.getBasePair() == null && basePair == null)) { return false; } + if (!other.getBasePair().equals(basePair) || !other.getAllSubstitutions().equals(getAllSubstitutions())) { return false; } } + return other.getPairOp() == pairOp && other.getLhsType().equals(lhs)