Give an exception if a method has been duplicated
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 9m6s

This commit is contained in:
Daniel Holle 2024-05-27 15:51:48 +02:00
parent 50f2a29e1e
commit 4880527d4d
2 changed files with 33 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.bytecode.FunNGenerator;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.environment.ByteArrayClassLoader;
@ -160,11 +161,11 @@ public class ASTToTargetAST {
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList();
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer)).flatMap(List::stream).toList();
var fields = input.getFieldDecl().stream().map(this::convert).toList();
var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(List::stream).toList();
var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(Set::stream).toList();
TargetMethod staticConstructor = null;
if (input.getStaticInitializer().isPresent())
staticConstructor = this.convert(input, input.getStaticInitializer().get()).get(0);
staticConstructor = this.convert(input, input.getStaticInitializer().get()).stream().findFirst().orElseThrow();
if (input instanceof Record)
return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
@ -289,11 +290,11 @@ public class ASTToTargetAST {
return res.toString();
}
private List<TargetMethod> convert(ClassOrInterface clazz, List<Method> overloadedMethods) {
private Set<TargetMethod> convert(ClassOrInterface clazz, List<Method> overloadedMethods) {
if (overloadedMethods.size() == 1) {
return convert(clazz, overloadedMethods.get(0));
return convert(clazz, overloadedMethods.getFirst());
}
var res = new ArrayList<Method>();
var methods = new ArrayList<Method>();
for (var method : overloadedMethods) {
var newMethod = new Method(
method.modifier,
@ -305,7 +306,7 @@ public class ASTToTargetAST {
method.getGenerics(),
method.getOffset()
);
res.add(newMethod);
methods.add(newMethod);
}
// TODO Record overloading
@ -328,7 +329,15 @@ public class ASTToTargetAST {
var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken());
res.add(entryPoint); // TODO*/
return res.stream().map(m -> convert(clazz, m)).flatMap(List::stream).toList();
var res = new HashSet<TargetMethod>();
for (var method : methods) {
var overloads = convert(clazz, method);
for (var overload : overloads) {
if (res.contains(overload)) throw new CodeGenException("Duplicate method found: " + overload.name() + " with signature " + overload.signature().getSignature());
res.add(overload);
}
}
return res;
}
private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) {
@ -372,9 +381,9 @@ public class ASTToTargetAST {
}).findFirst();
}
private List<TargetMethod> convert(ClassOrInterface currentClass, Method method) {
private Set<TargetMethod> convert(ClassOrInterface currentClass, Method method) {
generics = all.get(0);
List<TargetMethod> result = new ArrayList<>();
Set<TargetMethod> result = new HashSet<>();
Set<List<MethodParameter>> parameterSet = new HashSet<>();
for (var s : all) {
@ -402,10 +411,12 @@ public class ASTToTargetAST {
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics));
result.add(new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature));
var newMethod = new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature);
result.add(newMethod);
parameterSet.add(params);
}
}
return result;
}

View File

@ -6,6 +6,7 @@ import de.dhbwstuttgart.target.tree.type.TargetType;
import org.objectweb.asm.Opcodes;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) {
@ -64,5 +65,16 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature
public boolean isStatic() {
return (access & Opcodes.ACC_STATIC) != 0;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof TargetMethod otherMethod)) return false;
return otherMethod.signature.equals(this.signature) && otherMethod.name.equals(this.name);
}
@Override
public int hashCode() {
return Objects.hash(name, signature);
}
}