Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts

    Determine which class to load at runtime

    Hi all,

    I am working on a program where i have a classname mentioned in an xml file. I fetch the name of the class from this xml file. Now in my program, i have to load this class (it is a static class) and call some method on it. How do i do that. I have searchd for info on reflection, but i cannot get things to work correctly

    in the xmlfile there could be this string, but there could also be some other classname:

    foo.bar.myClassDef1

    now in code , i have to load this class and call the getIntValue of this class
    if i knew the classdefinition up front, there would be no problem, i could just go and call:
    int myIntValue = foo.bar.myClassDef1.getIntValue();

    the classdefinitions which i can expect are in fact limited, they all have the same interface, but the specific version of the class is configurable in the xml file.

    Is there anyone out there who can help me?
    I am the luckiest man in the world

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Whew, at least you used our best friend ever: an interface.
    You'll need to use that interface to capture the datatype of the class if you want to store it in a variable.
    I'm not sure the best way of doing this honestly, but the more information you give it the better things will be. If you specify what parameters are expected, and you cast to the appropriate interface you'll be able to run what you are looking for.
    Code:
    	public static void main(String[] argv)
    	{
    		String classname = "java.lang.Integer";
    		try
    		{
    			Class<?> fromClass = Class.forName(classname);
    			Class<?>[] paramType = new Class[]{Class.forName("java.lang.String")};
    			
    			Method method = fromClass.getMethod("parseInt", paramType[0]);
    
    			Object result = method.invoke(null, new String("15")); // The null is to indicate static
    			System.out.println("Result is: " + result + ", of type: " + result.getClass().getName());
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    That will tell me that the result is 15 and is of type java.lang.Integer which is good. Since its runtime though, I don't believe you can invoke the method and then treat it as an Integer without explicitly casting it to an Integer (java really has no clue what it is at that point).

    So, here's where the fun stuff begins. You should be able to write a new invoke method of some kind that lets you drop your parameters into it (either by a Object[] or by using variable arguments [which I hate in java btw]), and iterate the Methods of the Class<?> to determine which (if any) is proper to use. This is a little easier than the getMethod method which requires us to know which parameter signatures we want.

    Hope that gives you a starting point at least!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •