From f01ff10fba5e5c4c9a68bea4f8b4d4349832d268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 22 Feb 2021 15:56:50 +0800 Subject: [PATCH] narrow down Exception scope when possible --- .../core/accessor/PrivateAccessor.java | 62 ++++++++++++++----- .../EnablePrivateAccessTranslator.java | 5 +- .../processor/util/TestableLogger.java | 12 +++- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/testable-core/src/main/java/com/alibaba/testable/core/accessor/PrivateAccessor.java b/testable-core/src/main/java/com/alibaba/testable/core/accessor/PrivateAccessor.java index cdc6339..9016cac 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/accessor/PrivateAccessor.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/accessor/PrivateAccessor.java @@ -5,6 +5,7 @@ import com.alibaba.testable.core.util.TypeUtil; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** @@ -24,8 +25,10 @@ public class PrivateAccessor { Field declaredField = ref.getClass().getDeclaredField(field); declaredField.setAccessible(true); return (T)declaredField.get(ref); - } catch (Exception e) { - throw new MemberAccessException("Failed to get private field \"" + field + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private field \"" + field + "\"", e); + } catch (NoSuchFieldException e) { + throw new MemberAccessException("Private field \"" + field + "\" not exist", e); } } @@ -40,8 +43,10 @@ public class PrivateAccessor { Field declaredField = ref.getClass().getDeclaredField(field); declaredField.setAccessible(true); declaredField.set(ref, value); - } catch (Exception e) { - throw new MemberAccessException("Failed to set private field \"" + field + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private field \"" + field + "\"", e); + } catch (NoSuchFieldException e) { + throw new MemberAccessException("Private field \"" + field + "\" not exist", e); } } @@ -60,10 +65,15 @@ public class PrivateAccessor { declaredMethod.setAccessible(true); return (T)declaredMethod.invoke(ref, args); } - } catch (Exception e) { - throw new MemberAccessException("Failed to invoke private method \"" + method + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private method \"" + method + "\"", e); + } catch (InvocationTargetException e) { + if (e.getTargetException() instanceof RuntimeException) { + throw (RuntimeException)e.getTargetException(); + } + throw new MemberAccessException("Invoke private method \"" + method + "\" failed with exception", e); } - throw new MemberAccessException("Private method \"" + method + "\" not found"); + throw new MemberAccessException("Private method \"" + method + "\" not exist"); } /** @@ -76,8 +86,10 @@ public class PrivateAccessor { Field declaredField = clazz.getDeclaredField(field); declaredField.setAccessible(true); return (T)declaredField.get(null); - } catch (Exception e) { - throw new MemberAccessException("Failed to get private static field \"" + field + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private static field \"" + field + "\"", e); + } catch (NoSuchFieldException e) { + throw new MemberAccessException("Private static field \"" + field + "\" not exist", e); } } @@ -92,8 +104,10 @@ public class PrivateAccessor { Field declaredField = clazz.getDeclaredField(field); declaredField.setAccessible(true); declaredField.set(null, value); - } catch (Exception e) { - throw new MemberAccessException("Failed to set private static field \"" + field + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private static field \"" + field + "\"", e); + } catch (NoSuchFieldException e) { + throw new MemberAccessException("Private static field \"" + field + "\" not exist", e); } } @@ -120,10 +134,17 @@ public class PrivateAccessor { declaredMethod.setAccessible(true); return (T)declaredMethod.invoke(companionInstance, args); } - } catch (Exception e) { - throw new MemberAccessException("Failed to invoke private static method \"" + method + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private static method \"" + method + "\"", e); + } catch (NoSuchFieldException e) { + throw new MemberAccessException("Private static method \"" + method + "\" not exist"); + } catch (InvocationTargetException e) { + if (e.getTargetException() instanceof RuntimeException) { + throw (RuntimeException)e.getTargetException(); + } + throw new MemberAccessException("Invoke private static method \"" + method + "\" failed with exception", e); } - throw new MemberAccessException("Private static method \"" + method + "\" not found"); + throw new MemberAccessException("Neither Private static method nor companion method \"" + method + "\" exist"); } /** @@ -139,9 +160,16 @@ public class PrivateAccessor { constructor.setAccessible(true); return (T)constructor.newInstance(args); } - } catch (Exception e) { - throw new MemberAccessException("Failed to invoke private constructor of \"" + clazz.getSimpleName() + "\"", e); + } catch (IllegalAccessException e) { + throw new MemberAccessException("Failed to access private constructor of \"" + clazz.getSimpleName() + "\"", e); + } catch (InvocationTargetException e) { + if (e.getTargetException() instanceof RuntimeException) { + throw (RuntimeException)e.getTargetException(); + } + throw new MemberAccessException("Invoke private constructor of \"" + clazz.getSimpleName() + "\" failed with exception", e); + } catch (InstantiationException e) { + throw new MemberAccessException("Failed to instantiate object of \"" + clazz.getSimpleName() + "\"", e); } - throw new MemberAccessException("Private static constructor of \"" + clazz.getSimpleName() + "\" not found"); + throw new MemberAccessException("Private constructor of \"" + clazz.getSimpleName() + "\" not exist"); } } diff --git a/testable-processor/src/main/java/com/alibaba/testable/processor/translator/EnablePrivateAccessTranslator.java b/testable-processor/src/main/java/com/alibaba/testable/processor/translator/EnablePrivateAccessTranslator.java index ec367c6..b5e45b9 100644 --- a/testable-processor/src/main/java/com/alibaba/testable/processor/translator/EnablePrivateAccessTranslator.java +++ b/testable-processor/src/main/java/com/alibaba/testable/processor/translator/EnablePrivateAccessTranslator.java @@ -66,12 +66,13 @@ public class EnablePrivateAccessTranslator extends BaseTranslator { try { Class cls = getSourceClass(clazz, sourceClassFullName); if (cls == null) { - cx.logger.error("Failed to load source class: " + sourceClassFullName); + cx.logger.fatal("Failed to load source class \"" + sourceClassFullName + "\""); } else { findAllPrivateMembers(cls); } } catch (Exception e) { - e.printStackTrace(); + // for any reason, interrupt the compile process + cx.logger.fatal("Failed to load source class \"" + sourceClassFullName + "\": " + e); } this.privateAccessChecker = (p.verifyTargetExistence == null || p.verifyTargetExistence) ? new PrivateAccessChecker(cx, sourceClassShortName, memberRecord) : null; diff --git a/testable-processor/src/main/java/com/alibaba/testable/processor/util/TestableLogger.java b/testable-processor/src/main/java/com/alibaba/testable/processor/util/TestableLogger.java index 25f6b66..bead436 100644 --- a/testable-processor/src/main/java/com/alibaba/testable/processor/util/TestableLogger.java +++ b/testable-processor/src/main/java/com/alibaba/testable/processor/util/TestableLogger.java @@ -16,17 +16,27 @@ public class TestableLogger { this.messager = messager; } + /** + * Print hint message + */ public void info(String msg) { // Message level lower than warning is not shown by default, use stdout instead System.out.println("[INFO] " + msg); } + /** + * Print warning message + */ public void warn(String msg) { // Message level WARNING won't show, use MANDATORY_WARNING instead messager.printMessage(Diagnostic.Kind.MANDATORY_WARNING, msg); } - public void error(String msg) { + /** + * Print fatal message + * Note: this will stop current compile process + */ + public void fatal(String msg) { messager.printMessage(Diagnostic.Kind.ERROR, msg); } }