It is possible to create a list of generic Object

Asked

Viewed 726 times

2

I need to generate a generic list

List<?> listaGenerica; 

having as parameter a string with the class path, as shown below.

Class class = Class.forName("com.teste.Endereco");

That’s possible?

Sorry for the confusion in the description of the question because I’m really confused.. kkk

I have the following entity class of Hibernate.

@TypeDefs({
    @TypeDef(name = "jsonbEndereco", typeClass = JsonBinaryType.class})
})
@Entity
@Table(name = "teste")
@SequenceGenerator(name = "teste_id_seq", sequenceName = "teste_id_seq", allocationSize = 1)
public class Teste implements Serializable {

    private static final long serialVersionUID = 6800342991944554204L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "teste_id_seq")
    private Integer id;
    private String nome;
    private String cpf;
    @Type(type = "jsonbEndereco")
    @Column(columnDefinition = "jsonbEndereco")
    private List<Endereco> enderecos;

When converting to address list, it converts all fields as string, not allowing Typecast to address.

OBJECT_MAPPER.readValue(string, clazz);

That’s because clazz comes as java.util.List

I solved this problem by using the list where I know it is a List with the following code.

Teste teste = testeCTR.carregar(1);

Endereco endereco = null;    

ObjectMapper mapper = new ObjectMapper();

List<Endereco> enderecos = mapper.convertValue(teste.getEnderecos(), new TypeReference<List<Endereco>>() {});

My intention was to pass as parameter br.com.Jenkins.Addressaddress

@TypeDefs({
    @TypeDef(name = "jsonbEndereco", typeClass = JsonBinaryType.class, parameters = {
        @Parameter(name = JsonBinaryType.CLASS, value = "br.com.jenkins.Endereco")})
}) 

and when converting json wanted to convert and map correctly.

OBJECT_MAPPER.readValue(string, new TypeReference<List<com.teste.Endereco>>() {});

I’m sorry if you’re still confused...

[Piecemeal]

Creating the list of objects from a Type of a class I could not... But I checked the bug Creating a List addressee class that contains an address list, and that way it worked.

public class ListaEndereco {
    private List<Endereco> enderecos;

    public List<Endereco> getEnderecos() {
        return enderecos;
    }

    public void setEnderecos(List<Endereco> enderecos) {
        this.enderecos = enderecos;
    }
}


public class Endereco {
    private Integer enderecoid;
    private String endereco;

    public Integer getEnderecoid() {
        return enderecoid;
    }

    public void setEnderecoid(Integer enderecoid) {
        this.enderecoid = enderecoid;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
}

Type type = ListaEndereco.class;



 String json = "{\"enderecos\": [{\"endereco\": \"Teste\", \"enderecoid\": 1}, {\"endereco\": \"Teste2\", \"enderecoid\": 2}, {\"endereco\": \"Teste3\", \"enderecoid\": 3}]}";       
    ListaEndereco lista = (ListaEndereco) OBJECT_MAPPER.readValue(json, type.getClass());

Thank you all very much. For your help

  • 1

    List<T> listaGenerica = new ArrayList<Endereco>(); thus?

  • 1

    Similar http://answall.com/questions/126968/fazer-um-vetor-gen%C3%A9rico-em-java/

  • 2

    If I understand correctly and need a key/value structure, then you need a Map, nay List. Otherwise, you can use List<Object>, after all all all Java class is subclass of Object implicitly.

  • 1

    @Paulogustavo Like this here? Look at the answer.

  • It is that I wanted to pass parameter in Annotations "com.teste.Endereco" and create this list to make a correct object conversion. type like List<Address> addresses = mapper.convertValue(test.getEnderecos(), new Typereference<List<Address>() { });

  • I could better detail what you want in the question statement?

  • @Joseandersonpereira modified the statement. Managed to understand now... I need to instantiate a class and not an object...

  • @Renan maybe this would solve my problem tbm... my problem q when I convert json it returns address lists but all fields like String, so I can’t make typeacast

Show 3 more comments

1 answer

2

Yes it is possible.

Class<?> clazz = Class.forName("com.teste.Endereco");

List<Class<?>> classes = new ArrayList<Class<?>>();
classes.add(clazz);
  • needed to take the reference of a list of a specific object Typereference type = new Typereference<List<Address>>() { };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.