Skip to content

Instantly share code, notes, and snippets.

@yatszhash
Created November 7, 2017 03:17
Show Gist options
  • Save yatszhash/d064b7ef063e9af6b12512acdd89e86a to your computer and use it in GitHub Desktop.
Save yatszhash/d064b7ef063e9af6b12512acdd89e86a to your computer and use it in GitHub Desktop.
java reflection sample
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