javax.lang.model.util.SimpleTypeVisitor6 Java Examples

The following examples show how to use javax.lang.model.util.SimpleTypeVisitor6. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoreTypes.java    From doma with Apache License 2.0 5 votes vote down vote up
public ArrayType toArrayType(TypeMirror typeMirror) {
  assertNotNull(typeMirror);
  return typeMirror.accept(
      new SimpleTypeVisitor6<ArrayType, Void>() {

        public ArrayType visitArray(ArrayType t, Void p) {
          return t;
        }
      },
      null);
}
 
Example #2
Source File: SpecModelUtils.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * This method will "expand" the typeArguments of the given type, only if the type is a {@link
 * ClassNames#DIFF} or a {@link java.util.Collection}. Otherwise the typeArguments won't be
 * traversed and recorded.
 */
public static TypeSpec generateTypeSpec(TypeMirror type) {
  final TypeSpec defaultValue =
      new TypeSpec(safelyGetTypeName(type), type.getKind() != TypeKind.ERROR);

  return type.accept(
      new SimpleTypeVisitor6<TypeSpec, Void>(defaultValue) {
        @Override
        public TypeSpec visitDeclared(DeclaredType t, Void aVoid) {
          final TypeElement typeElement = (TypeElement) t.asElement();
          final String qualifiedName = typeElement.getQualifiedName().toString();
          final Supplier<TypeSpec> superclass =
              new SimpleMemoizingSupplier<>(
                  () -> {
                    final TypeMirror mirror = typeElement.getSuperclass();
                    return mirror.getKind() != TypeKind.DECLARED
                        ? null
                        : generateTypeSpec(mirror);
                  });

          final Supplier<ImmutableList<TypeSpec>> superinterfaces =
              new SimpleMemoizingSupplier<>(
                  () -> {
                    final List<? extends TypeMirror> mirrors = typeElement.getInterfaces();
                    return ImmutableList.copyOf(
                        mirrors != null && !mirrors.isEmpty()
                            ? mirrors.stream()
                                .filter(mirror -> mirror.getKind() == TypeKind.DECLARED)
                                .map(SpecModelUtils::generateTypeSpec)
                                .collect(Collectors.toList())
                            : Collections.emptyList());
                  });

          final Supplier<ImmutableList<TypeSpec>> typeArguments =
              new SimpleMemoizingSupplier<>(
                  () ->
                      ImmutableList.copyOf(
                          ClassName.bestGuess(qualifiedName).equals(ClassNames.DIFF)
                                  || superinterfaces.get().stream()
                                      .anyMatch(
                                          typeSpec ->
                                              typeSpec.isSubInterface(ClassNames.COLLECTION))
                              ? ((DeclaredType) type)
                                  .getTypeArguments().stream()
                                      .map(SpecModelUtils::generateTypeSpec)
                                      .collect(Collectors.toList())
                              : Collections.emptyList()));

          return new TypeSpec.DeclaredTypeSpec(
              safelyGetTypeName(t), qualifiedName, superclass, superinterfaces, typeArguments);
        }
      },
      null);
}