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:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java

/**
 * Test initialization of the title attribute according
 * <ul>/* w  w  w. j  a v a 2  s  . c o  m*/
 * <li>the title
 * <li>is title visible
 * <li>title orientation
 * <li>start row and cell
 * </ul>
 */
@Test
public void checkTitleAttribute() {
    Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsFreeElement
        if (f.isAnnotationPresent(XlsFreeElement.class)) {

            XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class);

            if (f.getName().equals("stringFreeAttribute1")) {
                assertEquals(xlsFreeElement.showTitle(), false);
                assertEquals(xlsFreeElement.title(), "String free element 1");
                assertEquals(xlsFreeElement.row(), 1);
                assertEquals(xlsFreeElement.cell(), 1);

            } else if (f.getName().equals("stringFreeAttribute2")) {
                assertEquals(xlsFreeElement.showTitle(), true);
                assertEquals(xlsFreeElement.titleOrientation(), TitleOrientationType.BOTTOM);
                assertEquals(xlsFreeElement.title(), "String free element 2");
                assertEquals(xlsFreeElement.row(), 1);
                assertEquals(xlsFreeElement.cell(), 2);

            } else if (f.getName().equals("stringFreeAttribute3")) {
                assertEquals(xlsFreeElement.showTitle(), false);
                assertEquals(xlsFreeElement.title(), "String free element 3");
                assertEquals(xlsFreeElement.row(), 1);
                assertEquals(xlsFreeElement.cell(), 3);

            } else if (f.getName().equals("stringFreeAttribute4")) {
                assertEquals(xlsFreeElement.showTitle(), true);
                assertEquals(xlsFreeElement.titleOrientation(), TitleOrientationType.RIGHT);
                assertEquals(xlsFreeElement.title(), "String free element 4");
                assertEquals(xlsFreeElement.row(), 1);
                assertEquals(xlsFreeElement.cell(), 4);
            }
        }
    }
}

From source file:de.lightful.testflux.drools.DroolsRuleTestListener.java

