|
/**
* The Code Project Open License (CPOL) 1.02
* see : http://www.codeproject.com/info/cpol10.aspx
*
* Author : Adrabi Abderrahim
* Mail : adrabi[at]gmail[dot]com
* Year : 2009
* Discrip : this code a part of "Create Your Own Programming Language, just game ?"
* and code using is from Alef++ project
* https://sourceforge.net/projects/alefpp/
*/
package st4tic.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import st4tic.core.St4ticValue;
/**
* Magical class for java reflection =)
*
* @author Adrabi Abderrahim
*
*/
public class St4ticReflection {
private static LinkedList<String> packages = new LinkedList<String>();
public static void pushPackage(String packageName )
{
packages.add( packageName );
}
/**
* Get a full class identifier
* @param className (String : Class Name)
* @return
*/
public static String fullIdentifier(String className ){
// Get a list of the used packages
for( String pkg : packages ){
try {
Class.forName( pkg + "." + className);
return pkg + "." + className;
} catch (ClassNotFoundException e) {}
}
return null;
}
/**
* making an object from string name
* @param className
* @return
*/
public static Class makeObject( String className )
{
try {
return Class.forName( className ) ;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* Check existence of the field
* @param classInstance (Object : Class Object)
* @param fieldName (String : Filed Name)
* @return
*/
@SuppressWarnings("unchecked")
public static boolean existsField(Object classInstance , String fieldName ){
//~ Public Field
try{
Class clazz = classInstance instanceof Class ? (Class)classInstance : classInstance.getClass();
clazz.getField( fieldName );
return true;
}catch (SecurityException e) {
} catch (NoSuchFieldException e) {
}
return false;
}
/**
* Return a field object from spicified class name for statics
* @param classInstance (Object : Class Object)
* @param fieldName (String : Field Name)
* @return
*/
public static Object getFieldObject(Object classInstance , String fieldName ){
//~ Public Field
try{
Field field = classInstance instanceof Class ? ((Class)classInstance).getField( fieldName ) : classInstance.getClass().getField( fieldName );
return field.get(classInstance);
}catch(IllegalAccessException iae){
}catch(NoSuchFieldException nsfe){
}
return null;
}
/**
* Check existence of the subroutine with a specified parameters
* @param classInstance (Object : Class Object)
* @param methodName (String : Subroutine Name)
* @param args (St4ticValue[] : Types)
* @return
*/
@SuppressWarnings("unchecked")
public static boolean existsSubroutine(Object classInstance, String methodName , St4ticValue ... args){
//~ Public Subroutine
try{
Class clazz = classInstance instanceof Class ? (Class)classInstance : classInstance.getClass();
// Subtoutine with parameters "sub[param_1,params_2,...]"
if( args != null ){
LinkedList<Class> classes = new LinkedList<Class>();
for( St4ticValue value : args ){
classes.add( value.getType() );
}
clazz.getMethod(methodName, classes.toArray(new Class[]{}));
}
// Subroutine without parameters "sub[]"
else{
clazz.getMethod(methodName, new Class[]{} );
}
return true;
}catch(NoSuchMethodException nsfe){
}
return false;
}
/**
* For invoking a static subroutines and return value if is not void
* @param classInstance (Object : Class Object)
* @param methodName (String : Method Name)
* @param args (St4ticValue[] : Method Arguments)
* @return
*/
@SuppressWarnings("unchecked")
public static Object invokeStaticSubroutine(Object classInstance, String methodName, St4ticValue ... args){
//~ Public Invokation
try{
Class clazz = classInstance instanceof Class ? (Class)classInstance : classInstance.getClass();
// Invoke Subroutine with parameters
if( args != null ){
//~ Get Static Method
LinkedList<Class> params = new LinkedList<Class>();
for( St4ticValue arg : args ){
params.add( arg.getType() );
}
Method method = clazz.getMethod(methodName, params.toArray(new Class[]{}));
//~ Invoke Static Method
LinkedList<Object> values = new LinkedList<Object>();
for( St4ticValue arg : args ){
values.add(arg.getValue());
}
return method.invoke(classInstance, values.toArray(new Object[]{}));
}
// Invoke Subroutine without parameters
else{
Method method = clazz.getMethod(methodName, new Class[]{});
return method.invoke(classInstance, new Object[]{});
}
}catch (SecurityException se) {
} catch (NoSuchMethodException nsme) {
} catch (IllegalArgumentException iae) {
} catch (IllegalAccessException iae) {
} catch (InvocationTargetException ate) {
}
return null;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.