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:org.springframework.data.elasticsearch.core.DefaultResultMapper.java

private <T> void populateScriptFields(T result, SearchHit hit) {
    if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
        for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
            ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
            if (scriptedField != null) {
                String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
                SearchHitField searchHitField = hit.getFields().get(name);
                if (searchHitField != null) {
                    field.setAccessible(true);
                    try {
                        field.set(result, searchHitField.getValue());
                    } catch (IllegalArgumentException e) {
                        throw new ElasticsearchException("failed to set scripted field: " + name
                                + " with value: " + searchHitField.getValue(), e);
                    } catch (IllegalAccessException e) {
                        throw new ElasticsearchException("failed to access scripted field: " + name, e);
                    }//  w  ww . j av  a  2  s .  c  o  m
                }
            }
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.BrowserVersionFeaturesTest.java

/**
 * Test of usage./*from w w  w  .j  a  v a  2  s . c  o  m*/
 * @throws Exception in case of problems
 */
@Test
public void unusedFeatures() throws Exception {
    final List<BrowserVersion> browsers = new LinkedList<>();
    browsers.add(BrowserVersion.FIREFOX_31);
    browsers.add(BrowserVersion.FIREFOX_38);
    browsers.add(BrowserVersion.INTERNET_EXPLORER_11);
    browsers.add(BrowserVersion.CHROME);
    browsers.add(BrowserVersion.EDGE);

    for (final BrowserVersionFeatures feature : BrowserVersionFeatures.values()) {
        int useCount = 0;
        for (BrowserVersion browserVersion : browsers) {
            if (browserVersion.hasFeature(feature)) {
                useCount++;
            }
        }
        assertTrue("BrowserVersionFeatures.java: '" + feature.name() + "' in no longer in use.", useCount > 0);
        assertTrue(
                "BrowserVersionFeatures.java: '" + feature.name() + "' is enabled for all supported browsers.",
                useCount < browsers.size());
    }

    for (final BrowserVersionFeatures feature : BrowserVersionFeatures.values()) {
        final Field field = BrowserVersionFeatures.class.getField(feature.name());
        final BrowserFeature browserFeature = field.getAnnotation(BrowserFeature.class);

        if (browserFeature != null) {
            for (final WebBrowser annotatedBrowser : browserFeature.value()) {
                boolean inUse = false;
                for (BrowserVersion supportedBrowser : browsers) {
                    if (expectedBrowserName(supportedBrowser).equals(annotatedBrowser.value().name())
                            && annotatedBrowser.minVersion() <= supportedBrowser.getBrowserVersionNumeric()
                            && annotatedBrowser.maxVersion() >= supportedBrowser.getBrowserVersionNumeric()) {
                        inUse = true;
                        continue;
                    }
                }
                assertTrue("BrowserVersionFeatures.java: Annotation '" + annotatedBrowser.toString()
                        + "' of feature '" + feature.name() + "' in no longer in use.", inUse);
            }
        }
    }
}

From source file:com.impetus.kundera.metadata.processor.relation.ManyToOneRelationMetadataProcessor.java

@Override
public void addRelationIntoMetadata(Field relationField, EntityMetadata metadata) {
    // taking field's type as foreign entity, ignoring "targetEntity"
    Class<?> targetEntity = relationField.getType();

    ManyToOne ann = relationField.getAnnotation(ManyToOne.class);
    Relation relation = new Relation(relationField, targetEntity, null, ann.fetch(),
            Arrays.asList(ann.cascade()), ann.optional(), null, // mappedBy is null
            Relation.ForeignKey.MANY_TO_ONE);

    boolean isJoinedByFK = relationField.isAnnotationPresent(JoinColumn.class);

    if (relationField.isAnnotationPresent(AssociationOverride.class)) {
        AssociationOverride annotation = relationField.getAnnotation(AssociationOverride.class);
        JoinColumn[] joinColumns = annotation.joinColumns();

        relation.setJoinColumnName(joinColumns[0].name());

    } else if (isJoinedByFK) {
        JoinColumn joinColumnAnn = relationField.getAnnotation(JoinColumn.class);
        relation.setJoinColumnName(//from   w ww .j  av  a  2s .c  om
                StringUtils.isBlank(joinColumnAnn.name()) ? relationField.getName() : joinColumnAnn.name());
    } else {
        relation.setJoinColumnName(relationField.getName());
    }

    relation.setBiDirectionalField(metadata.getEntityClazz());
    metadata.addRelation(relationField.getName(), relation);

}

From source file:gr.abiss.calipso.fs.FilePersistenceService.java

/**
 * The method saves the given multipart file to the path specified, ignoring the original file name.
 * @param fileField /* w w w .j  a va2 s .  c  o m*/
 * @param multipartFile
 * @param path
 * @return the UR: for the saved file
 */
public default String saveFile(Field fileField, FileDTO file) {
    String url = null;
    try {

        FilePersistence config = fileField.getAnnotation(FilePersistence.class);
        // ensure accepted content type
        validateContentType(file.getContentType(), config);
        BufferedImage img = ImageIO.read(file.getIn());

        // if image that needs scaling
        if (isImage(file.getContentType()) && (config.maxHeight() > 0 || config.maxWidth() > 0)) {
            url = saveScaled(img, file.getContentType(), config.maxWidth(), config.maxHeight(), file.getPath());
        } else {
            url = saveFile(img, file.getContentLength(), file.getContentType(), file.getPath());
        }

        // generate previews?
        Map<String, FilePersistencePreview> previews = getPreviews(fileField);
        if (isImage(file.getContentType()) && MapUtils.isNotEmpty(previews)) {
            for (String key : previews.keySet()) {
                FilePersistencePreview previewConfig = previews.get(key);
                saveScaled(img, file.getContentType(), previewConfig.maxWidth(), previewConfig.maxHeight(),
                        file.getPath() + "_" + key);
            }
        }
        //cleanup
        file.getIn().close();

    } catch (IOException e) {
        throw new RuntimeException("Failed persisting file", e);
    }

    return url;
}

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

@Test
public void getPropertyDescriptionsWithoutDescriptions() throws NoSuchFieldException {
    final Field field = getClass().getDeclaredField("noPropertyDescriptions");
    final OptionProperties properties = field.getAnnotation(OptionProperties.class);
    assertThat("no property descriptions",
            new OptionPropertiesField<OptionPropertiesFieldTest>(properties, field).getPropertyDescriptions()
                    .size(),/* w w w .  j a  va  2  s .  c  om*/
            is(0));
}

From source file:br.msf.commons.persistence.springframework.validation.AnnotatedEntityValidator.java

private boolean isSkipValidation(final Field field) throws Exception {
    final SkipValidation skip = field.getAnnotation(SkipValidation.class);
    return skip != null && skip.value();
}

From source file:com.l2jfree.config.model.ConfigFieldInfo.java

public ConfigFieldInfo(Field field) throws InstantiationException, IllegalAccessException {
    _field = field;//from  w  w  w .  ja  v a2  s .  c  o  m
    _configField = field.getAnnotation(ConfigField.class);
    _converter = getConfigField().converter().newInstance();
    _postLoadHook = getConfigField().postLoadHook().newInstance();
    _configGroupBeginning = field.getAnnotation(ConfigGroupBeginning.class);
    _configGroupEnding = field.getAnnotation(ConfigGroupEnding.class);

    // to standardize the default values (true, True -> true, etc..)
    final String value = getConfigField().value();
    final Object obj = getConverter().convertFromString(getField().getType(), value);

    _defaultValue = getConverter().convertToString(getField().getType(), obj);
}

From source file:org.vaadin.spring.i18n.Translator.java

private void analyzeFields(Class<?> clazz) {
    for (Field f : clazz.getDeclaredFields()) {
        if (f.isAnnotationPresent(TranslatedProperty.class)) {
            translatedFields.put(f.getAnnotation(TranslatedProperty.class), f);
        } else if (f.isAnnotationPresent(TranslatedProperties.class)) {
            for (TranslatedProperty annotation : f.getAnnotation(TranslatedProperties.class).value()) {
                translatedFields.put(annotation, f);
            }//from  ww  w  . j av  a2s  . co m
        }
    }
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

@Override
protected boolean isMandatory(Field field) {
    Column columnAnnotation = field.getAnnotation(Column.class);
    OneToOne oneToOneAnnotation = field.getAnnotation(OneToOne.class);
    OneToMany oneToManyAnnotation = field.getAnnotation(OneToMany.class);
    ManyToOne manyToOneAnnotation = field.getAnnotation(ManyToOne.class);
    ManyToMany manyToManyAnnotation = field.getAnnotation(ManyToMany.class);

    if (columnAnnotation != null) {
        return !columnAnnotation.nullable();
    } else if (oneToOneAnnotation != null) {
        return !oneToOneAnnotation.optional();
    } else if (oneToManyAnnotation != null) {
        return false;
    } else if (manyToOneAnnotation != null) {
        return !manyToOneAnnotation.optional();
    } else if (manyToManyAnnotation != null) {
        return false;
    } else {/* w w  w  .  j  av a2 s .c  o  m*/
        return super.isMandatory(field);
    }
}

From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java

@Override
protected String getInverseField(Field field) {
    OneToMany oneToManyAnnotation = field.getAnnotation(OneToMany.class);
    if (oneToManyAnnotation != null)
        return isBlank(oneToManyAnnotation.mappedBy()) ? null : oneToManyAnnotation.mappedBy();

    ManyToMany manyToManyAnnotation = field.getAnnotation(ManyToMany.class);
    if (manyToManyAnnotation != null)
        return isBlank(manyToManyAnnotation.mappedBy()) ? null : manyToManyAnnotation.mappedBy();

    OneToOne oneToOneAnnotation = field.getAnnotation(OneToOne.class);
    if (oneToOneAnnotation != null)
        return isBlank(oneToOneAnnotation.mappedBy()) ? null : oneToOneAnnotation.mappedBy();

    return null;/*from   w  ww  .j av a 2  s.c o  m*/
}