diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index e62fbf80..bddeeca7 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -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 convert(ClassOrInterface clazz, List overloadedMethods) { + private Set convert(ClassOrInterface clazz, List overloadedMethods) { if (overloadedMethods.size() == 1) { - return convert(clazz, overloadedMethods.get(0)); + return convert(clazz, overloadedMethods.getFirst()); } - var res = new ArrayList(); + var methods = new ArrayList(); 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(); + 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 overloadedMethods) { @@ -372,9 +381,9 @@ public class ASTToTargetAST { }).findFirst(); } - private List convert(ClassOrInterface currentClass, Method method) { + private Set convert(ClassOrInterface currentClass, Method method) { generics = all.get(0); - List result = new ArrayList<>(); + Set result = new HashSet<>(); Set> 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; } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 5a8b9368..bdabc81e 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -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); + } }