1

Let's say I have a method called Object classInstance = createInstance("Page", map, factory) which creates an instance of "Page" with java reflection and does some stuff with it. For demonstration purposes I've called it "Page", but it could be any of my classes.

Now I want to add this object to a List<Page>. To call list.add(classInstance) and add it to the list I need to parse it to "Page". Is there a way to do it, given the only information I have is the string containing the class name? So instead of doing (Page) classInstance I need to do something like (Class.forName("Page")) classInstance.

I can not modify the List or the way it is added to the list.

Thank you.

Edit: here is the createInstance Method:

    private static Object createInstance(String className, Map<?, ?> map, Meilenstein2Factory factory) throws InvocationTargetException, IllegalAccessException {

    try {
        String createMethodName = "create" + className;
        Method createMethod = factory.getClass().getMethod(createMethodName);
        Object classInstance = createMethod.invoke(factory);
        
        
        String methodName = "";
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            try {
                methodName = "set" + entry.getKey().toString().substring(0,1).toUpperCase() + entry.getKey().toString().substring(1);
                Method setNameMethod = classInstance.getClass().getMethod(methodName, getType(entry.getValue()));
                setNameMethod.invoke(classInstance, parseEntry(entry.getValue()));
            } catch (NoSuchMethodException e) {
                LOGGER.log(null, "Attribute " + entry.getKey().toString() + " is not a valid attribute for this object. Is it spelled correctly?");
            }
            
        }
        return classInstance;
    } catch(NoSuchMethodException nm) {
        LOGGER.log(null, "Folder " + className + " does not reference to a valid object. Is it spelled correctly?");
    }
    return null;

}

Edit 2: Screenshots of error and debugger

Do not bother about Page and PageImpl, I used Page in my question to simplify, but the factory accepts the Interface Page and returns an instance of PageImpl. As you can see in the second screenshot, the object is an instance of PageImpl, so this seems to be correct.

enter image description here

enter image description here

Edit 3: enter image description here

Edit 4: Something that works for now:

String methodName = "get" + "Page";
        Method getListMethod = site.getClass().getMethod(methodName);
        List<Object> list = (List<Object>) getListMethod.invoke(site);
        list.add(page);
4
  • You mix up generics and reflections i'm Not very Save in generics/reflections in Java but in C# you could use reflections to make a generic add-Method-Info and call ist with the list as Parameter. Commented Jun 19, 2021 at 13:00
  • The list is already generic If it’s a List<Page>. It’s only the way you treat it. Commented Jun 19, 2021 at 13:19
  • Sorry, must have accidentially deleted my comment. I still don't quite get how to do it. Commented Jun 19, 2021 at 13:30
  • no problem, for deleting your comment. You could undelete it in the Browser. Commented Jun 19, 2021 at 13:36

1 Answer 1

2

Your method createInstance returns a Class<Page> (the class object), but your list is a List<Page> (a list of instances).

You will need to create an instance and add it to the list, still using reflection:

list.add(classInstance.getDeclaredConstructor().newInstance());

The above is using an empty constructor for the class Page. If for example you wanted to use a constructor taking one string and one int, you would do it as such:

list.add(classInstance.getDeclaredConstructor(String.class, Integer.class).newInstance("my string", 4));

Edit:. Since classInstance is an Object and not a class, you should be able to do this:

list.add(Class.forName("Page").cast(classInstance));
Sign up to request clarification or add additional context in comments.

15 Comments

As far as I can see, OP has already an instance and ins only wants to cast it for adding.
yes, probably Class<Page> was wrong, but createInstance() already returns an instance of the class
@Nikolaus based on the first line he shows, OP gets back a Class<Page> and thinks it's enough to cast it to Page (apparently he didn't try to run it or would have seen that it will fail)
@pingpong if that's the case, you should fix your question cause it's misleading
@MatteoNNZ fixed it to Object
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.