-1

This question is in continuation with Java: Using Classes as a value in hashmap.

What is the difference between following two approaches?:

1)

String name = (
                  (
                       CustomClass1
                  )obj1
              ).getName();

and 2)

String name = (
                  (
                       mapQuery2ResponseType.get("string1")
                  )obj1
              ).getName();

where, mapQuery2ResponseType.get("string1") return value of type Class<?>

First approach works perfectly but in second approach it's giving an error saying Syntax error on token "obj1", delete this token.

How can I modify second approach (Using Map) so as to work as in first case?

How can I make mapQuery2ResponseType.get("string1") to return CustomClass1 instead of CustomClass1.class?

Community
  • 1
  • 1
N D Thokare
  • 1,703
  • 6
  • 35
  • 57

2 Answers2

3

A Cast is an instruction to the compiler, you can't wait until runtime to find out what Class you're going to cast to! There is no way for the compiler to know whether or not whatever comes out of that map when you call it is actually going to have a getName() method on it or not.

You'll need to put in a cast at compile time to some common supertype all of the contents of the map share that makes you confident they all have a 'getName()' method. If that's not possible you're off into the world of reflection to fine the method so that you can call it.

Here is a question that answers how to call a "getName" method on any arbitrary object when you don't know the class at compile time: How do I invoke a Java method when given the method name as a string?

If your methods are indeed actual bean property getters and setters, you could also use the apache BeanUtils library to get the values of named properties from an object.

Community
  • 1
  • 1
Affe
  • 47,174
  • 11
  • 83
  • 83
  • It's not possible to predict which method has which method. Can you please give details of how can we achieve it using reflection or provide any reference link? – N D Thokare Mar 23 '13 at 06:39
  • 2
    Then how are you sure it has a getName() on it?/what do you expect to happen if it didn't? – Affe Mar 23 '13 at 06:50
  • was planning to use reflection to know existence of that method in given class... – N D Thokare Mar 23 '13 at 08:02
  • ah-ha, I see, a job started with reflection, will have to be finished with reflection! – Affe Mar 23 '13 at 18:55
0

How can I make mapQuery2ResponseType.get("string1") to return CustomClass1 instead of CustomClass1.class?

You can't. A Java type cast requires a class/type that is known at compile time.

You should be able to do what your second example seems to be trying to do as follows.:

1) Declare an interface:

    public interface Named {
        String getName();
    }

2) Change all of the classes in the request type map to implements Named.

3) Change the "second approach" code above to:

    String name = ((Named) obj1).getName();

The other alternative would be to use reflection to lookup the getName() method on obj1.getClass(), and then use method.invoke(...) to call it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216