2

I have a class Bucket which is passed as a generic type in the Comparator interface. I am working with GSON to register a type adapter for this type.

The typeadapter function has the signature

public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter)

I want to pass the first argument as the type object

java.util.Comparator<org.elasticsearch.search.aggregations.bucket.terms.Terms$Bucket>

I tried Comparator<Terms.Bucket>.class which throws me a syntax error . What is the correct way of doing this ?

1 Answers1

1

Generics are only used for compile time safety, then they are erased (they don't exist at runtime). So writing something like this Comparator<Terms.Bucket>.class isnt possible. You need to create a explicit class:

public class BucketComparator implements Comparator<Terms.Bucket>
{
    ...
}

After this you are able to pass the type as follows: BucketComparator.class

kAliert
  • 768
  • 9
  • 21