diff --git a/resources/bytecode/javFiles/Matrix.jav b/resources/bytecode/javFiles/Matrix.jav index f8c426b4..b94c0783 100644 --- a/resources/bytecode/javFiles/Matrix.jav +++ b/resources/bytecode/javFiles/Matrix.jav @@ -1,6 +1,6 @@ import java.util.Vector; import java.lang.Integer; -import java.lang.Float; +//import java.lang.Float; //import java.lang.Byte; //import java.lang.Boolean; diff --git a/resources/bytecode/javFiles/Scalar.jav b/resources/bytecode/javFiles/Scalar.jav new file mode 100644 index 00000000..b731df11 --- /dev/null +++ b/resources/bytecode/javFiles/Scalar.jav @@ -0,0 +1,30 @@ +import java.util.Vector; +import java.lang.Integer; +import java.lang.Float; +//import java.lang.Byte; +//import java.lang.Boolean; + +public class Scalar extends Vector { + + +/* /statistics auskommentiert + Scalar(v) { + Integer i; + i = 0; + while(i < v.size()) { + this.add(v.elementAt(i)); + i=i+1; + } + } + */ + + mul(v) { + var ret = 0; + var i = 0; + while(i < size()) { + ret = ret + this.elementAt(i) * v.elementAt(i); + i = i+1; + } + return ret; + } +} diff --git a/resources/insertGenericsJav/TestAssign.jav b/resources/insertGenericsJav/TestAssign.jav new file mode 100644 index 00000000..d401641e --- /dev/null +++ b/resources/insertGenericsJav/TestAssign.jav @@ -0,0 +1,9 @@ +class TestAssign { + assign(x, y) { + x = y; + } + + assign2(x, y) { + assign(x, y); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index efa8def2..8c60ac42 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -717,10 +717,14 @@ public class Codegen { .toList()); impl = new TargetMethod( - 0, name, Set.of(), Set.of(), - parameters, - lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(), - lambda.block() + 0, name, + lambda.block(), + new TargetMethod.Signature( + Set.of(), + parameters, + lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType() + ), + null ); generateMethod(impl); lambdas.put(lambda, impl); @@ -758,7 +762,7 @@ public class Codegen { mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, - Type.getType(TargetMethod.getDescriptor(impl.returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) + Type.getType(TargetMethod.getDescriptor(impl.signature().returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) ); } @@ -995,8 +999,8 @@ public class Codegen { private void generateConstructor(TargetConstructor constructor) { MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); - if (!constructor.txGenerics().isEmpty()) - mv.visitAttribute(new JavaTXSignatureAttribute(constructor.getTXSignature())); + if (constructor.txGenerics() != null) + mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(constructor.getTXSignature()))); mv.visitCode(); var state = new State(null, mv, 1); @@ -1022,17 +1026,18 @@ public class Codegen { private void generateMethod(TargetMethod method) { // TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null); - if (!method.txGenerics().isEmpty()) - mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature())); + if (method.txSignature() != null) { + mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(method.getTXSignature()))); + } System.out.println(method.getDescriptor()); System.out.println(method.getSignature()); mv.visitCode(); - var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1); - for (var param: method.parameters()) + var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1); + for (var param: method.signature().parameters()) state.createVariable(param.name(), param.type()); generate(state, method.block()); - if (method.returnType() == null) + if (method.signature().returnType() == null) mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); @@ -1054,8 +1059,8 @@ public class Codegen { generateSignature(clazz, clazz.generics()), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); - if (!clazz.txGenerics().isEmpty()) - cw.visitAttribute(new JavaTXSignatureAttribute(generateSignature(clazz, clazz.txGenerics()))); + if (clazz.txGenerics() != null) + cw.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(generateSignature(clazz, clazz.txGenerics())))); clazz.fields().forEach(this::generateField); clazz.constructors().forEach(this::generateConstructor); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java index cc9d1487..bd5f6028 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java @@ -2,13 +2,10 @@ package de.dhbwstuttgart.bytecode; import org.objectweb.asm.*; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; - public class JavaTXSignatureAttribute extends Attribute { - final String signature; + final int signature; - protected JavaTXSignatureAttribute(String signature) { + protected JavaTXSignatureAttribute(int signature) { super("JavaTXSignature"); this.signature = signature; } @@ -17,13 +14,13 @@ public class JavaTXSignatureAttribute extends Attribute { protected Attribute read(ClassReader classReader, int offset, int length, char[] charBuffer, int codeAttributeOffset, Label[] labels) { var data = new byte[length]; System.arraycopy(classReader.b, offset, data, 0, length); - return new JavaTXSignatureAttribute(new String(data, StandardCharsets.UTF_8)); + return new JavaTXSignatureAttribute(data[0] << 8 | data[1]); } @Override protected ByteVector write(ClassWriter classWriter, byte[] code, int codeLength, int maxStack, int maxLocals) { var data = new ByteVector(); - data.putUTF8(this.signature); + data.putShort(this.signature); return data; } } diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 64b89f9a..7e80673d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -887,7 +887,7 @@ public class JavaTXCompiler { path = new File(outputPath ,sf.getPkgName().replace(".","/")); //add package path to root path } - var converter = new ASTToTargetAST(typeinferenceResult); + var converter = new ASTToTargetAST(typeinferenceResult, sf, classLoader); var generatedClasses = new HashMap(); for (var clazz : sf.getClasses()) { var codegen = new Codegen(converter.convert(clazz)); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 9a7d9ccf..3ebd5430 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -36,7 +36,11 @@ public class ASTToTargetAST { return all.stream().map(GenericsResult::new).toList(); } - record Generics(Set> javaGenerics, Set> txGenerics) {} + record Generics(Set> javaGenerics, Set> txGenerics, Set userDefinedGenerics) { + Generics(Set> javaGenerics, Set> txGenerics) { + this(javaGenerics, txGenerics, Set.of()); + } + } class Sigma { Map computedGenericsOfMethods = new HashMap<>(); @@ -112,11 +116,23 @@ public class ASTToTargetAST { } return false; }); + } else if (input.stream().anyMatch(p -> p.getLeft().equals(pair.getLeft()))) { + return; } input.add(pair); } + static void addToEquality(Map equality, TypePlaceholder from, TypePlaceholder to) { + for (var entry : new HashSet<>(equality.entrySet())) { + if (entry.getValue().equals(from)) { + equality.remove(entry.getKey()); + equality.put(entry.getKey(), to); + } + } + equality.put(from, to); + } + static Set> transitiveClosure(Set> generics) { Set> all = new HashSet<>(generics); HashSet> toAdd = new HashSet<>(); @@ -140,14 +156,17 @@ public class ASTToTargetAST { private void methodFindConstraints( ClassOrInterface owner, Method method, Set simplifiedConstraints, - HashSet typeVariables, - HashSet typeVariablesOfClass, + Set typeVariables, + Set userDefinedGenericsOfClass, + Set typeVariablesOfClass, Set> result, Map equality ) { // Type variables with bounds that are also type variables of the method for (var typeVariable : new HashSet<>(typeVariables)) { - if (typeVariablesOfClass.contains(typeVariable)) continue; + typeVariable = equality.getOrDefault(typeVariable, typeVariable); + if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) + continue; for (var pair : simplifiedConstraints) { if (pair.left.equals(typeVariable) && typeVariables.contains(pair.right)) { addToPairs(result, new PairTPHsmallerTPH(pair.left, equality.getOrDefault(pair.right, pair.right))); @@ -192,7 +211,6 @@ public class ASTToTargetAST { var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList()); if (optMethod.isEmpty()) return; var method = optMethod.get(); - var generics = generics(owner, method).javaGenerics(); // transitive and @@ -448,7 +466,8 @@ public class ASTToTargetAST { // All unbounded type variables (bounds not in method) outer: for (var typeVariable : typeVariables) { - if (typeVariablesOfClass.contains(typeVariable)) continue; + if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) + continue; for (var pair : result) { if (pair.getLeft().equals(typeVariable)) continue outer; @@ -463,12 +482,16 @@ public class ASTToTargetAST { if (pair.right.equals(pair2.left)) continue outer; } - if (!typeVariablesOfClass.contains(pair.right) && typeVariables.contains(pair.right)) { + if (!classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, pair.right) && typeVariables.contains(pair.right)) { addToPairs(result, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT)); } } } + private boolean classHasGeneric(Set userDefinedGenericsOfClass, Set typeVariablesOfClass, TypePlaceholder typeVariable) { + return typeVariablesOfClass.contains(typeVariable) || userDefinedGenericsOfClass.stream().anyMatch(g -> g.getName().equals(typeVariable.getName())); + } + private void methodFindTypeVariables( Method method, Set> genericsOfClass, @@ -514,7 +537,9 @@ public class ASTToTargetAST { var generics = new Generics(javaResult, txResult); computedGenericsOfMethods.put(method, generics); - var genericsOfClass = generics(owner).javaGenerics(); + var classGenerics = generics(owner); + var genericsOfClass = classGenerics.javaGenerics(); + var txGenericsOfClass = classGenerics.txGenerics(); var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints); HashSet txTypeVariables = new HashSet<>(); @@ -523,10 +548,10 @@ public class ASTToTargetAST { HashSet javaTypeVariablesOfClass = new HashSet<>(); methodFindTypeVariables(method, genericsOfClass, javaTypeVariablesOfClass, javaTypeVariables, equality); - methodFindTypeVariables(method, genericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality); + methodFindTypeVariables(method, txGenericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality); - methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, javaTypeVariablesOfClass, javaResult, equality); - methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, txTypeVariablesOfClass, txResult, txEquality); + methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, classGenerics.userDefinedGenerics, javaTypeVariablesOfClass, javaResult, equality); + methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, classGenerics.userDefinedGenerics, txTypeVariablesOfClass, txResult, txEquality); { // Java Generics var referenced = new HashSet(); @@ -562,7 +587,6 @@ public class ASTToTargetAST { } System.out.println(method.name + ": " + txResult + " & " + javaResult); - return generics; } @@ -583,14 +607,14 @@ public class ASTToTargetAST { if (left.equals(tph)) { var pair = new PairTPHsmallerTPH(tph, right); if (!generics.contains(pair)) { - generics.add(pair); + addToPairs(generics, pair); findAllBounds(right, generics, equality); found = true; } } } if (!found) - generics.add(new PairTPHequalRefTypeOrWildcardType(tph, OBJECT)); + addToPairs(generics, new PairTPHequalRefTypeOrWildcardType(tph, OBJECT)); } else if (type instanceof RefType refType) { refType.getParaList().forEach(t -> findAllBounds(t, generics, equality)); } @@ -610,33 +634,32 @@ public class ASTToTargetAST { findAllBounds(field.getType(), txResult, txEquality); } - { // Java Generics - var referenced = new HashSet(); - eliminateCycles(javaResult, equality, referenced); - eliminateInfima(javaResult, equality, referenced); - eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced); - equalizeTypeVariables(javaResult, equality); - } - { // TX Generics - var referenced = new HashSet(); - eliminateInfima(txResult, txEquality, referenced); - eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, referenced); - } + var referenced = new HashSet(); + eliminateCycles(javaResult, equality, referenced); + eliminateInfima(javaResult, equality, referenced); + var txReferenced = new HashSet(); + eliminateInfima(txResult, txEquality, txReferenced); - System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult); + eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced); + equalizeTypeVariables(javaResult, equality); + eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, txReferenced); + + System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult + ", " + javaResult); return generics; } void equalizeTypeVariables(Set> input, Map equality) { + System.out.println(input); for (var pair : new HashSet<>(input)) { if (pair instanceof PairTPHsmallerTPH ptph) { + System.out.println(pair + " " + ptph.left.getVariance() + " " + ptph.right.getVariance()); if (ptph.left.getVariance() == 1 && ptph.right.getVariance() == -1) { - equality.put(ptph.left, ptph.right); + addToEquality(equality, ptph.left, ptph.right); input.remove(ptph); - for (var pair2 : new HashSet<>(input)) { - if (pair2 instanceof PairTPHsmallerTPH ptph2 && ptph2.right.equals(ptph.left)) { - input.remove(pair2); - addToPairs(input, new PairTPHsmallerTPH(ptph2.left, ptph.right)); + for (var pair2 : new HashSet<>(simplifiedConstraints)) { + if (pair2.right.equals(ptph.left)) { + simplifiedConstraints.remove(pair2); + simplifiedConstraints.add(new PairTPHsmallerTPH(pair2.left, ptph.right)); } } } @@ -724,7 +747,7 @@ public class ASTToTargetAST { var right = cycle.get(i + 1); var pair = new PairTPHsmallerTPH(left, right); input.remove(pair); - equality.put(left, newTph); + addToEquality(equality, left, newTph); } } } @@ -748,7 +771,7 @@ public class ASTToTargetAST { addToPairs(input, new PairTPHsmallerTPH(left, newTph)); input.removeAll(infima); for (var infimum : infima) { - equality.put(infimum.right, newTph); + addToEquality(equality, infimum.right, newTph); new HashSet<>(input).forEach(pair -> { if (pair.getLeft().equals(infimum.right)) { input.remove(pair); @@ -922,22 +945,22 @@ public class ASTToTargetAST { return true; } - Set convert(Set> result) { + Set convert(Set> result, Map equality) { return result.stream().map(p -> { if (p instanceof PairTPHsmallerTPH pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else if (p instanceof PairTPHequalRefTypeOrWildcardType pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else { throw new IllegalArgumentException(); } }).collect(Collectors.toSet()); } - public List convert(GenericTypeVar typeVar) { + public List convert(GenericTypeVar typeVar, Map equality) { var ret = new ArrayList(); for (var bound : typeVar.getBounds()) { - ret.add(new TargetGeneric(typeVar.getName(), convert(bound))); + ret.add(new TargetGeneric(typeVar.getName(), convert(bound, equality))); } return ret; } @@ -949,16 +972,18 @@ public class ASTToTargetAST { var genericsIter = input.getGenerics().iterator(); if (genericsIter.hasNext()) { // Add empty set of generics to cache so that it doesn't try to calculate it later - sigma.computedGenericsOfClasses.put(input, new Generics(new HashSet<>(), new HashSet<>())); + var userDefinedGenerics = new HashSet(); + sigma.computedGenericsOfClasses.put(input, new Generics(new HashSet<>(), new HashSet<>(), userDefinedGenerics)); while (genericsIter.hasNext()) { var next = genericsIter.next(); - javaGenerics.addAll(convert(next)); + userDefinedGenerics.add(next); + javaGenerics.addAll(convert(next, sigma.equality)); } } else { // Generate generics only if there are no user defined ones var generics = sigma.generics(input); - javaGenerics = convert(generics.javaGenerics()); - txGenerics = convert(generics.txGenerics()); + javaGenerics = convert(generics.javaGenerics(), sigma.equality); + txGenerics = convert(generics.txGenerics(), sigma.txEquality); } TargetBlock fieldInitializer = null; @@ -975,9 +1000,9 @@ public class ASTToTargetAST { ); } - private List convert(ParameterList input) { + private List convert(ParameterList input, Map equality) { return input.getFormalparalist().stream() - .map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList(); + .map(param -> new MethodParameter(convert(param.getType(), equality), param.getName())).toList(); } private boolean hasGeneric(Set generics, GenericRefType type) { @@ -985,7 +1010,7 @@ public class ASTToTargetAST { } private Set collectMethodGenerics(Set> generics, Map equality, Method input) { - var convertedGenerics = new HashSet<>(convert(generics)); + var convertedGenerics = new HashSet<>(convert(generics, equality)); outer: for (GenericTypeVar typeVar : input.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) { @@ -993,9 +1018,9 @@ public class ASTToTargetAST { continue outer; } } - convertedGenerics.addAll(convert(typeVar)); + convertedGenerics.addAll(convert(typeVar, equality)); } - var returnType = sigma.getType(input.getReturnType(), equality); + /*var returnType = sigma.getType(input.getReturnType(), equality); if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT))); } @@ -1004,7 +1029,7 @@ public class ASTToTargetAST { if (type instanceof GenericRefType refType && !hasGeneric(convertedGenerics, refType)) { convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT))); } - } + }*/ return convertedGenerics; } @@ -1017,12 +1042,13 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); - result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer)); + result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, txParams, convert(input.block), fieldInitializer)); parameterSet.add(params); } } @@ -1038,16 +1064,18 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); + var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); result.add(new TargetMethod( input.modifier, - input.name, javaGenerics, txGenerics, params, - convert(input.getReturnType()), - convert(input.block) + input.name, convert(input.block), + new TargetMethod.Signature(javaGenerics, params, convert(input.getReturnType(), sigma.equality)), + new TargetMethod.Signature(txGenerics, txParams, convert(input.getReturnType(), sigma.txEquality)) )); parameterSet.add(params); } @@ -1079,7 +1107,11 @@ public class ASTToTargetAST { public Map auxiliaries = new HashMap<>(); - protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + return convert(input, sigma.equality); + } + + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map equality) { return input.acceptTV(new TypeVisitor<>() { @Override public TargetType visit(RefType refType) { @@ -1113,7 +1145,7 @@ public class ASTToTargetAST { @Override public TargetType visit(TypePlaceholder typePlaceholder) { - return sigma.getTargetType(typePlaceholder, sigma.equality); + return sigma.getTargetType(typePlaceholder, equality); } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java index e350707e..5f0008c0 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java @@ -18,14 +18,14 @@ public class GenericsResult { public Set> get(ClassOrInterface clazz) { var generics = this.sigma.computedGenericsOfClasses.get(clazz); if (generics == null) return Set.of(); - return generics.txGenerics(); + return generics.javaGenerics(); } // TODO Compute generics if not present? public Set> get(Method method) { var generics = this.sigma.computedGenericsOfMethods.get(method); if (generics == null) return Set.of(); - return generics.txGenerics(); + return generics.javaGenerics(); } public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java index 781df996..3118a1d8 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java @@ -25,7 +25,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addMethod(int access, String name, Set generics, List parameterTypes, TargetType returnType, TargetBlock block) { - this.methods.add(new TargetMethod(access, name, generics, Set.of(), parameterTypes, returnType, block)); + this.methods.add(new TargetMethod(access, name, block, new TargetMethod.Signature(generics, parameterTypes, returnType), null)); } public void addMethod(int access, String name, List parameterTypes, TargetType returnType, TargetBlock block) { @@ -33,7 +33,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addConstructor(int access, Set generics, List paramterTypes, TargetBlock block) { - this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, block, null)); + this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, List.of(), block, null)); } public void addConstructor(int access, List paramterTypes, TargetBlock block) { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index 7d3aa389..1da5dc93 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -1,12 +1,13 @@ package de.dhbwstuttgart.target.tree; +import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.target.tree.expression.TargetBlock; import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; import java.util.Set; -public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, TargetBlock block, TargetBlock fieldInitializer) { +public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, List txParameters, TargetBlock block, TargetBlock fieldInitializer) { public String getDescriptor() { return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); @@ -17,7 +18,7 @@ public record TargetConstructor(int access, Set generics, Set generics, Set txGenerics, List parameters, TargetType returnType, TargetBlock block) { +public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) { + public record Signature(Set generics, List parameters, TargetType returnType) { } + public static String getDescriptor(TargetType returnType, TargetType... parameters) { String ret = "("; for (var parameterType : parameters) { @@ -35,15 +37,15 @@ public record TargetMethod(int access, String name, Set generics, } public String getDescriptor() { - return getDescriptor(returnType, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); + return getDescriptor(signature.returnType, signature.parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); } public String getSignature() { - return getSignature(generics, parameters, returnType); + return getSignature(signature.generics, signature.parameters, signature.returnType); } public String getTXSignature() { - return getSignature(txGenerics, parameters, returnType); + return getSignature(txSignature.generics, txSignature.parameters, txSignature.returnType); } public boolean isStatic() { diff --git a/src/test/java/targetast/GreaterEqualTest.java b/src/test/java/targetast/GreaterEqualTest.java index c104433f..1edda1d3 100644 --- a/src/test/java/targetast/GreaterEqualTest.java +++ b/src/test/java/targetast/GreaterEqualTest.java @@ -16,7 +16,7 @@ public class GreaterEqualTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("GreaterEqual.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "GreaterEqual.jav"); classToTest = classFiles.get("GreaterEqual"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/GreaterThanTest.java b/src/test/java/targetast/GreaterThanTest.java index fd1d654a..4174492d 100644 --- a/src/test/java/targetast/GreaterThanTest.java +++ b/src/test/java/targetast/GreaterThanTest.java @@ -15,7 +15,7 @@ public class GreaterThanTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("GreaterThan.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "GreaterThan.jav"); classToTest = classFiles.get("GreaterThan"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/InheritTest.java b/src/test/java/targetast/InheritTest.java index 9e9fff71..9a8de6fd 100644 --- a/src/test/java/targetast/InheritTest.java +++ b/src/test/java/targetast/InheritTest.java @@ -17,14 +17,12 @@ public class InheritTest { @BeforeClass public static void setUpBeforeClass() throws Exception { var classLoader = new ByteArrayClassLoader(); - // TODO Box is loaded and has a method called m so it is generating overloads for it - classLoader.loadClass(Path.of(System.getProperty("user.dir"), "/src/test/resources/target/Box.class")); - classToTest = TestCodegen.generateClassFiles("Inherit.jav", classLoader).get("Inherit"); - classToTestAA = TestCodegen.generateClassFiles("AA.jav", classLoader).get("AA"); - classToTestBB = TestCodegen.generateClassFiles("BB.jav", classLoader).get("BB"); - classToTestCC = TestCodegen.generateClassFiles("CC.jav", classLoader).get("CC"); - classToTestDD = TestCodegen.generateClassFiles("DD.jav", classLoader).get("DD"); + classToTestAA = TestCodegen.generateClassFiles(classLoader, "AA.jav").get("AA"); + classToTestBB = TestCodegen.generateClassFiles(classLoader, "BB.jav").get("BB"); + classToTestCC = TestCodegen.generateClassFiles(classLoader, "CC.jav").get("CC"); + classToTestDD = TestCodegen.generateClassFiles(classLoader, "DD.jav").get("DD"); + classToTest = TestCodegen.generateClassFiles(classLoader, "Inherit.jav").get("Inherit"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); diff --git a/src/test/java/targetast/InheritTest2.java b/src/test/java/targetast/InheritTest2.java index 87e052f1..d1bad977 100644 --- a/src/test/java/targetast/InheritTest2.java +++ b/src/test/java/targetast/InheritTest2.java @@ -16,11 +16,11 @@ public class InheritTest2 { @BeforeClass public static void setUpBeforeClass() throws Exception { var classLoader = new ByteArrayClassLoader(); - classToTest = TestCodegen.generateClassFiles("Inherit2.jav", classLoader).get("Inherit2"); - classToTestAA = TestCodegen.generateClassFiles("AA.jav", classLoader).get("AA"); - classToTestBB = TestCodegen.generateClassFiles("BB.jav", classLoader).get("BB"); - classToTestCC = TestCodegen.generateClassFiles("CC.jav", classLoader).get("CC"); - classToTestDD = TestCodegen.generateClassFiles("DD.jav", classLoader).get("DD"); + classToTest = TestCodegen.generateClassFiles(classLoader, "Inherit2.jav").get("Inherit2"); + classToTestAA = TestCodegen.generateClassFiles(classLoader, "AA.jav").get("AA"); + classToTestBB = TestCodegen.generateClassFiles(classLoader, "BB.jav").get("BB"); + classToTestCC = TestCodegen.generateClassFiles(classLoader, "CC.jav").get("CC"); + classToTestDD = TestCodegen.generateClassFiles(classLoader,"DD.jav").get("DD"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); diff --git a/src/test/java/targetast/LessEqualTest.java b/src/test/java/targetast/LessEqualTest.java index af5237cc..bb1fe888 100644 --- a/src/test/java/targetast/LessEqualTest.java +++ b/src/test/java/targetast/LessEqualTest.java @@ -15,7 +15,7 @@ public class LessEqualTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("LessEqual.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "LessEqual.jav"); classToTest = classFiles.get("LessEqual"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/LessThanTest.java b/src/test/java/targetast/LessThanTest.java index 7b0d45c8..75eeb1b0 100644 --- a/src/test/java/targetast/LessThanTest.java +++ b/src/test/java/targetast/LessThanTest.java @@ -15,7 +15,7 @@ public class LessThanTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("LessThan.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "LessThan.jav"); classToTest = classFiles.get("LessThan"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/OLTest.java b/src/test/java/targetast/OLTest.java index 8afcc669..44f72411 100644 --- a/src/test/java/targetast/OLTest.java +++ b/src/test/java/targetast/OLTest.java @@ -16,7 +16,7 @@ public class OLTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("OL.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "OL.jav"); classToTest = classFiles.get("OL"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); classToTest1 = classFiles.get("OLMain"); diff --git a/src/test/java/targetast/PostIncTest.java b/src/test/java/targetast/PostIncTest.java index d0d0a02d..98fafc8e 100644 --- a/src/test/java/targetast/PostIncTest.java +++ b/src/test/java/targetast/PostIncTest.java @@ -14,7 +14,7 @@ public class PostIncTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("PostIncDec.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "PostIncDec.jav"); classToTest = classFiles.get("PostIncDec"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/PreIncTest.java b/src/test/java/targetast/PreIncTest.java index d946b0e3..8c23ea82 100644 --- a/src/test/java/targetast/PreIncTest.java +++ b/src/test/java/targetast/PreIncTest.java @@ -14,7 +14,7 @@ public class PreIncTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("PreInc.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "PreInc.jav"); classToTest = classFiles.get("PreInc"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/PutTest.java b/src/test/java/targetast/PutTest.java index 66600337..8a4b8811 100644 --- a/src/test/java/targetast/PutTest.java +++ b/src/test/java/targetast/PutTest.java @@ -16,7 +16,7 @@ public class PutTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Put.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Put.jav"); classToTest = classFiles.get("Put"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index 73d3dfdf..3d949f5d 100644 --- a/src/test/java/targetast/TestCodegen.java +++ b/src/test/java/targetast/TestCodegen.java @@ -20,6 +20,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -27,11 +29,11 @@ import java.util.stream.Collectors; public class TestCodegen { + static final Path outputPath = Path.of(System.getProperty("user.dir"), "src/test/resources/target/"); private static void writeClassFile(String name, byte[] code) throws IOException { - var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/"); - Files.createDirectories(path); - Files.write(path.resolve(name + ".class"), code); + Files.createDirectories(outputPath); + Files.write(outputPath.resolve(name + ".class"), code); } public static Class generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException { @@ -41,25 +43,29 @@ public class TestCodegen { return classLoader.loadClass(code); } - public static Map> generateClassFiles(String filename, IByteArrayClassLoader classLoader) throws IOException, ClassNotFoundException { - var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/", filename).toFile(); - var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile())); + public static Map> generateClassFiles(IByteArrayClassLoader classLoader, String... files) throws IOException, ClassNotFoundException { + var path = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/"); + var filenames = Arrays.stream(files).map(filename -> Path.of(path.toString(), filename).toFile()).toList(); + var compiler = new JavaTXCompiler(filenames, List.of(path.toFile(), outputPath.toFile())); var resultSet = compiler.typeInference(); - var sourceFile = compiler.sourceFiles.get(file); - var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader); - var classes = compiler.sourceFiles.get(file).getClasses(); + var result = new HashMap>(); + for (var file : filenames) { + var sourceFile = compiler.sourceFiles.get(file); + var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader); + var classes = compiler.sourceFiles.get(file).getClasses(); - var result = classes.stream().map(cli -> { - try { - return generateClass(converter.convert(cli), classLoader); - } catch (IOException exception) { - throw new RuntimeException(exception); + result.putAll(classes.stream().map(cli -> { + try { + return generateClass(converter.convert(cli), classLoader); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + }).collect(Collectors.toMap(Class::getName, Function.identity()))); + + for (var entry : converter.auxiliaries.entrySet()) { + writeClassFile(entry.getKey(), entry.getValue()); } - }).collect(Collectors.toMap(Class::getName, Function.identity())); - - for (var entry : converter.auxiliaries.entrySet()) { - writeClassFile(entry.getKey(), entry.getValue()); } return result; diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index e4391664..6fbccbb9 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -16,7 +16,7 @@ import static targetast.TestCodegen.generateClassFiles; public class TestComplete { @Test public void applyLambdaTest() throws Exception { - var classFiles = generateClassFiles("applyLambda.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "applyLambda.jav"); var applyLambda = classFiles.get("applyLambda"); var instance = applyLambda.getDeclaredConstructor().newInstance(); var m = applyLambda.getDeclaredMethod("m"); @@ -27,7 +27,7 @@ public class TestComplete { @Test public void binaryTest() throws Exception { - var classFiles = generateClassFiles("BinaryInMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "BinaryInMeth.jav"); var binaryInMeth = classFiles.get("BinaryInMeth"); var instance = binaryInMeth.getDeclaredConstructor().newInstance(); @@ -40,13 +40,13 @@ public class TestComplete { @Test public void classGenLamTest() throws Exception { - var classFiles = generateClassFiles("ClassGenLam.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "ClassGenLam.jav"); classFiles.get("ClassGenLam").getDeclaredConstructor().newInstance(); } @Test public void facTest() throws Exception { - var classFiles = generateClassFiles("Fac.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Fac.jav"); var fac = classFiles.get("Fac"); var instance = fac.getDeclaredConstructor().newInstance(); @@ -56,7 +56,7 @@ public class TestComplete { @Test public void facultyTest() throws Exception { - var classFiles = generateClassFiles("Faculty.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Faculty.jav"); var fac = classFiles.get("Faculty"); var constructor = fac.getDeclaredConstructor(); @@ -69,7 +69,7 @@ public class TestComplete { @Test public void fieldTest() throws Exception { - var classFiles = generateClassFiles("Field.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Field.jav"); var field = classFiles.get("Field"); var instance = field.getDeclaredConstructor().newInstance(); assertEquals(1, field.getFields().length); @@ -77,7 +77,7 @@ public class TestComplete { @Test public void fieldTph2Test() throws Exception { - var classFiles = generateClassFiles("FieldTph2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTph2.jav"); var fieldtph2 = classFiles.get("FieldTph2"); var instance = fieldtph2.getDeclaredConstructor().newInstance(); @@ -91,7 +91,7 @@ public class TestComplete { @Test public void fieldTphConsMethTest() throws Exception { - var classFiles = generateClassFiles("FieldTphConsMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTphConsMeth.jav"); var fieldTphConsMeth = classFiles.get("FieldTphConsMeth"); var ctor = fieldTphConsMeth.getDeclaredConstructor(Object.class); @@ -105,7 +105,7 @@ public class TestComplete { @Test public void fieldTphMMethTest() throws Exception { - var classFiles = generateClassFiles("FieldTphMMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTphMMeth.jav"); var fieldTphMMeth = classFiles.get("FieldTphMMeth"); var ctor = fieldTphMMeth.getDeclaredConstructor(Object.class, Object.class, Boolean.class); @@ -122,37 +122,37 @@ public class TestComplete { @Test public void genTest() throws Exception { - var classFiles = generateClassFiles("Gen.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Gen.jav"); } @Test public void idTest() throws Exception { - var classFiles = generateClassFiles("Id.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Id.jav"); var instance = classFiles.get("Id").getDeclaredConstructor().newInstance(); } @Test public void infTest() throws Exception { - var classFiles = generateClassFiles("Inf.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Inf.jav"); var instance = classFiles.get("Inf").getDeclaredConstructor().newInstance(); // TODO check generics } @Test public void kompTphTest() throws Exception { - var classFiles = generateClassFiles("KompTph.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "KompTph.jav"); var instance = classFiles.get("KompTph").getDeclaredConstructor().newInstance(); } @Test public void lambdaCaptureTest() throws Exception { - var classFiles = generateClassFiles("LambdaCapture.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "LambdaCapture.jav"); var instance = classFiles.get("LambdaCapture").getDeclaredConstructor().newInstance(); } @Test public void lambdaTest() throws Exception { - var classFiles = generateClassFiles("Lambda.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Lambda.jav"); var classToTest = classFiles.get("Lambda"); var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); @@ -167,21 +167,21 @@ public class TestComplete { @Test public void mathStrucInteger() throws Exception { - var classFiles = generateClassFiles("mathStrucInteger.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "mathStrucInteger.jav"); var mathStrucInteger = classFiles.get("mathStrucInteger"); mathStrucInteger.getDeclaredConstructor(Integer.class).newInstance(10); } @Test public void mathStruc() throws Exception { - var classFiles = generateClassFiles("mathStruc.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "mathStruc.jav"); var mathStruc = classFiles.get("mathStruc"); mathStruc.getDeclaredConstructor(Object.class).newInstance("A"); } @Test public void matrixOpTest() throws Exception { - var classFiles = generateClassFiles("MatrixOP.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "MatrixOP.jav"); var matrixOP = classFiles.get("MatrixOP"); Vector> vv = new Vector<>(); @@ -234,7 +234,7 @@ public class TestComplete { @Test public void matrixTest() throws Exception { - var classFiles = generateClassFiles("Matrix.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Matrix.jav"); var matrix = classFiles.get("Matrix"); Vector> vv = new Vector<>(); @@ -281,25 +281,46 @@ public class TestComplete { @Test public void scalarTest() throws Exception { - var classFiles = generateClassFiles("Scalar.jav", new ByteArrayClassLoader()); + + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Scalar.jav"); var scalar = classFiles.get("Scalar"); + + Vector> vv = new Vector<>(); + Vector v1 = new Vector<> (); + v1.addElement(2); + v1.addElement(2); + + var instanceOfClass_s1 = scalar.getDeclaredConstructor(Vector.class).newInstance(v1); + + Vector v2 = new Vector<> (); + v2.addElement(2); + v2.addElement(2); + + var instanceOfClass_s2 = scalar.getDeclaredConstructor(Vector.class).newInstance(v2); + + var mul = scalar.getDeclaredMethod("mul", Vector.class); + var result = mul.invoke(instanceOfClass_s1, instanceOfClass_s2); + System.out.println(instanceOfClass_s1.toString() + " * " + instanceOfClass_s2.toString() + " = " + result.toString()); + + assertEquals(result, 8); } + @Test public void mergeTest() throws Exception { - var classFiles = generateClassFiles("Merge.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Merge.jav"); var instance = classFiles.get("Merge").getDeclaredConstructor().newInstance(); } @Test public void overloadingSortingTest() throws Exception { - var classFiles = generateClassFiles("Sorting.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Sorting.jav"); var instance = classFiles.get("Sorting").getDeclaredConstructor().newInstance(); } @Test public void overloadingTest() throws Exception { - var classFiles = generateClassFiles("Overloading.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Overloading.jav"); var overloading = classFiles.get("Overloading"); var overloading2 = classFiles.get("Overloading2"); var instance1 = overloading.getDeclaredConstructor().newInstance(); @@ -313,7 +334,7 @@ public class TestComplete { @Test public void plusTest() throws Exception { - var classFiles = generateClassFiles("Plus.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Plus.jav"); var plus = classFiles.get("Plus"); var instance = plus.getDeclaredConstructor().newInstance(); @@ -326,7 +347,7 @@ public class TestComplete { @Test public void relOpsTest() throws Exception { - var classFiles = generateClassFiles("RelOps.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "RelOps.jav"); var relOps = classFiles.get("RelOps"); var instance = relOps.getDeclaredConstructor().newInstance(); @@ -336,26 +357,26 @@ public class TestComplete { @Test public void simpleCyclesTest() throws Exception { - var classFiles = generateClassFiles("SimpleCycle.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SimpleCycle.jav"); var instance = classFiles.get("SimpleCycle").getDeclaredConstructor().newInstance(); } @Test public void subMatTest() throws Exception { - var classFiles = generateClassFiles("SubMatrix.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SubMatrix.jav"); var instance = classFiles.get("SubMatrix").getDeclaredConstructor().newInstance(); } @Test public void tphTest() throws Exception { - var classFiles = generateClassFiles("Tph.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph.jav"); var tph = classFiles.get("Tph"); var instance = tph.getDeclaredConstructor().newInstance(); } @Test public void tph2Test() throws Exception { - var classFiles = generateClassFiles("Tph2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph2.jav"); var tph2 = classFiles.get("Tph2"); var instance = tph2.getDeclaredConstructor().newInstance(); @@ -382,7 +403,7 @@ public class TestComplete { @Test public void tph3Test() throws Exception { - var classFiles = generateClassFiles("Tph3.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph3.jav"); var tph3 = classFiles.get("Tph3"); var instance = tph3.getDeclaredConstructor().newInstance(); var m1 = tph3.getDeclaredMethod("m1", Object.class, Object.class); @@ -408,7 +429,7 @@ public class TestComplete { @Test public void tph4Test() throws Exception { - var classFiles = generateClassFiles("Tph4.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph4.jav"); var tph4 = classFiles.get("Tph4"); var instance = tph4.getDeclaredConstructor().newInstance(); var m = tph4.getDeclaredMethod("m", Object.class, Object.class); @@ -439,7 +460,7 @@ public class TestComplete { @Test public void tph5Test() throws Exception { - var classFiles = generateClassFiles("Tph5.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph5.jav"); var tph5 = classFiles.get("Tph5"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); @@ -467,7 +488,7 @@ public class TestComplete { @Test public void tph6Test() throws Exception { - var classFiles = generateClassFiles("Tph6.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph6.jav"); var tph5 = classFiles.get("Tph6"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); @@ -496,7 +517,7 @@ public class TestComplete { @Test public void Tph7Test() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Tph7.jav"); var classToTest = classFiles.get("Tph7"); var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); @@ -558,56 +579,56 @@ public class TestComplete { @Test public void typedIdTest() throws Exception { - var classFiles = generateClassFiles("TypedID.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "TypedID.jav"); var instance = classFiles.get("TypedID").getDeclaredConstructor().newInstance(); } @Test public void vectorAddTest() throws Exception { - var classFiles = generateClassFiles("VectorAdd.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "VectorAdd.jav"); var instance = classFiles.get("VectorAdd").getDeclaredConstructor().newInstance(); } @Test public void vectorSuperTest() throws Exception { - var classFiles = generateClassFiles("VectorSuper.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "VectorSuper.jav"); var instance = classFiles.get("VectorSuper").getDeclaredConstructor().newInstance(); } @Test public void yTest() throws Exception { - var classFiles = generateClassFiles("Y.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Y.jav"); var instance = classFiles.get("Y").getDeclaredConstructor().newInstance(); } @Test @Ignore("This one isn't working") public void boxTest() throws Exception { - var classFiles = generateClassFiles("Box.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Box.jav"); var instance = classFiles.get("Box_Main").getDeclaredConstructor().newInstance(); } @Test public void cycleTest() throws Exception { - var classFiles = generateClassFiles("Cycle.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Cycle.jav"); var instance = classFiles.get("Cycle").getDeclaredConstructor().newInstance(); } @Test public void olFunTest() throws Exception { - var classFiles = generateClassFiles("OLFun.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OLFun.jav"); var instance = classFiles.get("OLFun").getDeclaredConstructor().newInstance(); } @Test public void olFun2Test() throws Exception { - var classFiles = generateClassFiles("OLFun2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OLFun2.jav"); var instance = classFiles.get("OLFun2").getDeclaredConstructor().newInstance(); } @Test public void pairTest() throws Exception { - var classFiles = generateClassFiles("Pair.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Pair.jav"); var instance = classFiles.get("Pair").getDeclaredConstructor().newInstance(); } } diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 1131e076..f6308861 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -51,6 +51,7 @@ public class TestGenerics { var anyMethod = result.findMethod("anyMethod"); var otherMethod = result.findMethod("otherMethod"); var a = result.findField("a"); + var b = result.findField("b"); var generics = result.genericsResults.get(0); assertEquals(1, generics.get(anyMethod).size()); @@ -60,7 +61,7 @@ public class TestGenerics { var ECK2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, anyMethod); var ECKChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); assertEquals(ECK1, ECK2); - assertEquals(ECK2, ECKChain); + assertEquals(ECK2, generics.getBounds(b.getType(), result.clazz)); var M = generics.getBounds(a.getType(), result.clazz); var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT)); @@ -126,26 +127,22 @@ public class TestGenerics { assertEquals(R, RChain); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("AB")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(O, OChain); var AB = generics.getBounds(id.getReturnType(), result.clazz, id); + assertEquals(O, AB); assertEquals(AB, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); - var SChain = new BoundsList(new Bound(true, TypePlaceholder.of("R")), new Bound(false, ASTToTargetAST.OBJECT)); - assertEquals(S, SChain); + assertEquals(S, RChain); assertEquals(generics.getBounds(setA.getReturnType(), result.clazz, setA), RChain); var X = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); var XChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); - var YChain = new BoundsList(new Bound(true, TypePlaceholder.of("Y")), new Bound(true, ASTToTargetAST.OBJECT)); assertEquals(X, XChain); - assertEquals(Y, YChain); + assertEquals(Y, X); } @Test - @Ignore("Not implemented") public void testLocalVarLambda() throws Exception { var result = computeGenerics("TestLocalVarLambda.jav"); // TODO Generics of lambdas @@ -161,33 +158,30 @@ public class TestGenerics { var generics = result.genericsResults.get(0); assertEquals(1, generics.get(result.clazz).size()); - assertEquals(3, generics.get(id).size()); + assertEquals(2, generics.get(id).size()); assertEquals(2, generics.get(m).size()); assertEquals(3, generics.get(main).size()); - var M = generics.getBounds(a.getType(), result.clazz); - assertEquals(M, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); + var N = generics.getBounds(a.getType(), result.clazz); + assertEquals(N, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); - var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("P")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(O, OChain); - var AK = generics.getBounds(id.getReturnType(), result.clazz, id); - var AKChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(AK, AKChain); + var P = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); + var O = generics.getBounds(id.getReturnType(), result.clazz, id); + assertEquals(P, O); + assertEquals(O, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - // TODO Shouldn't AK and AK2 be the same and on the class? - var AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - assertEquals(AK2, AKChain); - assertEquals(Y, AKChain); + assertEquals(generics.resolve(m.getParameterList().getParameterAt(0).getType()), generics.resolve(m.getReturnType())); + var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); + var AA = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); + assertEquals(Y, AA); + assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); - var AG= generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); - var AK3 = generics.getBounds(main.getReturnType(), result.clazz, main); - var AFChain = new BoundsList(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(AF, AFChain); - assertEquals(AG, AKChain); - assertEquals(AK3, AKChain); + var AI = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); + var AJ = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); + var AH = generics.getBounds(m.getReturnType(), result.clazz, m); + assertEquals(AI, AJ); + assertEquals(AJ, AH); + assertEquals(AH, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); } @Test @@ -220,9 +214,10 @@ public class TestGenerics { var Q = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); assertEquals(Q, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + System.out.println(otherMethod.getReturnType()); var DYX2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod); assertEquals(DYX, DYX2); - assertEquals(DYX2, DYXChain); + assertEquals(DYX, generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod)); } @Test @@ -246,7 +241,7 @@ public class TestGenerics { var AA = generics.getBounds(m.getReturnType(), result.clazz, m); var AC = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(AC, new BoundsList(new Bound(true, TypePlaceholder.of("AD")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AC, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var AH = generics.getBounds(m2.getReturnType(), result.clazz, m2); var AL = generics.getBounds(m2.getParameterList().getParameterAt(0).getType(), result.clazz, m2); @@ -297,9 +292,9 @@ public class TestGenerics { } @Test - @Ignore("main changes in between runs") public void testTwoArgs2() throws Exception { var result = computeGenerics("TestTwoArgs2.jav"); + // TODO Test generics } @Test @@ -326,6 +321,7 @@ public class TestGenerics { public void testVector() throws Exception { var result = computeGenerics("TestVector.jav"); var m = result.findMethod("m"); + var id = result.findMethod("id"); var generics = result.genericsResults.get(0); var par1 = generics.resolve(m.getParameterList().getParameterAt(0).getType()); @@ -333,8 +329,12 @@ public class TestGenerics { var S = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, m); var ACM = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, m); - assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, TypePlaceholder.of("ACM")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, ASTToTargetAST.OBJECT))); assertEquals(ACM, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + + var Y = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); + assertEquals(Y, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(Y, generics.getBounds(id.getReturnType(), result.clazz, id)); } @Test @@ -361,4 +361,10 @@ public class TestGenerics { public void testVoidMeth() throws Exception { var result = computeGenerics("TestVoidMeth.jav"); } + + @Test + public void testAssign() throws Exception { + // TODO Check generics + var result = computeGenerics("TestAssign.jav"); + } } diff --git a/src/test/java/targetast/TphTest.java b/src/test/java/targetast/TphTest.java index b902b155..3fd6fa2c 100644 --- a/src/test/java/targetast/TphTest.java +++ b/src/test/java/targetast/TphTest.java @@ -15,7 +15,7 @@ public class TphTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Tph.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Tph.jav"); classToTest = classFiles.get("Tph"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/WhileTest.java b/src/test/java/targetast/WhileTest.java index 7b496f15..50371d19 100644 --- a/src/test/java/targetast/WhileTest.java +++ b/src/test/java/targetast/WhileTest.java @@ -14,7 +14,7 @@ public class WhileTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("While.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "While.jav"); classToTest = classFiles.get("While"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); }