Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Using Class<T> with immutables

I'm giving the immutables.org library a try. What I need is to be able to specify a class, so I've defined

@Value.Immutable
public interface Value<T> {
    Class<T> getType();
}

The builder generated by immutables does not accept this

ImmutableValue.builder().type(String.class)...

Because it expects a Class<Object>. How do I get the builder to accept String.class?

Annotating the type with @Value.Parameter nicely creates a of method that works

ImmutableValue.of(String.class)...

But the result is an instance, not a builder, so follow up values can only be set using withers.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can infer the type using type witness.

public class ImmutableExample {
    public static void main(String[] args) {
        ImmutableValue immutableValue = ImmutableValue.<String>builder()
                .type(String.class).build();
        System.out.println(immutableValue.getType());
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda