mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-07 19:00:45 +08:00
narrow down Exception scope when possible
This commit is contained in:
parent
f64b07e9da
commit
f01ff10fba
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user