for InvocationTargetException, bring root cause out

This commit is contained in:
金戟 2021-01-21 22:44:51 +08:00
parent 019e407a4f
commit f9be92e9d0
3 changed files with 52 additions and 37 deletions

View File

@ -1,6 +1,6 @@
package com.alibaba.testable.core.accessor;
import com.alibaba.testable.core.exception.MemberNotExistException;
import com.alibaba.testable.core.exception.MemberAccessException;
import com.alibaba.testable.core.util.TypeUtil;
import java.lang.reflect.Field;
@ -17,8 +17,8 @@ public class PrivateAccessor {
public static <T> T get(Object ref, String field) {
try {
return PrivateAccessor.get(ref, field);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
return null;
}
}
@ -26,16 +26,16 @@ public class PrivateAccessor {
public static <T> void set(Object ref, String field, T value) {
try {
PrivateAccessor.set(ref, field, value);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
}
}
public static <T> T invoke(Object ref, String method, Object... args) {
try {
return PrivateAccessor.invoke(ref, method, args);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
return null;
}
}
@ -43,8 +43,8 @@ public class PrivateAccessor {
public static <T> T getStatic(Class<?> clazz, String field) {
try {
return PrivateAccessor.getStatic(clazz, field);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
return null;
}
}
@ -52,19 +52,24 @@ public class PrivateAccessor {
public static <T> void setStatic(Class<?> clazz, String field, T value) {
try {
PrivateAccessor.setStatic(clazz, field, value);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
}
}
public static <T> T invokeStatic(Class<?> clazz, String method, Object... args) {
try {
return PrivateAccessor.invokeStatic(clazz, method, args);
} catch (MemberNotExistException e) {
System.err.println(e.toString());
} catch (MemberAccessException e) {
printError(e);
return null;
}
}
private static void printError(MemberAccessException e) {
Throwable cause = e.getCause() == null ? e : e.getCause();
System.err.println(cause.toString());
}
}
public static <T> T get(Object ref, String field) {
@ -73,7 +78,7 @@ public class PrivateAccessor {
declaredField.setAccessible(true);
return (T)declaredField.get(ref);
} catch (Exception e) {
throw new MemberNotExistException("Failed to get private field \"" + field + "\"", e);
throw new MemberAccessException("Failed to get private field \"" + field + "\"", e);
}
}
@ -83,7 +88,7 @@ public class PrivateAccessor {
declaredField.setAccessible(true);
declaredField.set(ref, value);
} catch (Exception e) {
throw new MemberNotExistException("Failed to set private field \"" + field + "\"", e);
throw new MemberAccessException("Failed to set private field \"" + field + "\"", e);
}
}
@ -97,9 +102,9 @@ public class PrivateAccessor {
return (T)declaredMethod.invoke(ref, args);
}
} catch (Exception e) {
throw new MemberNotExistException("Failed to invoke private method \"" + method + "\"", e);
throw new MemberAccessException("Failed to invoke private method \"" + method + "\"", e);
}
throw new MemberNotExistException("Private method \"" + method + "\" not found");
throw new MemberAccessException("Private method \"" + method + "\" not found");
}
public static <T> T getStatic(Class<?> clazz, String field) {
@ -108,7 +113,7 @@ public class PrivateAccessor {
declaredField.setAccessible(true);
return (T)declaredField.get(null);
} catch (Exception e) {
throw new MemberNotExistException("Failed to get private static field \"" + field + "\"", e);
throw new MemberAccessException("Failed to get private static field \"" + field + "\"", e);
}
}
@ -118,7 +123,7 @@ public class PrivateAccessor {
declaredField.setAccessible(true);
declaredField.set(null, value);
} catch (Exception e) {
throw new MemberNotExistException("Failed to set private static field \"" + field + "\"", e);
throw new MemberAccessException("Failed to set private static field \"" + field + "\"", e);
}
}
@ -140,8 +145,8 @@ public class PrivateAccessor {
return (T)declaredMethod.invoke(companionInstance, args);
}
} catch (Exception e) {
throw new MemberNotExistException("Failed to invoke private static method \"" + method + "\"", e);
throw new MemberAccessException("Failed to invoke private static method \"" + method + "\"", e);
}
throw new MemberNotExistException("Private static method \"" + method + "\" not found");
throw new MemberAccessException("Private static method \"" + method + "\" not found");
}
}

View File

@ -0,0 +1,26 @@
package com.alibaba.testable.core.exception;
import java.lang.reflect.InvocationTargetException;
/**
* @author flin
*/
public class MemberAccessException extends RuntimeException {
public MemberAccessException(String message) {
super(message);
}
public MemberAccessException(String message, Throwable cause) {
super(message, getRootCause(cause));
}
private static Throwable getRootCause(Throwable cause) {
if (cause instanceof InvocationTargetException) {
return ((InvocationTargetException)cause).getTargetException();
} else {
return cause;
}
}
}

View File

@ -1,16 +0,0 @@
package com.alibaba.testable.core.exception;
/**
* @author flin
*/
public class MemberNotExistException extends RuntimeException {
public MemberNotExistException(String message) {
super(message);
}
public MemberNotExistException(String message, Throwable cause) {
super(message, cause);
}
}