-4

I call a method with the signature save(Object o) this way:

EntityManager em = new EntityManager(); // My own EntityManager
User user = new User(); // Constructor provides values
em.save(user);

In the save-method I need to instantiate a new object, in this case it would be of type User this way: User user = (User) o;

Well, so far I can extract the class of the object with o.getClass(), which delivers class org.example.User, which is User.class I guess. But I can't figure out how I could realize the line User user = (User) o; with that.

Martin
  • 239
  • 1
  • 15
  • 3
    What exactly is your question? – rmlan May 13 '16 at 12:21
  • You usually pass in the `Class` so you could test if the cast would succeed; alternatively, you can use `instanceof`. Can you provide more context of how you're **loading** your saved `Object`(s)? – Elliott Frisch May 13 '16 at 12:21
  • Are you asking "how can I instantiate an object using reflection"? – Siguza May 13 '16 at 12:22
  • "I need to instantiate a new object this way: `User user = (User) o;`" => You are not instantiating a new object. You are declaring a new variable and assigning an object to it while you typecast it. What do you really want to do? – André Stannek May 13 '16 at 12:24
  • To clarify: It could be AnyClass instead of User, then I would need to write `AnyClass ac = (AnyClass) o`. And I dont need to test if its an instanceof, because I know allready that its of type "something", the object told me that. But I need to write the line at runtime. I cant use Object, because I need the methods of User (in this example). – Martin May 13 '16 at 12:32

3 Answers3

1

To realise the line Clazz user = (Clazz) o;, in the save method, where Clazz is the class of the object passed to the save method, so in this case User, you can make use of generics in the signature of save as follows:

public <T> void save(T user) throws IllegalAccessException, InstantiationException, InvocationTargetException {
    int i = 0;
    List<Object> args = new ArrayList<>();
    args.add(0,"Example");
    T newUser = (T)user.getClass().getConstructors()[i].newInstance(args);
    // Exciting things...
}

Note that the type of newUser is inferred from the type of object passed to the method, and that you will need to know what objects are expected to be passed to the constructor of that type, to create the args array, otherwise an exception will be thrown. Similarly you'll need to know the position of the constructor in the array in order to set i.

In short, you need to be sure what kind of constructor the user argument will have, otherwise you'll run into troubles.

However, I believe this answers your question.

  • I accepted your answer, because you tried, and the others didnt understand my problem (my fault). Bit still I cant' access the public methods of the Class User with newUser, the compiler still just shows me the methods of type Object. Thanks anyway. – Martin May 13 '16 at 18:21
  • That's because it doesn't know what methods `T` has, since it could be any object. The way around this is to create an interface with the methods common to all the User classes, and then make sure they all implement that interface, and change `public ` to `public `. That way the methods of UserInterface will be exposed. – Jonny Powell May 13 '16 at 18:29
  • I am correct in thinking that you will be passing in objects of different classes, not just all of the `User` class, right? Otherwise this is all over the top and you could do without the generics. – Jonny Powell May 13 '16 at 18:32
  • User with really just an example. Its a client who call the save(Object) method. And the client could have any class (and all of them I don't know). The save-method even just knows what methods to call with Bean Introspection. – Martin May 13 '16 at 18:34
  • If you cannot ensure that all the classes will extend a certain superclass or implement a certain interface, you won't be able to access their methods without some runtime wizardry. – Jonny Powell May 13 '16 at 18:36
  • That's what I feared. The mean thing is, that the data I need is virtually so near. Its all inside the object I pass. – Martin May 13 '16 at 18:44
0

Is calling the method newInstance() on Class that you need ? it will have the same effect as new User()

Edit : i didn't understand your question as well, but i think i am right

Pras
  • 1,068
  • 7
  • 18
0

If I understand you correctly, you are trying to instantiate a class using its class object?

You can get the class' available constructors via Class.getConstructors, then invoke any of them with Constructor.newInstance, passing your parameters.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94