Example usage for java.lang.reflect Field isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.katsu.jpa.dao.JpaDao.java

private boolean isOneToManyOrManyToManyField(Field f) {
    boolean result = (f.isAnnotationPresent(javax.persistence.OneToMany.class)
            || f.isAnnotationPresent(javax.persistence.ManyToMany.class));

    return result;
}

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

public default Map<String, FilePersistencePreview> getPreviews(Field fileField) {
    Map<String, FilePersistencePreview> previews = new HashMap<String, FilePersistencePreview>();
    if (fileField.isAnnotationPresent(FilePersistencePreviews.class)) {
        FilePersistencePreview[] tmp = fileField.getAnnotation(FilePersistencePreviews.class).value();
        if (tmp != null) {
            for (int i = 0; i < tmp.length; i++) {
                FilePersistencePreview preview = tmp[i];
                previews.put(preview.maxWidth() + "x" + preview.maxHeight(), preview);
            }//from   w ww . ja va 2 s .c  o  m
        }
    }
    if (fileField.isAnnotationPresent(FilePersistencePreview.class)) {
        FilePersistencePreview[] tmp = fileField.getAnnotationsByType(FilePersistencePreview.class);
        for (int i = 0; i < tmp.length; i++) {
            FilePersistencePreview preview = tmp[i];
            previews.put(preview.maxWidth() + "x" + preview.maxHeight(), preview);
        }
    }
    return previews;
}

From source file:net.ymate.platform.validation.ValidationMeta.java

/**
 * @param parentFieldName ??()/*from   w w w.ja va 2 s  .  co m*/
 * @param targetClass     
 * @return ?targetClassField?
 */
public Map<String, Annotation[]> __doGetMetaFromFields(String parentFieldName, Class<?> targetClass,
        Map<String, String> paramLabels) {
    Map<String, Annotation[]> _returnValues = new LinkedHashMap<String, Annotation[]>();
    //
    parentFieldName = StringUtils.trimToEmpty(parentFieldName);
    ClassUtils.BeanWrapper<?> _beanWrapper = ClassUtils.wrapper(targetClass);
    if (_beanWrapper != null) {
        for (String _fieldName : _beanWrapper.getFieldNames()) {
            // ??Field??
            VField _vField = null;
            Field _field = _beanWrapper.getField(_fieldName);
            if (_field.isAnnotationPresent(VField.class)) {
                _vField = _field.getAnnotation(VField.class);
                if (StringUtils.isNotBlank(_vField.name())) {
                    _fieldName = _vField.name();
                }
                if (StringUtils.isNotBlank(_vField.label())) {
                    __labels.put(_fieldName, _vField.label());
                    paramLabels.put(__doGetFieldName(parentFieldName, _fieldName), _vField.label());
                }
            }
            List<Annotation> _annotations = new ArrayList<Annotation>();
            for (Annotation _annotation : _beanWrapper.getFieldAnnotations(_field.getName())) {
                if (__doIsValid(_annotation)) {
                    _annotations.add(_annotation);
                } else if (_annotation instanceof VModel) {
                    // Field??
                    String _fieldNamePR = __doGetFieldName(parentFieldName, _fieldName);
                    if (_vField != null && StringUtils.isNotBlank(_vField.label())) {
                        paramLabels.put(_fieldNamePR, _vField.label());
                    }
                    // ?@VModel
                    _returnValues.putAll(__doGetMetaFromFields(_fieldNamePR, _field.getType(), paramLabels));
                }
            }
            if (!_annotations.isEmpty()) {
                // Field??
                String _fieldNamePR = __doGetFieldName(parentFieldName, _fieldName);
                _returnValues.put(_fieldNamePR, _annotations.toArray(new Annotation[_annotations.size()]));
            }
        }
    }
    return _returnValues;
}

From source file:org.artifactory.webapp.wicket.util.DescriptionExtractor.java

