Exemplo n.º 1
0
  public InferredMapperFactory(ResultSetMapper mapper) {
    this.mapper = mapper;
    ResolvedType rt = tr.resolve(mapper.getClass());
    List<ResolvedType> rs = rt.typeParametersFor(ResultSetMapper.class);
    if (rs.isEmpty() || rs.get(0).getErasedType().equals(Object.class)) {
      throw new UnsupportedOperationException("Must use a concretely typed ResultSetMapper here");
    }

    maps = rs.get(0).getErasedType();
  }
  private List<JavaTypeDescriptor> extractTypeParameters(ResolvedType resolvedType) {
    if (resolvedType.getTypeParameters().isEmpty()) {
      return Collections.emptyList();
    }

    final List<JavaTypeDescriptor> result =
        CollectionHelper.arrayList(resolvedType.getTypeParameters().size());
    for (ResolvedType typeParameter : resolvedType.getTypeParameters()) {
      result.add(getType(buildName(typeParameter.getErasedSignature())));
    }
    return result;
  }
  @Override
  public com.google.common.base.Optional<Model> modelFor(ModelContext modelContext) {
    ResolvedType propertiesHost = modelContext.alternateFor(modelContext.resolvedType(resolver));
    if (isContainerType(propertiesHost)
        || isMapType(propertiesHost)
        || propertiesHost.getErasedType().isEnum()
        || isBaseType(Types.typeNameFor(propertiesHost.getErasedType()))
        || modelContext.hasSeenBefore(propertiesHost)) {
      LOG.debug(
          "Skipping model of type {} as its either a container type, map, enum or base type, or its already "
              + "been handled",
          resolvedTypeSignature(propertiesHost).or("<null>"));
      return Optional.absent();
    }
    ImmutableMap<String, ModelProperty> propertiesIndex =
        uniqueIndex(properties(modelContext, propertiesHost), byPropertyName());
    LOG.debug(
        "Inferred {} properties. Properties found {}",
        propertiesIndex.size(),
        Joiner.on(", ").join(propertiesIndex.keySet()));

    SortedSet<Map.Entry<String, ModelProperty>> sortedProperties =
        new TreeSet<Map.Entry<String, ModelProperty>>(
            new Comparator<Map.Entry<String, ModelProperty>>() {
              @Override
              public int compare(
                  Map.Entry<String, ModelProperty> e1, Map.Entry<String, ModelProperty> e2) {
                if ((e1.getValue().getPosition().compareTo(e2.getValue().getPosition()) == 0)) {
                  return e1.getValue().getName().compareTo(e2.getValue().getName());
                }
                return new Integer(e1.getValue().getPosition())
                    .compareTo(e2.getValue().getPosition());
              }
            });

    sortedProperties.addAll(propertiesIndex.entrySet());

    Map<String, ModelProperty> properties = new LinkedHashMap<String, ModelProperty>();

    // Map<String, ModelProperty> properties = newTreeMap();
    // properties.putAll(propertiesIndex);
    for (Map.Entry<String, ModelProperty> value : sortedProperties) {
      properties.put(value.getKey(), value.getValue());
    }
    return Optional.of(modelBuilder(propertiesHost, properties, modelContext));
  }
  @Override
  public void execute(RequestMappingContext context) {
    HandlerMethod handlerMethod = context.getHandlerMethod();

    log.debug("Reading models for handlerMethod |{}|", handlerMethod.getMethod().getName());

    Map<String, Model> modelMap = newHashMap();
    SwaggerGlobalSettings swaggerGlobalSettings =
        (SwaggerGlobalSettings) context.get("swaggerGlobalSettings");
    HandlerMethodResolver handlerMethodResolver =
        new HandlerMethodResolver(swaggerGlobalSettings.getTypeResolver());
    ResolvedType modelType =
        ModelUtils.handlerReturnType(swaggerGlobalSettings.getTypeResolver(), handlerMethod);

    ApiOperation apiOperationAnnotation = handlerMethod.getMethodAnnotation(ApiOperation.class);
    if (null != apiOperationAnnotation && Void.class != apiOperationAnnotation.response()) {
      modelType =
          asResolved(swaggerGlobalSettings.getTypeResolver(), apiOperationAnnotation.response());
    }
    if (!swaggerGlobalSettings.getIgnorableParameterTypes().contains(modelType.getErasedType())) {
      ModelContext modelContext = ModelContext.returnValue(modelType);
      markIgnorablesAsHasSeen(swaggerGlobalSettings.getIgnorableParameterTypes(), modelContext);
      Optional<Model> model = modelProvider.modelFor(modelContext);
      if (model.isPresent() && !"void".equals(model.get().name())) {
        log.debug(
            "Swagger generated parameter model id: {}, name: {}, schema: {} models",
            model.get().id(),
            model.get().name());
        modelMap.put(model.get().id(), model.get());
      } else {
        log.debug("Swagger core did not find any models");
      }
      populateDependencies(modelContext, modelMap);
    }
    modelMap.putAll(
        readParametersApiModel(handlerMethodResolver, swaggerGlobalSettings, handlerMethod));

    log.debug(
        "Finished reading models for handlerMethod |{}|", handlerMethod.getMethod().getName());
    context.put("models", modelMap);
  }
 @Override
 public StringBuilder appendFullDescription(StringBuilder sb) {
   sb = _appendClassDescription(sb);
   if (_superClass != null) {
     sb.append(" extends ");
     sb = _superClass.appendBriefDescription(sb);
   }
   // interfaces 'extend' other interfaces...
   int count = _superInterfaces.length;
   if (count > 0) {
     sb.append(" implements ");
     for (int i = 0; i < count; ++i) {
       if (i > 0) {
         sb.append(",");
       }
       sb = _superInterfaces[i].appendBriefDescription(sb);
     }
   }
   return sb;
 }
  public ResolvedObjectType(
      Class<?> erased, TypeBindings bindings, ResolvedType superClass, ResolvedType[] interfaces) {
    super(erased, bindings);
    /* 19-Aug-2014, tatu: bit unclean, but has to do for now.
     *   Problem is, there is no common super-type, nor can we yet
     *   force or coerce recursive types. Rather, they may only get
     *   resolved only slightly after construction. So... need to
     *   keep a reference.
     */
    if (superClass != null) {
      if (!(superClass instanceof ResolvedObjectType)
          && !(superClass instanceof ResolvedRecursiveType)) {
        throw new IllegalArgumentException(
            "Unexpected parent type for "
                + erased.getName()
                + ": "
                + superClass.getClass().getName());
      }
    }

    _superClass = superClass;
    _superInterfaces = (interfaces == null) ? NO_TYPES : interfaces;
    _modifiers = erased.getModifiers();
  }
Exemplo n.º 7
0
 @Override
 public Class<?> getReturnClass() {
   return type.getErasedType();
 }
Exemplo n.º 8
0
 public static boolean isVoid(ResolvedType returnType) {
   return Void.class.equals(returnType.getErasedType())
       || Void.TYPE.equals(returnType.getErasedType());
 }
Exemplo n.º 9
0
 @Override
 public boolean appliesTo(ResolvedType type) {
   return type.getErasedType() == genericType
       && !type.getTypeBindings().isEmpty()
       && boundTypeIndex <= type.getTypeBindings().size() - 1;
 }
Exemplo n.º 10
0
 @Override
 public ResolvedType alternateFor(ResolvedType type) {
   return type.getTypeBindings().getBoundType(boundTypeIndex);
 }