Created
August 12, 2011 16:43
-
-
Save Palmr/1142435 to your computer and use it in GitHub Desktop.
Inelegant Coding
This file contains hidden or 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
//Because the normal initcap function lowercases the string first | |
private static String upperFirstchar( String pString) { | |
return pString.substring(0,1).toUpperCase() + pString.substring(1); | |
} | |
//Use java reflection to try and get private member values from an object | |
// MemberPath is of format "Object.Object.lPrivateVariable", the first object being a member of pStartingObject | |
//This function is quite possibly the inelegant thing I've coded for a while | |
public static Object getPrivateMembers (String pMemberPath, Object pStartingObject) | |
throws ExInternal { | |
if (pStartingObject == null) { | |
return null; | |
} | |
Object lParentObject = null; | |
Object lReturnObject = null; | |
String lCutPath = ""; | |
String lMemberName = ""; | |
if (pMemberPath.indexOf('.') >= 0) { | |
//Pop off member | |
lMemberName = pMemberPath.substring(pMemberPath.lastIndexOf('.')+1); | |
lCutPath = pMemberPath.substring(0, pMemberPath.lastIndexOf('.')); | |
lParentObject = getPrivateMembers(lCutPath, pStartingObject); | |
if (lParentObject == null) { | |
return null; | |
} | |
//Get object | |
//Try to use getter method | |
try { | |
Method lMethod = lParentObject.getClass().getMethod("get"+upperFirstchar(lMemberName), null); | |
return lMethod.invoke(lParentObject, null); | |
} | |
catch (InvocationTargetException ex) { | |
ex.printStackTrace(); | |
} | |
catch (NoSuchMethodException ex) { | |
// TODO | |
} | |
catch (IllegalAccessException ex) { | |
ex.printStackTrace(); | |
} | |
if (lReturnObject == null) { | |
//Try to get field | |
try { | |
Field memberField = lParentObject.getClass().getDeclaredField(lMemberName); | |
memberField.setAccessible(true); | |
lReturnObject = memberField.get(lParentObject); | |
} | |
catch (IllegalAccessException e) { | |
throw new ExInternal("Couldn't read field", e); | |
} | |
catch (NoSuchFieldException e) { | |
// TODO | |
} | |
} | |
} | |
else { | |
//If it's the first object in the list... | |
if (lReturnObject == null) { | |
//Try Getter method | |
try { | |
Method lMethod = pStartingObject.getClass().getMethod("get"+upperFirstchar(pMemberPath), null); | |
if(lMethod.invoke(pStartingObject, null) instanceof Object){ | |
return lMethod.invoke(pStartingObject, null); | |
} | |
else { | |
return (Integer)lMethod.invoke(pStartingObject, null); | |
} | |
} | |
catch (InvocationTargetException ex) { | |
ex.printStackTrace(); | |
} | |
catch (NoSuchMethodException ex) { | |
// TODO | |
} | |
catch (IllegalAccessException ex) { | |
ex.printStackTrace(); | |
} | |
//Try field | |
try { | |
Field memberField = pStartingObject.getClass().getDeclaredField(pMemberPath); | |
memberField.setAccessible(true); | |
return memberField.get(pStartingObject); | |
} | |
catch (IllegalAccessException e) { | |
throw new ExInternal("Couldn't read field", e); | |
} | |
catch (NoSuchFieldException e) { | |
// TODO | |
} | |
} | |
} | |
return lReturnObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment