List of usage examples for java.lang.reflect Method getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
From source file:org.apache.qpid.disttest.message.ParticipantAttributeExtractor.java
public static Map<ParticipantAttribute, Object> getAttributes(Object targetObject) { Map<ParticipantAttribute, Object> attributes = new HashMap<ParticipantAttribute, Object>(); PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(targetObject); for (PropertyDescriptor propertyDescriptor : descriptors) { final Method readMethod = getPropertyReadMethod(targetObject, propertyDescriptor); for (Annotation annotation : readMethod.getDeclaredAnnotations()) { if (annotation instanceof OutputAttribute) { OutputAttribute outputAttribute = (OutputAttribute) annotation; Object value = getPropertyValue(targetObject, propertyDescriptor.getName()); attributes.put(outputAttribute.attribute(), value); }/*from w w w.j a va 2 s .c o m*/ } } return attributes; }
From source file:org.openinfinity.core.util.AspectUtil.java
/** * Returns the specified annotation.// ww w.ja v a 2 s . c o m * * @param joinPoint Represents the join point. * @return * @return LogLevel representing the log level. */ public static <T extends Annotation> T getAnnotation(JoinPoint joinPoint, Class<T> requiredAnnotationClass) { Method[] methods = joinPoint.getTarget().getClass().getMethods(); for (Method method : methods) { if (method.getName().equals(joinPoint.getSignature().getName())) { Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation annotation : annotations) { if (requiredAnnotationClass.isAssignableFrom(annotation.getClass())) { return (T) annotation; } } } } throw new SystemException(new AopConfigException("Annotation not found.")); }
From source file:com.lidroid.xutils.ViewUtils.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject ContentView ContentView contentView = handlerType.getAnnotation(ContentView.class); if (contentView != null) { try {/*from w w w . ja va2s . c o m*/ Method setContentViewMethod = handlerType.getMethod("setContentView", int.class); setContentViewMethod.invoke(handler, contentView.value()); } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { ResInject resInject = field.getAnnotation(ResInject.class); if (resInject != null) { try { Object res = ResLoader.loadRes(resInject.type(), finder.getContext(), resInject.id()); if (res != null) { field.setAccessible(true); field.set(handler, res); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class); if (preferenceInject != null) { try { Preference preference = finder.findPreference(preferenceInject.value()); if (preference != null) { field.setAccessible(true); field.set(handler, preference); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { Class<?> annType = annotation.annotationType(); if (annType.getAnnotation(EventBase.class) != null) { method.setAccessible(true); try { // ProGuard-keep class * extends java.lang.annotation.Annotation { *; } Method valueMethod = annType.getDeclaredMethod("value"); Method parentIdMethod = null; try { parentIdMethod = annType.getDeclaredMethod("parentId"); } catch (Throwable e) { } Object values = valueMethod.invoke(annotation); Object parentIds = parentIdMethod == null ? null : parentIdMethod.invoke(annotation); int parentIdsLen = parentIds == null ? 0 : Array.getLength(parentIds); int len = Array.getLength(values); for (int i = 0; i < len; i++) { ViewInjectInfo info = new ViewInjectInfo(); info.value = Array.get(values, i); info.parentId = parentIdsLen > i ? (Integer) Array.get(parentIds, i) : 0; EventListenerManager.addEventMethod(finder, info, annotation, handler, method); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } }
From source file:com.zhoukl.androidRDP.RdpUtils.RdpAnnotationUtil.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject ContentView ContentView contentView = handlerType.getAnnotation(ContentView.class); if (contentView != null) { try {//from w w w. jav a 2 s . c o m Method setContentViewMethod = handlerType.getMethod("setContentView", int.class); setContentViewMethod.invoke(handler, contentView.value()); } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } else { ResInject resInject = field.getAnnotation(ResInject.class); if (resInject != null) { try { Object res = ResLoader.loadRes(resInject.type(), finder.getContext(), resInject.id()); if (res != null) { field.setAccessible(true); field.set(handler, res); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } else { PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class); if (preferenceInject != null) { try { Preference preference = finder.findPreference(preferenceInject.value()); if (preference != null) { field.setAccessible(true); field.set(handler, preference); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } } } } } // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { Class<?> annType = annotation.annotationType(); if (annType.getAnnotation(EventBase.class) != null) { method.setAccessible(true); try { // ProGuard-keep class * extends java.lang.annotation.Annotation { *; } Method valueMethod = annType.getDeclaredMethod("value"); Method parentIdMethod = null; try { parentIdMethod = annType.getDeclaredMethod("parentId"); } catch (Throwable e) { } Object values = valueMethod.invoke(annotation); Object parentIds = parentIdMethod == null ? null : parentIdMethod.invoke(annotation); int parentIdsLen = parentIds == null ? 0 : Array.getLength(parentIds); int len = Array.getLength(values); for (int i = 0; i < len; i++) { ViewInjectInfo info = new ViewInjectInfo(); info.value = Array.get(values, i); info.parentId = parentIdsLen > i ? (Integer) Array.get(parentIds, i) : 0; EventListenerManager.addEventMethod(finder, info, annotation, handler, method); } } catch (Throwable e) { //LogUtils.e(e.getMessage(), e); } } } } } } }
From source file:com.higgses.griffin.annotation.app.GinInjector.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, ViewFinder finder) { Class<?> handlerType = handler.getClass(); // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { GinInjectView viewInject = field.getAnnotation(GinInjectView.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.id(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); }/*from w ww.ja va 2s .c o m*/ } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { ResInject resInject = field.getAnnotation(ResInject.class); if (resInject != null) { try { Object res = ResLoader.loadRes(resInject.type(), finder.getContext(), resInject.id()); if (res != null) { field.setAccessible(true); field.set(handler, res); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } else { PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class); if (preferenceInject != null) { try { Preference preference = finder.findPreference(preferenceInject.value()); if (preference != null) { field.setAccessible(true); field.set(handler, preference); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { Class<?> annType = annotation.annotationType(); if (annType.getAnnotation(EventBase.class) != null) { method.setAccessible(true); try { // ProGuard-keep class * extends // java.lang.annotation.Annotation { *; } Method valueMethod = annType.getDeclaredMethod("id"); Method parentIdMethod = null; try { parentIdMethod = annType.getDeclaredMethod("parentId"); } catch (Throwable e) { } Object values = valueMethod.invoke(annotation); Object parentIds = parentIdMethod == null ? null : parentIdMethod.invoke(annotation); int parentIdsLen = parentIds == null ? 0 : Array.getLength(parentIds); int len = Array.getLength(values); for (int i = 0; i < len; i++) { ViewInjectInfo info = new ViewInjectInfo(); info.value = Array.get(values, i); info.parentId = parentIdsLen > i ? (Integer) Array.get(parentIds, i) : 0; EventListenerManager.addEventMethod(finder, info, annotation, handler, method); } } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } } } } } }
From source file:com.ocs.dynamo.utils.ClassUtils.java
/** * Looks for an annotation on a (getter) method * /*from w w w. ja v a 2s . c om*/ * @param method * the method * @param annotationClass * the class of the annotation * @return */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotationOnMethod(Method method, Class<T> annotationClass) { T result = null; if (method != null) { for (Annotation a : method.getDeclaredAnnotations()) { if (a.annotationType().equals(annotationClass)) { result = (T) a; } } } return result; }
From source file:com.espertech.esper.util.PopulateUtil.java
public static void populateObject(String operatorName, int operatorNum, String dataFlowName, Map<String, Object> objectProperties, Object top, EngineImportService engineImportService, EPDataFlowOperatorParameterProvider optionalParameterProvider, Map<String, Object> optionalParameterURIs) throws ExprValidationException { Class applicableClass = top.getClass(); Set<WriteablePropertyDescriptor> writables = PropertyHelper.getWritableProperties(applicableClass); Set<Field> annotatedFields = JavaClassHelper.findAnnotatedFields(top.getClass(), DataFlowOpParameter.class); Set<Method> annotatedMethods = JavaClassHelper.findAnnotatedMethods(top.getClass(), DataFlowOpParameter.class); // find catch-all methods Set<Method> catchAllMethods = new LinkedHashSet<Method>(); if (annotatedMethods != null) { for (Method method : annotatedMethods) { DataFlowOpParameter anno = (DataFlowOpParameter) JavaClassHelper .getAnnotations(DataFlowOpParameter.class, method.getDeclaredAnnotations()).get(0); if (anno.all()) { if (method.getParameterTypes().length == 2 && method.getParameterTypes()[0] == String.class && method.getParameterTypes()[1] == Object.class) { catchAllMethods.add(method); continue; }//from w w w .ja v a 2 s.co m throw new ExprValidationException("Invalid annotation for catch-call"); } } } // map provided values for (Map.Entry<String, Object> property : objectProperties.entrySet()) { boolean found = false; String propertyName = property.getKey(); // invoke catch-all setters for (Method method : catchAllMethods) { try { method.invoke(top, new Object[] { propertyName, property.getValue() }); } catch (IllegalAccessException e) { throw new ExprValidationException("Illegal access invoking method for property '" + propertyName + "' for class " + applicableClass.getName() + " method " + method.getName(), e); } catch (InvocationTargetException e) { throw new ExprValidationException("Exception invoking method for property '" + propertyName + "' for class " + applicableClass.getName() + " method " + method.getName() + ": " + e.getTargetException().getMessage(), e); } found = true; } if (propertyName.toLowerCase().equals(CLASS_PROPERTY_NAME)) { continue; } // use the writeable property descriptor (appropriate setter method) from writing the property WriteablePropertyDescriptor descriptor = findDescriptor(applicableClass, propertyName, writables); if (descriptor != null) { Object coerceProperty = coerceProperty(propertyName, applicableClass, property.getValue(), descriptor.getType(), engineImportService, false); try { descriptor.getWriteMethod().invoke(top, new Object[] { coerceProperty }); } catch (IllegalArgumentException e) { throw new ExprValidationException("Illegal argument invoking setter method for property '" + propertyName + "' for class " + applicableClass.getName() + " method " + descriptor.getWriteMethod().getName() + " provided value " + coerceProperty, e); } catch (IllegalAccessException e) { throw new ExprValidationException("Illegal access invoking setter method for property '" + propertyName + "' for class " + applicableClass.getName() + " method " + descriptor.getWriteMethod().getName(), e); } catch (InvocationTargetException e) { throw new ExprValidationException("Exception invoking setter method for property '" + propertyName + "' for class " + applicableClass.getName() + " method " + descriptor.getWriteMethod().getName() + ": " + e.getTargetException().getMessage(), e); } continue; } // find the field annotated with {@link @GraphOpProperty} for (Field annotatedField : annotatedFields) { DataFlowOpParameter anno = (DataFlowOpParameter) JavaClassHelper .getAnnotations(DataFlowOpParameter.class, annotatedField.getDeclaredAnnotations()).get(0); if (anno.name().equals(propertyName) || annotatedField.getName().equals(propertyName)) { Object coerceProperty = coerceProperty(propertyName, applicableClass, property.getValue(), annotatedField.getType(), engineImportService, true); try { annotatedField.setAccessible(true); annotatedField.set(top, coerceProperty); } catch (Exception e) { throw new ExprValidationException( "Failed to set field '" + annotatedField.getName() + "': " + e.getMessage(), e); } found = true; break; } } if (found) { continue; } throw new ExprValidationException("Failed to find writable property '" + propertyName + "' for class " + applicableClass.getName()); } // second pass: if a parameter URI - value pairs were provided, check that if (optionalParameterURIs != null) { for (Field annotatedField : annotatedFields) { try { annotatedField.setAccessible(true); String uri = operatorName + "/" + annotatedField.getName(); if (optionalParameterURIs.containsKey(uri)) { Object value = optionalParameterURIs.get(uri); annotatedField.set(top, value); if (log.isDebugEnabled()) { log.debug("Found parameter '" + uri + "' for data flow " + dataFlowName + " setting " + value); } } else { if (log.isDebugEnabled()) { log.debug("Not found parameter '" + uri + "' for data flow " + dataFlowName); } } } catch (Exception e) { throw new ExprValidationException( "Failed to set field '" + annotatedField.getName() + "': " + e.getMessage(), e); } } for (Method method : annotatedMethods) { DataFlowOpParameter anno = (DataFlowOpParameter) JavaClassHelper .getAnnotations(DataFlowOpParameter.class, method.getDeclaredAnnotations()).get(0); if (anno.all()) { if (method.getParameterTypes().length == 2 && method.getParameterTypes()[0] == String.class && method.getParameterTypes()[1] == Object.class) { for (Map.Entry<String, Object> entry : optionalParameterURIs.entrySet()) { String[] elements = URIUtil.parsePathElements(URI.create(entry.getKey())); if (elements.length < 2) { throw new ExprValidationException("Failed to parse URI '" + entry.getKey() + "', expected " + "'operator_name/property_name' format"); } if (elements[0].equals(operatorName)) { try { method.invoke(top, new Object[] { elements[1], entry.getValue() }); } catch (IllegalAccessException e) { throw new ExprValidationException( "Illegal access invoking method for property '" + entry.getKey() + "' for class " + applicableClass.getName() + " method " + method.getName(), e); } catch (InvocationTargetException e) { throw new ExprValidationException( "Exception invoking method for property '" + entry.getKey() + "' for class " + applicableClass.getName() + " method " + method.getName() + ": " + e.getTargetException().getMessage(), e); } } } } } } } // third pass: if a parameter provider is provided, use that if (optionalParameterProvider != null) { for (Field annotatedField : annotatedFields) { try { annotatedField.setAccessible(true); Object provided = annotatedField.get(top); Object value = optionalParameterProvider.provide(new EPDataFlowOperatorParameterProviderContext( operatorName, annotatedField.getName(), top, operatorNum, provided, dataFlowName)); if (value != null) { annotatedField.set(top, value); } } catch (Exception e) { throw new ExprValidationException( "Failed to set field '" + annotatedField.getName() + "': " + e.getMessage(), e); } } } }
From source file:Main.java
public static void dumpMethod(final Method method) { final StringBuilder builder = new StringBuilder(); builder.append("------------------------------\n"); builder.append("MethodName: ").append(method.getName()).append("\n"); builder.append("ParameterTypes:{"); for (Class<?> cls : method.getParameterTypes()) { builder.append(cls.getName()).append(", "); }/*from w ww . j a v a2 s. c om*/ builder.append("}\n"); builder.append("GenericParameterTypes:{"); for (Type cls : method.getGenericParameterTypes()) { builder.append(cls.getClass()).append(", "); } builder.append("}\n"); builder.append("TypeParameters:{"); for (TypeVariable<Method> cls : method.getTypeParameters()) { builder.append(cls.getName()).append(", "); } builder.append("}\n"); builder.append("DeclaredAnnotations:{"); for (Annotation cls : method.getDeclaredAnnotations()) { builder.append(cls).append(", "); } builder.append("}\n"); builder.append("Annotations:{"); for (Annotation cls : method.getAnnotations()) { builder.append(cls).append(", "); } builder.append("}\n"); builder.append("ExceptionTypes:{"); for (Class<?> cls : method.getExceptionTypes()) { builder.append(cls.getName()).append(", "); ; } builder.append("}\n"); builder.append("ReturnType: ").append(method.getReturnType()); builder.append("\nGenericReturnType: ").append(method.getGenericReturnType()); builder.append("\nDeclaringClass: ").append(method.getDeclaringClass()); builder.append("\n"); System.out.println(builder.toString()); }
From source file:com.web.services.ExecutorServicesConstruct.java
/** * This method configures the executor services during deployment of the war file * @param serverdigester//from w w w .j a v a2 s . com * @param servicesMap * @param exectorServicesXml * @param customClassLoader * @throws Exception */ public void getExecutorServices(Digester serverdigester, Hashtable servicesMap, File exectorServicesXml, WebClassLoader customClassLoader) throws Exception { ExecutorServices executorServices = (ExecutorServices) serverdigester .parse(new InputSource(new FileInputStream(exectorServicesXml))); CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices(); ExecutorServiceAnnot executorServiceAnnot; for (ExecutorService executorService : executorServicesList) { Class executorServiceClass = customClassLoader .loadClass(executorService.getExecutorserviceclass().toString()); //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass); //System.out.println(); Method[] methods = executorServiceClass.getDeclaredMethods(); for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof ExecutorServiceAnnot) { executorServiceAnnot = (ExecutorServiceAnnot) annotation; ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo(); executorServiceInfo.setExecutorServicesClass(executorServiceClass); executorServiceInfo.setMethod(method); System.out.println(executorServiceAnnot.servicename()); System.out.println("method.getName()=" + method.getName()); System.out.println("method.getParameterTypes()=" + method.getParameterTypes()); executorServiceInfo.setMethodParams(method.getParameterTypes()); //System.out.println("method="+executorServiceAnnot.servicename()); //System.out.println("method info="+executorServiceInfo); //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception(); servicesMap.put(executorServiceAnnot.servicename(), executorServiceInfo); } } } } }
From source file:com.app.services.ExecutorServicesConstruct.java
public void getExecutorServices(Digester serverdigester, Hashtable servicesMap, File exectorServicesXml, WebClassLoader customClassLoader) throws Exception { ExecutorServices executorServices = (ExecutorServices) serverdigester .parse(new InputSource(new FileInputStream(exectorServicesXml))); CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices(); ExecutorServiceAnnot executorServiceAnnot; for (ExecutorService executorService : executorServicesList) { Class executorServiceClass = customClassLoader .loadClass(executorService.getExecutorserviceclass().toString()); //log.info("executor class in ExecutorServicesConstruct"+executorServiceClass); //log.info(); Method[] methods = executorServiceClass.getDeclaredMethods(); for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof ExecutorServiceAnnot) { executorServiceAnnot = (ExecutorServiceAnnot) annotation; ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo(); executorServiceInfo.setExecutorServicesClass(executorServiceClass); executorServiceInfo.setMethod(method); //log.info(executorServiceAnnot.servicename()); //log.info("method.getName()="+method.getName()); //log.info("method.getParameterTypes()="+method.getParameterTypes()); executorServiceInfo.setMethodParams(method.getParameterTypes()); //log.info("method="+executorServiceAnnot.servicename()); //log.info("method info="+executorServiceInfo); //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception(); servicesMap.put(executorServiceAnnot.servicename(), executorServiceInfo); }//from w ww . j a v a 2 s . com } } } }