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.gmail.sretof.db.jdbc.processor.CamelBeanProcessor.java

/**
 * Returns a PropertyDescriptor[] for the given Class.
 * /*from  w  ww . j  a  v  a  2  s.c  o m*/
 * @param c
 *            The Class to retrieve PropertyDescriptors for.
 * @return A PropertyDescriptor[] describing the Class.
 * @throws SQLException
 *             if introspection failed.
 */
private PropertyDescriptor[] propertyDescriptors(Class<?> c) throws SQLException {
    // Introspector caches BeanInfo classes for better performance
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(c);
    } catch (IntrospectionException e) {
        throw new SQLException("Bean introspection failed: " + e.getMessage());
    }
    List<PropertyDescriptor> propList = Lists.newArrayList();
    PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        String propName = prop.getName();
        try {
            Field field = ReflectionUtils.findField(c, propName);
            if (field != null && !field.isAnnotationPresent(Transient.class)) {// 1.field=null
                // 2.Transient??
                propList.add(prop);
            }
        } catch (SecurityException e) {
            throw new SQLException("Bean Get Field failed: " + e.getMessage());
        }
    }
    return propList.toArray(new PropertyDescriptor[propList.size()]);
}

From source file:adalid.core.Instance.java

private void annotateInstanceDataGen(Field field) {
    Class<? extends Annotation> annotationClass = InstanceDataGen.class;
    _annotatedWithInstanceDataGen = field.isAnnotationPresent(annotationClass);
    if (_annotatedWithInstanceDataGen) {
        InstanceDataGen annotation = field.getAnnotation(InstanceDataGen.class);
        _dataGenWeight = Math.min(100, Math.max(0, annotation.weight()));
    }/*  www .j a  v  a  2s . c  om*/
}

From source file:com.wit.android.support.fragment.BaseFragment.java

/**
 * Called to process all annotations of the specified <var>classOfFragment</var>.
 *
 * @param classOfFragment The class of which annotations to process.
 *//*from   w w  w  .  j a  v  a 2  s. c o  m*/
private void processClassAnnotations(Class<?> classOfFragment) {
    // Obtain content view.
    this.mContentView = FragmentAnnotations.obtainAnnotationFrom(classOfFragment, ContentView.class,
            BaseFragment.class);
    // Obtain clickable view ids.
    // Note, that we will gather ids from all annotated class to this parent.
    this.mClickableViewIds = this.gatherClickableViewIds(classOfFragment, new ArrayList<Integer>());
    if (mClickableViewIds.isEmpty()) {
        this.mClickableViewIds = null;
    }
    // Store all fields to inject as views.
    this.mViewsToInject = new ArrayList<>();
    this.iterateInjectableViewFields(classOfFragment, new FragmentAnnotations.FieldProcessor() {

        /**
         */
        @Override
        public void onProcessField(@NonNull Field field, @NonNull String name) {
            if (field.isAnnotationPresent(InjectView.class)
                    || field.isAnnotationPresent(InjectView.Last.class)) {
                mViewsToInject.add(field);
            }
        }
    });
    if (mViewsToInject.isEmpty()) {
        this.mViewsToInject = null;
    }
}

From source file:com.opensymphony.xwork2.util.finder.DefaultClassFinder.java

public List<Field> findAnnotatedFields(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();//from w ww . j  a va2 s  .c  o m
    List<ClassInfo> seen = new ArrayList<ClassInfo>();
    List<Field> fields = new ArrayList<Field>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof FieldInfo) {
            FieldInfo fieldInfo = (FieldInfo) info;
            ClassInfo classInfo = fieldInfo.getDeclaringClass();

            if (seen.contains(classInfo))
                continue;

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(annotation)) {
                        fields.add(field);
                    }
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return fields;
}

From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java

/**
 * Get a list of fields annotated with {@link LdapAttribute} of the implementing mapped class and it's super
 * classes.//  w ww .  j a  v  a 2s . c o m
 *
 * @return list of annotated fields
 */
