List of usage examples for java.lang.reflect Method getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:net.firejack.platform.core.validation.MatchProcessor.java
@Override public List<ValidationMessage> validate(Method readMethod, String property, Object value, ValidationMode mode) throws RuleValidationException { Match matchAnnotation = readMethod.getAnnotation(Match.class); if (matchAnnotation != null && StringUtils.isNotBlank(matchAnnotation.expression())) { Class<?> returnType = readMethod.getReturnType(); if (returnType == String.class) { Pattern pattern = getCachedPatterns().get(matchAnnotation.expression()); if (pattern == null) { try { pattern = Pattern.compile(matchAnnotation.expression()); getCachedPatterns().put(matchAnnotation.expression(), pattern); } catch (PatternSyntaxException e) { logger.error(e.getMessage(), e); throw new ImproperValidationArgumentException( "Pattern expression should have correct syntax."); }//from w ww . j a v a 2 s . c o m } List<ValidationMessage> messages = null; if (value != null) { String sValue = (String) value; if (StringUtils.isNotBlank(sValue) && !pattern.matcher(sValue).matches()) { messages = new ArrayList<ValidationMessage>(); messages.add(new ValidationMessage(property, matchAnnotation.msgKey(), matchAnnotation.parameterName())); } } return messages; } } return null; }
From source file:cc.kune.core.server.rack.filters.rest.DefaultRESTMethodFinder.java
@Override public RESTMethod findMethod(final String methodName, final Parameters parameters, final Class<?> serviceType) { final RESTServiceDefinition serviceDefinition = getServiceDefinition(serviceType); final Method[] serviceMethods = serviceDefinition.getMethods(); LOG.debug("SERVICE METHODS: " + Arrays.toString(serviceMethods)); for (final Method method : serviceMethods) { LOG.debug("CHECKING: " + method.toString()); if (method.getName().equals(methodName)) { final REST methodAnnotation = method.getAnnotation(REST.class); if (checkParams(methodAnnotation, parameters)) { return new RESTMethod(method, methodAnnotation.params(), parameters, methodAnnotation.format()); }//from www . j ava 2 s. c om } } return null; }
From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiDecorationProvider.java
/** * Load controller mappings at the end of Spring context init *//* w ww.j av a2 s. co m*/ public void onApplicationEvent(ContextRefreshedEvent event) { Map<Class<?>, Method> hrefedBeansControllerMethodMapping = new HashMap<Class<?>, Method>(); Map<String, ?> abstractMethodHandlers = event.getApplicationContext() .getBeansOfType(AbstractHandlerMethodMapping.class); for (Entry<String, ?> abstractMethodHandler : abstractMethodHandlers.entrySet()) { Map<?, HandlerMethod> methods = ((AbstractHandlerMethodMapping<?>) abstractMethodHandler.getValue()) .getHandlerMethods(); for (Entry<?, HandlerMethod> handMet : methods.entrySet()) { Method method = handMet.getValue().getMethod(); Hrefed hrefedAnnotation = method.getAnnotation(Hrefed.class); if (hrefedAnnotation != null) { for (Class<?> hrefedClass : hrefedAnnotation.value()) { if (hrefedBeansControllerMethodMapping.containsKey(hrefedClass)) { throw new RestEventApiRuntimeException("Cannot have " + Hrefed.class.getSimpleName() + " annotation on more that 1 controller handler methods." + " The related Hrefed bean class is: " + hrefedClass + ". The incriminated controller class: " + method.getDeclaringClass().getSimpleName() + ", with method: " + method.getName()); } hrefedBeansControllerMethodMapping.put(hrefedClass, method); } } } } if (hrefedBeansControllerMethodMapping != null && !hrefedBeansControllerMethodMapping.isEmpty()) { RestEventApiDecorationProvider.hrefedBeansControllerMethodMapping.clear(); } RestEventApiDecorationProvider.hrefedBeansControllerMethodMapping .putAll(hrefedBeansControllerMethodMapping); }
From source file:org.querybyexample.jpa.JpaUniqueUtil.java
private Method columnNameToMethod(Class<?> clazz, String columnName) { for (Method method : clazz.getMethods()) { Column column = method.getAnnotation(Column.class); if (column != null && equalsIgnoreCase(columnName, column.name())) { return method; }//w w w .ja v a 2s.co m } return null; }
From source file:asia.gkc.vneedu.authorization.interceptor.AuthorizationInterceptor.java
/** * This implementation always returns {@code true}. * * @param request/* ww w . j ava 2s . co m*/ * @param response * @param handler */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // ? if (!(handler instanceof HandlerMethod)) { return true; } HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); // ??? if (method.getAnnotation(RequireLogin.class) == null) { return true; } // ?? String authorization = request.getHeader(Constants.AUTHORIZATION); // ? if (authorization == null) { return exitWithHeaderError(response); } String[] auths = authorization.split(" "); if (!auths[0].equals("token") || (auths.length > 1 && StringUtils.isEmpty(auths[1]))) { return exitWithHeaderError(response); } // ?Token try { logger.debug(auths[1]); String uuid = IdentityUtil.verifyToken(auths[1]); logger.debug(uuid); request.setAttribute(Constants.USER_ID_IN_REQUEST, uuid); } catch (ExpiredJwtException e) { return exitWithExpiredAuth(response); } catch (Exception e) { return exitWithTokenError(response); } return true; }
From source file:net.firejack.platform.core.validation.GreaterThanProcessor.java
@Override public List<ValidationMessage> validate(Method readMethod, String property, Object value, ValidationMode mode) throws RuleValidationException { GreaterThan greaterThanAnnotation = readMethod.getAnnotation(GreaterThan.class); List<ValidationMessage> messages = null; if (greaterThanAnnotation != null) { messages = new ArrayList<ValidationMessage>(); Class<?> returnType = readMethod.getReturnType(); String parameterName = StringUtils.isBlank(greaterThanAnnotation.parameterName()) ? property : greaterThanAnnotation.parameterName(); if (value != null) { if (returnType.getSuperclass() == Number.class || returnType.isPrimitive()) { boolean checkEquality = greaterThanAnnotation.checkEquality(); Number val = null; if (returnType == Float.class || returnType == float.class) { Float f = (Float) value; if (checkEquality && f < greaterThanAnnotation.floatVal() || !checkEquality && f <= greaterThanAnnotation.floatVal()) { val = greaterThanAnnotation.floatVal(); }//from ww w. ja v a 2 s .c om } else if (returnType == Double.class || returnType == double.class) { Double d = (Double) value; if (checkEquality && d < greaterThanAnnotation.doubleVal() || !checkEquality && d <= greaterThanAnnotation.doubleVal()) { val = greaterThanAnnotation.doubleVal(); } } else { Long longValue = ((Number) value).longValue(); Long rangeValue = ((Integer) greaterThanAnnotation.intVal()).longValue(); if (checkEquality && longValue < rangeValue || !checkEquality && longValue <= rangeValue) { val = greaterThanAnnotation.intVal(); } } if (val != null) { messages.add(new ValidationMessage(property, checkEquality ? greaterThanAnnotation.equalityMsgKey() : greaterThanAnnotation.msgKey(), parameterName, val)); } } } } return messages; }
From source file:com.linkedin.pinot.controller.api.ControllerRestApplication.java
private void attachRoutesForClass(Router router, Class<? extends ServerResource> clazz) { TreeSet<String> pathsOrderedByLength = new TreeSet<String>( ComparatorUtils.chainedComparator(new Comparator<String>() { private IntComparator _intComparator = IntComparators.NATURAL_COMPARATOR; @Override/*w w w .j a v a2s .co m*/ public int compare(String o1, String o2) { return _intComparator.compare(o1.length(), o2.length()); } }, ComparatorUtils.NATURAL_COMPARATOR)); for (Method method : clazz.getDeclaredMethods()) { Annotation annotationInstance = method.getAnnotation(Paths.class); if (annotationInstance != null) { pathsOrderedByLength.addAll(Arrays.asList(((Paths) annotationInstance).value())); } } for (String routePath : pathsOrderedByLength) { LOGGER.info("Attaching route {} -> {}", routePath, clazz.getSimpleName()); router.attach(routePath, clazz); } }
From source file:io.ingenieux.lambada.maven.LambadaGenerateMojo.java
private LambadaFunctionDefinition extractFunctionDefinitions(Method m) throws Exception { LambadaFunction lF = m.getAnnotation(LambadaFunction.class); final LambadaFunctionDefinition result = new LambadaFunctionDefinition(); final String name = defaultIfBlank(lF.name(), m.getName()); result.setName(name); // #1 final String handler = m.getDeclaringClass().getCanonicalName() + "::" + m.getName(); result.setAlias(lF.alias());//from ww w . ja va 2 s . c o m result.setHandler(handler); result.setMemorySize(lF.memorySize()); // #2 if (isNotBlank(lF.role())) { result.setRole(lF.role()); // #3 } result.setTimeout(lF.timeout()); // #4 if (isNotBlank(lF.description())) { result.setDescription(lF.description()); // #5 } result.setBindings(asList(lF.bindings())); if (null != lF.api() && 1 == lF.api().length) { ApiGateway apiGatewayAnn = lF.api()[0]; APIGatewayDefinition def = new APIGatewayDefinition(); def.setMethodType(APIGatewayDefinition.MethodType.valueOf(apiGatewayAnn.method().name())); def.setPath(apiGatewayAnn.path()); def.setTemplate(apiGatewayAnn.template()); def.setCorsEnabled(apiGatewayAnn.corsEnabled()); def.setPatches(compilePatches(apiGatewayAnn.patches())); result.setApi(def); } return result; }
From source file:ch.digitalfondue.npjt.QueryFactory.java
private QueryTypeAndQuery extractQueryAnnotation(Class<?> clazz, Method method) { Query q = method.getAnnotation(Query.class); QueriesOverride qs = method.getAnnotation(QueriesOverride.class); // only one @Query annotation, thus we return the value without checking the database if (qs == null) { return new QueryTypeAndQuery(q.type(), q.value(), q.mapper()); }//w w w . j a v a 2 s. c o m for (QueryOverride query : qs.value()) { if (query.db().equals(activeDb)) { return new QueryTypeAndQuery(q.type(), query.value(), query.mapper()); } } return new QueryTypeAndQuery(q.type(), q.value(), q.mapper()); }
From source file:net.sf.jasperreports.export.PropertiesDefaultsConfigurationFactory.java
/** * //from w w w . j ava2 s . com */ protected Object getPropertyValue(Method method) { Object value = null; ExporterProperty exporterProperty = method.getAnnotation(ExporterProperty.class); if (exporterProperty != null) { value = getPropertyValue(jasperReportsContext, exporterProperty, method.getReturnType()); } return value; }