Java reflection value checker

Asked

Viewed 50 times

-1

I’m trying to use Reflection and Recursion to capture values.

For example:

public class Endereco{
       String rua;
       Integer numero;
}
public class Cachorro{
       String nome;
       Integer idade;
}
public class Pessoa{ 
       String nome;
       Cachorro cachorro;
       List<Endereco> endereco;
}

I create a List<Pessoa> and I populate it, only that I’m trying to go through these people and count their null and non-null attributes. For example: I have a list of 5 People and I want to check whether in the name attribute, dog (within the dog class), or any other is different than null and tell it. I need it to be recursive, but I couldn’t do it.

I’m sorry if I didn’t express myself right.

  • What reflection code have you tried to do? Why didn’t you get it? What went wrong?

  • Post the code you used.

  • I first step to a method that contains a For and it goes through my list, after that, I pick up all the objects within each Person object, ie name, address list, dog. But I can’t understand how I could go into each attribute and specifically count the attribute. Example: getNome(), verifies if in these 5 people the name is null or not and counts, next, Dog, enters the object Dog picks up all the objects inside this Dog and counts again each attribute, so until I finish my classes.

  • Post your code in the question.

1 answer

0

All classes that implement the Attribute interface can be counted.

public interface AtributoContavel {
 public int getQuantidadeAtributosNulos();
 public int getQuantidadeAtributosNaoNulos();

}

The App class is the main class that will recursively call the methods of calculating null and non-null attributes and at the end returns the result.

public class App {

public static void main(String[] args) {
    List<AtributoContavel> pessoas = inicializar();
    System.out.println("Quantidade de Atributos Nulos = " + calcularAtributosNulos(pessoas,0,0));
    System.out.println("Quantidade de Atributos Nao Nulos = " + calcularAtributosNaoNulos(pessoas,0,0));

}

public static List<AtributoContavel> inicializar() {

    List<Endereco> enderecos = new ArrayList<>();
    enderecos.add(new Endereco("Rua Teste ", 13));
    enderecos.add(new Endereco("Rua Teste 2", 14));

    List<AtributoContavel> pessoas = new ArrayList<>();
    Pessoa p1 = new Pessoa("Saulo", null, null);
    Pessoa p2 = new Pessoa("Thiago", new Cachorro("Jack", 2),null);
    Pessoa p3 = new Pessoa("Saulo", new Cachorro("Tob", 2), enderecos );

    pessoas.add(p1);
    pessoas.add(p2);
    pessoas.add(p3);

    return pessoas;
}

public static int calcularAtributosNulos(List<AtributoContavel> pessoas, int posicao, int total) {
    total+=pessoas.get(posicao).getQuantidadeAtributosNulos();
    posicao++;
    if(pessoas.size() == posicao)
        return total;
    return calcularAtributosNulos(pessoas, posicao, total);
}


public static int calcularAtributosNaoNulos(List<AtributoContavel> pessoas, int posicao, int total) {
    total+=pessoas.get(posicao).getQuantidadeAtributosNaoNulos();
    posicao++;
    if(pessoas.size() == posicao) 
        return total;
    return calcularAtributosNaoNulos(pessoas, posicao, total);
}

The other classes only implement the Attribute interface and have their respective implementations of abstract methods

public class Endereco implements AtributoContavel {
private String rua;
private Integer numero;

Endereco(String rua, Integer numero){
    this.rua = rua;
    this.numero = numero;
}

@Override
public int getQuantidadeAtributosNulos() {
    int qtdAtributosNulos = 0;
    if(this.rua == null)  qtdAtributosNulos++;
    if(this.numero == null) qtdAtributosNulos++;
    return  qtdAtributosNulos++;
}

public int getQuantidadeAtributosNaoNulos() {
    int qtdAtributosNaoNulos = 0;
    if(this.rua != null)  qtdAtributosNaoNulos++;
    if(this.numero != null) qtdAtributosNaoNulos++;
    return  qtdAtributosNaoNulos++;
}}





public class Cachorro implements AtributoContavel {
private String nome;
private Integer idade;

Cachorro(String nome, Integer idade){
    this.nome = nome;
    this.idade = idade;
}

@Override
public int getQuantidadeAtributosNulos() {
    int qtdAtributosNulos = 0;
    if(this.nome == null)  qtdAtributosNulos++;
    if(this.idade == null) qtdAtributosNulos++;
    return  qtdAtributosNulos++;
}

public int getQuantidadeAtributosNaoNulos() {
    int qtdAtributosNaoNulos = 0;
    if(this.nome != null)  qtdAtributosNaoNulos++;
    if(this.idade != null) qtdAtributosNaoNulos++;
    return  qtdAtributosNaoNulos++;
} 

}

public class Pessoa implements AtributoContavel {
private String nome;
private Cachorro cachorro;
private List<Endereco> enderecos;

Pessoa(String nome, Cachorro cachorro, List<Endereco> enderecos){
    this.nome = nome;
    this.cachorro = cachorro;
    this.enderecos = enderecos;
}

@Override
public int getQuantidadeAtributosNulos() {
    int qtdAtributosNulos = 0;
    if(this.nome == null)  qtdAtributosNulos++;

    if(this.cachorro == null)
         qtdAtributosNulos++;
    else
        qtdAtributosNulos += this.cachorro.getQuantidadeAtributosNulos();

    if(this.enderecos == null) {
        qtdAtributosNulos++;
    }
    else {
        for (Endereco endereco : enderecos) {
            if(endereco == null)
                 qtdAtributosNulos++;
            else
                qtdAtributosNulos+=endereco.getQuantidadeAtributosNulos();
        }
    }
    return  qtdAtributosNulos++;
}

@Override
public int getQuantidadeAtributosNaoNulos() {
    int qtdAtributosNaoNulos = 0;
    if(this.nome != null)  qtdAtributosNaoNulos++;

    if(this.cachorro != null)
        qtdAtributosNaoNulos += this.cachorro.getQuantidadeAtributosNaoNulos();

    if(this.enderecos != null) {
        for (Endereco endereco : enderecos) {
            if(endereco != null)
                qtdAtributosNaoNulos+=endereco.getQuantidadeAtributosNaoNulos();
        }
    }
    return  qtdAtributosNaoNulos++;
}}

Browser other questions tagged

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