Example usage for java.lang Class getDeclaredFields

List of usage examples for java.lang Class getDeclaredFields

Introduction

In this page you can find the example usage for java.lang Class getDeclaredFields.

Prototype

@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException 

Source Link

Document

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Usage

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Find fields with annotation./*from w ww . j a va  2  s. c o m*/
 *
 * @param clazz the clazz
 * @param annotation the annotation
 * @param res the res
 */
public static void findFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation,
        List<Pair<Field, ? extends Annotation>> res) {
    for (Field f : clazz.getDeclaredFields()) {
        Annotation an = f.getAnnotation(annotation);
        if (an == null) {
            continue;
        }
        res.add(new Pair<Field, Annotation>(f, an));
    }
    if (clazz == Object.class || clazz.getSuperclass() == null) {
        return;
    }
    findFieldsWithAnnotation(clazz.getSuperclass(), annotation, res);
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Fetch all fields.//  www.j  av a  2  s . c  om
 *
 * @param clazz the clazz
 * @param matcher the matcher
 * @param ret the ret
 */
public static void fetchAllFields(Class<?> clazz, Matcher<Field> matcher, List<Field> ret) {
    if (clazz == null) {
        return;
    }
    for (Field f : clazz.getDeclaredFields()) {
        if (matcher.match(f) == true) {
            ret.add(f);
        }
    }
    fetchAllFields(clazz.getSuperclass(), matcher, ret);
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader.java

/**
 * Extracts all fields in the class hierarchy of the given class.
 *
 * @param clazz The given class.//from www.ja  va  2  s  . co m
 * @return All fields in the class hierarchy of the given class.
 */
protected List<Field> extractFieldFromClassHierarchy(Class clazz) {
    List<Field> fields = new ArrayList<Field>();
    while (clazz != null) {
        CollectionUtils.addAll(fields, clazz.getDeclaredFields());
        clazz = clazz.getSuperclass();
    }
    return fields;
}

From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java

/**
 * Builds the enum dict.//from  w  w  w  . j a v a 2  s  .co m
 *
 * @param <E> the element type
 * @param c   the c
 * @return the map
 */
private static <E extends Enum<E>> Map<String, ExchangeVersion> buildEnumDict(Class<E> c) {
    Map<String, ExchangeVersion> dict = new HashMap<String, ExchangeVersion>();
    Field[] fields = c.getDeclaredFields();
    for (Field f : fields) {
        if (f.isEnumConstant() && f.isAnnotationPresent(RequiredServerVersion.class)) {
            RequiredServerVersion ewsEnum = f.getAnnotation(RequiredServerVersion.class);
            String fieldName = f.getName();
            ExchangeVersion exchangeVersion = ewsEnum.version();
            dict.put(fieldName, exchangeVersion);
        }
    }
    return dict;
}

From source file:org.cybercat.automation.core.PageFactoryImpl.java

@SuppressWarnings("unchecked")
public <T extends AbstractPageObject> T createPage(Class<T> page) throws PageObjectException {
    Constructor<T> cons;/* w w  w.  j a  va 2  s  .c  o m*/
    try {
        cons = page.getConstructor();
        T result = cons.newInstance();
        for (Field field : page.getDeclaredFields()) {
            field.setAccessible(true);
            if (field.getAnnotation(CCProperty.class) != null) {
                AnnotationBuilder.processPropertyField(result, field);
            }
        }
        AspectJProxyFactory proxyFactory = new AspectJProxyFactory(result);
        proxyFactory.addAspect(new PageObjectStateControlAcpect(this));
        result = (T) proxyFactory.getProxy();
        return result;
    } catch (Exception e) {
        throw new PageObjectException("Page object creation problem.", e);
    }
}

From source file:ar.com.zauber.commons.repository.utils.SpringInjectionInterceptor.java

/**
 * @param persistibleClasses persistible classes that may requiere dependency
 *                           injection/*w ww  . j  a v  a 2  s  .c  om*/
 */
public SpringInjectionInterceptor(final List<Class<?>> persistibleClasses) {
    Validate.noNullElements(persistibleClasses);

    for (final Class<?> clazz : persistibleClasses) {
        final List<Entry<Field, String>> fields = new LinkedList<Entry<Field, String>>();
        for (final Annotation annotation : clazz.getAnnotations()) {
            if (annotation instanceof Configurable) {
                final List<Field> clazzFields = new LinkedList<Field>();
                Class<?> c = clazz;
                while (c != null) {
                    clazzFields.addAll(Arrays.asList(c.getDeclaredFields()));
                    c = c.getSuperclass();
                }

                for (final Field field : clazzFields) {
                    for (final Annotation a : field.getAnnotations()) {
                        if (a instanceof Qualifier) {
                            String n = ((Qualifier) a).value();
                            if (StringUtils.isBlank(n)) {
                                n = field.getName();
                            }
                            final String name = n;
                            fields.add(new Entry<Field, String>() {
                                public Field getKey() {
                                    return field;
                                }

                                public String getValue() {
                                    return name;
                                }

                                public String setValue(final String value) {
                                    return null;
                                }
                            });
                        }
                    }
                }
            }
        }

        if (fields.size() > 0) {
            dependencyCache.put(clazz,
                    new DependencyInjection(fields, InitializingBean.class.isAssignableFrom(clazz)));
        }
    }
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private void populateOptionFields(Class<?> beanClass) {
    Class<?> superClass = beanClass.getSuperclass();
    if (!superClass.equals(Object.class)) {
        populateOptionFields(superClass);
    }/*from   w w  w  .  j a v  a  2  s.c o  m*/
    for (Field field : beanClass.getDeclaredFields()) {
        Option option = field.getAnnotation(Option.class);
        if (option != null) {
            checkWriteableProperty(option, beanClass, field);
            options.put(option, new OptionFieldHandler(option, field));
        }
    }
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private void populateOperandFields(Class<?> beanClass) {
    Class<?> superClass = beanClass.getSuperclass();
    if (!superClass.equals(Object.class)) {
        populateOperandFields(superClass);
    }/*from www  .  ja  va  2  s . co  m*/
    for (Field field : beanClass.getDeclaredFields()) {
        Operand operand = field.getAnnotation(Operand.class);
        if (operand != null) {
            checkWriteableProperty(operand, beanClass, field);
            operands.put(operand, new OperandFieldHandler(operand, field));
        }
    }
}

From source file:net.minder.config.impl.DefaultConfigurationInjector.java

private void injectClass(Class type, Object target, ConfigurationAdapter config, ConfigurationBinding binding)
        throws ConfigurationException {
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        injectFieldValue(field, target, config, binding);
    }//from w w w . j  av a  2s .co  m
    Method[] methods = type.getDeclaredMethods();
    for (Method method : methods) {
        injectMethodValue(method, target, config, binding);
    }
}

From source file:com.impetus.kundera.persistence.AssociationBuilder.java

/**
 * Returns associated bi-directional field.
 * //from w  w  w. j  a  va  2 s  . c om
 * @param originalClazz
 *            Original class
 * @param referencedClass
 *            Referenced class.
 */
public Field getBiDirectionalField(Class originalClazz, Class referencedClass) {
    Field[] fields = referencedClass.getDeclaredFields();
    Class<?> clazzz = null;
    Field biDirectionalField = null;
    for (Field field : fields) {
        clazzz = field.getType();
        if (PropertyAccessorHelper.isCollection(clazzz)) {
            ParameterizedType type = (ParameterizedType) field.getGenericType();
            Type[] types = type.getActualTypeArguments();
            clazzz = (Class<?>) types[0];
        }
        if (clazzz.equals(originalClazz)) {
            biDirectionalField = field;
            break;
        }
    }

    return biDirectionalField;
}