Compare commits

..
1 Commits
Author SHA1 Message Date
dholle d6c59f07f6 Add check for duplicate class definition in source files, fixes #422
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 1m52s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 2m24s
2026-11-10 15:04:47 +01:00
7 changed files with 19 additions and 40 deletions

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
public class Bug422 {}
public class Bug422 {}
-5
View File
@@ -1,5 +0,0 @@
import java.lang.Integer;
interface Bug423I { Integer f(); }
public class Bug423 implements Bug423I { }
@@ -15,7 +15,6 @@ interface C {
}
class ClassX implements A {
void method1() {}
}
record ClassY(Integer myInt) implements C {}
@@ -1846,35 +1846,6 @@ public class Codegen {
clazz.constructors().forEach(this::generateConstructor);
clazz.methods().forEach(this::generateMethod);
// Check if non abstract class implements all interface methods
if ((access & ACC_ABSTRACT) == 0) {
for (var intf : clazz.implementingInterfaces()) {
var intfClass = compiler.getClass(new JavaClassName(intf.name()));
for (var method : intfClass.getMethods()) {
if ((method.modifier & ACC_ABSTRACT) != 0) {
// Check if the method is implemented in the current class
// TODO This is duplicate code, we should probably refactor instances of this pattern
var foundMethods = clazz.methods().stream().filter(m -> {
if (!m.name().equals(method.name)) return false;
if (m.signature().parameters().size() != method.getParameterList().getFormalparalist().size())
return false;
for (var i = 0; i < m.signature().parameters().size(); i++) {
var param1 = m.signature().parameters().get(i).pattern().type();
var param2 = converter.convert(
method.getParameterList().getParameterAt(i).getType(),
ASTToTargetAST.Generics.nullGenerics(compiler).javaGenerics());
if (!param1.equals(param2)) return false;
}
return true;
}).toList();
if (foundMethods.size() != 1) {
throw new CodeGenException("Class " + clazz.qualifiedName() + " does not implement method " + method.name + " from interface " + intfClass.getClassName());
}
}
}
}
}
if (clazz.staticConstructor() != null)
generateStaticConstructor(clazz.staticConstructor());
@@ -5,6 +5,7 @@ import de.dhbwstuttgart.bytecode.Codegen;
import de.dhbwstuttgart.bytecode.FunNGenerator;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.exceptions.CompilerException;
import de.dhbwstuttgart.exceptions.CompilerWarning;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.WarningsException;
@@ -171,6 +172,8 @@ public class JavaTXCompiler {
var numberOfGenerics = decl == null ? 0 : decl.genericTypeVar().size();
var className = pkgName + (!pkgName.isEmpty() ? "." : "") + name;
if (classRegistry.contains(className))
throw new CompilerException("Duplicate class definition: " + className);
classRegistry.addName(className, numberOfGenerics);
input.add(classRegistry.getName(className));
}
@@ -0,0 +1,7 @@
package de.dhbwstuttgart.exceptions;
public class CompilerException extends RuntimeException {
public CompilerException(String message) {
super(message);
}
}
+7 -5
View File
@@ -1,6 +1,8 @@
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.core.ConsoleInterface;
import de.dhbwstuttgart.exceptions.CompilerException;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.WarningsException;
import de.dhbwstuttgart.util.Logger;
import de.dhbwstuttgart.util.Logger.LogLevel;
@@ -1706,12 +1708,12 @@ public class TestComplete {
}
@Test
public void testBug423() throws Exception {
public void testBug422() throws Exception {
try {
var classFiles = generateClassFiles(createClassLoader(), "Bug423.jav");
fail("No exception thrown!");
} catch (CodeGenException e) {
assertEquals("Class Bug423 does not implement method f from interface Bug423I", e.getMessage());
var classFiles = generateClassFiles(createClassLoader(), "Bug422.jav");
fail("Shouldn't compile!");
} catch (CompilerException e) {
assertEquals("Duplicate class definition: Bug422", e.getMessage());
}
}
}