Compare commits

..
1 Commits
Author SHA1 Message Date
dholle eb1c5c6629 Add check for missing interface methods, fixes #423
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 1m22s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 2m11s
2026-11-10 14:46:41 +01:00
7 changed files with 47 additions and 26 deletions

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
public class Bug421 {
public m() { return 1 } // Missing semicolon
}
+5
View File
@@ -0,0 +1,5 @@
import java.lang.Integer;
interface Bug423I { Integer f(); }
public class Bug423 implements Bug423I { }
@@ -15,6 +15,7 @@ interface C {
}
class ClassX implements A {
void method1() {}
}
record ClassY(Integer myInt) implements C {}
@@ -1846,6 +1846,35 @@ 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());
@@ -2,8 +2,9 @@ package de.dhbwstuttgart.exceptions;
import org.antlr.v4.runtime.Token;
public class ParserError extends RuntimeException{
public ParserError(String message) {
super(message);
public class ParserError extends TypeinferenceException{
public ParserError(Token offset){
super("Fehler beim Parsen", offset);
}
}
@@ -1,7 +1,6 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.exceptions.ParserError;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java17Lexer;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
@@ -9,7 +8,9 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.util.Logger;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -20,25 +21,13 @@ public class JavaTXParser {
public static Logger logger = new Logger("Parser");
public static class ThrowingErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
throw new ParserError("Syntax error at line " + line + ":" + charPositionInLine + " - " + msg);
}
}
public static Java17Parser.SourceFileContext parse(File source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = new FileInputStream(source);
// DEPRECATED: ANTLRInputStream input = new ANTLRInputStream(stream);
var errorListener = new ThrowingErrorListener();
CharStream input = CharStreams.fromStream(stream);
Java17Lexer lexer = new Java17Lexer(input);
lexer.addErrorListener(errorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java17Parser parser = new Java17Parser(tokens);
parser.addErrorListener(errorListener);
return parser.sourceFile();
/*
* SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source)); return generator.convert(tree);
+5 -6
View File
@@ -1,7 +1,6 @@
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.core.ConsoleInterface;
import de.dhbwstuttgart.exceptions.ParserError;
import de.dhbwstuttgart.exceptions.WarningsException;
import de.dhbwstuttgart.util.Logger;
import de.dhbwstuttgart.util.Logger.LogLevel;
@@ -1707,12 +1706,12 @@ public class TestComplete {
}
@Test
public void testBug421() throws Exception {
public void testBug423() throws Exception {
try {
var classFiles = generateClassFiles(createClassLoader(), "Bug421.jav");
fail("Shouldn't compile!");
} catch (ParserError e) {
assertEquals("Syntax error at line 2:26 - no viable alternative at input 'm() { return 1 }'", e.getMessage());
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());
}
}
}