fathom.utils.Util.toString()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(16.0k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中fathom.utils.Util.toString()方法的一些代码示例,展示了Util.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.toString()方法的具体详情如下:
包路径:fathom.utils.Util
类名称:Util
方法名:toString

Util.toString介绍

暂无

代码示例

代码示例来源:origin: gitblit/fathom

public static String toString(Method method) {
  return toString(method.getDeclaringClass(), method.getName());
}

代码示例来源:origin: gitblit/fathom

/**
 * Validates that the declared content-types can actually be generated by Fathom.
 *
 * @param fathomContentTypes
 */
protected void validateProduces(Collection<String> fathomContentTypes) {
  Set<String> ignoreProduces = new TreeSet<>();
  ignoreProduces.add(Produces.TEXT);
  ignoreProduces.add(Produces.HTML);
  ignoreProduces.add(Produces.XHTML);
  for (String produces : declaredProduces) {
    if (ignoreProduces.contains(produces)) {
      continue;
    }
    if (!fathomContentTypes.contains(produces)) {
      throw new FatalException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!",
          Util.toString(method), Produces.class.getSimpleName(), produces);
    }
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

/**
 * Validates that the declared content-types can actually be generated by Fathom.
 *
 * @param fathomContentTypes
 */
protected void validateProduces(Collection<String> fathomContentTypes) {
  Set<String> ignoreProduces = new TreeSet<>();
  ignoreProduces.add(Produces.TEXT);
  ignoreProduces.add(Produces.HTML);
  ignoreProduces.add(Produces.XHTML);
  for (String produces : declaredProduces) {
    if (ignoreProduces.contains(produces)) {
      continue;
    }
    if (!fathomContentTypes.contains(produces)) {
      throw new FatalException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!",
          Util.toString(method), Produces.class.getSimpleName(), produces);
    }
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-xmlrpc

Object invokeMethod(Method method, List<Object> methodParameters) throws Exception {
  Object[] argValues = null;
  if (methodParameters != null) {
    argValues = new Object[methodParameters.size()];
    for (int i = 0; i < methodParameters.size(); i++) {
      argValues[i] = methodParameters.get(i);
    }
  }
  try {
    method.setAccessible(true);
    return method.invoke(invokeTarget, argValues);
  } catch (IllegalAccessException | IllegalArgumentException e) {
    throw e;
  } catch (InvocationTargetException e) {
    Throwable t = e.getTargetException();
    log.error("Failed to execute {}", Util.toString(method), t);
    throw new FathomException(t.getMessage());
  }
}

代码示例来源:origin: gitblit/fathom

public static Class<?> getParameterGenericType(Method method, Parameter parameter) {
  Type parameterType = parameter.getParameterizedType();
  if (!ParameterizedType.class.isAssignableFrom(parameterType.getClass())) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        parameter.getType().getName(), Util.toString(method));
  }
  ParameterizedType parameterizedType = (ParameterizedType) parameterType;
  try {
    Class<?> genericClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    return genericClass;
  } catch (ClassCastException e) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        parameter.getType().getName(), Util.toString(method));
  }
}

代码示例来源:origin: gitblit/fathom

