• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Reflection problem...new one

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote 3 small programs:

1)Mainclass(loaded at runtime)

public class MainClass {
public void setRole(int i,MinorClass[] m){
System.out.println("setRole is invoked");
}

2)MinorClass(loaded at runtime)

public class MinorClass {
public static void main(String[] args) {
}
}

3)Invoker

public class Invoker {
public static void main(String[] args) {
try{
Object o1 = null;
Object[] o2 = ?;//how do I initialize this array?
Class c1 = Class.forName("com.test.MainClass");
o1 = c1.getConstructors()[0].newInstance(null);
//Class c2 = Class.forName("com.test.MinorClass");
Class c2 = Class.forName("com.test.MinorClass");
o2[0] = c2.getConstructors()[0].newInstance(null);
Method m = c1.getMethod("setRole",new Class[]{int.class,
?});
m.invoke(o1,new Object[]{new Integer("55"),o2});
}catch(Exception e){
e.printStackTrace();
}
}
}

What could I possibly use in place of the question marks?Or if there is any other way out please suggest...Please note that I am loading MinorClass and can't use it's name directly in my code...
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know, so this has been useful.

See Sun Reflection Docs.

You can use a strange bastardised notation in Class.forName() to get a Class object for an array type.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B][/B]
These are the objects that will eventually show up as parameters in your call to setRole(int i,MinorClass[] m). From your code shown, it really doesn't matter how many of them there are. So you could write

or

or

And as for

the ? needs to be an object representing the type of the parameter, which (from the method declaration) should be MinorClass[]. To get a Class object for this, the simplest way is a Class literal:

or the "strange bastardised notation" Peter mentioned:

This is also covered in the Class API, specifically at Class.getName().
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim what you say is true but as I have mentioned in my post the issue is that I can't use hardcoded form of MinorClass anywhere in my code whether it be for instance creation or as a parameter of the invoked method...
So probably the bastardised version should work...will try that out and see.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:

You can use a strange bastardised notation in Class.forName() to get a Class object for an array type.



A nicer approach probably would be to use java.lang.reflect.Array.newInstance
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:


A nicer approach probably would be to use java.lang.reflect.Array.newInstance



To use that here you'd have to do things a bit backwards, wouldn't you? You'd have to use the above to create the array object and then use getClass() on the array object to get its Class.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:


To use that here you'd have to do things a bit backwards, wouldn't you? You'd have to use the above to create the array object and then use getClass() on the array object to get its Class.



No. The method gets the class object of the component type, not of the array type. That is, to create a String[], you would pass it a String.class
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, I know, but the second question mark in the original posting is about getting an instance of Class to describe an array of MinorClass objects, without referring to MinorClass directly in the code.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Yea, I know, but the second question mark in the original posting is about getting an instance of Class to describe an array of MinorClass objects, without referring to MinorClass directly in the code.



Yeah, so the code would be

Array.newInstance(Class.forName(minorClassName), length)

What's backward about that???
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, when doing a call by reflection, I would expect to do things in the following order: -
  • Create the Class[] array describing formal parameter types
  • Call getMethod()
  • Create the Object[] array containing the actual parameters
  • Call method.invoke()



  • If you want to call the same method several times, with different arguments, then with the above order, it's easy: just repeat the last two steps as many times as required.

    To use Array.newInstance(), you'd need to do it in the following order: -

  • Create the Object[] array containing the actual parameters. One of the objects in the array would be created via Array.newInstance().
  • Create the Class[] array describing formal parameter types. One of the Class objects in the array would be obtained by calling getClass() on the array created by Array.newInstance().
  • Call getMethod()
  • Call method.invoke()



  • If you want to call the same method several times, with different arguments, then with this order, it's slightly less easy.
    [ July 05, 2007: Message edited by: Peter Chase ]
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, I see - seems like I was a bit confused... :roll:

    Yes, what is missing is a Class.getArrayType method or something...
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!