0

So heres my problem. I have two classes...SingularEntity and a ListEntity. As the name suggests, SingularEntity represents an Entity and a ListEntity represents a List of Singular Entities....the list obviously has more common properties than the SingularEntity itself. So I have an AuthorizationEntity which extends the SingularEntity class

public class SingularEntity{
}

public class AuthorizationEntity extends SingularEntity{

}

Now I have a single ListEntity which represents a List of any SingularEntities

public class ListEntity{

  public List<? extends SingularEntity> data;

  public ListEntity(List<? extends SingularEntity> data){
        this.data = data;
  }
}

Now, I want to use reflection to populate a list always....and I always run into an error in the results.add method. Says that method List.add(CAP#1) is not applicable. Any help is gladly appreciated

public List<? extends SingularEntity> build() {
            List<? extends SingularEntity> results = new ArrayList<SingularEntity>();
            try {

                Constructor javaBeanClassConstructor =
                        (Constructor) DTOClass.getDeclaredConstructors()[0];
                Class<?>[] constructorParameterTypes =
                        javaBeanClassConstructor.getParameterTypes();


                for (Object[] columns : lstInput) {
                    Object[] constructorArgs = new Object[constructorParameterTypes.length];
                    for (int j = 0; j < columns.length; j++) {
                        Object columnValue = columns[j];
                        Class<?> parameterType = constructorParameterTypes[j];
                        // convert the column value to the correct type--if possible  
                        constructorArgs[j] = ConversionManager.getDefaultManager()
                                .convertObject(columnValue, parameterType);
                    }

                    *results.add(javaBeanClassConstructor.newInstance(constructorArgs));*
                }
            }catch(Exception ex){
            }
            return results;
        }
1
  • what type javaBeanClassConstructor.newInstance(constructorArgs) is returning ?
    – sanbhat
    Jun 18, 2013 at 17:24

2 Answers 2

5

You should parameterize your ListEntity class

public class ListEntity<T extends SingularEntity>{

  public List<T> data;

  public ListEntity(List<T> data){
    this.data = data;
  }
}
1
  • Thanks folks...much appreciated....So now the compilation is fine after the type safety....but while invocation of the method List<AuthorizationDTO> authData = DTOBuilder.newBuilder().DTOClassNameCalled(AuthorizationDTO.class).InputListCalled(lstObject).build(); Jun 19, 2013 at 5:34
0

The problem here is that you've defined a List<? extends SingularEntity>, so you cannot add to this List. Why not? Because to the compiler, this List could be of anything that extends SingularEntity, say a new MultipleEntity class. Java cannot allow you to add any arbitrary SingularEntity to any List of any possible subclass of SingularEntity.

To add to the List, just use a List<SingularEntity>.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.