Example usage for java.lang.reflect Parameter getClass

List of usage examples for java.lang.reflect Parameter getClass

Introduction

In this page you can find the example usage for java.lang.reflect Parameter getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.jspare.forvertx.web.handler.DefaultHandler.java

/**
 * Resolve parameter.//  ww  w .j  av a2 s .  c o m
 *
 * @param parameter
 *            the parameter
 * @param routingContext
 *            the routing context
 * @return the object
 */
@SuppressWarnings("unchecked")
protected Object resolveParameter(Parameter parameter, RoutingContext routingContext) {

    if (parameter.getType().equals(RoutingContext.class)) {

        return routingContext;
    }
    if (parameter.getType().equals(HttpServerRequest.class)) {

        return routingContext.request();
    }

    if (parameter.getType().equals(HttpServerResponse.class)) {

        return routingContext.response();
    }
    if (StringUtils.isNotEmpty(routingContext.request().getParam(parameter.getName()))) {

        return routingContext.request().getParam(parameter.getName());
    }

    if (parameter.isAnnotationPresent(ArrayModel.class)) {

        ArrayModel am = parameter.getAnnotation(ArrayModel.class);
        Class<? extends Collection<?>> collection = (Class<? extends Collection<?>>) am.collectionClass();
        Class<?> clazz = am.value();
        return ArrayModelParser.toList(routingContext.getBody().toString(), collection, clazz);
    }

    if (parameter.isAnnotationPresent(MapModel.class)) {

        MapModel mm = parameter.getAnnotation(MapModel.class);
        Class<?> mapClazz = mm.mapClass();
        Class<?> key = mm.key();
        Class<?> value = mm.value();
        return MapModelParser.toMap(routingContext.getBody().toString(), mapClazz, key, value);
    }

    if (parameter.getType().getPackage().getName().endsWith(".model")
            || parameter.getType().isAnnotationPresent(Model.class)
            || parameter.isAnnotationPresent(Model.class)) {

        try {
            if (routingContext.getBody() == null) {

                return null;
            }
            return my(Json.class).fromJSON(routingContext.getBody().toString(), parameter.getType());
        } catch (SerializationException e) {

            log.debug("Invalid content of body for class [{}] on parameter [{}]", parameter.getClass(),
                    parameter.getName());
            return null;
        }
    }
    if (parameter.isAnnotationPresent(org.jspare.forvertx.web.mapping.handling.Parameter.class)) {

        String parameterName = parameter.getAnnotation(org.jspare.forvertx.web.mapping.handling.Parameter.class)
                .value();
        // Test types
        Type typeOfParameter = parameter.getType();
        if (typeOfParameter.equals(Integer.class)) {
            return Integer.parseInt(routingContext.request().getParam(parameterName));
        }
        if (typeOfParameter.equals(Double.class)) {
            return Double.parseDouble(routingContext.request().getParam(parameterName));
        }
        if (typeOfParameter.equals(Long.class)) {
            return Long.parseLong(routingContext.request().getParam(parameterName));
        }
        return routingContext.request().getParam(parameterName);
    }
    if (parameter.isAnnotationPresent(org.jspare.forvertx.web.mapping.handling.Header.class)) {

        String headerName = parameter.getAnnotation(org.jspare.forvertx.web.mapping.handling.Header.class)
                .value();
        return routingContext.request().getHeader(headerName);
    }

    return null;
}

From source file:org.jspare.server.transaction.TransactionExecutorImpl.java

/**
 * Resolve parameter.//from  w  ww.j a  v  a  2  s  .c o m
 *
 * @param parameter
 *            the parameter
 * @param request
 *            the request
 * @param response
 *            the response
 * @return the object
 */
private Object resolveParameter(Parameter parameter, Request request, Response response) {

    if (parameter.getType().equals(Request.class)) {

        return request;
    }
    if (parameter.getType().equals(Response.class)) {

        return response;
    }
    if (!StringUtils.isEmpty(request.getParameter(parameter.getName()))) {

        return request.getParameter(parameter.getName());
    }

    if (parameter.getType().getPackage().getName().endsWith(".model")
            || parameter.getType().isAnnotationPresent(Model.class)
            || parameter.isAnnotationPresent(Model.class)) {

        try {

            return my(Serializer.class).fromJSON(String.valueOf(request.getEntity().get()),
                    parameter.getType());
        } catch (SerializationException e) {

            log.warn("Invalid content of entity for class [{}] on parameter [{}]", parameter.getClass(),
                    parameter.getName());
            return null;
        }
    }
    if (parameter.isAnnotationPresent(org.jspare.server.mapping.Parameter.class)) {

        String parameterName = parameter.getAnnotation(org.jspare.server.mapping.Parameter.class).value();
        return request.getParameter(parameterName);
    }

    return null;
}