Checks if a class has a @interface

Asked

Viewed 68 times

0

I have the class below as an example:

package br.com.nomeMarca.testes;

import br.com.nomeMarca.cidog.interfaces.*;
import javafx.beans.property.*;

import java.io.*;
import java.math.*;
import java.time.*;

@Table
public class ClientePO implements Serializable {

    private static final long serialVersionUID = 1L;
    private LongProperty id = new SimpleLongProperty();
    private IntegerProperty numero = new SimpleIntegerProperty();
    private StringProperty nome = new SimpleStringProperty();
    private ObjectProperty<LocalDate> dataNascimento = new SimpleObjectProperty();
    private BooleanProperty verificaValidade = new SimpleBooleanProperty();
    private ObjectProperty<BigDecimal> valorPessoa = new SimpleObjectProperty();
    private ObjectProperty<EnderecoPO> enderecoPO = new SimpleObjectProperty();

    public EnderecoPO getEnderecoPO() {
        return enderecoPO.get();
    }

    public ObjectProperty<EnderecoPO> enderecoPOProperty() {
        return enderecoPO;
    }

    public void setEnderecoPO(EnderecoPO enderecoPO) {
        this.enderecoPO.set(enderecoPO);
    }

    public long getId() {
        return id.get();
    }

    public LongProperty idProperty() {
        return id;
    }

    public void setId(long id) {
        this.id.set(id);
    }

    public int getNumero() {
        return numero.get();
    }

    public IntegerProperty numeroProperty() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero.set(numero);
    }

    public String getNome() {
        return nome.get();
    }

    public StringProperty nomeProperty() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome.set(nome);
    }

    public LocalDate getDataNascimento() {
        return dataNascimento.get();
    }

    public ObjectProperty<LocalDate> dataNascimentoProperty() {
        return dataNascimento;
    }

    public void setDataNascimento(LocalDate dataNascimento) {
        this.dataNascimento.set(dataNascimento);
    }

    public boolean isVerificaValidade() {
        return verificaValidade.get();
    }

    public BooleanProperty verificaValidadeProperty() {
        return verificaValidade;
    }

    public void setVerificaValidade(boolean verificaValidade) {
        this.verificaValidade.set(verificaValidade);
    }

    public BigDecimal getValorPessoa() {
        return valorPessoa.get();
    }

    public ObjectProperty<BigDecimal> valorPessoaProperty() {
        return valorPessoa;
    }

    public void setValorPessoa(BigDecimal valorPessoa) {
        this.valorPessoa.set(valorPessoa);
    }
}

I want to change the ID by inserting the @Id.

Staying:

@Id
public long getId() {
    return id.get();
}

I need to check now if this class has the @Id, by checking method by method I can, however there is a way to get the information without listing it? Some command that brings a boolean or the name of the method?

  • I think you have to iterate the same expensive methods..

  • Too bad, I’ll do it then, thank you

1 answer

1

As far as I know the only way is through even iteration, I even have two methods that I use for this:

-The first returns only if the annotation exists

public boolean isAnotacaoMetodoPresente(Class classe, Class<? extends Annotation> anotacaoProcurada)
{
    Method[] metodos = classe.getDeclaredMethods();

    for(Method m : metodos)
    {
        if(m.isAnnotationPresent(anotacaoProcurada))
            return true;
    }
    return false;
}

-The second returns the method where the annotation is

public Method getMetodoAnotacaoPresente(Class classe, Class<? extends Annotation> anotacaoProcurada)
{
    Method[] metodos = classe.getDeclaredMethods();

    for(Method m : metodos)
    {
        if(m.isAnnotationPresent(anotacaoProcurada))
            return m;
    }
    return null;
}

Remembering that, your note needs to have the retention policy as follows

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //Considerando que ela vá anotar métodos, pode ser removido
public @interface AnotacaoExemplo {}

Browser other questions tagged

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