private String getElementName(Field field) {
    // default element name is the field name
    String elementName = field.getName();

    if (field.isAnnotationPresent(XmlElementWrapper.class)) {
        XmlElementWrapper wrapper = field.getAnnotation(XmlElementWrapper.class);
        // use the element name from the annotation only if it's not the default
        if (notDefaultName(wrapper.name())) {
            elementName = wrapper.name();
        }//  w  w  w.java2  s .c  om
    } else if (field.isAnnotationPresent(XmlElement.class)) {
        XmlElement annotation = field.getAnnotation(XmlElement.class);
        // use the element name from the annotation only if it's not the default
        if (notDefaultName(annotation.name())) {
            elementName = annotation.name();
        }
    }

    return elementName;
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MapperInfoHandler.java

/**
 * Is this a map-only job?//  ww w . ja va 2 s .c o m
 * 
 * @param job      the job
 * @param jobField   the field to reflect for annotations
 * @return         <code>true</code> if map only, <code>false</code> otherwise.
 */
protected boolean isMapOnlyJob(Job job, Field jobField) {
    if (job.getNumReduceTasks() > 0) {
        return false;
    }

    // See if we have a ReducerInfo annotation - otherwise 
    // we'll consider this a "map only" job
    return !jobField.isAnnotationPresent(ReducerInfo.class);
}

From source file:org.pmp.budgeto.app.SwaggerDispatcherConfigTest.java

@Test
public void springConf() throws Exception {

    Class<?> clazz = swaggerDispatcherConfig.getClass();
    Assertions.assertThat(clazz.getAnnotations()).hasSize(4);
    Assertions.assertThat(clazz.isAnnotationPresent(Configuration.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(EnableWebMvc.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(EnableSwagger.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(ComponentScan.class)).isTrue();
    Assertions.assertThat(clazz.getAnnotation(ComponentScan.class).basePackages())
            .containsExactly("com.ak.swaggerspringmvc.shared.app", "com.ak.spring3.music");

    Field fSpringSwaggerConfig = clazz.getDeclaredField("springSwaggerConfig");
    Assertions.assertThat(fSpringSwaggerConfig.getAnnotations()).hasSize(1);
    Assertions.assertThat(fSpringSwaggerConfig.isAnnotationPresent(Autowired.class)).isTrue();

    Method mCustomImplementation = clazz.getDeclaredMethod("customImplementation", new Class[] {});
    Assertions.assertThat(mCustomImplementation.getAnnotations()).hasSize(1);
    Assertions.assertThat(mCustomImplementation.getAnnotation(Bean.class)).isNotNull();
}

From source file:org.apache.pulsar.broker.service.BrokerService.java

private static ConcurrentOpenHashMap<String, Field> prepareDynamicConfigurationMap() {
    ConcurrentOpenHashMap<String, Field> dynamicConfigurationMap = new ConcurrentOpenHashMap<>();
    for (Field field : ServiceConfiguration.class.getDeclaredFields()) {
        if (field != null && field.isAnnotationPresent(FieldContext.class)) {
            field.setAccessible(true);//  w w w  .j a  v  a  2 s  . c  om
            if (((FieldContext) field.getAnnotation(FieldContext.class)).dynamic()) {
                dynamicConfigurationMap.put(field.getName(), field);
            }
        }
    }
    return dynamicConfigurationMap;
}

From source file:org.xaloon.wicket.component.inject.spring.CdiProxyFieldValueFactory.java

public boolean supportsField(Field field) {
    boolean supports = false;
    for (SpringAnnotationResolver beanNameResolver : beanNameResolvers) {
        supports = field.isAnnotationPresent(beanNameResolver.getAnnotationToSupport());
        if (supports) {
            break;
        }//from  w w  w.ja  va2s .  c  om
    }
    return supports;
}

From source file:beans.config.ConfigBean.java

private void injectConfiguration(Object obj, Configuration conf) {
    Set<Field> allFields = ReflectionUtils.getAllFields(obj.getClass(), Predicates.alwaysTrue());
    for (Field field : allFields) {
        String configKey = field.getName();
        Config configAnn = null;//  w ww.  j a  v  a  2  s .  co  m
        if (field.isAnnotationPresent(Config.class)) {
            configAnn = field.getAnnotation(Config.class);
            String playKey = configAnn.playKey();
            // use the annotated information only if not empty.
            configKey = StringUtils.isEmpty(playKey) ? configKey : playKey;
        }

        if (handlers.containsKey(field.getType())) {
            try {
                Object value = handlers.get(field.getType()).getValue(conf, configKey);
                if (value != null || !isIgnoreNullValue(configAnn)) {
                    field.set(obj, value);
                }
            } catch (Exception e) {
                logger.error(String.format("unable to set value for field [%s.%s]",
                        field.getDeclaringClass().getName(), field.getName()), e);
            }
        } else { // this is probably an Object. need to instantiate
            try {
                if (conf.getConfig(configKey) != null) {

                    // important : we assume the field is not null.
                    // this way we will be able to refresh configuration on command.
                    Object value = field.get(obj);
                    injectConfiguration(value, conf.getConfig(configKey));
                }
            } catch (Exception e) {
                throw new RuntimeException(String.format("unable to populate configuration for key %s.%s",
                        obj.getClass(), field.getName()), e);
            }
        }

        ConfigValueHandler handler = handlers.containsKey(field.getType()) ? handlers.get(field.getType())
                : handlers.get(Configuration.class);

    }
}

From source file:Database.Handler.java

@SuppressWarnings("unchecked")
private List<T> mapRersultSetToObject(ResultSet rs, Class outputClass) {
    List<T> outputList = null;
    try {/*from  w  ww.ja  va2  s .c o m*/
        if (rs != null) {
            if (outputClass.isAnnotationPresent(Entity.class)) {
                ResultSetMetaData rsmd = rs.getMetaData();
                Field[] fields = outputClass.getDeclaredFields();
                while (rs.next()) {
                    T bean = (T) outputClass.newInstance();
                    //System.out.println("rsmd = "+rsmd.getColumnCount());
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        String columnName = rsmd.getColumnName(i);
                        Object columnValue = rs.getObject(i);
                        for (Field field : fields) {
                            if (field.isAnnotationPresent(Column.class)) {
                                Column column = field.getAnnotation(Column.class);
                                if (column.name().equalsIgnoreCase(columnName) && columnValue != null) {
                                    //System.out.println(field.getName() + "=====>" + columnValue);
                                    BeanUtils.setProperty(bean, field.getName(), columnValue);
                                    break;
                                }
                            }
                        }
                    }
                    if (outputList == null) {
                        outputList = new ArrayList<T>();
                    }
                    outputList.add(bean);
                }
            } else {
                // throw some error
                System.out.println("output class is not annotationPresented");
            }
        } else {
            return null;
        }
    } catch (SQLException ex) {
        Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException ex) {
        Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return outputList;
}