From 1b6af9ab7e2a70461c4a916820fa3b02c360837d Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Mon, 13 Apr 2020 16:08:42 +0200 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java modified: src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java modified: src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java Bug 180 - Andere Einsetzung als im Bytecode geloest --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 8 ++- .../typedeployment/TypeInsertFactory.java | 70 ++++++++++++++----- .../typedeployment/TypeInsertPlacer.java | 39 +++++++++-- 3 files changed, 92 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index d44ac77f0..2b5c76fb7 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -714,10 +714,14 @@ public class JavaTXCompiler { return ret; } - public void generateBytecodForFile(String path, HashMap classFiles, SourceFile sf, - List typeinferenceResult) throws IOException { + public synchronized void generateBytecodForFile(String path, /* HashMap classFiles,*/ SourceFile sf, + List typeinferenceResult, List genericResultsRet) throws IOException { + //genericResultsRet is empty; fuer die Rückgabe try { + HashMap classFiles = new HashMap<>(); + List genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + genericResultsRet.addAll(genericResults);//fuer Rueckgabe BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path); bytecodeGen.visit(sf); this.writeClassFile(bytecodeGen.getClassFiles(), path); diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 9baea74c3..4959585f5 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typedeployment; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Optional; import java.util.Set; import org.antlr.v4.runtime.Token; @@ -47,18 +48,21 @@ public class TypeInsertFactory { private static List newResults; - public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List newResults){ + public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List newResults, List simplifyResultsForAllSourceFiles){ TypeInsertFactory.newResults = newResults; - return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults); + return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults, simplifyResultsForAllSourceFiles); } public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m, - ResultSet resultSet) { + ResultSet resultSet, List constraints, List classConstraints) { - try { + /* PL 2020-04-11 auskommentiert + * try { + */ ResolvedType resolvedType = resultSet.resolveType(type); TypeInsertPoint insertPoint = new TypeInsertPoint(offset, - new TypeToInsertString(resolvedType.resolvedType).insert, KindOfTypeInsertPoint.NORMAL_INSERT); + new TypeToInsertString(resolvedType.resolvedType, constraints, classConstraints).insert, KindOfTypeInsertPoint.NORMAL_INSERT); + /* PL 2020-04-11 auskommentiert List simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(newResults); for (GenericGenratorResultForSourceFile simplifyResultsEntries : simplifyResults) { GenericsGeneratorResultForClass genericResultsForClass = simplifyResultsEntries.getSimplifyResultsByName("", cl.getClassName().toString()); @@ -66,19 +70,25 @@ public class TypeInsertFactory { } return new TypeInsert(insertPoint, new HashSet<>(), resolvedType.getResultPair()); + */ + //GenericsGeneratorResultForClass genericResultsForClass = genericResult.getSimplifyResultsByName("", cl.getClassName().toString()); + return new TypeInsert(insertPoint, createGenericInsert(constraints, classConstraints, cl, m, resultSet, offset), resolvedType.getResultPair()); + + /* PL 2020-04-11 auskommentiert } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } + */ } - private static synchronized Set createGenericInsert(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ - Set result = createGenericClassInserts(genericResult, cl); + private static synchronized Set createGenericInsert(List methodConstraints, List classConstraints,ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ + Set result = createGenericClassInserts(classConstraints, cl); Resolver resolver = new Resolver(resultSet); if (m != null) { - List methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, m)); + //List methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, m)); result.addAll(createMethodConstraints(methodConstraints, m.getOffset() != null ? m.getOffset() : mOffset)); } @@ -111,13 +121,13 @@ public class TypeInsertFactory { return result; } - private static Set createGenericClassInserts(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl) { + private static Set createGenericClassInserts(List classConstraints, ClassOrInterface cl) { Set result = new HashSet<>(); Token offset = cl.getGenerics().getOffset(); - List classConstraints = genericResult.getClassConstraints(); + //List classConstraints = genericResult.getClassConstraints(); - if (classConstraints.size() == 0) { + if (classConstraints == null || classConstraints.size() == 0) { return result; } @@ -143,9 +153,14 @@ public class TypeInsertFactory { class TypeToInsertString implements ResultSetVisitor{ String insert = ""; + private List constraints; + private List classConstraints; + - TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type){ - type.accept(this); + TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type, List constraints, List classConstraints){ + this.constraints = constraints; + this.classConstraints = classConstraints; + type.accept(this); } @Override @@ -171,7 +186,7 @@ class TypeToInsertString implements ResultSetVisitor{ Iterator iterator = resolved.getParaList().iterator(); while(iterator.hasNext()){ RefTypeOrTPHOrWildcardOrGeneric typeParam = iterator.next(); - insert += new TypeToInsertString(typeParam).insert; + insert += new TypeToInsertString(typeParam, constraints, classConstraints).insert; if(iterator.hasNext())insert += ", "; } insert += ">"; @@ -185,16 +200,37 @@ class TypeToInsertString implements ResultSetVisitor{ @Override public void visit(SuperWildcardType superWildcardType) { - insert += "? super " + new TypeToInsertString(superWildcardType.getInnerType()).insert; + insert += "? super " + new TypeToInsertString(superWildcardType.getInnerType(), constraints, classConstraints).insert; } @Override public void visit(TypePlaceholder typePlaceholder) { - insert += typePlaceholder.getName(); + String realName = typePlaceholder.getName(); + //String realName = sig2.substring(1, sig2.length() - 1); + //String toVisit = realName+SPECIAL_CHAR; + //if(!genericsAndBounds.containsKey(toVisit)) {//PL 202-04-1 vielleicht braucht man das, vgl. Signature.java + Optional equalTPH = getEqualTPHFromClassConstraints(classConstraints, realName); + if(equalTPH.isPresent()){ + insert += equalTPH.get().getConstraint().getLeft();// + SPECIAL_CHAR; + } else { + insert += getEqualTPH(constraints, realName);// + SPECIAL_CHAR; + } } @Override public void visit(ExtendsWildcardType extendsWildcardType) { - insert += "? extends " + new TypeToInsertString(extendsWildcardType.getInnerType()).insert; + insert += "? extends " + new TypeToInsertString(extendsWildcardType.getInnerType(), constraints, classConstraints).insert; } + + private Optional getEqualTPHFromClassConstraints(List listOfConstraints, String tph) { + return listOfConstraints.stream() + .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) + .findFirst(); + } + + private String getEqualTPH(List constraints2, String tph) { + return constraints2.stream() + .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) + .findFirst().get().getConstraint().getLeft(); + } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index e0627e5e8..ef65a4458 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,10 +1,16 @@ package de.dhbwstuttgart.typedeployment; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; +import de.dhbwstuttgart.bytecode.utilities.MethodUtility; +import de.dhbwstuttgart.bytecode.utilities.Resolver; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -12,47 +18,68 @@ import java.util.Set; public class TypeInsertPlacer extends AbstractASTWalker{ Set inserts = new HashSet<>(); private ResultSet withResults; + String pkgName; + private List simplifyResultsForAllSourceFiles; - public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults){ + public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults, List simplifyResultsForAllSourceFiles){ this.withResults = withResults; + this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles; + pkgName = forSourceFile.getPkgName(); forSourceFile.accept(this); return inserts; } @Override public void visit(ClassOrInterface classOrInterface) { - TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults); + GenericsGeneratorResultForClass generatedGenerics = simplifyResultsForAllSourceFiles + .stream() + .map(sr->sr.getSimplifyResultsByName(pkgName, classOrInterface.getClassName().toString())) + .findFirst() + .get(); + TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, generatedGenerics); this.inserts.addAll(cl.inserts); } } class TypeInsertPlacerClass extends AbstractASTWalker{ protected final ResultSet results; + private GenericsGeneratorResultForClass generatedGenerics; protected final ClassOrInterface cl; public final Set inserts = new HashSet<>(); private Method method; + + private Resolver resolver; + + List constraints; + List classConstraints; - TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){ + TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults, GenericsGeneratorResultForClass generatedGenerics){ this.cl = forClass; this.method = null; this.results = withResults; + this.generatedGenerics = generatedGenerics; + resolver = new Resolver(withResults); //PL 2020-04-12 Ob das stimmt weiss ich nicht forClass.accept(this); } @Override public void visit(Method method) { this.method = method; + String id = MethodUtility.createID(resolver, method); + constraints = generatedGenerics.getMethodConstraintsByID(id); + classConstraints = generatedGenerics.getClassConstraints(); if(method.getReturnType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - method.getReturnType(), method.getReturnType().getOffset(), cl, method, results)); + method.getReturnType(), method.getReturnType().getOffset(), cl, method, results, constraints, classConstraints)); super.visit(method); } @Override public void visit(Field field) { if(field.getType() instanceof TypePlaceholder){ + classConstraints = generatedGenerics.getClassConstraints(); inserts.add(TypeInsertFactory.createInsertPoints( - field.getType(), field.getType().getOffset(), cl, method, results)); + field.getType(), field.getType().getOffset(), cl, method, results, new ArrayList<>(), classConstraints)); } super.visit(field); } @@ -61,7 +88,7 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ public void visit(FormalParameter param) { if(param.getType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - param.getType(), param.getType().getOffset(), cl, method, results)); + param.getType(), param.getType().getOffset(), cl, method, results, constraints, classConstraints)); super.visit(param); }