Example usage for java.lang.reflect Field getAnnotation

List of usage examples for java.lang.reflect Field getAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotation.

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:candr.yoclip.option.OptionPropertiesFieldTest.java

public OptionPropertiesFieldTest() throws NoSuchFieldException {
    final Field field = getClass().getDeclaredField("properties");
    final OptionProperties properties = field.getAnnotation(OptionProperties.class);
    test = new OptionPropertiesField<OptionPropertiesFieldTest>(properties, field);
}

From source file:com.thoughtworks.go.server.service.RailsAssetsServiceTest.java

@Test
public void shouldHaveAssetsAsTheSerializedNameForAssetsMapInRailsAssetsManifest_ThisIsRequiredSinceManifestFileGeneratedBySprocketsHasAMapOfAssetsWhichThisServiceNeedsAccessTo() {
    List<Field> fields = new ArrayList<>(
            Arrays.asList(RailsAssetsService.RailsAssetsManifest.class.getDeclaredFields()));
    List<Field> fieldsAnnotatedWithSerializedNameAsAssets = fields.stream().filter(new Predicate<Field>() {
        @Override/*w ww  . j a va 2  s  .  c o m*/
        public boolean test(Field field) {
            if (field.isAnnotationPresent(SerializedName.class)) {
                SerializedName annotation = field.getAnnotation(SerializedName.class);
                if (annotation.value().equals("assets")) {
                    return true;
                }
                return false;
            }
            return false;
        }
    }).collect(Collectors.toList());
    assertThat("Expected a field annotated with SerializedName 'assets'",
            fieldsAnnotatedWithSerializedNameAsAssets.isEmpty(), is(false));
    assertThat(fieldsAnnotatedWithSerializedNameAsAssets.size(), is(1));
    assertThat(fieldsAnnotatedWithSerializedNameAsAssets.get(0).getType().getCanonicalName(),
            is(HashMap.class.getCanonicalName()));
}

From source file:com.madrobot.di.wizard.json.JSONSerializer.java

private String getKeyName(final Field field) {
    if (field.isAnnotationPresent(SerializedName.class)) {
        SerializedName serializedName = field.getAnnotation(SerializedName.class);
        return serializedName.value();
    } else {//from w  w  w . j av a  2  s .co m
        return field.getName();
    }
}

From source file:io.github.huherto.springyRecords.BaseTable.java

