List of usage examples for java.lang.annotation Annotation getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
/** * Access to the value of a field in an annotation using reflection. * @param annotation the annotation object * @param field the name of the field. In fact implemented as a method. * @return object. The result must then be coerced. *///ww w.j av a 2s . co m public static Object getAnnotationField(Annotation annotation, String field) { Class<?> annotClass = annotation.getClass(); try { Method getValue = annotClass.getMethod(field); return getValue.invoke(annotation); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:org.openinfinity.core.util.AspectUtil.java
/** * Returns the specified annotation.// ww w . jav a 2 s .com * * @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:ReflectionHelper.java
@SuppressWarnings("unchecked") public static <T> T getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) { try {/*from ww w.ja va 2s . com*/ Method m = annotation.getClass().getMethod(parameterName); Object o = m.invoke(annotation); if (o.getClass().getName().equals(type.getName())) { return (T) o; } else { String msg = "Wrong parameter type. Expected: " + type.getName() + " Actual: " + o.getClass().getName(); throw new RuntimeException(msg); } } catch (NoSuchMethodException e) { String msg = "The specified annotation defines no parameter '" + parameterName + "'."; throw new RuntimeException(msg, e); } catch (IllegalAccessException e) { String msg = "Unable to get '" + parameterName + "' from " + annotation.getClass().getName(); throw new RuntimeException(msg, e); } catch (InvocationTargetException e) { String msg = "Unable to get '" + parameterName + "' from " + annotation.getClass().getName(); throw new RuntimeException(msg, e); } }
From source file:org.dbg4j.core.DebugUtils.java
/** * Checking if the given annotation exists in the provided array of annotations. * * @param annotations array of annotations (may be <code>null</code>) * @param annotation annotation to search * @return//from w w w.ja v a 2 s. co m */ public static boolean containsAnnotation(@Nullable Annotation[] annotations, @Nonnull Class annotation) { if (annotations == null || annotations.length == 0) { return false; } for (Annotation a : annotations) { if (annotation.isAssignableFrom(a.getClass())) { return true; } } return false; }
From source file:org.dbg4j.core.DebugUtils.java
/** * Retrieve the annotation of given type from array of annotations * * @param annotations//from ww w .j a va 2 s .c om * @param annotation * @return */ @Nullable public static <T> T getAnnotation(@Nonnull Annotation[] annotations, @Nonnull Class<T> annotation) { if (annotations == null || annotations.length == 0) { return null; } for (Annotation a : annotations) { if (annotation.isAssignableFrom(a.getClass())) { return (T) a; } } return null; }
From source file:com.wrmsr.search.dsl.util.DerivedSuppliers.java
private static void cloneParameterAnnotation(MethodDefinition methodDefinition, int parameterIndex, Annotation annotation) throws ReflectiveOperationException { Class<?> annotationInterface = getOnlyElement(ImmutableList.copyOf(annotation.getClass().getInterfaces())); AnnotationDefinition annotationDefinition = methodDefinition.declareParameterAnnotation(annotationInterface, parameterIndex);/* ww w . j av a2 s .c om*/ for (java.lang.reflect.Method interfaceMethod : annotationInterface.getDeclaredMethods()) { String name = interfaceMethod.getName(); Object value = interfaceMethod.invoke(annotation); // :| java.lang.reflect.Method setValueMethod = AnnotationDefinition.class.getDeclaredMethod("setValue", String.class, value.getClass()); setValueMethod.invoke(annotationDefinition, name, value); } }
From source file:com.espertech.esper.epl.annotation.AnnotationUtil.java
public static Annotation findAnnotation(Annotation[] annotations, Class annotationClass) { if (!annotationClass.isAnnotation()) { throw new IllegalArgumentException( "Class " + annotationClass.getName() + " is not an annotation class"); }//from w w w . j a va 2 s .co m if (annotations == null || annotations.length == 0) { return null; } for (Annotation anno : annotations) { if (JavaClassHelper.isImplementsInterface(anno.getClass(), annotationClass)) { return anno; } } return null; }
From source file:com.espertech.esper.epl.annotation.AnnotationUtil.java
public static List<Annotation> findAnnotations(Annotation[] annotations, Class annotationClass) { if (!annotationClass.isAnnotation()) { throw new IllegalArgumentException( "Class " + annotationClass.getName() + " is not an annotation class"); }/*from w ww . j av a 2 s. c o m*/ if (annotations == null || annotations.length == 0) { return null; } List<Annotation> annotationsList = new ArrayList<Annotation>(); for (Annotation anno : annotations) { if (JavaClassHelper.isImplementsInterface(anno.getClass(), annotationClass)) { annotationsList.add(anno); } } return annotationsList; }
From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java
public static void initAnnotatedFields(Class clazz, Object object, ViewFinder viewFinder, boolean initForLazy) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {/*ww w . ja v a2 s.c o m*/ Field fields[] = clazz.getDeclaredFields(); Map<Class, Object> targetListenerCache = new HashMap<Class, Object>(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; Annotation[] annotations = field.getAnnotations(); if (annotations == null || annotations.length == 0) { continue; } for (int j = 0; j < annotations.length; ++j) { Annotation anno = annotations[j]; if (!InjectView.class.isAssignableFrom(anno.getClass())) { continue; } InjectView injectViewAnno = (InjectView) anno; if ((!initForLazy && injectViewAnno.lazy()) || (initForLazy && !injectViewAnno.lazy())) { continue; } View view = viewFinder.findViewById(injectViewAnno.value()); if (view == null) { if (initForLazy && injectViewAnno.lazy()) { // view == null is tolerable in this case, we assume that this field // be injected later with another call to ViewWrapper.lazyInitializeLayout(). continue; } throw new IllegalArgumentException("View 0x" + Integer.toHexString(injectViewAnno.value()) + " specified in @InjectView on this field is not found: " + field.getName() + ", if this field is meant to be injected lazily, remember to specify the 'lazy' attribute."); } try { field.setAccessible(true); field.set(object, view); } catch (IllegalAccessException e) { String errMsg = "@InjectView() on '" + field.getName() + "' failed. "; if (field.getType() != view.getClass()) { errMsg += (view.getClass().getSimpleName() + " cannot be cast to " + field.getType().getSimpleName()); } throw new IllegalAccessException(errMsg); } catch (IllegalArgumentException e) { String errMsg = "@InjectView() on '" + field.getName() + "' failed. "; if (field.getType() != view.getClass()) { errMsg += (view.getClass().getSimpleName() + " cannot be cast to " + field.getType().getSimpleName()); } throw new IllegalArgumentException(errMsg); } Class[] listenerTypes = injectViewAnno.listenerTypes(); if (listenerTypes == null || listenerTypes.length == 0) { continue; } Object targetListener = getTargetListener(clazz, object, targetListenerCache, injectViewAnno.listener(), "@InjectView"); if (targetListener == null) { targetListener = object; } AnnotationUtils.setListenersForView(view, injectViewAnno.listenerTypes(), targetListener); } } }
From source file:io.fabric8.forge.rest.dto.UICommands.java
protected static boolean isJsonObject(Object value) { if (value != null) { Class<?> aClass = value.getClass(); while (aClass != null && !aClass.equals(Object.class)) { Annotation[] annotations = aClass.getAnnotations(); if (annotations != null) { for (Annotation annotation : annotations) { String annotationClassName = annotation.getClass().getName(); if (annotationClassName != null && annotationClassName.startsWith("com.fasterxml.jackson.")) { // lets assume its a JSON DTO! return true; } else { String text = annotation.toString(); // because of the Forge proxying we can't just use the actual class here... if (text.indexOf("com.fasterxml.jackson.") >= 0) { return true; }//from w ww .j ava 2 s . c om } } } aClass = aClass.getSuperclass(); } } return false; }