Skip to content

Commit 673e6cb

Browse files
paulirwinclaude
andauthored
Map Throwable.getMessage() to Exception.Message (#165) (#174)
Fixes #165. Java's `e.getMessage()` converted to `e.GetMessage()`, which doesn't compile against C#'s `Exception`. `TypeHelper.TryTransformMethodCall` already had a `ReplaceMethodByProperty` helper backing `length`→`Length` and `size`→`Count` — that's exactly the method-call-to-property-access handling the issue noted was needed, so `getMessage`→`Message` slots in there rather than into the name-only `ReplaceCommonMethodNames` table. The case is guarded on zero arguments, matching the surrounding cases, so an unrelated `obj.getMessage(i)` still converts to a normal method call. ## Tests - Two cases in `ConvertExpressionTests` (`e.getMessage()` → `e.Message`, and the arity-guard negative case). - A new `Resources/ExceptionGetMessage.java` fixture wired into `FullIntegrationTests`, so the generated C# is compiled and run: it throws an `IllegalArgumentException`, catches it, and asserts the printed message. Full suite passes at 363 tests. ## Note Java's `Throwable` has sibling accessors with the same problem (`getLocalizedMessage()`, `getCause()`). This PR stays scoped to `getMessage()` as the issue described; happy to follow up on the others. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 5868599 commit 673e6cb

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎JavaToCSharp.Tests/ConvertExpressionTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ConvertExpressionTests
1212
[InlineData("lst.set(i, value)", "lst[i] = value")]
1313
[InlineData("str.length()", "str.Length")]
1414
[InlineData("arr.length", "arr.Length")]
15+
[InlineData("e.getMessage()", "e.Message")]
16+
[InlineData("obj.getMessage(i)", "obj.GetMessage(i)")]
1517

1618
//Conversion not done if param number does not match the required
1719
[InlineData("obj.size(i)", "obj.Size(i)")]

‎JavaToCSharp.Tests/IntegrationTests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void GeneralUnsuccessfulConversionTest(string filePath)
8888
[InlineData("Resources/InstanceInitializers.java")]
8989
[InlineData("Resources/StaticImports.java")]
9090
[InlineData("Resources/LabeledBreakContinue.java")]
91+
[InlineData("Resources/ExceptionGetMessage.java")]
9192
public void FullIntegrationTests(string filePath, bool allowWarnings = false)
9293
=> RunFullIntegrationTest(filePath, allowWarnings);
9394

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// Expect:
2+
/// - output: "caught: boom\n"
3+
package example;
4+
5+
public class Program {
6+
public static void main(String[] args) {
7+
try {
8+
throw new IllegalArgumentException("boom");
9+
} catch (IllegalArgumentException e) {
10+
System.out.println("caught: " + e.getMessage());
11+
}
12+
}
13+
}

‎JavaToCSharp/TypeHelper.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ public static bool TryTransformMethodCall(ConversionContext context, MethodCallE
273273
return true;
274274
}
275275

276+
case "getMessage" when args.size() == 0:
277+
{
278+
var scopeSyntaxMessage = ExpressionVisitor.VisitExpression(context, scope);
279+
transformedSyntax = ReplaceMethodByProperty(scopeSyntaxMessage, "Message");
280+
return true;
281+
}
282+
276283
case "get" when args.size() == 1:
277284
{
278285
var scopeSyntaxGet = ExpressionVisitor.VisitExpression(context, scope);

0 commit comments

Comments
 (0)