protected synchronized List<Field> getAnnotatedFields() {
    if (annotatedFields == null) {
        annotatedFields = Collections.synchronizedList(new ArrayList<Field>());
        Class<?> superC = clazz;
        while (superC != null) {
            Field[] fields = superC.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(LdapAttribute.class)) {
                    annotatedFields.add(field);
                }
            }
            superC = superC.getSuperclass();
        }
    }
    return annotatedFields;
}

From source file:org.apache.struts2.convention.DefaultClassFinder.java

public List<Field> findAnnotatedFields(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();/*from  w  w  w. j  a v a2  s . c om*/
    List<ClassInfo> seen = new ArrayList<>();
    List<Field> fields = new ArrayList<>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof FieldInfo) {
            FieldInfo fieldInfo = (FieldInfo) info;
            ClassInfo classInfo = fieldInfo.getDeclaringClass();

            if (seen.contains(classInfo)) {
                continue;
            }

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(annotation)) {
                        fields.add(field);
                    }
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return fields;
}

From source file:com.fonoster.astive.agi.AgiRequest.java

private void fillFields() throws IllegalArgumentException, IllegalAccessException {
    for (String af : fieldsMap.keySet()) {
        for (Field f : AgiRequest.class.getDeclaredFields()) {
            RequestField rf;//  w w  w.j  a  v  a2s  . c  om

            if (f.isAnnotationPresent(RequestField.class)) {
                rf = f.getAnnotation(RequestField.class);
            } else {
                continue;
            }

            if (rf.value().equals(af)) {
                // There is an exception for the variables:
                // channelType, enhanced and network
                if (f.getName().equals("network")) {
                    if (fieldsMap.get(af).equals("yes")) {
                        f.set(this, Boolean.TRUE);
                    } else {
                        f.set(this, Boolean.FALSE);
                    }

                    break;
                } else if (f.getName().equals("enhanced")) {
                    if (fieldsMap.get(af).equals("1.0")) {
                        f.set(this, Boolean.TRUE);
                    } else {
                        f.set(this, Boolean.FALSE);
                    }

                    break;
                } else if (f.getType().equals(ChannelType.class)) {
                    f.set(this, ChannelType.get(fieldsMap.get(af)));

                    break;
                } else if (f.getType().equals(int.class)) {
                    f.set(this, Integer.valueOf(fieldsMap.get(af)));

                    break;
                } else {
                    f.set(this, fieldsMap.get(af));

                    break;
                }
            }
        }
    }
}

From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java

protected boolean isMetaPropertyField(Field field) {
    return field.isAnnotationPresent(com.haulmont.chile.core.annotations.MetaProperty.class);
}

From source file:com.wingnest.play2.origami.plugin.OrigamiPlugin.java

