/** * Fetches all TelemetryFields and create a map associating field's name (key) to field's value (value). * @return a map with key (field's name) and value (field's value). */ public Map<String, ?> createTelemetryFieldToValue() { return AccessController.doPrivileged((PrivilegedAction<Map<String, ?>>) () -> { telemetryFieldToValue = new HashMap<>(); Set<Field> fields = ReflectionUtils.getFields(this.getClass(), ReflectionUtils.withAnnotation(TelemetryField.class)); for (Field field : fields) { try { field.setAccessible(true); telemetryFieldToValue.put(field.getName(), field.get(this)); } catch (IllegalAccessException e) { logger.error("The field is not inaccessible: ", e); } } return telemetryFieldToValue; }); }
public static <T> void inject(final Object object, final Class<? extends Annotation> annotation, final Map<Class<? extends T>, T> source) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { for (Field field : ReflectionUtils.getAllFields(object.getClass(), ReflectionUtils.withAnnotation(annotation))) { Object value = source.get(field.getType()); if (value != null) { try { field.setAccessible(true); field.set(object, value); } catch (IllegalAccessException e) { logger.error("Failed to inject value {} into field {}", value, field, e); } } else { logger.error("Failed to inject into field {}, nothing to inject", field); } } return null; }); }
public static void inject(final Object object, Context context) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { for (Field field : ReflectionUtils.getAllFields(object.getClass(), ReflectionUtils.withAnnotation(In.class))) { Object value = context.get(field.getType()); if (value != null) { try { field.setAccessible(true); field.set(object, value); } catch (IllegalAccessException e) { logger.error("Failed to inject value {} into field {} of {}", value, field, object, e); } } } return null; }); }
public static void inject(final Object object) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { for (Field field : ReflectionUtils.getAllFields(object.getClass(), ReflectionUtils.withAnnotation(In.class))) { Object value = CoreRegistry.get(field.getType()); if (value != null) { try { field.setAccessible(true); field.set(object, value); } catch (IllegalAccessException e) { logger.error("Failed to inject value {} into field {} of {}", value, field, object, e); } } } return null; }); }
/** * get all constructors annotated with a given annotation, including annotation member values matching * <p/>depends on MethodAnnotationsScanner configured */ public Set<Constructor> getConstructorsAnnotatedWith(final Annotation annotation) { return filter(getConstructorsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
/** * get all methods annotated with a given annotation, including annotation member values matching * <p/>depends on MethodAnnotationsScanner configured */ public Set<Method> getMethodsAnnotatedWith(final Annotation annotation) { return filter(getMethodsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
/** * get all methods annotated with a given annotation, including annotation member values matching * <p/>depends on FieldAnnotationsScanner configured */ public Set<Field> getFieldsAnnotatedWith(final Annotation annotation) { return filter(getFieldsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
@Override @SuppressWarnings({"unchecked" }) public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Set<Field> requestFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Request.class)); for(Field field : requestFields) { beanMap.put(field.getName(), request); } }
/** * get all methods annotated with a given annotation, including annotation member values matching * <p/>depends on FieldAnnotationsScanner configured */ public Set<Field> getFieldsAnnotatedWith(final Annotation annotation) { return filter(getFieldsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
@Override public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Map<String, Object> fieldMap = new HashMap<String, Object>(); Set<Field> htmlFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(HtmlField.class)); for (Field htmlField : htmlFields) { Object value = injectHtmlField(request, response, htmlField, bean.getClass()); if(value != null) { fieldMap.put(htmlField.getName(), value); } } beanMap.putAll(fieldMap); }
/** * get all methods annotated with a given annotation, including annotation member values matching * <p/>depends on MethodAnnotationsScanner configured */ public Set<Method> getMethodsAnnotatedWith(final Annotation annotation) { return filter(getMethodsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
/** * get all constructors annotated with a given annotation, including annotation member values matching * <p/>depends on MethodAnnotationsScanner configured */ public Set<Constructor> getConstructorsAnnotatedWith(final Annotation annotation) { return filter(getConstructorsAnnotatedWith(annotation.annotationType()), withAnnotation(annotation)); }
/** * Get a list of all the telemetry field names marked with {@link org.terasology.telemetry.TelemetryField} annotation in this class. * The field name is in the form telemetryCategory.id() + ":" fieldName. * @return the list of all the telemetry field names in this class. */ public List<String> createTelemetryFieldList() { TelemetryCategory telemetryCategory = this.getClass().getAnnotation(TelemetryCategory.class); List<String> fieldsList = new ArrayList<>(); if (!telemetryCategory.isOneMapMetric()) { Set<Field> fields = ReflectionUtils.getFields(this.getClass(), ReflectionUtils.withAnnotation(TelemetryField.class)); for (Field field : fields) { String fieldName = telemetryCategory.id() + ":" + field.getName(); fieldsList.add(fieldName); } } return fieldsList; }
@Override @SuppressWarnings("unchecked") public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Map<String, Object> fieldMap = new HashMap<String, Object>(); Set<Field> ajaxFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Ajax.class)); for (Field ajaxField : ajaxFields) { Object value = injectAjaxField(request, beanMap, ajaxField); if(value != null) { fieldMap.put(ajaxField.getName(), value); } } beanMap.putAll(fieldMap); }
@Override @SuppressWarnings("unchecked") public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Map<String, Object> fieldMap = new HashMap<String, Object>(); Set<Field> imageFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Image.class)); for (Field imageField : imageFields) { Object value = injectImageField(request, beanMap, bean, imageField); if(value != null) { fieldMap.put(imageField.getName(), value); } } beanMap.putAll(fieldMap); }
/** * Registers all available command methods annotated with {@link org.terasology.logic.console.commandSystem.annotations.Command}. */ public static void registerAvailable(Object provider, Console console, Context context) { Predicate<? super Method> predicate = Predicates.<Method>and(ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withAnnotation(Command.class)); Set<Method> commandMethods = ReflectionUtils.getAllMethods(provider.getClass(), predicate); for (Method method : commandMethods) { if (!hasSenderAnnotation(method)) { logger.error("Command {} provided by {} contains a EntityRef without @Sender annotation, may cause a NullPointerException", method.getName(), provider.getClass().getSimpleName()); } logger.debug("Registering command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName()); try { SpecificAccessibleObject<Method> specificMethod = new SpecificAccessibleObject<>(method, provider); MethodCommand command = referringTo(specificMethod, context); console.registerCommand(command); logger.debug("Registered command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName()); } catch (RuntimeException t) { logger.error("Failed to load command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName(), t); } } }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation member values matching * <p>{@link java.lang.annotation.Inherited} is honored according to given honorInherited * <p/>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated(names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
public ReflectionProvider(T target, Context context) { try { ReflectFactory reflectFactory = context.get(ReflectFactory.class); CopyStrategyLibrary copyStrategies = context.get(CopyStrategyLibrary.class); ClassMetadata<T, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), (Class<T>) target.getClass(), reflectFactory, copyStrategies); for (Field field : getAllFields(target.getClass(), and(withAnnotation(Range.class), or(withType(Float.TYPE), withType(Float.class))))) { Range range = field.getAnnotation(Range.class); FieldMetadata<T, Float> fieldMetadata = (FieldMetadata<T, Float>) classMetadata.getField(field.getName()); Property property = new FloatProperty(target, fieldMetadata, range.min(), range.max()); properties.add(property); } } catch (NoSuchMethodException e) { logger.error("Cannot provide provide inspection for {}, does not have a default constructor", target.getClass()); } }
/** * Add a listener to the checkbox in the telemetry category row. * If this checkbox is checked, all the sub telemetry fields will be enabled/disabled. */ private void addGroupEnablingListener() { fetchTelemetryCategoriesFromEnvironment(); for (Map.Entry<TelemetryCategory, Class> telemetryCategory: telemetryCategories.entrySet()) { if (!telemetryCategory.getKey().isOneMapMetric()) { UICheckbox uiCheckbox = this.find(telemetryCategory.getKey().id(), UICheckbox.class); if (uiCheckbox == null) { continue; } uiCheckbox.subscribe((checkbox) -> { Map<String, Boolean> bindingMap = config.getTelemetryConfig().getMetricsUserPermissionConfig().getBindingMap(); if (bindingMap.containsKey(telemetryCategory.getKey().id())) { boolean isGroupEnable = bindingMap.get(telemetryCategory.getKey().id()); Set<Field> fields = ReflectionUtils.getFields(telemetryCategory.getValue(), ReflectionUtils.withAnnotation(TelemetryField.class)); for (Field field : fields) { String fieldName = telemetryCategory.getKey().id() + ":" + field.getName(); bindingMap.put(fieldName, isGroupEnable); } } }); } } }