From 8fb4f79757547378c7d8c29759d04037bdd47c7e Mon Sep 17 00:00:00 2001 From: Philipp Eichhorn Date: Sun, 24 Jun 2012 20:38:00 +0200 Subject: [Issue 381] @SneakyThrows respects constructor calls --- .../lombok/javac/handlers/HandleSneakyThrows.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src') 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 { } if (method.body == null) return; + if (method.body.stats.isEmpty()) return; - List contents = method.body.stats; + final JCStatement constructorCall = method.body.stats.get(0); + final boolean isConstructorCall = isConstructorCall(constructorCall); + List 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 contents, String exception, JCTree source) { TreeMaker maker = node.getTreeMaker(); -- cgit