Example #1
0
 @Override
 public JsonType visitTypeVariable(TypeVariable typeVariable, Void o) {
   DeclaredType type = getDeclaredTypeForTypeVariable(typeVariable);
   if (type != null) // null: un-parameterized usage of a generics-having type
   {
     return type.accept(this, o);
   } else {
     return null;
   }
 }
Example #2
0
 private JsonType newJsonType(TypeMirror typeMirror) {
   if (isJsonPrimitive(typeMirror)) {
     return new JsonPrimitive(typeMirror.toString());
   } else if (typeMirror.getKind() == TypeKind.DECLARED) {
     // some sort of object... walk it
     DeclaredType type = (DeclaredType) typeMirror;
     return newJsonType(type, type.getTypeArguments(), new HashSet<DeclaredType>());
   } else if (typeMirror.getKind() == TypeKind.VOID) {
     return null;
   } else if (typeMirror.getKind() == TypeKind.ARRAY) {
     return newJsonType(((ArrayType) typeMirror).getComponentType());
   } else {
     throw new UnsupportedOperationException(typeMirror.toString());
   }
 }
Example #3
0
 /**
  * Create a new JSON type for the given declared type. The caller is responsible for providing a
  * list of concrete types to use to replace parameterized type placeholders.
  */
 private JsonType newJsonType(
     DeclaredType type,
     List<? extends TypeMirror> concreteTypes,
     Collection<DeclaredType> typeRecursionGuard) {
   TypeVisitorImpl visitor = new TypeVisitorImpl(type, concreteTypes, typeRecursionGuard);
   return type.accept(visitor, null);
 }
Example #4
0
    private boolean isInstanceOf(TypeMirror typeMirror, Class type) {
      if (!(typeMirror instanceof DeclaredType)) {
        return false;
      }

      if (typeMirror.toString().startsWith(type.getName())) {
        return true;
      }

      DeclaredType declaredType = (DeclaredType) typeMirror;
      TypeElement typeElement = (TypeElement) declaredType.asElement();
      for (TypeMirror iface : typeElement.getInterfaces()) {
        if (isInstanceOf(iface, type)) {
          return true;
        }
      }
      return isInstanceOf(typeElement.getSuperclass(), type);
    }
Example #5
0
    private void buildTypeContents(JsonObject o, TypeElement element) {
      if ("org.springframework.web.servlet.ModelAndView"
          .equals(element.getQualifiedName().toString())) {
        return;
      }
      if (element.getSuperclass().getKind() != TypeKind.NONE) {
        // an interface's superclass is TypeKind.NONE

        DeclaredType sup = (DeclaredType) element.getSuperclass();
        if (!isJsonPrimitive(sup)) {
          buildTypeContents(o, (TypeElement) sup.asElement());
        }
      }

      for (Element e : element.getEnclosedElements()) {
        if (e instanceof ExecutableElement) {
          addFieldFromBeanMethod(o, (ExecutableElement) e);
        }
      }
    }
Example #6
0
    public TypeVisitorImpl(
        DeclaredType type,
        List<? extends TypeMirror> typeArguments,
        Collection<DeclaredType> typeRecursionGuard) {

      TypeElement elem = (TypeElement) type.asElement();
      _typeRecursionGuard = typeRecursionGuard;
      _type = type;
      List<? extends TypeParameterElement> generics = elem.getTypeParameters();
      for (int i = 0; i < generics.size(); i++) {
        DeclaredType value =
            (typeArguments.isEmpty() || !(typeArguments.get(i) instanceof DeclaredType))
                ? null
                : (DeclaredType) typeArguments.get(i);
        _typeArguments.put(generics.get(i).getSimpleName(), value);
      }
    }
Example #7
0
    @Override
    public JsonType visitDeclared(DeclaredType declaredType, Void o) {
      if (isJsonPrimitive(declaredType)) {
        // 'primitive'-ish things
        return new JsonPrimitive(declaredType.toString());

      } else if (isInstanceOf(declaredType, Collection.class)) {

        if (declaredType.getTypeArguments().size() == 0) {
          return new JsonArray(new JsonPrimitive(Object.class.getName()));
        } else {
          TypeMirror elem = declaredType.getTypeArguments().get(0);
          return new JsonArray(elem.accept(this, o));
        }

      } else if (isInstanceOf(declaredType, Map.class)) {

        if (declaredType.getTypeArguments().size() == 0) {
          return new JsonDict(
              new JsonPrimitive(Object.class.getName()), new JsonPrimitive(Object.class.getName()));
        } else {
          TypeMirror key = declaredType.getTypeArguments().get(0);
          TypeMirror val = declaredType.getTypeArguments().get(1);
          return new JsonDict(key.accept(this, o), val.accept(this, o));
        }

      } else {
        TypeElement element = (TypeElement) declaredType.asElement();
        if (element.getKind() == ElementKind.ENUM) {
          List<String> enumConstants = new ArrayList();
          for (Element e : element.getEnclosedElements()) {
            if (e.getKind() == ElementKind.ENUM_CONSTANT) {
              enumConstants.add(e.toString());
            }
          }
          JsonPrimitive primitive =
              new JsonPrimitive(String.class.getName()); // TODO is this always a string?
          primitive.setRestrictions(enumConstants);
          return primitive;
        } else {
          return buildType(declaredType, element);
        }
      }
    }