@SuppressWarnings("unchecked")
private void maintainProperties(final OClass oClass, final Class<?> javaClass) {
    final Map<String, Map<String, Object>> compositeIndexMap = new HashMap<String, Map<String, Object>>();
    final Map<String, OIndex<?>> classIndexCache = new HashMap<String, OIndex<?>>();
    final Map<String, OIndex<?>> compositeIndexCache = new HashMap<String, OIndex<?>>();
    final Set<String> wkCurIndexNames = new HashSet<String>();
    //      for ( final OProperty prop : oClass.properties() ) {
    //         debug("[b] prop name =%s, type = %s", prop.getName(), prop.getType());
    //      }//from  w  w  w .  j  a v  a  2  s  . com
    for (final OIndex<?> index : oClass.getClassIndexes()) {
        wkCurIndexNames.add(index.getName());
        if (index.getName().indexOf('.') > -1) {
            classIndexCache.put(index.getName(), index);
        } else {
            compositeIndexCache.put(index.getName(), index);
        }
        //         debug("[b] index name =%s, type = %s", index.getName(), index.getType());
    }
    for (final Field field : javaClass.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers()) || field.isAnnotationPresent(Id.class)
                || field.isAnnotationPresent(Version.class) || field.isAnnotationPresent(Transient.class)
                || field.isAnnotationPresent(DisupdateFlag.class))
            continue;

        OProperty prop = oClass.getProperty(field.getName());
        final OType type = guessType(field);
        if (prop == null) {
            if (type != null) {
                debug("create property : %s", field.getName());
                prop = oClass.createProperty(field.getName(), type);
            }
        } else {
            if (!type.equals(prop.getType())) {
                deleteIndex(oClass, oClass.getName() + "." + field.getName(), classIndexCache,
                        compositeIndexCache);
                debug("drop property : %s", field.getName());
                oClass.dropProperty(field.getName());
                debug("create property : %s", field.getName());
                prop = oClass.createProperty(field.getName(), type);
            }
        }
        final Index index = field.getAnnotation(Index.class);
        if (index != null) {
            final String indexName = makeIndexName(javaClass, field);
            OIndex<?> oindex = classIndexCache.get(indexName);
            if (oindex == null) {
                debug("create Class Index : %s.%s", javaClass.getSimpleName(), field.getName());
                if (prop != null) {
                    oindex = oClass.createIndex(indexName, index.indexType(), field.getName());
                } else {
                    error("could not create Class Index : property(%s.%s) has't type", javaClass.getName(),
                            field.getName());
                }
            }
            if (oindex != null) {
                wkCurIndexNames.remove(oindex.getName());
            }
        }
        final CompositeIndex cindex = field.getAnnotation(CompositeIndex.class);
        if (cindex != null) {
            final String indexName = javaClass.getSimpleName() + "_" + cindex.indexName();
            Map<String, Object> ci = compositeIndexMap.get(indexName);
            if (ci == null) {
                ci = new HashMap<String, Object>();
                ci.put("fields", new HashSet<Field>());
                ci.put("indexType", OClass.INDEX_TYPE.UNIQUE);
            }
            if (!cindex.indexType().equals(OClass.INDEX_TYPE.UNIQUE))
                ci.put("indexType", cindex.indexType());
            ((Set<Field>) ci.get("fields")).add(field);
            compositeIndexMap.put(indexName, ci);
        }
    }

    for (final String cindexName : compositeIndexMap.keySet()) {
        final Map<String, Object> ci = compositeIndexMap.get(cindexName);
        final Set<Field> fields = (Set<Field>) ci.get("fields");
        final String[] fieldNames = new String[fields.size()];
        int i = 0;
        for (final Field f : fields) {
            fieldNames[i++] = f.getName();
        }
        final OIndex<?> oindex = compositeIndexCache.get(cindexName);
        if (oindex != null && !CollectionUtils.isEqualCollection(Arrays.asList(fieldNames),
                oindex.getDefinition().getFields())) {
            debug("recreate composite index : %s", cindexName);
            deleteIndex(oClass, cindexName, classIndexCache, compositeIndexCache);
        } else if (oindex == null) {
            debug("create composite index : %s", cindexName);
        }
        oClass.createIndex(cindexName, (OClass.INDEX_TYPE) ci.get("indexType"), fieldNames);
        wkCurIndexNames.remove(cindexName);
    }

    for (final String indexName : wkCurIndexNames) {
        final int ind = indexName.indexOf('.');
        if (ind > -1) {
            debug("delete index : %s", indexName);
        } else {
            debug("delete composite index : %s", indexName);
        }
        deleteIndex(oClass, indexName, classIndexCache, compositeIndexCache);
    }

    //      for ( final OProperty prop : oClass.properties() ) {
    //         debug("[a] prop name =%s, type = %s", prop.getName(), prop.getType());
    //      }
    //      for ( final OIndex<?> index : oClass.getClassIndexes() ) {
    //         debug("[a] class index name =%s, type = %s", index.getName(), index.getType());
    //      }
}

From source file:com.all.shared.stats.UserSessionStat.java

private void addStat(UserActionStat userActionStat) {
    try {//ww  w  .j  av a 2s  .c o  m
        Field[] declaredFields = getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getType().equals(boolean.class) && field.isAnnotationPresent(UserAction.class)) {
                UserAction userAction = field.getAnnotation(UserAction.class);
                for (int actionType : userAction.value()) {
                    if (actionType == userActionStat.getAction()) {
                        field.setAccessible(true);
                        field.set(this, true);
                    }
                }
            }
        }
    } catch (IllegalArgumentException e) {
        LOG.error(e, e);
    } catch (IllegalAccessException e) {
        LOG.error(e, e);
    }
}