Reflection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pb2000
    New Member
    • Apr 2010
    • 13

    Reflection

    Hello.

    I would like to write function which will invoke any method of any object in artificial way.

    Code, I have already written:

    Code:
    import java.lang.reflect.Method;
    import java.lang.Class;
    import java.lang.reflect.Type;
    import static java.lang.System.out;
    
    class Calc
    {
    	public long add(long a, long b) { return a + b; }
    }
    
    public class Main {
    
    	public void manipulate (Object className, String methodName, String methodArg1, String methodArg2)
    	{
    		Class<?> c= className.getClass();
            Method mlist[] = c.getDeclaredMethods();
            for (Method m : mlist)
            {
            	if (m.getName().equals(methodName))
            	{
            		Type[] paramTypes = m.getParameterTypes();
            		Type returnType = m.getReturnType();
            		m.setAccessible(true);
            		Object ReturnValue = null;
    
            		//out.format(returnType.toString(), ReturnValue);
            		//out = m.invoke(className, ...);
            	}
            }
    	}
    	public static void main(String[] args) {
    		Main main = new Main();
    		Calc c = new Calc();
    		main.manipulate(c, "add", "2", "4");
    		
    		System.out.println("end");
    		//System.out.println(c.add(-1,-2));
    	}
    }
    Problem is with casting.
    I have "2" and "4" (for example) as arguments. In paramTypes i have types, but i cannot initiate variables of those type.

    I found in one solution Casting.ToType but it does not work.

    Finally, I want to invoke method on object passed as parameter in method "manipulate ".

    I will be grateful for any idea. Thank You!
    Paul
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    pb2000,

    First off, you should probably experiment with the java.lang.refle ct.Method class.

    I did my reflection work under Eclipse, so I was able to inject a break-point into the method loop and trivially inspect the objects. This was tremendously useful in understanding how things actually work and the overall structure. From the command line this is a bit more problematic, and will be left as an exercise for (you) the student. *smile*

    Once you understand the structure, the invocation in your line 27 will look like (using implicit variadic-method object hoisting)
    Code:
    m.invoke(className, 2, 4);
    or (if you invoke explicitly)
    Code:
    m.invoke(className, new Long(2), new Long(4));
    In any case, the return is an object of type java.lang.Objec t, which may (or may not) require you to do an explicit cast to the type you want.

    Why you're trying to replace the java.lang.Syste m.out object in that same line is beyond me. It's declared as "final", so you shouldn't be able to modify it (except, perhaps, through reflection).

    Also, "className" is a misnomer. That is an object of the class you're applying reflection to, and should likely be named such. Your example "Calc" class requires an object, so an appropriate object is required.

    The documentation for "invoke(... )" is here: http://java.sun.com/javase/7/docs/ap...ang.Object...)

    Good luck!

    Comment

    Working...