aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorPhilipp Eichhorn <peichhor@web.de>2012-06-24 20:38:00 +0200
committerPhilipp Eichhorn <peichhor@web.de>2012-06-24 20:38:00 +0200
commit8fb4f79757547378c7d8c29759d04037bdd47c7e (patch)
treefd41fdd53f79aeca77b93463eca9fb357c642b7a /src/core
parent3ed67fc8aecf50757d85bdfb55075015c6627740 (diff)
downloadlombok-8fb4f79757547378c7d8c29759d04037bdd47c7e.tar.gz
lombok-8fb4f79757547378c7d8c29759d04037bdd47c7e.tar.bz2
lombok-8fb4f79757547378c7d8c29759d04037bdd47c7e.zip
[Issue 381] @SneakyThrows respects constructor calls
Diffstat (limited to 'src/core')
-rw-r--r--src/core/lombok/javac/handlers/HandleSneakyThrows.java19
1 files changed, 16 insertions, 3 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();