Example usage for java.lang.reflect Parameter isAnnotationPresent

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

Introduction

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

Prototype

default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Document

Returns true if an annotation for the specified type is present on this element, else false.

Usage

From source file:telegram.polling.BotApiProxy.java

@Override
public Object invoke(Object o, Method method, Object[] os) throws Throwable {
    ArrayList<NameValuePair> nvps = new ArrayList<>();
    for (int i = 0; i < method.getParameterCount(); i++) {
        Parameter parameter = method.getParameters()[i];
        if (os[i] != null) {
            String name;//from w w  w .  jav  a  2  s  . c om
            if (parameter.isAnnotationPresent(Param.class)) {
                name = parameter.getDeclaredAnnotation(Param.class).name();
            } else {
                name = parameter.getName();
            }
            nvps.add(new BasicNameValuePair(name, os[i].toString()));
        }
    }
    return callApiMethod(method.getName(), nvps, method.getReturnType());
}

From source file:alba.solr.docvalues.DynamicDocValuesHelper.java

public Object eval(int doc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    // TODO Auto-generated method stub

    /*if (doc < 0 || doc > this.readerContext.reader().maxDoc()) {
       return null;//ww w. jav  a2  s  .c  o  m
    }*/

    Map<String, Object> params = new HashMap<String, Object>();

    for (String s : args.keySet()) {
        if (args.get(s).startsWith("\"")) {
            params.put(s, args.get(s));
        } else if (NumberUtils.isNumber(args.get(s))) {
            Object objVal;

            try {
                objVal = Long.parseLong(args.get(s));
            } catch (NumberFormatException nfe1) {
                try {
                    objVal = Float.parseFloat(args.get(s));
                } catch (NumberFormatException nfe2) {
                    objVal = s;
                }

            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        } else if ("false".equals(args.get(s).toLowerCase())) {
            params.put(s, false);
        } else if ("true".equals(args.get(s).toLowerCase())) {
            params.put(s, true);
        } else {
            SchemaField f = fp.getReq().getSchema().getField(args.get(s));

            ValueSource vs = f.getType().getValueSource(f, fp);

            Object objVal = null;

            try {
                objVal = vs.getValues(this.context, this.readerContext).longVal(doc);
                params.put(s, objVal);
            } catch (IOException | UnsupportedOperationException e) {
                // TODO Auto-generated catch block
                // TODO Log properly

                try {
                    objVal = vs.getValues(this.context, this.readerContext).floatVal(doc);
                } catch (IOException | UnsupportedOperationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                    try {
                        objVal = vs.getValues(this.context, this.readerContext).strVal(doc);
                    } catch (IOException | UnsupportedOperationException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                }

                logger.error("error converting values ", e);
            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        }

    }

    CallableFunction cf = functions.get(this.functionName);

    if (cf == null) {
        logger.error("unable to get function " + this.functionName);
    }

    if (cf != null) {
        List<Object> fnParams = new ArrayList<Object>();
        Parameter[] methodParameters = cf.getMethod().getParameters();

        //TODO spostare quanto pi codice possibile in fase di inizializzazione
        for (Parameter p : methodParameters) {
            if (p.isAnnotationPresent(Param.class)) {
                Param paramAnnotation = p.getAnnotation(Param.class);
                fnParams.add(params.get(paramAnnotation.name()));
            }
        }

        return cf.getMethod().invoke(cf.getInstance(), fnParams.toArray());
    } else {
        return null;
    }

}

From source file:alba.solr.docvalues.FloatFunction.java

@Override
public float floatVal(int doc) {
    // TODO Auto-generated method stub
    Map<String, Object> params = new HashMap<String, Object>();

    for (String s : args.keySet()) {
        if (args.get(s).startsWith("\"")) {
            params.put(s, args.get(s));// w  w  w  .  j  a va  2s .  c  o  m
        } else if (NumberUtils.isNumber(args.get(s))) {
            Object objVal;

            try {
                objVal = Long.parseLong(args.get(s));
            } catch (NumberFormatException nfe1) {
                try {
                    objVal = Float.parseFloat(args.get(s));
                } catch (NumberFormatException nfe2) {
                    objVal = s;
                }

            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        } else if ("false".equals(args.get(s).toLowerCase())) {
            params.put(s, false);
        } else if ("true".equals(args.get(s).toLowerCase())) {
            params.put(s, true);
        } else {
            SchemaField f = fp.getReq().getSchema().getField(args.get(s));

            ValueSource vs = f.getType().getValueSource(f, fp);

            Object objVal = null;

            try {
                objVal = vs.getValues(this.context, this.readerContext).longVal(doc);
                params.put(s, objVal);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // TODO Log properly

                try {
                    objVal = vs.getValues(this.context, this.readerContext).floatVal(doc);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                    try {
                        objVal = vs.getValues(this.context, this.readerContext).strVal(doc);
                    } catch (IOException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                }

                e.printStackTrace();
            }

            if (objVal != null) {
                params.put(s, objVal);
            } else {
                params.put(s, "N/A");
            }

        }

    }

    CallableFunction cf = functions.get(this.functionName);

    if (cf == null) {
        logger.error("unable to get function " + this.functionName);
    }

    if (cf != null) {
        List<Object> fnParams = new ArrayList<Object>();
        Parameter[] methodParameters = cf.getMethod().getParameters();

        //TODO spostare quanto pi codice possibile in fase di inizializzazione
        for (Parameter p : methodParameters) {
            if (p.isAnnotationPresent(Param.class)) {
                Param paramAnnotation = p.getAnnotation(Param.class);
                fnParams.add(params.get(paramAnnotation.name()));
            }
        }

        try {
            return (float) cf.getMethod().invoke(cf.getInstance(), fnParams.toArray());
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            logger.error("errore mentre chiamavo " + cf.getMethod().getName(), e);
            for (Object o : fnParams) {
                logger.error("p " + o.toString());
            }
        }
    }

    return -1f;

}

From source file:com.phoenixnap.oss.ramlapisync.parser.SpringMvcResourceParser.java

protected boolean isPathParameter(Parameter param) {
    return param.isAnnotationPresent(PathVariable.class);
}

From source file:com.phoenixnap.oss.ramlapisync.parser.SpringMvcResourceParser.java

protected boolean isNonPathParameter(Parameter param) {
    return param.isAnnotationPresent(RequestParam.class) || param.isAnnotationPresent(RequestBody.class);
}

From source file:com.phoenixnap.oss.ramlapisync.parser.SpringMvcResourceParser.java

protected boolean isQueryParameter(Parameter param) {
    return param.isAnnotationPresent(RequestParam.class);
}

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

/**
 * Resolve parameter.//  w w  w .  j  a  v  a 2s  . 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.// w  w  w .  j a v a2s  .  c  om
 *
 * @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;
}