private List<Field> findFieldsToInjectKnowledgeBaseInto(Class<?> realTestClass) {
    final Field[] declaredFields = realTestClass.getDeclaredFields();
    List<Field> matchingFields = new ArrayList<Field>();
    for (Field declaredField : declaredFields) {
        if (canAcceptKnowledgeBase(declaredField)) {
            matchingFields.add(declaredField);
        }//from  w w  w  . j  a  v  a 2s.co  m
    }
    return matchingFields;
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

public Field getInheritedPrivateFields(Class<?> type, Class<?> annotation) {
    log.debug("Find private field from hierarchy with annotation {} for entity class {}",
            annotation.getCanonicalName(), type.getCanonicalName());

    Class<?> i = type;
    while (i != null && i != Object.class) {
        for (Field declaredField : i.getDeclaredFields()) {
            if (filter.matches(declaredField, annotation)) {
                log.trace("Found inherited private field : {}", declaredField);
                return declaredField;
            }/*w  w w .j  av a  2s . c  o  m*/
        }
        i = i.getSuperclass();
    }
    return null;
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

public Field getInheritedPrivateFields(Class<?> type, Class<?> annotation, String name) {
    log.debug("Find private field with name {} having annotation {} from hierarchy for entity class {}", name,
            annotation.getCanonicalName(), type.getCanonicalName());

    Class<?> i = type;
    while (i != null && i != Object.class) {
        for (Field declaredField : i.getDeclaredFields()) {
            if (filter.matches(declaredField, annotation, name)) {
                log.trace("Found inherited private field : {}", declaredField);
                return declaredField;
            }//from   w  ww.  j ava2s . c  o m
        }
        i = i.getSuperclass();
    }
    return null;
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

public List<Field> getInheritedPrivateFields(Class<?> type) {
    log.debug("Find inherited private fields from hierarchy for entity class {}", type.getCanonicalName());

    List<Field> fields = new ArrayList<Field>();

    Class<?> i = type;
    while (i != null && i != Object.class) {
        for (Field declaredField : i.getDeclaredFields()) {
            if (filter.matches(declaredField)) {
                fields.add(declaredField);
            }/*from  ww  w  .j  a  v  a  2 s  . co  m*/
        }
        i = i.getSuperclass();
    }
    if (log.isTraceEnabled()) {
        log.trace("Found inherited private fields : {}", Lists.transform(fields, fieldToStringFn));
    }
    return fields;
}

From source file:springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander.java

private List<Field> getInstanceFields(final Class<?> type) {

    List<Field> result = new ArrayList<Field>();

    Class<?> i = type;
    while (!rootType(i)) {
        result.addAll(Arrays.asList(i.getDeclaredFields()));
        i = i.getSuperclass();/*  www  . ja v  a  2  s  .  co m*/
    }
    return from(result).filter(not(staticField())).filter(not(syntheticFields())).toList();
}

From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java

public AnnotatedCLI(Class... clazz) {
    final List<AnnotatedOption> list = new ArrayList<AnnotatedOption>();
    for (Class aClass : clazz) {
        for (Field field : aClass.getDeclaredFields()) {
            Param param = field.getAnnotation(Param.class);
            if (param != null) {
                list.add(new AnnotatedOption(aClass, field, param));
            }/*from   www . j a v a 2 s  . c o  m*/
            Args arg = field.getAnnotation(Args.class);
            if (arg != null) {
                args.put(aClass, field);
            }
        }
        for (Constructor ctor : aClass.getConstructors()) {
            AnnotatedConstructor actor = new AnnotatedConstructor(aClass, ctor);
            Class[] types = ctor.getParameterTypes();
            Annotation[][] annotations = ctor.getParameterAnnotations();
            for (int i = 0; i < types.length; i++) {
                Class type = types[i];
                Annotation[] ann = annotations[i];
                for (Annotation annotation : ann) {
                    if (annotation instanceof Param) {
                        actor.addParam((Param) annotation, type);
                    }
                }
            }
            if (actor.isValid()) {
                ctors.put(aClass, actor);
            }
        }
    }
    options = new Options();
    mappings = Multimaps.newMultimap(Maps.<Class, Collection<AnnotatedOption>>newHashMap(),
            new Supplier<Collection<AnnotatedOption>>() {
                public Collection<AnnotatedOption> get() {
                    return new ArrayList<AnnotatedOption>();
                }
            });
    for (AnnotatedConstructor constructor : ctors.values()) {
        for (AnnotatedConstructor.AnnotatedParam param : constructor.getParams()) {
            boolean hasArgs = !(param.getType().equals(boolean.class) || param.getType().equals(Boolean.class));
            String option = param.getParam().option();
            while (options.hasOption(option)) {
                option = option + option;
            }
            options.addOption(option, param.getParam().name(), hasArgs, param.getParam().desc());
        }
    }
    for (AnnotatedOption opt : list) {
        boolean hasArgs = !(opt.field.getType().equals(boolean.class)
                || opt.field.getType().equals(Boolean.class));
        while (options.hasOption(opt.getOpt())) {
            opt.setOpt(opt.getOpt() + opt.getOpt());
        }
        options.addOption(opt.getOpt(), opt.getName(), hasArgs, opt.getParam().desc());
        mappings.put(opt.clazz, opt);
    }
}

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

/**
 * Builds the schema to enum mapping dictionary.
 *
 * @param <E> Type of the enum.//from www.j a  v a2  s. c o  m
 * @param c   Class
 * @return The mapping from enum to schema name
 */
private static <E extends Enum<E>> Map<String, String> buildSchemaToEnumDict(Class<E> c) {
    Map<String, String> dict = new HashMap<String, String>();

    Field[] fields = c.getDeclaredFields();
    for (Field f : fields) {
        if (f.isEnumConstant() && f.isAnnotationPresent(EwsEnum.class)) {
            EwsEnum ewsEnum = f.getAnnotation(EwsEnum.class);
            String fieldName = f.getName();
            String schemaName = ewsEnum.schemaName();
            if (!schemaName.isEmpty()) {
                dict.put(schemaName, fieldName);
            }
        }
    }
    return dict;
}

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

/**
 * Copy all instance fiels from source to target. static fields are not copied.
 * /*from   ww  w  . j  ava  2 s . c o m*/
 * @param <T> the generic type
 * @param targetClass the target class
 * @param source the source
 * @param target the target
 */
public static <T> void copyInstanceProperties(Class<?> targetClass, T source, T target) {
    if (targetClass == null) {
        return;
    }

    for (Field f : targetClass.getDeclaredFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) == true) {
            continue;
        }
        Object value = readField(source, f.getName());
        writeField(target, f, value);
    }
    copyInstanceProperties(targetClass.getSuperclass(), source, target);
}

From source file:com.epam.ta.reportportal.ws.validation.JaskonRequiredPropertiesValidator.java

private List<Field> collectFields(Class<?> clazz) {
    List<Field> fields = null;
    if (!Object.class.equals(clazz.getSuperclass())) {
        fields = collectFields(clazz.getSuperclass());
    }//from w  ww.jav a 2s.c om

    fields = (fields == null) ? new ArrayList<>() : fields;
    fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    return fields;
}