Object invokeMethod(Method method, List<Object> methodParameters) throws Exception {
  Object[] argValues = null;
  if (methodParameters != null) {
    argValues = new Object[methodParameters.size()];
    for (int i = 0; i < methodParameters.size(); i++) {
      argValues[i] = methodParameters.get(i);
    }
  }
  try {
    method.setAccessible(true);
    return method.invoke(invokeTarget, argValues);
  } catch (IllegalAccessException | IllegalArgumentException e) {
    throw e;
  } catch (InvocationTargetException e) {
    Throwable t = e.getTargetException();
    log.error("Failed to execute {}", Util.toString(method), t);
    throw new FathomException(t.getMessage());
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

/**
 * Finds the named controller method.
 *
 * @param controllerClass
 * @param name
 * @return the controller method or null
 */
protected Method findMethod(Class<?> controllerClass, String name) {
  // identify first method which matches the name
  Method controllerMethod = null;
  for (Method method : controllerClass.getMethods()) {
    if (method.getName().equals(name)) {
      if (controllerMethod == null) {
        controllerMethod = method;
      } else {
        throw new FatalException("Found overloaded controller method '{}'. Method names must be unique!",
            Util.toString(method));
      }
    }
  }
  return controllerMethod;
}

代码示例来源:origin: gitblit/fathom

/**
 * Finds the named controller method.
 *
 * @param controllerClass
 * @param name
 * @return the controller method or null
 */
protected Method findMethod(Class<?> controllerClass, String name) {
  // identify first method which matches the name
  Method controllerMethod = null;
  for (Method method : controllerClass.getMethods()) {
    if (method.getName().equals(name)) {
      if (controllerMethod == null) {
        controllerMethod = method;
      } else {
        throw new FatalException("Found overloaded controller method '{}'. Method names must be unique!",
            Util.toString(method));
      }
    }
  }
  return controllerMethod;
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

/**
 * Validates the declared Returns of the controller method.  If the controller method returns an object then
 * it must also declare a successful @Return with a status code in the 200 range.
 */
protected void validateDeclaredReturns() {
  boolean returnsObject = void.class != method.getReturnType();
  if (returnsObject) {
    for (Return declaredReturn : declaredReturns) {
      if (declaredReturn.code() >= 200 && declaredReturn.code() < 300) {
        return;
      }
    }
    throw new FatalException("{} returns an object but does not declare a successful @{}(code=200, onResult={}.class)",
        Util.toString(method), Return.class.getSimpleName(), method.getReturnType().getSimpleName());
  }
}

代码示例来源:origin: gitblit/fathom

/**
 * Validates the declared Returns of the controller method.  If the controller method returns an object then
 * it must also declare a successful @Return with a status code in the 200 range.
 */
protected void validateDeclaredReturns() {
  boolean returnsObject = void.class != method.getReturnType();
  if (returnsObject) {
    for (Return declaredReturn : declaredReturns) {
      if (declaredReturn.code() >= 200 && declaredReturn.code() < 300) {
        return;
      }
    }
    throw new FatalException("{} returns an object but does not declare a successful @{}(code=200, onResult={}.class)",
        Util.toString(method), Return.class.getSimpleName(), method.getReturnType().getSimpleName());
  }
}

代码示例来源:origin: gitblit/fathom

protected Class<?> getParameterGenericType(Parameter parameter) {
  Type parameterType = parameter.getParameterizedType();
  if (!ParameterizedType.class.isAssignableFrom(parameterType.getClass())) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        ControllerUtil.getParameterName(parameter), Util.toString(method));
  }
  ParameterizedType parameterizedType = (ParameterizedType) parameterType;
  try {
    Class<?> genericClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    return genericClass;
  } catch (ClassCastException e) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        ControllerUtil.getParameterName(parameter), Util.toString(method));
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

protected Class<?> getParameterGenericType(Parameter parameter) {
  Type parameterType = parameter.getParameterizedType();
  if (!ParameterizedType.class.isAssignableFrom(parameterType.getClass())) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        ControllerUtil.getParameterName(parameter), Util.toString(method));
  }
  ParameterizedType parameterizedType = (ParameterizedType) parameterType;
  try {
    Class<?> genericClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    return genericClass;
  } catch (ClassCastException e) {
    throw new FathomException("Please specify a generic parameter type for '{}' of '{}'",
        ControllerUtil.getParameterName(parameter), Util.toString(method));
  }
}

代码示例来源:origin: gitblit/fathom

/**
 * Validate that the parameters specified in the uri pattern are declared in the method signature.
 *
 * @param uriPattern
 * @throws FatalException if the controller method does not declare all named uri parameters
 */
