List of usage examples for java.lang.reflect Method getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:org.flite.cach3.aop.L2InvalidateMultiCacheAdvice.java
private void doInvalidate(final JoinPoint jp, final Object retVal) throws Throwable { if (isCacheDisabled()) { LOG.debug("Caching is disabled."); return;//from w ww .j av a2s . co m } final Method methodToCache = getMethodToCache(jp); List<L2InvalidateMultiCache> lAnnotations; // if (methodToCache.getAnnotation(InvalidateMultiCache.class) != null) { lAnnotations = Arrays.asList(methodToCache.getAnnotation(L2InvalidateMultiCache.class)); // } else { // lAnnotations = Arrays.asList(methodToCache.getAnnotation(InvalidateMultiCaches.class).value()); // } for (int i = 0; i < lAnnotations.size(); i++) { // 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 AnnotationInfo info = getAnnotationInfo(lAnnotations.get(i), methodToCache.getName()); final List<Object> keyObjects = (List<Object>) UpdateMultiCacheAdvice.getIndexObject( info.getAsInteger(AType.KEY_INDEX), retVal, jp.getArgs(), methodToCache.toString()); final List<String> baseKeys = UpdateMultiCacheAdvice.getBaseKeys(keyObjects, info.getAsString(AType.KEY_TEMPLATE), retVal, jp.getArgs(), factory, methodStore); final List<String> fullKeys = new ArrayList<String>(baseKeys.size()); for (final String base : baseKeys) { final String cacheKey = buildCacheKey(base, info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX)); fullKeys.add(cacheKey); } getCache().invalidateBulk(fullKeys); } catch (Throwable ex) { if (LOG.isDebugEnabled()) { LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error.", ex); } else { LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error: " + ex.getMessage()); } } } }
From source file:com.wordnik.swagger.testframework.JavaTestCaseExecutor.java
/** * Gets the list of input query and path parameters and post data vlues and covenrt them to arguments that * can be used for calling the method. This logic will be different in each driver language depends on how method * input arguments are created./* w ww .j a v a 2s. c o m*/ */ private Object[] populateArgumentsForTestCaseExecution(Method methodToExecute, Map<String, String> queryAndPathParameters, String postData, String serviceName, String resourcePath) throws Exception { MethodArgumentNames argNames = methodToExecute.getAnnotation(MethodArgumentNames.class); String[] argNamesArray = null; if (argNames != null && argNames.value().length() > 0) { argNamesArray = argNames.value().split(","); } Class[] argTypesArray = methodToExecute.getParameterTypes(); Object output = null; String inputClassName = namingPolicyProvider.getInputObjectName(serviceName, resourcePath); if (argNamesArray != null && argNamesArray.length > 0) { Object[] arguments = new Object[argNamesArray.length]; for (int i = 0; i < argNamesArray.length; i++) { Object argument = null; //if the method takes input model instead of individual arguments, convert individual arguments into input model object if (argTypesArray[i].getName().equalsIgnoreCase(inputClassName)) { argument = populateInputModelObject(argTypesArray[i], queryAndPathParameters); } else if (datatypeMppingProvider.isPrimitiveType(argTypesArray[i].getName())) { argument = queryAndPathParameters.get(argNamesArray[i].trim()); } else if (argNamesArray[i].trim().equals(APITestRunner.POST_PARAM_NAME)) { String input = postData; if (postData.startsWith("\"") && postData.endsWith("\"")) { input = postData.substring(1, postData.length() - 1); } input = input.replaceAll("\\\\\"", "\""); argument = APITestRunner.convertJSONStringToObject(input, argTypesArray[i]); } else { //some times input can be list of primitives for supporting multivalued values. however test case sends the input as comma separated values //so we need to convert comma separated string into JSON list format if (List.class.isAssignableFrom(argTypesArray[i]) && !queryAndPathParameters.get(argNamesArray[i].trim()).startsWith("[")) { String listInput = "["; int x = 0; String[] values = queryAndPathParameters.get(argNamesArray[i].trim()).split(","); for (String value : values) { if (x > 0) { listInput = listInput + ","; } listInput = listInput + "\"" + value + "\""; x++; } listInput = listInput + "]"; argument = APITestRunner.convertJSONStringToObject(listInput, argTypesArray[i]); } else { argument = APITestRunner.convertJSONStringToObject( queryAndPathParameters.get(argNamesArray[i].trim()), argTypesArray[i]); } } arguments[i] = argument; } return arguments; } return null; }
From source file:org.flite.cach3.aop.L2InvalidateSingleCacheAdvice.java
private void doInvalidate(final JoinPoint jp, final Object retVal) throws Throwable { if (isCacheDisabled()) { LOG.debug("Caching is disabled."); return;/*w w w . j ava2 s . co m*/ } final Method methodToCache = getMethodToCache(jp); List<L2InvalidateSingleCache> lAnnotations; // if (methodToCache.getAnnotation(InvalidateSingleCache.class) != null) { lAnnotations = Arrays.asList(methodToCache.getAnnotation(L2InvalidateSingleCache.class)); // } else { // lAnnotations = Arrays.asList(methodToCache.getAnnotation(InvalidateSingleCaches.class).value()); // } for (int i = 0; i < lAnnotations.size(); i++) { // 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 AnnotationInfo info = getAnnotationInfo(lAnnotations.get(i), methodToCache.getName()); final String baseKey = CacheBase.getBaseKey(info.getAsString(AType.KEY_TEMPLATE), info.getAsInteger(AType.KEY_INDEX, null), retVal, jp.getArgs(), methodToCache.toString(), factory, methodStore); final String cacheKey = buildCacheKey(baseKey, info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX)); LOG.debug("Invalidating cache for key " + cacheKey); getCache().invalidateBulk(Arrays.asList(cacheKey)); } catch (Throwable ex) { if (LOG.isDebugEnabled()) { LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error.", ex); } else { LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error: " + ex.getMessage()); } } } }
From source file:es.logongas.ix3.rule.impl.RuleEngineImpl.java
@Override public void fireConstraintRules(Object rulesObject, RuleContext<T> ruleContext, Class<?>... groups) throws BusinessException { List<Method> methods = getRuleMethods(rulesObject, ConstraintRule.class); List<BusinessMessage> businessMessages = new ArrayList<BusinessMessage>(); Collections.sort(methods, new Comparator<Method>() { @Override//from w ww .jav a 2 s . c om public int compare(Method method1, Method method2) { ConstraintRule constraintRule1 = method1.getAnnotation(ConstraintRule.class); ConstraintRule constraintRule2 = method2.getAnnotation(ConstraintRule.class); if (constraintRule1.priority() == constraintRule2.priority()) { return 0; } else if (constraintRule1.priority() > constraintRule2.priority()) { return 1; } else if (constraintRule1.priority() < constraintRule2.priority()) { return -1; } else { throw new RuntimeException("Error de lgica"); } } }); for (Method method : methods) { ConstraintRule constraintRule = method.getAnnotation(ConstraintRule.class); if (isExecuteConstrainRule(constraintRule, groups)) { try { int numArgs = method.getParameterTypes().length; boolean result; if (numArgs == 0) { result = (Boolean) method.invoke(rulesObject); } else if (numArgs == 1) { Class argumenType = method.getParameterTypes()[0]; if (RuleContext.class.isAssignableFrom(argumenType) == false) { throw new RuntimeException("El mtodo " + method.getName() + " debe tener el nico argumento del tipo:" + RuleContext.class.getName()); } result = (Boolean) method.invoke(rulesObject, ruleContext); } else { throw new RuntimeException("El mtodo " + method.getName() + " solo puede tener 0 o 1 argumentos pero tiene:" + numArgs); } if (result == false) { BusinessMessage businessMessage = getBusinessMessage(constraintRule, ruleContext); businessMessages.add(businessMessage); if (constraintRule.stopOnFail() == true) { break; } } } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { BusinessException businessException = ExceptionUtil.getBusinessExceptionFromThrowable(ex); if (businessException != null) { throw businessException; } else { throw new RuntimeException(ex); } } } } if (businessMessages.size() > 0) { throw new BusinessException(businessMessages); } }
From source file:com.haulmont.cuba.core.config.AppPropertiesLocator.java
protected String getDefaultValue(Method method) { Default defaultAnn = method.getAnnotation(Default.class); if (defaultAnn != null) { return defaultAnn.value(); }/*from ww w . j a v a2s . c o m*/ DefaultString defaultStringAnn = method.getAnnotation(DefaultString.class); if (defaultStringAnn != null) { return defaultStringAnn.value(); } DefaultInt defaultIntAnn = method.getAnnotation(DefaultInt.class); if (defaultIntAnn != null) { return String.valueOf(defaultIntAnn.value()); } DefaultInteger defaultIntegerAnn = method.getAnnotation(DefaultInteger.class); if (defaultIntegerAnn != null) { return String.valueOf(defaultIntegerAnn.value()); } DefaultLong defaultLongAnn = method.getAnnotation(DefaultLong.class); if (defaultLongAnn != null) { return String.valueOf(defaultLongAnn.value()); } DefaultShort defaultShortAnn = method.getAnnotation(DefaultShort.class); if (defaultShortAnn != null) { return String.valueOf(defaultShortAnn.value()); } DefaultBoolean defaultBooleanAnn = method.getAnnotation(DefaultBoolean.class); if (defaultBooleanAnn != null) { return String.valueOf(defaultBooleanAnn.value()); } DefaultDouble defaultDoubleAnn = method.getAnnotation(DefaultDouble.class); if (defaultDoubleAnn != null) { return String.valueOf(defaultDoubleAnn.value()); } DefaultFloat defaultFloatAnn = method.getAnnotation(DefaultFloat.class); if (defaultFloatAnn != null) { return String.valueOf(defaultFloatAnn.value()); } DefaultChar defaultCharAnn = method.getAnnotation(DefaultChar.class); if (defaultCharAnn != null) { return String.valueOf(defaultCharAnn.value()); } return null; }
From source file:gda.jython.accesscontrol.ProtectedMethodComponent.java
/** * Analyse the given class and add any methods annotated to be protected to the protectedMethods array. * /*from ww w .ja v a 2 s .co m*/ * @param clazz */ @SuppressWarnings("rawtypes") private void analyseClass(Class clazz) { // loop over this classes methods Method[] newMethods = clazz.getMethods(); for (Method thisMethod : newMethods) { if (thisMethod.isAnnotationPresent(gda.jython.accesscontrol.MethodAccessProtected.class) && thisMethod.getAnnotation(MethodAccessProtected.class).isProtected()) { addMethod(thisMethod); } } // loop over the interfaces the class implements Class[] newInterfaces = clazz.getInterfaces(); for (Class thisInterface : newInterfaces) { // if its a new interface (for performance, ensure each interface only looked at once) if (!ArrayUtils.contains(analysedInterfaces, thisInterface)) { analysedInterfaces = (Class[]) ArrayUtils.add(analysedInterfaces, thisInterface); // check if any method in that interface is annotated that it should be protected for (Method method : thisInterface.getMethods()) { if (method.isAnnotationPresent(gda.jython.accesscontrol.MethodAccessProtected.class) && method.getAnnotation(MethodAccessProtected.class).isProtected()) { addMethod(method); } } } } }
From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java
private boolean scanPath(SubResource subResource, Method method) { Path path = method.getAnnotation(Path.class); if (path != null) { subResource.setPath(normalizePath(path.value())); return true; }//from ww w.j ava2 s . c o m return false; }
From source file:com.ryantenney.metrics.spring.MetricParamAnnotationTest.java
@Test public void exceptionMeteredParameterMethod() { Meter meteredMethod = forExceptionMeteredMethod(metricRegistry, MetricParamClass.class, "exceptionMeteredParameterMethod", 5); assertNull(meteredMethod);//ww w . j ava 2s. c o m //throw wrong exception try { metricParamClass.exceptionMeteredParameterMethod(RuntimeException.class, 5); fail(); } catch (Throwable t) { assert t instanceof RuntimeException; } meteredMethod = forExceptionMeteredMethod(metricRegistry, MetricParamClass.class, "exceptionMeteredParameterMethod", RuntimeException.class, 5); assertNotNull(meteredMethod); assertEquals(0, meteredMethod.getCount()); //throw correct exception try { metricParamClass.exceptionMeteredParameterMethod(BogusException.class, 5); fail(); } catch (Throwable t) { assert t instanceof BogusException; } meteredMethod = forExceptionMeteredMethod(metricRegistry, MetricParamClass.class, "exceptionMeteredParameterMethod", BogusException.class, 5); assertNotNull(meteredMethod); assertEquals(1, meteredMethod.getCount()); Method method = findMethod(MetricParamClass.class, "exceptionMeteredParameterMethod"); String metricName = forExceptionMeteredMethod(MetricParamClass.class, method, method.getAnnotation(ExceptionMetered.class), BogusException.class, 5); assertEquals("exception-metered-param.5.exceptions", metricName); }
From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java
private boolean scanName(SubResource subResource, Method method) { Name name = method.getAnnotation(Name.class); if (name != null) { subResource.setName(name.value()); return true; }/* w w w . j a v a 2 s.co m*/ return false; }
From source file:com.evolveum.midpoint.repo.sql.query2.definition.ClassDefinitionParser.java
private ItemPath getJaxbName(Method method) { if (method.isAnnotationPresent(JaxbName.class)) { JaxbName jaxbName = method.getAnnotation(JaxbName.class); return new ItemPath(new QName(jaxbName.namespace(), jaxbName.localPart())); } else if (method.isAnnotationPresent(JaxbPath.class)) { JaxbPath jaxbPath = method.getAnnotation(JaxbPath.class); List<QName> names = new ArrayList<>(jaxbPath.itemPath().length); for (JaxbName jaxbName : jaxbPath.itemPath()) { names.add(new QName(jaxbName.namespace(), jaxbName.localPart())); }//from w w w. j a va2 s . c om return new ItemPath(names.toArray(new QName[0])); } else { return new ItemPath(new QName(SchemaConstantsGenerated.NS_COMMON, getPropertyName(method.getName()))); } }