Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb1c5c6629 |
No files matched your search
@@ -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 {}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
public class LazyMethodCallParser {
|
||||
|
||||
public m(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
public testEager() {
|
||||
return m(1);
|
||||
}
|
||||
|
||||
public testLazy() {
|
||||
return m.(1);
|
||||
}
|
||||
}
|
||||
@@ -598,10 +598,7 @@ expressionList
|
||||
;
|
||||
|
||||
methodCall
|
||||
// eager: m(...)
|
||||
: identifier '(' expressionList? ')'
|
||||
// lazy: m.(...)
|
||||
| identifier '.' '(' expressionList? ')'
|
||||
| THIS '(' expressionList? ')'
|
||||
| SUPER '(' expressionList? ')'
|
||||
;
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -734,9 +734,6 @@ public class StatementGenerator {
|
||||
ret = new SuperCall(argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
} else if(expr.THIS() != null) {
|
||||
ret = new ThisCall(argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
} else if (expr.DOT() != null) {
|
||||
// Lazy-Aufruf: m.(...)
|
||||
ret = new LazyMethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
} else {
|
||||
ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
}
|
||||
@@ -756,13 +753,7 @@ public class StatementGenerator {
|
||||
ArgumentList argumentList = convertArguments(expr.expressionList());
|
||||
ArrayList<TypePlaceholder> signature = argumentList.getArguments().stream().map(x -> TypePlaceholder.fresh(offset)).collect(Collectors.toCollection(ArrayList::new));
|
||||
signature.add(TypePlaceholder.fresh(offset)); // return type
|
||||
MethodCall ret;
|
||||
if (expr.DOT() != null) {
|
||||
// Lazy-Aufruf: receiver.m.(...)
|
||||
ret = new LazyMethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
} else {
|
||||
ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
}
|
||||
MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset);
|
||||
ret.setStatement();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package de.dhbwstuttgart.syntaxtree.statement;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
// Lazy-Methodenaufruf: m.(...)
|
||||
public class LazyMethodCall extends MethodCall {
|
||||
|
||||
public LazyMethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName,
|
||||
ArgumentList argumentList, RefTypeOrTPHOrWildcardOrGeneric receiverType,
|
||||
ArrayList<TypePlaceholder> signature, Token offset) {
|
||||
super(retType, receiver, methodName, argumentList, receiverType, signature, offset);
|
||||
}
|
||||
}
|
||||
@@ -1704,4 +1704,14 @@ public class TestComplete {
|
||||
var clazz = classFiles.get("Bug406");
|
||||
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug423() 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package parser;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LazyMethodCall;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
// Prüft: m(...) -> MethodCall, m.(...) -> LazyMethodCall
|
||||
public class LazyMethodCallParserTest {
|
||||
|
||||
@Test
|
||||
public void eagerAndLazyMethodCallAreDistinguished() throws Exception {
|
||||
File src = new File("resources/bytecode/javFiles/LazyMethodCallParser.jav");
|
||||
assertTrue(src.exists(), "Testdatei fehlt: " + src.getAbsolutePath());
|
||||
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(src);
|
||||
compiler.parseAll();
|
||||
|
||||
SourceFile sf = compiler.sourceFiles.get(src);
|
||||
assertNotNull(sf);
|
||||
|
||||
List<MethodCall> eagerCalls = new ArrayList<>();
|
||||
List<LazyMethodCall> lazyCalls = new ArrayList<>();
|
||||
|
||||
new AbstractASTWalker() {
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
if (methodCall instanceof LazyMethodCall lazy) {
|
||||
lazyCalls.add(lazy);
|
||||
} else {
|
||||
eagerCalls.add(methodCall);
|
||||
}
|
||||
super.visit(methodCall);
|
||||
}
|
||||
}.visit(sf);
|
||||
|
||||
assertFalse(eagerCalls.isEmpty(), "Erwartet mindestens einen eager-Aufruf m(...)");
|
||||
assertFalse(lazyCalls.isEmpty(), "Erwartet mindestens einen Lazy-Aufruf m.(...)");
|
||||
|
||||
assertTrue(eagerCalls.stream().anyMatch(c -> c.name.equals("m")),
|
||||
"Eager-Aufruf m(...) nicht gefunden");
|
||||
assertTrue(lazyCalls.stream().anyMatch(c -> c.name.equals("m")),
|
||||
"Lazy-Aufruf m.(...) nicht gefunden");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user