• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array comparison

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Whats the best way to compare the contents of a Object array and a Class array?

Actualy im using reflections to invoke an overloaded method in another class, so I am left with coding the logic for calling the appropriate method by comparing the actual and formal arguements, which are an Object and a Class array.

Thanks in Advance
Mohamed Yahya
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I follow what you're doing.

Does the Object array contain the actual method arguments, while the Class array represents the types of the Objects that are supposed to be in the Object array? If that's the case, how do you want to compare them? Are you trying to ascertain whether the objects in the Object array are indeed instances of the classes in the Class array?
 
Mohamed Yahya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to check whether the objects in an array are instance of class types in another array

eg Array1(obj1, obj2, obj3) and Array2(String, int, float)

another problem is the object array Array1 does not hold primitives, whereas the Array2 gives me mix of primitives and Objects. how do overocome this comparison (java.lang.Integer == int)
 
Mohamed Yahya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks people for your timely help. Sheldon for your suggestion on differnt approach I have described the secenario below any suggestion would be appreciated.

I am using a Controller to dynamicaly invoke services(Using Reflections) based on the service name and reading the service details(like ejb class name, method name, jndi name) from a xml file, something like Struts. Now regarding the service invoktion from Controller it goes like this.

First time the method is called for ejbhome create, it works. Second time it is called for the remote business method it workd well. But when I want to handle overloaded methods I included the methods[i].getParameterTypes(); and the isInstance logic where it fails to compare the parameter types of the method.

Hope i was able to describe it clearly.

[ September 28, 2004: Message edited by: Mohamed Yahya ]
[ September 28, 2004: Message edited by: Mohamed Yahya ]
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to get Class objects for your Object arguments in Array1 first. Invoke getClass for your Array1 arguments and then compare resulting array with Array2.
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to check whether the objects in an array are instance of class types in another array


You can use the isInstance(Object obj) method of class Class.
You need to worry about one more thing, the objects in your first array could be null, in which case this checking is not possible.

I am left with coding the logic for calling the appropriate method by comparing the actual and formal arguements, which are an Object and a Class array.


Why do you need to do this? Knowing the reason might help in suggesting a different approach.

Sheldon Fernandes
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a comment on the invoke() method:
- The isInstance() method has been used incorrectly. It should have been


The current approach has 2 identified problems:
- handling primitive arguments
- Object[] argsObj could contain null references
There are probably more, if you want to make this generic.

Just one question. The invoke method accepts the Object[] argsObj. How are you creating this object array? What logic are you using to determine the order of objects in the array and the number of objects? You could probably use the same logic to determine the Class array (Class[]) to use. If this is possible, then instead of looping through all the methods of the class, you could directly get the desired method using the following method of class Class: Method getMethod(String name, Class[] parameterTypes)

Sheldon Fernandes
 
Mohamed Yahya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheldon, I would like to thank for you the valuable hint(Class.getMethod()) it simplified the code a lot, check this, it works fine.



Only thing is its not yet generalized for null and primitives. Anyhow in my case the object array has just one ValueObject(objArray[0] = reqData ) hence no problem as far as this requirement doesnt change.

Once again thank you
Mohamed
[ September 29, 2004: Message edited by: Mohamed Yahya ]
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic