List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:edu.usu.sdl.openstorefront.core.util.EntityUtil.java
/** * Compares to object of the same type//from w w w .j a v a 2s . co m * * @param original * @param compare * @param consumeFieldsOnly * @return True is different, false if the same */ public static boolean isObjectsDifferent(Object original, Object compare, boolean consumeFieldsOnly) { boolean changed = false; if (original != null && compare == null) { changed = true; } else if (original == null && compare != null) { changed = true; } else if (original != null && compare != null) { if (original.getClass().isInstance(compare)) { List<Field> fields = getAllFields(original.getClass()); for (Field field : fields) { boolean check = true; if (consumeFieldsOnly) { ConsumeField consume = (ConsumeField) field.getAnnotation(ConsumeField.class); if (consume == null) { check = false; } } if (check) { try { changed = isFieldsDifferent(BeanUtils.getProperty(original, field.getName()), BeanUtils.getProperty(compare, field.getName())); if (changed) { break; } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new OpenStorefrontRuntimeException("Can't compare object types", ex); } } } } else { throw new OpenStorefrontRuntimeException("Can't compare different object types", "Check objects"); } } return changed; }
From source file:com.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildDialog(Class<? extends Object> formClass, ActionList actions, FormLayout layout) {/* w w w. jav a 2 s . c o m*/ AForm formAnnotation = formClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); } MessageSupport messages = MessageSupport.getMessages(formClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(messages.get(formAnnotation.name())); XForm form = createForm(builder, layout); for (Field field : formClass.getFields()) { AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { addFormField(form, field, fieldAnnotation, messages); } catch (Exception e) { e.printStackTrace(); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? builder.buildOkCancelActions() : builder.buildOkCancelHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { actions.addActions(defaultActions); } XFormDialog dialog = builder.buildDialog(actions, messages.get(formAnnotation.description()), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:com.linkedin.pinot.perf.QueryRunner.java
private static void printUsage() { System.out.println("Usage: QueryRunner"); for (Field field : QueryRunner.class.getDeclaredFields()) { if (field.isAnnotationPresent(Option.class)) { Option option = field.getAnnotation(Option.class); System.out.println(String.format("\t%-15s: %s (required=%s)", option.name(), option.usage(), option.required())); }/* ww w . j a va 2 s . c o m*/ } }
From source file:com.htmlhifive.pitalium.core.config.PtlTestConfig.java
/** * {@link PtlConfigurationProperty}????????? * // w ww.ja va 2 s . c o m * @param object ? * @param arguments ? */ private static void fillConfigProperties(Object object, Map<String, String> arguments) { // Collect all fields include super classes Class clss = object.getClass(); List<Field> fields = new ArrayList<Field>(); Collections.addAll(fields, clss.getDeclaredFields()); while ((clss = clss.getSuperclass()) != Object.class) { Collections.addAll(fields, clss.getDeclaredFields()); } for (Field field : fields) { PtlConfigurationProperty propertyConfig = field.getAnnotation(PtlConfigurationProperty.class); if (propertyConfig == null) { PtlConfiguration config = field.getAnnotation(PtlConfiguration.class); if (config == null) { continue; } // Field is nested config class try { field.setAccessible(true); Object prop = field.get(object); if (prop != null) { fillConfigProperties(prop, arguments); } } catch (TestRuntimeException e) { throw e; } catch (Exception e) { throw new TestRuntimeException(e); } continue; } String value = arguments.get(propertyConfig.value()); if (value == null) { continue; } try { Object applyValue = convertFromString(field.getType(), value); field.setAccessible(true); field.set(object, applyValue); LOG.trace("[Load config] override property ({}). [{} => {}]", clss.getSimpleName(), field.getName(), applyValue); } catch (TestRuntimeException e) { throw e; } catch (Exception e) { throw new TestRuntimeException("ConfigurationProperty convert error", e); } } }
From source file:com.eviware.x.form.support.ADialogBuilder.java
/** * Allow to use custom Ok, Cancel buttons... * <p/>//from www. j a v a 2s . c o m * This means user have to add control for closing dialog. * * @param formClass * @param actions * @param useDefaultOkCancel * @return */ public static XFormDialog buildDialog(Class<? extends Object> formClass, ActionList actions, boolean useDefaultOkCancel) { if (useDefaultOkCancel) { return buildDialog(formClass, actions); } AForm formAnnotation = formClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); } MessageSupport messages = MessageSupport.getMessages(formClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(messages.get(formAnnotation.name())); XForm form = createForm(builder, null); for (Field field : formClass.getFields()) { AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { addFormField(form, field, fieldAnnotation, messages); } catch (Exception e) { e.printStackTrace(); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? null : builder.buildHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { // since there is only one action do it like this actions.insertAction(defaultActions.getActionAt(0), 0); } XFormDialog dialog = builder.buildDialog(actions, messages.get(formAnnotation.description()), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:com.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildTabbedDialog(Class<? extends Object> tabbedFormClass, ActionList actions) { AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); }//from w w w .j a v a 2 s . c om MessageSupport messages = MessageSupport.getMessages(tabbedFormClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name()); for (Field field : tabbedFormClass.getFields()) { APage pageAnnotation = field.getAnnotation(APage.class); if (pageAnnotation != null) { buildForm(builder, pageAnnotation.name(), field.getType(), messages); } AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { Class<?> formClass = Class.forName(fieldAnnotation.description()); buildForm(builder, fieldAnnotation.name(), formClass, messages); } catch (Exception e) { SoapUI.logError(e); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? builder.buildOkCancelActions() : builder.buildOkCancelHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { actions.addActions(defaultActions); } XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:com.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildTabbedDialogWithCustomActions(Class<? extends Object> tabbedFormClass, ActionList actions) {//from w w w . j a v a2 s . co m AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); } MessageSupport messages = MessageSupport.getMessages(tabbedFormClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name()); for (Field field : tabbedFormClass.getFields()) { APage pageAnnotation = field.getAnnotation(APage.class); if (pageAnnotation != null) { buildForm(builder, pageAnnotation.name(), field.getType(), messages); } AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { Class<?> formClass = Class.forName(fieldAnnotation.description()); buildForm(builder, fieldAnnotation.name(), formClass, messages); } catch (Exception e) { SoapUI.logError(e); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? null : builder.buildHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { defaultActions.addActions(actions); actions = defaultActions; } XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:com.hurence.logisland.util.kura.Metrics.java
public static <T> T readFrom(final T object, final Map<String, Object> metrics) { Objects.requireNonNull(object); for (final Field field : FieldUtils.getFieldsListWithAnnotation(object.getClass(), Metric.class)) { final Metric m = field.getAnnotation(Metric.class); final boolean optional = field.isAnnotationPresent(Optional.class); final Object value = metrics.get(m.value()); if (value == null && !optional) { throw new IllegalArgumentException( String.format("Field '%s' is missing metric '%s'", field.getName(), m.value())); }//from www.jav a 2 s . c om if (value == null) { // not set but optional continue; } try { FieldUtils.writeField(field, object, value, true); } catch (final IllegalArgumentException e) { // provide a better message throw new IllegalArgumentException(String.format("Failed to assign '%s' (%s) to field '%s'", value, value.getClass().getName(), field.getName()), e); } catch (final IllegalAccessException e) { throw new RuntimeException(e); } } for (final Method method : MethodUtils.getMethodsListWithAnnotation(object.getClass(), Metric.class)) { final Metric m = method.getAnnotation(Metric.class); final boolean optional = method.isAnnotationPresent(Optional.class); final Object value = metrics.get(m.value()); if (value == null && !optional) { throw new IllegalArgumentException( String.format("Method '%s' is missing metric '%s'", method.getName(), m.value())); } if (value == null) { // not set but optional continue; } try { method.invoke(object, value); } catch (final IllegalArgumentException e) { // provide a better message throw new IllegalArgumentException(String.format("Failed to call '%s' (%s) with method '%s'", value, value.getClass().getName(), method.getName()), e); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } return object; }
From source file:io.github.eternalbits.compactvd.CompactVD.java
private static void dump(Object obj, String in) { for (Field fld : obj.getClass().getDeclaredFields()) { try {/*from w ww. j a va 2s . c o m*/ if (!Modifier.isPrivate(fld.getModifiers())) { if (fld.getAnnotation(Deprecated.class) == null) { if (!fld.getType().isAssignableFrom(List.class)) { System.out.println(in + fld.getName() + ": " + fld.get(obj)); } else { int i = 0; for (Object item : (List<?>) fld.get(obj)) { System.out.println(in + fld.getName() + "[" + i + "]"); dump(item, in + " "); i++; } } } } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } }
From source file:com.linkedin.pinot.tools.segment.converter.DictionaryToRawIndexConverter.java
/** * Helper method to print usage at the command line interface. */// w ww .j a v a 2 s . c o m private static void printUsage() { System.out.println("Usage: DictionaryTORawIndexConverter"); for (Field field : ColumnarToStarTreeConverter.class.getDeclaredFields()) { if (field.isAnnotationPresent(Option.class)) { Option option = field.getAnnotation(Option.class); System.out.println(String.format("\t%-15s: %s (required=%s)", option.name(), option.usage(), option.required())); } } }