diff options
4 files changed, 37 insertions, 4 deletions
diff --git a/src/core/lombok/javac/handlers/HandleSneakyThrows.java b/src/core/lombok/javac/handlers/HandleSneakyThrows.java index 866e4f6e..a5bd74e7 100644 --- a/src/core/lombok/javac/handlers/HandleSneakyThrows.java +++ b/src/core/lombok/javac/handlers/HandleSneakyThrows.java @@ -36,6 +36,8 @@ import org.mangosdk.spi.ProviderFor; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; +import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; @@ -83,17 +85,28 @@ public class HandleSneakyThrows extends JavacAnnotationHandler<SneakyThrows> { } if (method.body == null) return; + if (method.body.stats.isEmpty()) return; - List<JCStatement> contents = method.body.stats; + final JCStatement constructorCall = method.body.stats.get(0); + final boolean isConstructorCall = isConstructorCall(constructorCall); + List<JCStatement> contents = isConstructorCall ? method.body.stats.tail : method.body.stats; for (String exception : exceptions) { contents = List.of(buildTryCatchBlock(methodNode, contents, exception, annotation.get())); } - method.body.stats = contents; + method.body.stats = isConstructorCall ? List.of(constructorCall).appendList(contents) : contents; methodNode.rebuild(); } - + + private boolean isConstructorCall(final JCStatement supect) { + if (!(supect instanceof JCExpressionStatement)) return false; + final JCExpression supectExpression = ((JCExpressionStatement) supect).expr; + if (!(supectExpression instanceof JCMethodInvocation)) return false; + final String methodName = ((JCMethodInvocation) supectExpression).meth.toString(); + return "super".equals(methodName) || "this".equals(methodName); + } + private JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) { TreeMaker maker = node.getTreeMaker(); diff --git a/test/transform/resource/after-delombok/SneakyThrowsPlain.java b/test/transform/resource/after-delombok/SneakyThrowsPlain.java index 28c39b40..93f734f8 100644 --- a/test/transform/resource/after-delombok/SneakyThrowsPlain.java +++ b/test/transform/resource/after-delombok/SneakyThrowsPlain.java @@ -1,4 +1,12 @@ class SneakyThrowsPlain { + SneakyThrowsPlain() { + super(); + try { + System.out.println("constructor"); + } catch (final java.lang.Throwable $ex) { + throw lombok.Lombok.sneakyThrow($ex); + } + } public void test() { try { System.out.println("test1"); diff --git a/test/transform/resource/after-ecj/SneakyThrowsPlain.java b/test/transform/resource/after-ecj/SneakyThrowsPlain.java index 6e75a75c..56016b5a 100644 --- a/test/transform/resource/after-ecj/SneakyThrowsPlain.java +++ b/test/transform/resource/after-ecj/SneakyThrowsPlain.java @@ -1,7 +1,14 @@ import lombok.SneakyThrows; class SneakyThrowsPlain { - SneakyThrowsPlain() { + @lombok.SneakyThrows SneakyThrowsPlain() { super(); + try + { + System.out.println("constructor"); + } + catch (final java.lang.Throwable $ex) { + throw lombok.Lombok.sneakyThrow($ex); + } } public @lombok.SneakyThrows void test() { try diff --git a/test/transform/resource/before/SneakyThrowsPlain.java b/test/transform/resource/before/SneakyThrowsPlain.java index 97fecf8f..261e1b53 100644 --- a/test/transform/resource/before/SneakyThrowsPlain.java +++ b/test/transform/resource/before/SneakyThrowsPlain.java @@ -1,5 +1,10 @@ import lombok.SneakyThrows; class SneakyThrowsPlain { + @lombok.SneakyThrows SneakyThrowsPlain() { + super(); + System.out.println("constructor"); + } + @lombok.SneakyThrows public void test() { System.out.println("test1"); } |