List of usage examples for org.springframework.core.annotation AnnotationUtils findAnnotation
@Nullable public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType)
From source file:tv.arte.resteventapi.web.errors.GlobalDefaultExceptionHandler.java
/** * Handle all exceptions of type {@link ImageRepositoryException} thrown by (or passing trough) the Controller's layer * /* w ww. j a v a2s . co m*/ * @param response The HttpServletResponse * @param e Thrown NoResultInApiException * @return * @throws Exception */ @ExceptionHandler(value = NoResultInApiException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public ModelAndView noResultInApiExceptionHandler(HttpServletResponse response, NoResultInApiException e) throws Exception { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } RestEventApiStandardResponse<RestEventApiMessage> restEventApiStandardResponse = new RestEventApiStandardResponse<RestEventApiMessage>(); restEventApiStandardResponse.addError(RestEventApiError.PRE_DEFINED.RIA_ERR_G_SEARCH_NO_RESULTS); ModelAndView mav = new ModelAndView(); mav.addObject(restEventApiStandardResponse); mav.setView(restEventApiDefaultErrorView); return mav; }
From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java
/** * Convenience method for finding the first annotated method in a given class. * //w w w . j a va 2 s .c o m * @param clazz the class to reflect * @param annotationClasses the annotations to look for * @return the first method annotated with the provided list */ @SuppressWarnings("unchecked") public Method findAnnotatedMethod(Class<?> clazz, Class<? extends Annotation>... annotationClasses) { for (Method method : clazz.getDeclaredMethods()) { for (Class<? extends Annotation> annotationClass : annotationClasses) { if (AnnotationUtils.findAnnotation(method, annotationClass) != null) { return method; } } } return null; }
From source file:de.escalon.hypermedia.spring.AffordanceBuilderFactory.java
private Cardinality getCardinality(Method invokedMethod, RequestMethod httpMethod, Type genericReturnType) { Cardinality cardinality;//w ww . j a v a 2 s.com ResourceHandler resourceHandlerAnn = AnnotationUtils.findAnnotation(invokedMethod, ResourceHandler.class); if (resourceHandlerAnn != null) { cardinality = resourceHandlerAnn.value(); } else { if (RequestMethod.POST == httpMethod || containsCollection(genericReturnType)) { cardinality = Cardinality.COLLECTION; } else { cardinality = Cardinality.SINGLE; } } return cardinality; }
From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java
/** * Convenience method for finding the first annotated method in a given class. * /*from w w w .ja v a2 s.c om*/ * @param clazz the class to reflect * @param annotationClasses the annotations to look for * @return the methods annotated with the provided list */ @SuppressWarnings("unchecked") public List<Method> findAnnotatedMethods(Class<?> clazz, Class<? extends Annotation>... annotationClasses) { List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { for (Class<? extends Annotation> annotationClass : annotationClasses) { if (AnnotationUtils.findAnnotation(method, annotationClass) != null) { methods.add(method); } } } return methods; }
From source file:tv.arte.resteventapi.web.errors.GlobalDefaultExceptionHandler.java
/** * Handle all exceptions thrown by Controller's layer and that have not been already handled * /*ww w .j ava 2s.c om*/ * @param response The HttpServletResponse * @param e Thrown exception * @return * @throws Exception */ @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ModelAndView defaultErrorHandler(HttpServletResponse response, Exception e) throws Exception { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } RestEventApiStandardResponse<RestEventApiMessage> restEventApiStandardResponse = new RestEventApiStandardResponse<RestEventApiMessage>(); restEventApiStandardResponse.addError(RestEventApiError.PRE_DEFINED.RIA_ERR_G_UNKNOWN); if (e instanceof NullPointerException) { //Log full stacktrace in case of NullPointer logger.error(ExceptionUtils.getFullStackTrace(e)); } ModelAndView mav = new ModelAndView(); mav.addObject(restEventApiStandardResponse); mav.setView(restEventApiDefaultErrorView); return mav; }
From source file:com.revolsys.ui.web.rest.interceptor.WebAnnotationMethodHandlerAdapter.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public ModelAndView getModelAndView(final Method handlerMethod, final Class<?> handlerType, final Object returnValue, final ExtendedModelMap implicitModel, final ServletWebRequest webRequest) throws Exception { boolean responseArgumentUsed = false; final ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class); if (responseStatusAnn != null) { final HttpStatus responseStatus = responseStatusAnn.value(); // to be picked up by the RedirectView webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, responseStatus); webRequest.getResponse().setStatus(responseStatus.value()); responseArgumentUsed = true;/*from w w w . j av a 2s . c o m*/ } // Invoke custom resolvers if present... if (WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers != null) { for (final ModelAndViewResolver mavResolver : WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers) { final ModelAndView mav = mavResolver.resolveModelAndView(handlerMethod, handlerType, returnValue, implicitModel, webRequest); if (mav != ModelAndViewResolver.UNRESOLVED) { return mav; } } } if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) { final View view = handleResponseBody(returnValue, webRequest); return new ModelAndView(view).addAllObjects(implicitModel); } if (returnValue instanceof ModelAndView) { final ModelAndView mav = (ModelAndView) returnValue; mav.getModelMap().mergeAttributes(implicitModel); return mav; } else if (returnValue instanceof Model) { return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap()); } else if (returnValue instanceof View) { return new ModelAndView((View) returnValue).addAllObjects(implicitModel); } else if (AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class) != null) { addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel); return new ModelAndView().addAllObjects(implicitModel); } else if (returnValue instanceof Map) { return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue); } else if (returnValue instanceof String) { return new ModelAndView((String) returnValue).addAllObjects(implicitModel); } else if (returnValue == null) { // Either returned null or was 'void' return. if (responseArgumentUsed || webRequest.isNotModified()) { return null; } else { // Assuming view name translation... return new ModelAndView().addAllObjects(implicitModel); } } else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) { // Assume a single model attribute... addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel); return new ModelAndView().addAllObjects(implicitModel); } else { throw new IllegalArgumentException("Invalid handler method return value: " + returnValue); } }
From source file:com.excilys.ebi.spring.dbunit.test.TestConfigurationProcessor.java
/** * @param method the test method/* w w w . jav a2 s . com*/ * @param clazz the test class * @return the {@link DataSet} at method level if found, otherwise at class level */ private <A extends Annotation> A findAnnotation(Method method, Class<?> clazz, Class<A> annotationType) { A annotation = AnnotationUtils.findAnnotation(method, annotationType); return annotation == null ? annotation = AnnotationUtils.findAnnotation(clazz, annotationType) : annotation; }
From source file:com.tyro.oss.pact.spring4.pact.provider.PactTestRunner.java
private Map<String, Method> getProviderStateMethods(Class<?> clazz) { Map<String, Method> providerStateMethods = new HashMap<>(); for (Method method : clazz.getMethods()) { ProviderState annotation = AnnotationUtils.findAnnotation(method, ProviderState.class); if (annotation != null) { String state = annotation.value(); if (StringUtils.isEmpty(state)) { state = method.getName(); }//from w ww . j a va 2 s . co m if (providerStateMethods.containsKey(state)) { throw new IllegalStateException("There must be only one setup method per provider state"); } providerStateMethods.put(state, method); } } return providerStateMethods; }
From source file:com.taobao.ad.easyschedule.security.ActionSecurityBean.java
/** * ??BO?Annotation??Annotation?//from ww w . jav a 2 s .c o m */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { String[] beanNames = applicationContext.getBeanDefinitionNames(); if (beanNames == null || beanNames.length == 0) { System.err.println( "Warning: seems there are no Spring Beans defined, please double check.................."); return; } for (String name : beanNames) { if (!name.endsWith("Action")) { continue; } Class<?> c = AopUtils.getTargetClass(applicationContext.getBean(name)); Object bean = applicationContext.getBean(name); while (Proxy.isProxyClass(c) && (bean instanceof Advised)) { try { bean = ((Advised) bean).getTargetSource(); bean = ((TargetSource) bean).getTarget(); c = bean.getClass(); } catch (Exception e) { throw new IllegalStateException("Can't initialize SecurityBean, due to:" + e.getMessage()); } } long moduleIdDefinedInInterface = -1; ActionSecurity moduleAnno = AnnotationUtils.findAnnotation(c, ActionSecurity.class); if (moduleAnno != null) { moduleIdDefinedInInterface = moduleAnno.module(); } Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { ActionSecurity methodAnno = AnnotationUtils.findAnnotation(method, ActionSecurity.class); if (methodAnno != null || moduleIdDefinedInInterface != -1) { if (methodAnno == null) { methodAnno = moduleAnno; // use interface annotation } long module = methodAnno.module() == 0 ? moduleIdDefinedInInterface : methodAnno.module(); Module myModule = moduleIndex.get(module); if (myModule == null) { throw new IllegalArgumentException( "Found invalid module id:" + module + " in method annotation:" + method + " valid module ids are: " + moduleIndex.keySet()); } if (methodAnno.enable()) { myModule.addOperation(method, methodAnno, methodIndex); } } } } System.out.println("[ActionSecurityBean] Total " + methodIndex.size() + " methods are secured!"); }
From source file:org.vaadin.spring.security.Security.java
/** * Uses the {@link org.springframework.security.access.annotation.Secured} annotation on the specified method to check if the current user has access to the secured object. * * @param securedObject the secured object, must not be {@code null}. * @param methodName the name of the method holding the {@link org.springframework.security.access.annotation.Secured} annotation. * @param methodParameterTypes the parameter types of the method holding the {@link org.springframework.security.access.annotation.Secured} annotation. * @return true if the current user is authorized, false if not. * @see #hasAccessToSecuredObject(Object) *//*from w w w.j a va 2s . co m*/ public boolean hasAccessToSecuredMethod(Object securedObject, String methodName, Class<?>... methodParameterTypes) { try { final Method method = securedObject.getClass().getMethod(methodName, methodParameterTypes); final Secured secured = AnnotationUtils.findAnnotation(method, Secured.class); Assert.notNull(secured, "securedObject did not have @Secured annotation"); return hasAccessToObject(securedObject, secured.value()); } catch (NoSuchMethodException ex) { throw new IllegalArgumentException("Method " + methodName + " does not exist", ex); } }