From 3aace094f336393330ed275e1fb6d6c1f9187e14 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 13 Nov 2020 04:37:27 +0100 Subject: [refactor] reflection code streamlined by sending it through the Permit class --- src/delombok/lombok/delombok/Delombok.java | 17 ++++------------- src/delombok/lombok/delombok/DelombokApp.java | 6 +++--- src/delombok/lombok/delombok/PrettyPrinter.java | 8 +------- src/delombok/lombok/delombok/ant/DelombokTask.java | 14 ++++++++------ 4 files changed, 16 insertions(+), 29 deletions(-) (limited to 'src/delombok/lombok') diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 6c15068a..6b745015 100755 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -34,7 +34,6 @@ import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; import java.net.URLDecoder; @@ -841,12 +840,8 @@ public class Delombok { } } } - try { - return attributeMethod.invoke(compiler, arg); - } catch (Exception e) { - if (e instanceof InvocationTargetException) throw Lombok.sneakyThrow(e.getCause()); - throw Lombok.sneakyThrow(e); - } + + return Permit.invokeSneaky(attributeMethod, compiler, arg); } private static Method flowMethod; @@ -863,12 +858,8 @@ public class Delombok { } } } - try { - flowMethod.invoke(compiler, arg); - } catch (Exception e) { - if (e instanceof InvocationTargetException) throw Lombok.sneakyThrow(e.getCause()); - throw Lombok.sneakyThrow(e); - } + + Permit.invokeSneaky(flowMethod, compiler, arg); } private static String canonical(File dir) { diff --git a/src/delombok/lombok/delombok/DelombokApp.java b/src/delombok/lombok/delombok/DelombokApp.java index da1975b4..8467bf77 100644 --- a/src/delombok/lombok/delombok/DelombokApp.java +++ b/src/delombok/lombok/delombok/DelombokApp.java @@ -53,11 +53,11 @@ public class DelombokApp extends LombokApp { return 1; } try { - Permit.getMethod(loadDelombok(args), "main", String[].class).invoke(null, new Object[] {args.toArray(new String[0])}); + Permit.invoke(Permit.getMethod(loadDelombok(args), "main", String[].class), null, new Object[] {args.toArray(new String[0])}); } catch (InvocationTargetException e1) { Throwable t = e1.getCause(); - if (t instanceof Error) throw (Error)t; - if (t instanceof Exception) throw (Exception)t; + if (t instanceof Error) throw (Error) t; + if (t instanceof Exception) throw (Exception) t; throw e1; } return 0; diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index 54fa4ebf..9f47e5ec 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1547,13 +1547,7 @@ public class PrettyPrinter extends JCTree.Visitor { } public static JCTree getExtendsClause(JCClassDecl decl) { - try { - return (JCTree) getExtendsClause.invoke(decl); - } catch (IllegalAccessException e) { - throw sneakyThrow(e); - } catch (InvocationTargetException e) { - throw sneakyThrow(e.getCause()); - } + return (JCTree) Permit.invokeSneaky(getExtendsClause, decl); } static RuntimeException sneakyThrow(Throwable t) { diff --git a/src/delombok/lombok/delombok/ant/DelombokTask.java b/src/delombok/lombok/delombok/ant/DelombokTask.java index defd1709..7c585836 100644 --- a/src/delombok/lombok/delombok/ant/DelombokTask.java +++ b/src/delombok/lombok/delombok/ant/DelombokTask.java @@ -177,9 +177,11 @@ class Tasks { } return Class.forName(name, true, shadowLoader); - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException) e; - throw new RuntimeException(e); + } catch (Throwable t) { + if (t instanceof InvocationTargetException) t = t.getCause(); + if (t instanceof RuntimeException) throw (RuntimeException) t; + if (t instanceof Error) throw (Error) t; + throw new RuntimeException(t); } } @@ -207,10 +209,10 @@ class Tasks { Method m = instance.getClass().getMethod("execute", Location.class); m.invoke(instance, loc); - } catch (Exception e) { - Throwable t = (e instanceof InvocationTargetException) ? e.getCause() : e; - if (t instanceof Error) throw (Error) t; + } catch (Throwable t) { + if (t instanceof InvocationTargetException) t = t.getCause(); if (t instanceof RuntimeException) throw (RuntimeException) t; + if (t instanceof Error) throw (Error) t; throw new RuntimeException(t); } } -- cgit