18

this is what my code looks if I use a predefined class:

List<MyClass> result = new ArrayList<MyClass>();
try {
    Query query = mgr.newQuery(MyClass.class);
    for(Object obj : (List<Object>) query.execute()) {
        result.add(((MyClass) obj));
    }
}
.... 
return result;

now I need to be more generic: starting from a generic class name (as a string, ie "TheChosenOne") I need to do the same thing, but I can't figure out how the cast part should be done..

Just to make an example of what I'm trying to do:

String str = "TheChosenOne"; //this value may change
Class cls;
List<Object> result = new ArrayList<Object>();
try {
    if(str.equals("TheChosenOne"))
        cls = Class.forName("com.blabla.TheChosenOne");
    else if(str.equals("Rabbit"))
        cls = Class.forName("com.blabla.Rabbit");
    else if(str.equals("Batman"))
        cls = Class.forName("com.heroes.Batman");
    else
        cls = null;

    if(cls != null){
        Query query = mgr.newQuery(MyClass.class);
        for(Object obj : (List<Object>) query.execute()) {
            result.add(((???) obj)); //I need help here!
        }
    }
}
...
return result;

I took the "Class.forName()..." part here.

Any suggestions? Thanks in advance, best regards

6 Answers 6

25

What you are trying to do is unnecessary because your list is declared as List<Object> so the cast is not needed.

-- Before Edit --

Not sure I understand what you need, but did you try to use:

Class.cast(object)

This is a method of the java.lang.Class

Sign up to request clarification or add additional context in comments.

1 Comment

result.add((Class.cast(obj))) is the same as result.add(obj). Because list is of type Object
3

I think what you are trying to do can be achieved with generics.

For example a method like this:

     public <E> List<E> getList(Class<E> clazz) {
         List<E> result = new ArrayList<E>();
         if(clazz != null){
             Query query = mgr.newQuery(MyClass.class);
             for(E obj : (List<E>)query.execute()) {
                 result.add(obj); 
             }
         }

         return result;
     }

Can be called with:

 getList(TheChosenOne.class)

And it will return a List<TheChosenOne> object

Comments

2

If result is a List<Object> then you don't need a cast at all, simply add(obj) will work fine.

If you need compile time type safety then you will have to pass in the Class object rather than a string containing its name, and use a generic method signature

public <T> List<T> doQuery(Class<T> theClass) {
  List<T> result = new ArrayList<T>();
  try {
    Query query = mgr.newQuery(theClass);
    for(T obj : (List<T>) query.execute()) {
      result.add(obj);
    }
  }
  .... 
  return result;
}

If you go this route, and have the option of modifying the Query class then you might want to consider making that class parameterized

public class Query<E> {
  public List<E> execute() { ... }
}

// and in mgr
public <T> Query<T> newQuery(Class<T> cls)

which would then let you say

    Query<T> query = mgr.newQuery(theClass);
    for(T obj : query.execute()) {
      result.add(obj);

with no casting at all.

1 Comment

I need to return a list of TheChosenOne class, or a list of Rabbit class or a list of Batman class, based on the value of the str string..
0

I believe cls.cast(obj) is what you're looking for.

Comments

0

I think you need to do it in this way: using instance of operator to check the type of the class and then do the type casting

for(Object obj : (List<Object>) query.execute()) {
  if(obj instance of Batman)
    result.add(((Batman) obj));
  else if(obj instance of Cat)
    result.add(((Cat) obj));

  // and so on
  ...
  ...
}

Comments

0

You dont need cast while putting object in the ArrayList,

 result.add(obj); 

If you want you can cast while getting from the list

 cls .cast(result.get(1));

Comments

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.