2
private static Void createTbody(List object) {

    Iterator hritr = object.iterator();
        while(hritr.hasNext()) {

        UserDto users = (UserDto)hritr.next();
        users.userId;
        users.userName;
    }
}

DtoClass::

UserDto

public class UserDto {

    public String userName;
    public String userId;

}

In the above code i dont have any problem...

put in my createTbody function i dont know the UserDto Class at compile time...It should dynamic class.This class name i will get through reflection.

UserDto users = (UserDto)hritr.next();

Why because is, private static Void createTbody(List object) object may contain List of CustomerDto,UserDtoetc...

3
  • cant you do instanceof to check the type? Commented Feb 23, 2012 at 7:03
  • If you don't know the class at compile-time, what are you going to do with the entry after fetching it? You won't know that it has userName and userId fields... Commented Feb 23, 2012 at 7:08
  • i will know the class at run time.I want to retrieve all propertis of that class.If the calss is userDto it will be userId and userName,if it is customerDto the properties may differ customerId,customerName etc.. Commented Feb 23, 2012 at 7:26

3 Answers 3

2

At first, please throw away your iterator. Iterating through list is better done with modern syntax:

for(Object iterm: listOfObjects)  {}

In case you have not so much clases in question, you can check for class object with getClass().equals(someClassInQuestion) or object instanceof SomeClass

If you have too much classes , just stick back to reflection:

object.getClass().getMethod("getFoo",null).invoke(oject);

( code is simplified, you wuill need some guarding agains null values )

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

Comments

2

If all your DTOs have common fields (like the id and the name) and you only need to access those in your method, you could use an interface for all DTOs:

interface SuperDTO {
  public Long getId;
  public String getName;
  public void setId(Long id);
  public void setName(String name);
}

All of the DTOs have to extend the interface: public class UserDTO extends SuperDTO{}.

So you can iterate your Lists like this:

for(SuperDTO dto : someListWithDTOs){
  dto.setName("foobar");
  ....
}

Edit to add:

If your list contains different types of DTO's, you could use instanceof:

for(Object item : list){
  if(item instanceof UserDTO){
   ...
  else if (item instanceof SomeDTO){
   ...
  }
}

2 Comments

my Dto will not have common fields.it will have differnt fields. Is there any way to achieve this?
In that case you could use Konstantin Pribluda's approach. But if your DTOs have many different fields your code will become hard to read, error-prone and will use more memory. (reflection is quite resource intensive). Assuming every list contains only one type of DTO, I'd recommend to iterate through every list seperately. Otherwise, see my edited post.
2

posElement is your object.

Field[] fields = posElement.getClass().getDeclaredFields();
for (Field field : fields) {
  Type type = field.getGenericType();
  if (type instanceof ParameterizedType) {
    ParameterizedType pt = (ParameterizedType) type;

    if (PropertyUtils.getProperty(posElement, field.getName()) != null) {
        for (int i = 0; i < pt.getActualTypeArguments().length; i++) {
            Object obj = PropertyUtils.getIndexedProperty(posElement, field.getName(), i);
            System.out.println(obj.getClass());
        }
    }
  }
}

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.