From 850af6a6ae8bd3604c412bfed814cdd7680387d3 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Thu, 19 Mar 2020 18:36:20 +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/typeinference/unify/TypeUnifyTask.java Vererbung korrigiert --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 73 ++++++++++++------- .../syntaxtree/ClassOrInterface.java | 11 +++ .../typeinference/unify/TypeUnifyTask.java | 4 +- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 157c6b82..8c2b6d41 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -135,50 +135,67 @@ public class JavaTXCompiler { //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; - if (cl.getSuperClass().getName().equals(new JavaClassName("java.lang.Object"))) { - superclass = objectClass; - } - else { - Optional optSuperclass = - importedClasses.stream().filter(x -> x.getClassName().equals( + SourceFile sf_new = sf; + sf.KlassenVektor.forEach(cl -> addMethods(sf_new, cl, importedClasses, objectClass)); + allClasses.addAll(sf.getClasses()); + } + allClasses.addAll(importedClasses); + return new TYPE(sourceFiles.values(), allClasses).getConstraints(); + } + + void addMethods(SourceFile sf, ClassOrInterface cl, List importedClasses, ClassOrInterface objectClass) { + if (!cl.areMethodsAdded()) { + ClassOrInterface superclass = null; + if (cl.getSuperClass().getName().equals(new JavaClassName("java.lang.Object"))) { + superclass = objectClass; + } + else { + Optional optSuperclass = + importedClasses.stream().filter(x -> x.getClassName().equals( cl.getSuperClass().getName())).findFirst(); + if (optSuperclass.isPresent()) { + superclass = optSuperclass.get(); + } + else { + optSuperclass = + sf.KlassenVektor.stream().filter(x -> x.getClassName().equals( + cl.getSuperClass().getName())).findFirst(); if (optSuperclass.isPresent()) { superclass = optSuperclass.get(); + addMethods(sf, superclass, importedClasses, objectClass); } - else { + else { //throw new ClassNotFoundException(""); } } - Iterator paraIt= cl.getSuperClass().getParaList().iterator(); - Iterator tvarVarIt = superclass.getGenerics().iterator(); + } + Iterator paraIt= cl.getSuperClass().getParaList().iterator(); + Iterator tvarVarIt = superclass.getGenerics().iterator(); - HashMap gtvs = new HashMap<>(); - while (paraIt.hasNext()) { - gtvs.put(tvarVarIt.next().getName(), paraIt.next()); - } - Iterator methodIt = superclass.getMethods().iterator(); - while(methodIt.hasNext()) { - Method m = methodIt.next(); - ParameterList newParaList = new ParameterList( + HashMap gtvs = new HashMap<>(); + while (paraIt.hasNext()) { + gtvs.put(tvarVarIt.next().getName(), paraIt.next()); + } + Iterator methodIt = superclass.getMethods().iterator(); + while(methodIt.hasNext()) { + Method m = methodIt.next(); + ParameterList newParaList = new ParameterList( m.getParameterList() .getFormalparalist() .stream() .map(fp -> new FormalParameter(fp.getName(), fp.getType().acceptTV(new TypeExchanger(gtvs)), fp.getOffset())) .collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset()); - cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block, + 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())); - } + (GenericDeclarationList)m.getGenerics(), + m.getOffset())); + } - }); - allClasses.addAll(sf.getClasses()); } - allClasses.addAll(importedClasses); - return new TYPE(sourceFiles.values(), allClasses).getConstraints(); - } + cl.setMethodsAdded(); + } + + public List getAvailableClasses(SourceFile forSourceFile) throws ClassNotFoundException { //PL 2018-09-18: List durch Set ersetzt, damit die Klassen nur einmal hinzugefuegt werden diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 87f11980..d75602d6 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -23,6 +23,7 @@ import java.util.Optional; * Stellt jede Art von Klasse dar. Auch abstrakte Klassen und Interfaces */ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ + private Boolean methodAdded = false; //wird benoetigt bei in JavaTXCompiler.getConstraints() protected int modifiers; protected JavaClassName name; private List fields = new ArrayList<>(); @@ -68,6 +69,16 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ this.constructors = new ArrayList<>(cl.constructors); } + //Gets if it is added + public Boolean areMethodsAdded() { + return methodAdded; + } + + //Sets taht it is added + public void setMethodsAdded() { + methodAdded = true; + } + // Gets class name public JavaClassName getClassName(){ return this.name; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 53390bbc..8f13f01f 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -641,7 +641,9 @@ public class TypeUnifyTask extends RecursiveTask>> { x.getPairOp() == PairOperator.EQUALSDOT) .map(x -> ((PlaceholderType)x.getGroundBasePair().getLhsType()).getVariance()) .findAny(); - variance = optVariance.isPresent() ? optVariance.get() : 2; + //Fuer Operatorenaufrufe wird variance auf 2 gesetzt + //Es werden alle Elemente des Kartesischen Produkts abgearbeitet + variance = optVariance.isPresent() ? optVariance.get() : 2; } //if (variance == 1 && nextSetasList.size() > 1) {