public Number insert(R record) {

    Field[] fields = recordClass().getFields();
    String autoIncrementFieldName = null;
    if (autoIncrementField != null)
        autoIncrementFieldName = autoIncrementField.getName();
    Map<String, Object> parameters = new HashMap<String, Object>(fields.length);
    for (Field field : fields) {
        int mod = field.getModifiers();
        if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
            Column col = field.getAnnotation(Column.class);
            if (col != null && !field.getName().equals(autoIncrementFieldName)) {
                Object value;//  ww w .  j av a  2  s  .c  om
                try {
                    value = field.get(record);
                    parameters.put(col.name(), value);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    if (autoIncrementField != null)
        return insertCommand.executeAndReturnKey(parameters);
    insertCommand.execute(parameters);
    return null;
}

From source file:candr.yoclip.option.OptionPropertiesFieldTest.java

@Test(expected = OptionsBadTypeException.class)
public void initBadType() throws NoSuchFieldException {
    final Field field = getClass().getDeclaredField("badProperties");
    final OptionProperties properties = field.getAnnotation(OptionProperties.class);
    new OptionPropertiesField(properties, field);
}

From source file:be.redlab.context.spring.support.AutowiredFieldSetter.java

/**
 * {@inheritDoc} First calls {@link DefaultFieldSetter#getValue} method if the value is not found.
 *
 * @throws UnsettableFieldException/*from  w w w  .  j  ava2s  .  c o  m*/
 */
@Override
protected Object getValue(final Field f, final Object o, final ApplicationContext context)
        throws UnsettableFieldException {
    Object value = null;
    value = super.getValue(f, o, context);
    if (null == value) {
        Autowired auto = f.getAnnotation(Autowired.class);
        if (null != auto) {
            value = context.find(f.getType());
            setApplicationContext(f, context, value);
            if (null == value && auto.required()) {
                throw new UnsettableFieldException(String.format("Required field %s could not be set for %s",
                        f.getName(), o.getClass().getName()));
            }
        }
    }
    return value;
}

From source file:management.limbr.ui.entity.EntityEditorViewImpl.java

private Field<?> getUIField(java.lang.reflect.Field field) {
    String label = messages.get(field.getName() + "FieldLabel");
    Password passwordAnnotation = field.getAnnotation(Password.class);
    if (passwordAnnotation != null) {
        return new PasswordField(label);
    } else if (field.getType().isEnum()) {
        ComboBox comboBox = new ComboBox(label);
        comboBox.setTextInputAllowed(false);
        comboBox.addItems(field.getType().getEnumConstants());
        return comboBox;
    }//from w  w w  . ja  v  a  2  s  . co  m
    return new TextField(label);
}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

private void injectValue(final Object object, final Field field) {
    //Should be null due we ask before if the annotation exists !!
    String propertyToUseName = field.getAnnotation(InjectValue.class).useProperty();
    try {//from   w ww .  j  a  va  2 s  .  c  o  m
        Object propertyValue = PropertyUtils.getProperty(object, propertyToUseName);
        Object valueToInject = injectValueFactory.getObjectFor(
                PropertyUtils.getPropertyType(object, field.getName()), propertyValue, propertyToUseName,
                object);
        if (valueToInject != null) {
            PropertyUtils.setProperty(object, field.getName(), valueToInject);
        }
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        log.error("Unable to inject value " + field.getName() + " for class " + object.getClass(), e);
    }
}

From source file:ro.pippo.spring.AnnotationFieldValueProvider.java

@Override
public Object getFieldValue(Field field, Object fieldOwner) {
    if (!supportsField(field)) {
        return null;
    }//from   ww  w .  j  a va 2 s.  c o  m

    Named named = field.getAnnotation(Named.class);
    String name = (named != null) ? named.value() : "";

    log.debug("Search bean for field '{}' of '{}'", field.getName(), fieldOwner);

    String beanName = getBeanName(field, name);
    if (beanName == null) {
        log.warn("Bean name is null");
        return null;
    }

    Class<?> beanType = field.getType();

    log.debug("Retrieve bean with name '{}' and type '{}'", beanName, beanType);
    Object bean = applicationContext.getBean(beanName, beanType);
    log.debug("Found bean '{}'", bean);

    return bean;
}

From source file:com.jedi.oracle.OracleCall.java

private String createSQL(String queryName) {
    List<Field> fields = FieldUtils.getFieldsListWithAnnotation(getClass(), OracleParameterMapping.class);
    String retVal = "";
    String params = "";

    if (fields != null && !fields.isEmpty()) {
        List<Field> orderingFields = Ordering.natural().nullsFirst().onResultOf(new Function<Field, Integer>() {
            public Integer apply(Field field) {
                OracleParameterMapping mapping = field.getAnnotation(OracleParameterMapping.class);
                return mapping.index();
            }//w  w  w  . jav a  2 s  . c  o m
        }).sortedCopy(fields);

        for (Field field : orderingFields) {
            OracleParameterMapping mapping = field.getAnnotation(OracleParameterMapping.class);
            switch (mapping.direction()) {
            case ReturnValue:
                retVal = "? :=";
                break;
            default:
                if (params.indexOf(',') == -1) {
                    params += mapping.name() + "?";
                } else {
                    params += ", " + mapping.name() + "?";
                }
                break;
            }
        }
    }

    StringBuilder sb = new StringBuilder();
    sb.append("BEGIN");
    sb.append(" ");
    if (!retVal.isEmpty()) {
        sb.append(retVal);
        sb.append(" ");
    }
    sb.append(queryName);
    sb.append("(");
    if (!params.isEmpty()) {
        sb.append(params);
    }
    sb.append(");");
    sb.append(" ");
    sb.append("END;");

    return sb.toString();

}