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:com.googlecode.dex2jar.tools.BaseCmd.java

protected void initOptionFromClass(Class<?> clz) {
    if (clz == null) {
        return;/*from w  w w  .j  ava  2s.  c  o m*/
    } else {
        initOptionFromClass(clz.getSuperclass());
    }
    Field[] fs = clz.getDeclaredFields();
    for (Field f : fs) {
        Opt opt = f.getAnnotation(Opt.class);
        if (opt != null) {
            f.setAccessible(true);
            if (!opt.hasArg()) {
                Class<?> type = f.getType();
                if (!type.equals(boolean.class)) {
                    throw new RuntimeException(
                            "the type of " + f + " must be boolean, as it is declared as no args");
                }
                boolean b;
                try {
                    b = (Boolean) f.get(this);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (b) {
                    throw new RuntimeException(
                            "the value of " + f + " must be false, as it is declared as no args");
                }
            }
            Option option = new Option(opt.opt(), opt.hasArg(), opt.description());
            option.setRequired(opt.required());
            if (!"".equals(opt.longOpt())) {
                option.setLongOpt(opt.longOpt());
            }
            if (!"".equals(opt.argName())) {
                option.setArgName(opt.argName());
            }
            options.addOption(option);
            map.put(opt.opt(), f);
        }
    }
}

From source file:net.erdfelt.android.sdkfido.configer.Configer.java

private final void walkFields(String prefix, Class<?> clazz, String scope) {
    for (Field field : clazz.getDeclaredFields()) {
        ConfigOption opt = field.getAnnotation(ConfigOption.class);
        if (opt == null) {
            continue; // skip
        }/*  w w w. j  a va2s  .c o  m*/

        String key = StringUtils.defaultString(prefix) + field.getName();
        if (opt.suboption()) {
            walkFields(key + ".", field.getType(), opt.scope());
        } else {
            Configurable cfgrbl = new Configurable(field, opt, key, scope);
            configurables.put(key, cfgrbl);

            scopes.add(cfgrbl.getScope());

            if (field.getType().isEnum()) {
                // register converter for this field type.
                Class<?> enumclass = field.getType();
                this.bub.getConvertUtils().register(new EnumConverter(enumclass), enumclass);
            }
        }
    }
}

From source file:com.oltpbenchmark.WorkloadConfiguration.java

@Override
public String toString() {
    Class<?> confClass = this.getClass();
    Map<String, Object> m = new ListOrderedMap<String, Object>();
    for (Field f : confClass.getDeclaredFields()) {
        Object obj = null;//  ww  w .j  a v  a 2 s  .c  om
        try {
            obj = f.get(this);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
        m.put(f.getName().toUpperCase(), obj);
    } // FOR
    return StringUtil.formatMaps(m);
}

From source file:com.epam.jdi.uitests.web.selenium.elements.complex.table.EntityTable.java

public EntityTable(Class<E> entityClass) {
    this.entityClass = checkEntityIsNotNull(entityClass);
    hasColumnHeaders(select(entityClass.getDeclaredFields(), Field::getName));
}

From source file:dslistevents.DSListEvents.java

private String getOpDescription(int event) {

    try {/*from   w w  w .  ja  v  a2  s.  c o  m*/
        Class cls = Class.forName("com.xerox.docushare.event.DSEventObj");
        // Get a list of all the fields DSEventObj
        Field[] fields = cls.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            // Get the value of the field
            int val = fields[i].getInt(null);
            // Compare val to the value of the event passed.
            if (val == event) {
                // we found it!
                return fields[i].getName();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "Event description not found.";
}

From source file:dirty.mockito.junit.rules.ActiveTestRule.java

/**
 * @param unitTest/*from  w  w w  .  ja  v  a2 s  .co m*/
 *        the JUnit test we're intercepting
 * @param clazz
 *        the class we're scanning
 */
@SuppressWarnings("deprecation")
private void scan(final Object unitTest, final Class<?> clazz) {
    final AnnotationEngine annotationEngine = new GlobalConfiguration().getAnnotationEngine();
    final Field[] fields = clazz.getDeclaredFields();
    for (final Field field : fields) {
        for (final Annotation annotation : field.getAnnotations()) {
            final Object mock = annotationEngine.createMockFor(annotation, field);
            if (mock != null) {
                try {
                    Reflection.set(field).of(unitTest).to(mock);
                } catch (final IllegalAccessException e) {
                    throw new MockitoException("Problems initializing fields annotated with " + annotation, e);
                }

                if (mock instanceof EntityManager) {
                    registerMockEntityManagerFactory((EntityManager) mock);
                }
                beanFactory.registerSingleton(field.getName(), mock);
            }
        }
    }
}

From source file:com.lioland.harmony.services.dao.ODBClass.java

private ODocument createODocument(ODBClass odbc) {
    Class cls = odbc.getClass();
    ODocument doc;//w w  w  .  j  av  a 2  s . co m
    if (rid == null) {
        doc = new ODocument(cls.getSimpleName());
    } else {
        doc = getODocument();
    }
    for (Field field : cls.getDeclaredFields()) {
        try {
            String fieldName = field.getName();
            Object fieldValue = PropertyUtils.getProperty(odbc, fieldName);
            if (fieldValue instanceof ODBClass) {
                fieldValue = createODocument((ODBClass) fieldValue);
            }
            if (fieldValue != null) {
                doc.field(fieldName, fieldValue);
            }
        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
                | NoSuchMethodException ex) {
            Logger.getLogger(ODBClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return doc;
}

From source file:luki.x.XConfig.java

@SuppressWarnings("deprecation")
private void initTagKey(Context context, String name) {
    try {//from   w ww .jav  a  2  s  .co  m
        sContext = context.getApplicationContext();
        WindowManager wm = (WindowManager) sContext.getSystemService(Context.WINDOW_SERVICE);
        SCREEN_WIDTH = wm.getDefaultDisplay().getWidth();

        Class<?> clazz = Class.forName(sContext.getPackageName() + name);
        Object obj = clazz.newInstance();
        Field[] s = clazz.getDeclaredFields();
        HOLDER_KEY = s[0].getInt(obj);
        HOLDER_POSITION = s[1].getInt(obj);
    } catch (Exception e) {
        try {
            initTagKey(sContext, TAG_KEY_CLASS[tagKeyPos++]);
        } catch (Exception e1) {
            XLog.e("InjectAdapter", "you should have at least two same resource type in 'res'.");
        }
    }
}

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

/**
 * Read all field into a map./*from   w  w  w.j  av a  2 s  .co  m*/
 *
 * @param ret the ret
 * @param clz the clz
 * @param bean the bean
 * @param modifiederMask required Modified.XXX fields set.
 * @param negModifierMask disalowwed Modified.XXX fields set.
 */
public static void fetchAllFields(Map<String, Object> ret, Class<?> clz, Object bean, int modifiederMask,
        int negModifierMask) {
    if (clz == null) {
        return;
    }
    for (Field f : clz.getDeclaredFields()) {
        int mod = f.getModifiers();
        if ((mod & negModifierMask) != 0) {
            continue;
        }
        if ((mod & modifiederMask) != modifiederMask) {
            continue;
        }
        if (ret.containsKey(f.getName()) == true) {
            continue;
        }
        Object value = readField(bean, f);
        ret.put(f.getName(), value);
    }
    fetchAllFields(ret, clz.getSuperclass(), bean, modifiederMask, negModifierMask);
}

From source file:com.google.api.ads.adwords.awreporting.model.csv.CsvReportEntitiesMapping.java

/**
 * Adds all the mapped report properties to the selection list.
 *
 * @param propertiesToSelect the selection list
 * @param currentClass the actual class/* ww w. ja  v a 2 s . co m*/
 * @param propertyExclusions the properties that must not be added to the report.
 */
private void addAllMappedSelectionProperties(List<String> propertiesToSelect, Class<?> currentClass,
        Set<String> propertyExclusions) {

    Field[] declaredFields = currentClass.getDeclaredFields();
    for (int i = 0; i < declaredFields.length; i++) {

        Field field = declaredFields[i];
        this.addPropertyNameIfAnnotationPresent(propertiesToSelect, field, propertyExclusions);
    }
}