Created
November 7, 2017 03:17
-
-
Save yatszhash/d064b7ef063e9af6b12512acdd89e86a to your computer and use it in GitHub Desktop.
java reflection sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void setPrivateField(Object targetObj, String fieldName, Object value) | |
throws NoSuchFieldException, IllegalAccessException | |
{ | |
Class c = targetObj.getClass(); | |
Field field = c.getDeclaredField(fieldName); | |
field.setAccessible(true); | |
field.set(targetObj, value); | |
} | |
public static Object invokePrivateMethod(Object targetObj, String methodName, | |
Class[] argClasses, Object[] args) | |
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException | |
{ | |
Class c = targetObj.getClass(); | |
Method method = c.getDeclaredMethod(methodName, argClasses); | |
method.setAccessible(true); | |
return method.invoke(targetObj, args); | |
} | |
public static String trimAllSpace(String str) { | |
if (str == null || str.length() == 0) { | |
return str; | |
} | |
int st = 0; | |
int len = str.length(); | |
char[] val = str.toCharArray(); | |
while ((st < len) && ((val[st] <= '\u0020') || (val[st] == '\u00A0') || (val[st] == '\u3000'))) { | |
st++; | |
} | |
while ((st < len) && ((val[len - 1] <= '\u0020') || (val[len - 1] == '\u00A0') || (val[len - 1] == '\u3000'))) { | |
len--; | |
} | |
return ((st > 0) || (len < str.length())) ? str.substring(st, len) : str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment