List of usage examples for java.lang.reflect Method getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.feilong.core.lang.annotation.AnnotationTest.java
/** * TestAnnotationTest./*w ww . ja v a 2 s . com*/ */ @Test public void testAnnotationTest() { LOGGER.debug("" + AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)); MyAnnotation myAnnotation = AnnotationTest.class.getAnnotation(MyAnnotation.class); LOGGER.debug(myAnnotation.name()); // ************************************************************* Method[] methods = AnnotationTest.class.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(MyAnnotation.class)) { LOGGER.debug("[Test." + method.getName() + "].annotation:"); MyAnnotation fieldAnnotation = method.getAnnotation(MyAnnotation.class); LOGGER.debug(ArrayUtils.toString(fieldAnnotation.loveStrings())); } } }
From source file:info.sargis.eventbus.config.EventBusHandlerBeanDefinitionParser.java
private boolean isEventBusHandlerCandidate(BeanDefinition component, ParserContext parserContext) { try {/*from www. j av a2 s.co m*/ String beanClassName = component.getBeanClassName(); Class<?> beanClass = Class.forName(beanClassName); for (Method method : beanClass.getMethods()) { Subscribe annotation = method.getAnnotation(Subscribe.class); if (annotation != null) { return true; } } } catch (ClassNotFoundException e) { logger.error("", e); } return false; }
From source file:com.iisigroup.cap.operation.step.CapCaptchaOpStep.java
@Override public OpStepContext execute(OpStepContext ctx, IRequest params, IHandler handler) { String methodId = params.get(FormHandler.FORM_ACTION, ""); if (!CapString.isEmpty(methodId)) { for (Method method : handler.getClass().getDeclaredMethods()) { if (methodId.equals(method.getName())) { if (method.isAnnotationPresent(Captcha.class)) { String key = method.getAnnotation(Captcha.class).value(); CapSecurityCaptcha captcha = CapAppContext.getBean(CapCaptchaServlet.DEF_RENDERER); if (captcha == null || !CaptchaStatus.SUCCESS.equals(captcha.valid(params.get(key)))) { // ? throw new CapMessageException(CapAppContext.getMessage(captcha.getErrorMessage()), getClass()); }/*from ww w. jav a2 s . co m*/ } } } } return ctx.setGoToStep(NEXT); }
From source file:jp.rough_diamond.framework.web.struts.BaseAction.java
private boolean hasRole(Method method) { AllowRole classRole = this.getClass().getAnnotation(AllowRole.class); AllowRole methodRole = method.getAnnotation(AllowRole.class); if (methodRole != null) { return hasRole(methodRole); } else {/*from w w w. j a v a 2 s .com*/ return hasRole(classRole); } }
From source file:org.flite.cach3.aop.L2ReadThroughAssignCacheAdvice.java
@Around("getL2SingleAssign()") public Object cacheL2Assign(final ProceedingJoinPoint pjp) throws Throwable { // If we've disabled the caching programmatically (or via properties file) just flow through. if (isCacheDisabled()) { LOG.debug("Caching is disabled."); return pjp.proceed(); }/*from ww w.j a v a 2 s. c o m*/ // This is injected caching. If anything goes wrong in the caching, LOG the crap outta it, // but do not let it surface up past the AOP injection itself. final String cacheKey; final AnnotationInfo info; try { final Method methodToCache = getMethodToCache(pjp); final L2ReadThroughAssignCache annotation = methodToCache.getAnnotation(L2ReadThroughAssignCache.class); info = getAnnotationInfo(annotation, methodToCache.getName()); cacheKey = buildCacheKey(info.getAsString(AType.ASSIGN_KEY), info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX)); final Map<String, Object> results = getCache().getBulk(Arrays.asList(cacheKey), info.<Duration>getAsType(AType.WINDOW, null)); final Object result = results == null ? null : results.get(cacheKey); if (result != null) { // LOG.debug("Cache hit for key " + cacheKey); return (result instanceof PertinentNegativeNull) ? null : result; } } catch (Throwable ex) { if (LOG.isDebugEnabled()) { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex); } else { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage()); } return pjp.proceed(); } final Object result = pjp.proceed(); // This is injected caching. If anything goes wrong in the caching, LOG the crap outta it, // but do not let it surface up past the AOP injection itself. try { final Object submission = (result == null) ? new PertinentNegativeNull() : result; boolean cacheable = true; if (submission instanceof CacheConditionally) { cacheable = ((CacheConditionally) submission).isCacheable(); } if (cacheable) { getCache().setBulk(ImmutableMap.of(cacheKey, submission), info.<Duration>getAsType(AType.WINDOW, null)); } } catch (Throwable ex) { if (LOG.isDebugEnabled()) { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex); } else { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error: " + ex.getMessage()); } } return result; }
From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java
@Override protected final void formatBean(Object bean) throws Exception { Class<?> bclass = bean.getClass(); ManagedResource mr = bclass.getAnnotation(ManagedResource.class); String help = mr == null ? null : mr.description(); formatBean(false, bclass, help);/* w w w. j a va2s. co m*/ boolean section = false; for (PropertyDescriptor pd : Introspector.getBeanInfo(bclass).getPropertyDescriptors()) { Method getter = pd.getReadMethod(); if (!XMLSerializationManager.isHidden(getter)) { if (!section) { section = true; formatSection(false, "Properties"); } formatProperty(bean, pd); } } if (section) { formatSection(true, "Properties"); } section = false; for (Method method : bean.getClass().getMethods()) { ManagedOperation mo = method.getAnnotation(ManagedOperation.class); if (mo != null && method.getParameterTypes().length == 0) { help = mo.description(); if (!section) { section = true; formatSection(false, "Operations"); } formatOperation(method); } if (section) { formatSection(true, "Operations"); } } }
From source file:net.firejack.platform.core.validation.LessThanProcessor.java
@Override public List<Constraint> generate(Method readMethod, String property, Map<String, String> params) { List<Constraint> constraints = null; Annotation annotation = readMethod.getAnnotation(LessThan.class); if (annotation != null) { LessThan lessThan = (LessThan) annotation; Constraint constraint = new Constraint(lessThan.annotationType().getSimpleName()); Class<?> returnType = readMethod.getReturnType(); if (returnType.getSuperclass() == Number.class || returnType.isPrimitive()) { List<RuleParameter> ruleParameters = new ArrayList<RuleParameter>(); boolean checkEquality = lessThan.checkEquality(); RuleParameter checkEqualityParam = new RuleParameter("checkEquality", checkEquality); ruleParameters.add(checkEqualityParam); String errorMessage;/* w w w . j a v a 2 s. co m*/ RuleParameter valueParam; String messageKey = checkEquality ? lessThan.equalityMsgKey() : lessThan.msgKey(); if (returnType == Float.class || returnType == float.class) { errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property, lessThan.floatVal()); valueParam = new RuleParameter("lessThanValue", lessThan.floatVal()); } else if (returnType == Double.class || returnType == double.class) { errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property, lessThan.doubleVal()); valueParam = new RuleParameter("lessThanValue", lessThan.doubleVal()); } else { errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property, lessThan.intVal()); valueParam = new RuleParameter("lessThanValue", lessThan.intVal()); } constraint.setErrorMessage(errorMessage); ruleParameters.add(valueParam); constraint.setParams(ruleParameters); constraints = new ArrayList<Constraint>(); constraints.add(constraint); } } return constraints; }
From source file:org.ambraproject.struts2.TransactionInterceptor.java
private <A extends Annotation> A getMethodAnnotation(Class<? extends Action> actionClass, String methodName, Class<A> annotationType) { try {//from w ww.jav a2 s. c o m Method method = actionClass.getMethod(methodName); A annotation = method.getAnnotation(annotationType); if (annotation == null) { Class parent = actionClass.getSuperclass(); if (Action.class.isAssignableFrom(parent)) { annotation = getMethodAnnotation((Class<? extends Action>) parent, methodName, annotationType); } } return annotation; } catch (NoSuchMethodException e) { return null; } }
From source file:com.neelo.glue.ApplicationModule.java
public void register(Class<?> resourceClass) { log.info("Registering: " + resourceClass.getName()); Resource resource = null;//from w ww . ja v a 2 s. c o m if (resourceClass.isAnnotationPresent(Resource.class)) { resource = resourceClass.getAnnotation(Resource.class); } else { log.warn("Class not registerd. Missing " + Resource.class.getSimpleName() + " annotation"); return; } ResourceBuilder builder = new ResourceBuilder(resourceClass, resource.path(), resource.layout()); Method[] methods = resourceClass.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(Get.class)) { Get get = method.getAnnotation(Get.class); RouteBuilder route = builder.get(method, get.path(), get.content()); route.setLayout(StringUtils.isNotBlank(get.layout()) ? get.layout() : resource.layout()); if (StringUtils.isNotBlank(get.name())) route.setName(get.name()); routes.add(route); } else if (method.isAnnotationPresent(Post.class)) { Post post = method.getAnnotation(Post.class); RouteBuilder route = builder.post(method, post.path(), post.content()); route.setLayout(StringUtils.isNotBlank(post.layout()) ? post.layout() : resource.layout()); if (StringUtils.isNotBlank(post.name())) route.setName(post.name()); routes.add(route); } } }
From source file:net.nelz.simplesm.aop.InvalidateMultiCacheAdvice.java
@Around("invalidateMulti()") public Object cacheInvalidateMulti(final ProceedingJoinPoint pjp) throws Throwable { // This is injected caching. If anything goes wrong in the caching, LOG the crap outta it, // but do not let it surface up past the AOP injection itself. List<String> cacheKeys = null; final AnnotationData annotationData; final String methodDescription; try {/*from w ww . j a v a 2 s .c o m*/ final Method methodToCache = getMethodToCache(pjp); methodDescription = methodToCache.toString(); final InvalidateMultiCache annotation = methodToCache.getAnnotation(InvalidateMultiCache.class); annotationData = AnnotationDataBuilder.buildAnnotationData(annotation, InvalidateMultiCache.class, methodToCache); if (annotationData.getKeyIndex() > -1) { final Object keyObject = getIndexObject(annotationData.getKeyIndex(), pjp, methodToCache); final List<Object> keyObjects = convertToKeyObjects(keyObject, annotationData.getKeyIndex(), methodDescription); cacheKeys = getCacheKeys(keyObjects, annotationData); } } catch (Throwable ex) { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex); return pjp.proceed(); } final Object result = pjp.proceed(); // This is injected caching. If anything goes wrong in the caching, LOG the crap outta it, // but do not let it surface up past the AOP injection itself. try { // If we have a -1 key index, then build the cacheKeys now. if (annotationData.getKeyIndex() == -1) { final List<Object> keyObjects = convertToKeyObjects(result, annotationData.getKeyIndex(), methodDescription); cacheKeys = getCacheKeys(keyObjects, annotationData); } if (cacheKeys != null && cacheKeys.size() > 0) { for (final String key : cacheKeys) { if (key != null && key.trim().length() > 0) { cache.delete(key); } } } } catch (Throwable ex) { LOG.warn("Caching on " + pjp.toShortString() + " aborted due to an error.", ex); } return result; }