public void validateMethodArgs(String uriPattern) {
  Set<String> namedParameters = new LinkedHashSet<>();
  for (ArgumentExtractor extractor : extractors) {
    if (extractor instanceof NamedExtractor) {
      NamedExtractor namedExtractor = (NamedExtractor) extractor;
      namedParameters.add(namedExtractor.getName());
    }
  }
  // validate the url specification and method signature agree on required parameters
  List<String> requiredParameters = getParameterNames(uriPattern);
  if (!namedParameters.containsAll(requiredParameters)) {
    throw new FatalException("Controller method '{}' declares parameters {} but the URL specification requires {}",
        Util.toString(method), namedParameters, requiredParameters);
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

/**
 * Validate that the parameters specified in the uri pattern are declared in the method signature.
 *
 * @param uriPattern
 * @throws FatalException if the controller method does not declare all named uri parameters
 */
public void validateMethodArgs(String uriPattern) {
  Set<String> namedParameters = new LinkedHashSet<>();
  for (ArgumentExtractor extractor : extractors) {
    if (extractor instanceof NamedExtractor) {
      NamedExtractor namedExtractor = (NamedExtractor) extractor;
      namedParameters.add(namedExtractor.getName());
    }
  }
  // validate the url specification and method signature agree on required parameters
  List<String> requiredParameters = getParameterNames(uriPattern);
  if (!namedParameters.containsAll(requiredParameters)) {
    throw new FatalException("Controller method '{}' declares parameters {} but the URL specification requires {}",
        Util.toString(method), namedParameters, requiredParameters);
  }
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest-swagger

protected String getNotes(Method method) {
  if (method.isAnnotationPresent(ApiNotes.class)) {
    ApiNotes apiNotes = method.getAnnotation(ApiNotes.class);
    String resource = "classpath:swagger/" + method.getDeclaringClass().getName().replace('.', '/')
        + "/" + method.getName() + ".md";
    if (!Strings.isNullOrEmpty(apiNotes.value())) {
      resource = apiNotes.value();
    }
    if (resource.startsWith("classpath:")) {
      String content = ClassUtil.loadStringResource(resource);
      if (Strings.isNullOrEmpty(content)) {
        log.error("'{}' specifies @{} but '{}' was not found!",
            Util.toString(method), ApiNotes.class.getSimpleName(), resource);
      }
      return content;
    } else {
      String notes = translate(apiNotes.key(), apiNotes.value());
      return notes;
    }
  }
  return null;
}

代码示例来源:origin: gitblit/fathom

protected String getNotes(Method method) {
  if (method.isAnnotationPresent(ApiNotes.class)) {
    ApiNotes apiNotes = method.getAnnotation(ApiNotes.class);
    String resource = "classpath:swagger/" + method.getDeclaringClass().getName().replace('.', '/')
        + "/" + method.getName() + ".md";
    if (!Strings.isNullOrEmpty(apiNotes.value())) {
      resource = apiNotes.value();
    }
    if (resource.startsWith("classpath:")) {
      String content = ClassUtil.loadStringResource(resource);
      if (Strings.isNullOrEmpty(content)) {
        log.error("'{}' specifies @{} but '{}' was not found!",
            Util.toString(method), ApiNotes.class.getSimpleName(), resource);
      }
      return content;
    } else {
      String notes = translate(apiNotes.key(), apiNotes.value());
      return notes;
    }
  }
  return null;
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

public ControllerHandler(Injector injector, Class<? extends Controller> controllerClass, String methodName) {
  if (controllerClass.isAnnotationPresent(Singleton.class)
      || controllerClass.isAnnotationPresent(javax.inject.Singleton.class)) {
    throw new FathomException("Controller '{}' may not be annotated as a Singleton!", controllerClass.getName());
  }
  this.controllerClass = controllerClass;
  this.controllerProvider = injector.getProvider(controllerClass);
  this.method = findMethod(controllerClass, methodName);
  this.messages = injector.getInstance(Messages.class);
  Preconditions.checkNotNull(method, "Failed to find method '%s'", Util.toString(controllerClass, methodName));
  log.trace("Obtained method for '{}'", Util.toString(method));
  this.routeInterceptors = new ArrayList<>();
  for (Class<? extends RouteHandler<Context>> handlerClass : ControllerUtil.collectRouteInterceptors(method)) {
    RouteHandler<Context> handler = injector.getInstance(handlerClass);
    this.routeInterceptors.add(handler);
  }
  ContentTypeEngines engines = injector.getInstance(ContentTypeEngines.class);
  this.declaredConsumes = ControllerUtil.getConsumes(method);
  validateConsumes(engines.getContentTypes());
  this.declaredProduces = ControllerUtil.getProduces(method);
  validateProduces(engines.getContentTypes());
  this.declaredReturns = ControllerUtil.getReturns(method);
  validateDeclaredReturns();
  this.contentTypeSuffixes = configureContentTypeSuffixes(engines);
  configureMethodArgs(injector);
  this.isNoCache = ClassUtil.getAnnotation(method, NoCache.class) != null;
}

代码示例来源:origin: gitblit/fathom

public ControllerHandler(Injector injector, Class<? extends Controller> controllerClass, String methodName) {
  if (controllerClass.isAnnotationPresent(Singleton.class)
      || controllerClass.isAnnotationPresent(javax.inject.Singleton.class)) {
    throw new FathomException("Controller '{}' may not be annotated as a Singleton!", controllerClass.getName());
  }
  this.controllerClass = controllerClass;
  this.controllerProvider = injector.getProvider(controllerClass);
  this.method = findMethod(controllerClass, methodName);
  this.messages = injector.getInstance(Messages.class);
  Preconditions.checkNotNull(method, "Failed to find method '%s'", Util.toString(controllerClass, methodName));
  log.trace("Obtained method for '{}'", Util.toString(method));
  this.routeInterceptors = new ArrayList<>();
  for (Class<? extends RouteHandler<Context>> handlerClass : ControllerUtil.collectRouteInterceptors(method)) {
    RouteHandler<Context> handler = injector.getInstance(handlerClass);
    this.routeInterceptors.add(handler);
  }
  ContentTypeEngines engines = injector.getInstance(ContentTypeEngines.class);
  this.declaredConsumes = ControllerUtil.getConsumes(method);
  validateConsumes(engines.getContentTypes());
  this.declaredProduces = ControllerUtil.getProduces(method);
  validateProduces(engines.getContentTypes());
  this.declaredReturns = ControllerUtil.getReturns(method);
  validateDeclaredReturns();
  this.contentTypeSuffixes = configureContentTypeSuffixes(engines);
  configureMethodArgs(injector);
  this.isNoCache = ClassUtil.getAnnotation(method, NoCache.class) != null;
}

代码示例来源:origin: com.gitblit.fathom/fathom-rest

protected void handleDeclaredThrownException(Exception e, Method method, Context context) {
  Class<? extends Exception> exceptionClass = e.getClass();
  for (Return declaredReturn : declaredReturns) {
    if (exceptionClass.isAssignableFrom(declaredReturn.onResult())) {
      context.status(declaredReturn.code());
      // prefer declared message to exception message
      String message = Strings.isNullOrEmpty(declaredReturn.description()) ? e.getMessage() : declaredReturn.description();
      if (!Strings.isNullOrEmpty(declaredReturn.descriptionKey())) {
        // retrieve localized message, fallback to declared message
        message = messages.getWithDefault(declaredReturn.descriptionKey(), message, context);
      }
      if (!Strings.isNullOrEmpty(message)) {
        context.setLocal("message", message);
      }
      validateResponseHeaders(declaredReturn, context);
      log.warn("Handling declared return exception '{}' for '{}'", e.getMessage(), Util.toString(method));
      return;
    }
  }
  if (e instanceof RuntimeException) {
    // pass-through the thrown exception
    throw (RuntimeException) e;
  }
  // undeclared exception, wrap & throw
  throw new FathomException(e);
}

代码示例来源:origin: gitblit/fathom

protected void handleDeclaredThrownException(Exception e, Method method, Context context) {
  Class<? extends Exception> exceptionClass = e.getClass();
  for (Return declaredReturn : declaredReturns) {
    if (exceptionClass.isAssignableFrom(declaredReturn.onResult())) {
      context.status(declaredReturn.code());
      // prefer declared message to exception message
      String message = Strings.isNullOrEmpty(declaredReturn.description()) ? e.getMessage() : declaredReturn.description();
      if (!Strings.isNullOrEmpty(declaredReturn.descriptionKey())) {
        // retrieve localized message, fallback to declared message
        message = messages.getWithDefault(declaredReturn.descriptionKey(), message, context);
      }
      if (!Strings.isNullOrEmpty(message)) {
        context.setLocal("message", message);
      }
      validateResponseHeaders(declaredReturn, context);
      log.warn("Handling declared return exception '{}' for '{}'", e.getMessage(), Util.toString(method));
      return;
    }
  }
  if (e instanceof RuntimeException) {
    // pass-through the thrown exception
    throw (RuntimeException) e;
  }
  // undeclared exception, wrap & throw
  throw new FathomException(e);
}

相关文章