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
This commit is contained in:
pl@gohorb.ba-horb.de 2020-04-13 16:08:42 +02:00
parent 6afee86066
commit 1b6af9ab7e
3 changed files with 92 additions and 25 deletions

View File

@ -714,10 +714,14 @@ public class JavaTXCompiler {
return ret;
}
public void generateBytecodForFile(String path, HashMap<String, byte[]> classFiles, SourceFile sf,
List<ResultSet> typeinferenceResult) throws IOException {
public synchronized void generateBytecodForFile(String path, /* HashMap<String, byte[]> classFiles,*/ SourceFile sf,
List<ResultSet> typeinferenceResult, List<GenericGenratorResultForSourceFile> genericResultsRet) throws IOException {
//genericResultsRet is empty; fuer die Rückgabe
try {
HashMap<String, byte[]> classFiles = new HashMap<>();
List<GenericGenratorResultForSourceFile> 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);

View File

@ -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<ResultSet> newResults;
public static Set<TypeInsert> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List<ResultSet> newResults){
public static Set<TypeInsert> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List<ResultSet> newResults, List<GenericGenratorResultForSourceFile> 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<GenericsGeneratorResult> constraints, List<GenericsGeneratorResult> 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<GenericGenratorResultForSourceFile> 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<TypeInsertPoint> createGenericInsert(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){
Set<TypeInsertPoint> result = createGenericClassInserts(genericResult, cl);
private static synchronized Set<TypeInsertPoint> createGenericInsert(List<GenericsGeneratorResult> methodConstraints, List<GenericsGeneratorResult> classConstraints,ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){
Set<TypeInsertPoint> result = createGenericClassInserts(classConstraints, cl);
Resolver resolver = new Resolver(resultSet);
if (m != null) {
List<GenericsGeneratorResult> methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, m));
//List<GenericsGeneratorResult> 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<TypeInsertPoint> createGenericClassInserts(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl) {
private static Set<TypeInsertPoint> createGenericClassInserts(List<GenericsGeneratorResult> classConstraints, ClassOrInterface cl) {
Set<TypeInsertPoint> result = new HashSet<>();
Token offset = cl.getGenerics().getOffset();
List<GenericsGeneratorResult> classConstraints = genericResult.getClassConstraints();
//List<GenericsGeneratorResult> 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<GenericsGeneratorResult> constraints;
private List<GenericsGeneratorResult> classConstraints;
TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type){
type.accept(this);
TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type, List<GenericsGeneratorResult> constraints, List<GenericsGeneratorResult> classConstraints){
this.constraints = constraints;
this.classConstraints = classConstraints;
type.accept(this);
}
@Override
@ -171,7 +186,7 @@ class TypeToInsertString implements ResultSetVisitor{
Iterator<RefTypeOrTPHOrWildcardOrGeneric> 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<GenericsGeneratorResult> 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<GenericsGeneratorResult> getEqualTPHFromClassConstraints(List<GenericsGeneratorResult> listOfConstraints, String tph) {
return listOfConstraints.stream()
.filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph))
.findFirst();
}
private String getEqualTPH(List<GenericsGeneratorResult> constraints2, String tph) {
return constraints2.stream()
.filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph))
.findFirst().get().getConstraint().getLeft();
}
}

View File

@ -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<TypeInsert> inserts = new HashSet<>();
private ResultSet withResults;
String pkgName;
private List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles;
public Set<TypeInsert> getTypeInserts(SourceFile forSourceFile, ResultSet withResults){
public Set<TypeInsert> getTypeInserts(SourceFile forSourceFile, ResultSet withResults, List<GenericGenratorResultForSourceFile> 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<TypeInsert> inserts = new HashSet<>();
private Method method;
private Resolver resolver;
List<GenericsGeneratorResult> constraints;
List<GenericsGeneratorResult> 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);
}