ZeroUtil { public static List<TypeVariableName> extractTypeVars(TypeName type) { if (!maybeTypevars(type)) { return emptyList(); } List<TypeVariableName> builder = new ArrayList<>(); TypeWalk walk = new TypeWalk(type); while (walk.hasNext()) { TypeName next = walk.next(); if (next instanceof TypeVariableName) { if (!builder.contains(next)) { builder.add((TypeVariableName) next); } } } return builder; } private  ZeroUtil(); static String upcase(String s); static String downcase(String s); static CodeBlock statement(String format, Object... args); static ParameterSpec parameterSpec(TypeName type, String name); static FieldSpec fieldSpec(TypeName type, String name, Modifier... modifiers); static ClassName rawClassName(TypeName typeName); static List<E> transform(Collection<? extends X> input, Function<X, E> function); static List<P> cons(P first, List<? extends P> list); static List<P> concat(List<? extends P> left, List<? extends P> right); static Collector<CodeBlock, List<CodeBlock>, CodeBlock> joinCodeBlocks(String delimiter); static Collector<List<E>, List<E>, List<E>> flatList(); static MethodSpec constructor(Modifier... modifiers); static String simpleName(TypeName type); static TypeName parameterizedTypeName(ClassName raw, List<TypeVariableName> typeVars); static Modifier[] modifiers(Access access, Modifier modifiers); static Modifier[] modifiers(Access access); static boolean references(TypeName type, TypeVariableName test); static List<TypeVariableName> extractTypeVars(TypeName type); static List<E> reverse(List<E> in); static final CodeBlock emptyCodeBlock; static final Collector<CodeBlock, List<CodeBlock>, CodeBlock> joinCodeBlocks; }
@Test public void testExtractTypeVars() { TypeVariableName a = TypeVariableName.get("A"); TypeVariableName b = TypeVariableName.get("B"); TypeVariableName c = TypeVariableName.get("C"); TypeVariableName d = TypeVariableName.get("D"); ParameterizedTypeName ab = ParameterizedTypeName.get(ClassName.get(HashMap.class), a, b); ParameterizedTypeName abc = ParameterizedTypeName.get(ClassName.get(HashMap.class), ab, c); ParameterizedTypeName dabc = ParameterizedTypeName.get(ClassName.get(HashMap.class), d, abc); ParameterizedTypeName dabca = ParameterizedTypeName.get(ClassName.get(HashMap.class), dabc, a); ParameterizedTypeName dabcab = ParameterizedTypeName.get(ClassName.get(HashMap.class), dabca, b); ParameterizedTypeName ddabcab = ParameterizedTypeName.get(ClassName.get(HashMap.class), d, dabcab); List<TypeVariableName> vars = extractTypeVars(ddabcab); assertThat(new HashSet<>(vars), is(new HashSet<>(asList(a, b, c, d)))); }