Add test and fix up the generics a bit

This commit is contained in:
Victorious3 2023-04-19 19:08:52 +02:00
parent d945947362
commit af4db1817a
5 changed files with 82 additions and 4 deletions

View File

@ -0,0 +1,11 @@
import java.lang.String;
class TXGenerics {
a;
b;
test() {
var c = new Cycle();
c.m(a, b);
}
}

View File

@ -19,6 +19,7 @@ import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.target.tree.type.TargetRefType;
import de.dhbwstuttgart.util.Pair;
import javassist.bytecode.SignatureAttribute;
import org.antlr.v4.runtime.Token;
@ -207,7 +208,8 @@ public class ASTFactory {
}
return new GenericDeclarationList(gtvs, new NullToken());
} else {
return createGenerics(signature);
var res = createGenerics(signature);
return res;
}
}
@ -220,8 +222,31 @@ public class ASTFactory {
final Stack<RefTypeOrTPHOrWildcardOrGeneric> bound = new Stack<>();
final Stack<RefType> classTypes = new Stack<>();
// All hail the mighty visitor pattern
final SignatureVisitor doNothing = new SignatureVisitor(Opcodes.ASM7) {};
char wildcard = '=';
@Override
public SignatureVisitor visitSuperclass() {
return doNothing;
}
@Override
public SignatureVisitor visitParameterType() {
return doNothing;
}
@Override
public SignatureVisitor visitReturnType() {
return doNothing;
}
@Override
public SignatureVisitor visitExceptionType() {
return doNothing;
}
@Override
public void visitFormalTypeParameter(String name) {
bounds = new ArrayList<>();
@ -231,8 +256,12 @@ public class ASTFactory {
@Override
public void visitTypeVariable(String name) {
var refType = new GenericRefType(name, new NullToken());
if (classTypes.isEmpty()) {
((List<RefTypeOrTPHOrWildcardOrGeneric>) gtvs.get(gtvs.size() - 1).getBounds()).add(refType);
} else {
pushType(refType);
}
}
@Override
public void visitClassType(String name) {

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.util;
import java.util.Objects;
import java.util.Optional;
public class Pair<T, T1> {
@ -22,4 +23,17 @@ public class Pair<T, T1> {
public String toString() {
return "(" + key.toString() + "," + value.toString() + ")\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
}

View File

@ -1,4 +1,5 @@
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
@ -57,6 +58,24 @@ public class GenericsParserTest {
);
}
@Test
public void testMethodTypeVars() {
var signature = "<A:TB;B:LClassA;C:LClassB<TA;>;D:TB;>()V";
var generics = ASTFactory.createGenerics(signature);
assertEquals(generics, new GenericDeclarationList(
List.of(
new GenericTypeVar("A", List.of(new GenericRefType("B", new NullToken())), new NullToken(), new NullToken()),
new GenericTypeVar("B", List.of(new RefType(new JavaClassName("ClassA"), new NullToken())), new NullToken(), new NullToken()),
new GenericTypeVar("C", List.of(new RefType(new JavaClassName("ClassB"),
List.of(new GenericRefType("A", new NullToken())),
new NullToken())), new NullToken(), new NullToken()
),
new GenericTypeVar("D", List.of(new GenericRefType("B", new NullToken())), new NullToken(), new NullToken())
),
new NullToken()
));
}
@Test
public void testMethodComplex() {
var signature = "<T:LClassA<+TT;LClassB<+LClassA;-LClassC<LClassA;>;>;>;U:LClassC<LClassC;>;>()V";

View File

@ -575,6 +575,11 @@ public class TestComplete {
assertEquals(fstArgm2, m2.getGenericReturnType());
}
@Test
public void testTXGenerics() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "TXGenerics.jav");
var instance = classFiles.get("TXGenerics").getDeclaredConstructor().newInstance();
}
@Test
public void typedIdTest() throws Exception {