From bb11d24101fb238df4b170d4076daeb40ed54e86 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 11 Nov 2024 15:47:38 +0100 Subject: [PATCH] First implementation of generating a bridge method --- .../target/generate/ASTToTargetAST.java | 33 +++++++++++++++++++ .../target/tree/TargetMethod.java | 4 +++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 6bbe7969..8b515c93 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -332,6 +332,39 @@ public class ASTToTargetAST { } // Generate dispatch method + var firstParam = firstMethod.signature().parameters().get(0); + var cases = new ArrayList(); + TargetExpression expr = new TargetLocalVar(firstParam.pattern().type(), firstParam.pattern().name()); + + var classType = new TargetRefType(clazz.getClassName().getClassName()); + + for (var method : res) { + TargetExpression caseBody = new TargetMethodCall( + method.signature().returnType(), + new TargetThis(classType), + List.of(expr), + classType, + method.name(), + false, false, method.isPrivate() + ); + + if (method.signature().returnType() != null) { + caseBody = new TargetReturn(caseBody); + } + var body = new TargetBlock(List.of(caseBody)); + var case_ = new TargetSwitch.Case(List.of(method.signature().parameters().getFirst().pattern()), body); + + cases.add(case_); + } + + var stmt = new TargetSwitch(expr, cases, null); + var block = new TargetBlock(List.of(stmt)); + + var parameters = List.of(new MethodParameter(firstParam.pattern().type(), firstParam.pattern().name())); + var signature = new TargetMethod.Signature(firstMethod.signature().generics(), parameters, firstMethod.signature().returnType()); + var bridgeMethod = new TargetMethod(firstMethod.access(), firstMethod.name(), block, signature, firstMethod.txSignature()); + + res.add(bridgeMethod); return res; } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 2f3a689b..abc00083 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -79,6 +79,10 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature return (access & Opcodes.ACC_STATIC) != 0; } + public boolean isPrivate() { + return (access & Opcodes.ACC_PRIVATE) != 0; + } + @Override public boolean equals(Object other) { if (!(other instanceof TargetMethod otherMethod)) return false;