Add missing object bounds

This commit is contained in:
Victorious3 2023-01-17 15:57:41 +01:00
parent 43e8837308
commit abe7553c37
3 changed files with 49 additions and 5 deletions

View File

@ -0,0 +1,12 @@
import java.lang.String;
import java.lang.Integer;
class Generics4<B extends String> {
<C extends Integer> C m1(C b){
return b;
}
m2(x) {
return m1();
}
}

View File

@ -17,7 +17,6 @@ import de.dhbwstuttgart.typeinference.result.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* @author dholle
@ -453,11 +452,14 @@ public class ASTToTargetAST {
eliminateInfima(input);
}
RefTypeOrTPHOrWildcardOrGeneric getType(TypePlaceholder tph) {
if (equality.containsKey(tph)) {
return getType(equality.get(tph));
RefTypeOrTPHOrWildcardOrGeneric getType(RefTypeOrTPHOrWildcardOrGeneric type) {
if (type instanceof TypePlaceholder tph) {
if (equality.containsKey(tph)) {
return getType(equality.get(tph));
}
return concreteTypes.getOrDefault(tph, tph);
}
return concreteTypes.getOrDefault(tph, tph);
return type;
}
TargetType getTargetType(TypePlaceholder tph) {
@ -655,6 +657,10 @@ public class ASTToTargetAST {
.map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList();
}
private boolean hasGeneric(Set<TargetGeneric> generics, GenericRefType type) {
return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName()));
}
private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Method input) {
var convertedGenerics = new HashSet<>(convert(generics));
outer:
@ -666,6 +672,17 @@ public class ASTToTargetAST {
}
convertedGenerics.addAll(convert(typeVar));
}
var returnType = sigma.getType(input.getReturnType());
if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) {
convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT)));
}
for (var param : input.getParameterList()) {
var type = sigma.getType(param.getType());
if (type instanceof GenericRefType refType && !hasGeneric(convertedGenerics, refType)) {
convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT)));
}
}
return convertedGenerics;
}

View File

@ -148,4 +148,19 @@ public class ASTToTypedTargetAST {
var generics3 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
}
@Test
public void definedGenerics4() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics4.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
var classes = compiler.sourceFiles.get(file).getClasses();
var generics4 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
//var instance = generics4.getDeclaredConstructor().newInstance();
//var method = generics4.getDeclaredMethod("m2", Object.class);
//method.invoke(instance, new Object());
}
}