Example usage for java.lang Class getFields

List of usage examples for java.lang Class getFields

Introduction

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

Prototype

@CallerSensitive
public Field[] getFields() throws SecurityException 

Source Link

Document

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

Usage

From source file:org.apache.storm.config.ConfigUtil.java

@ClojureClass(className = "backtype.storm.config#ALL-CONFIGS")
public static List<Object> All_CONFIGS() {
    List<Object> rtn = new ArrayList<Object>();
    Config config = new Config();
    Class<?> ConfigClass = config.getClass();
    Field[] fields = ConfigClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        try {//from   w ww. j a va2s. c o m
            Object obj = fields[i].get(null);
            rtn.add(obj);
        } catch (IllegalArgumentException e) {
            LOG.error(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return rtn;
}

From source file:org.apache.storm.config.ConfigUtil.java

/**
 * Create a mapping of config-string -> validator Config fields must have a
 * SCHEMA field defined/* w ww  . ja v  a  2s .c  o  m*/
 * 
 * @return
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
@ClojureClass(className = "backtype.storm.config#CONFIG-SCHEMA-MAP")
public static Map<Object, FieldValidator> configSchemaMap()
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Config config = new Config();
    Class<?> configClass = config.getClass();
    Field[] fields = configClass.getFields();
    Map<Object, FieldValidator> result = new HashMap<Object, FieldValidator>();
    for (Field field : fields) {

        String configString = field.getName();
        Pattern p = Pattern.compile(".*_SCHEMA$");
        Matcher m = p.matcher(configString);
        if (!m.matches()) {
            Object valid = configClass.getField(configString + "_SCHEMA").get(null);
            if (null != valid) {
                Object key = field.get(null);
                FieldValidator fieldValidator = getFieldValidator(valid);
                result.put(key, fieldValidator);
            }
        }
    }
    return result;
}

From source file:com.xlythe.engine.theme.Theme.java

@SuppressWarnings("rawtypes")
public static void buildResourceMap(Class r) {
    RES_MAP = new SparseArray<Theme.Res>();
    try {/*from  ww w  .j av a  2 s. com*/
        Log.d("Theme", "Building resource map");

        Class color = Class.forName(r.getName() + "$color");
        for (Field f : color.getFields()) {
            RES_MAP.put(f.getInt(null), new Res(COLOR, f.getName()));
        }
        Log.d("Theme", "color loaded");

        Class drawable = Class.forName(r.getName() + "$drawable");
        for (Field f : drawable.getFields()) {
            RES_MAP.put(f.getInt(null), new Res(DRAWABLE, f.getName()));
        }
        Log.d("Theme", "drawable loaded");

        Class bool = Class.forName(r.getName() + "$bool");
        for (Field f : bool.getFields()) {
            RES_MAP.put(f.getInt(null), new Res(BOOLEAN, f.getName()));
        }
        Log.d("Theme", "bool loaded");

        Class raw = Class.forName(r.getName() + "$raw");
        for (Field f : raw.getFields()) {
            RES_MAP.put(f.getInt(null), new Res(RAW, f.getName()));
        }
        Log.d("Theme", "raw loaded");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // Do nothing here
    }
}

From source file:com.alibaba.jstorm.cluster.StormConfig.java

public static List<Object> All_CONFIGS() {
    List<Object> rtn = new ArrayList<Object>();
    Config config = new Config();
    Class<?> ConfigClass = config.getClass();
    Field[] fields = ConfigClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        try {/*from   w w  w  .j  a va  2  s .  com*/
            Object obj = fields[i].get(null);
            rtn.add(obj);
        } catch (IllegalArgumentException e) {
            LOG.error(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return rtn;
}

From source file:lapin.load.Loader.java

static private Object _impSymbols(Class clazz, Object export, Env env) throws IllegalAccessException {
    Lisp lisp = env.lisp();//w  w w  .  j ava 2 s.c  o m
    Field[] fields = clazz.getFields();
    Class symClass = Symbol.class;
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        int m = f.getModifiers();
        if (!Modifier.isStatic(m))
            continue;
        if (!symClass.isAssignableFrom(f.getType()))
            continue;
        Symbol sym = Data.symbol(f.get(null));
        lisp.getObarray().imp(sym.pkg(), sym);
        if (!Data.isNot(export))
            lisp.getObarray().exp(sym.pkg(), sym);
    }
    return Symbols.T;
}

From source file:com.microsoft.tfs.core.clients.build.internal.soapextensions.BuildEnumerationHelper.java

@SuppressWarnings("rawtypes")
public static String[] getDisplayTextValues(final Class enumType) {
    final List<String> displayValues = new ArrayList<String>();

    // Look for public static final fields in class that are same type as
    // the passed class.
    final Field[] fields = enumType.getFields();
    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getType().equals(enumType) && Modifier.isPublic(fields[i].getModifiers())
                && Modifier.isFinal(fields[i].getModifiers()) && Modifier.isStatic(fields[i].getModifiers())) {
            try {
                displayValues.add(getDisplayText(fields[i].get(null)));
            } catch (final IllegalAccessException e) {
                throw new BuildException(
                        MessageFormat.format("IllegalAccessException calculating display values for {0}", //$NON-NLS-1$
                                enumType.getName()),
                        e);/*from  w  w  w .  j  a  v  a 2  s .  co m*/
            }
        }
    }
    return displayValues.toArray(new String[displayValues.size()]);
}

From source file:com.github.yongchristophertang.config.PropertyHandler.java

public static void loadProperties(Class<?> testClass, Object[] testInstances) {
    Properties prop = new Properties();
    PropertyConfig[] propertyConfigs = getAnnotations(null, testClass, PropertyConfig.class);
    Lists.newArrayList(propertyConfigs).stream().filter(pc -> pc.value().endsWith(".properties"))
            .forEach(pc -> {//from   w w w  . jav  a  2  s  .  c o  m
                try {
                    prop.load(testClass.getClassLoader().getResourceAsStream(pc.value()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

    Field[] fields = testClass.getFields();
    Lists.newArrayList(fields).stream().filter(f -> f.isAnnotationPresent(Property.class))
            .filter(f -> f.getType() == String.class).peek(f -> f.setAccessible(true)).forEach(f -> {
                for (Object instance : testInstances) {
                    try {
                        f.set(instance, prop.getProperty(f.getAnnotation(Property.class).value()));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            });
}

From source file:lapin.load.Loader.java

static private Object _impSubrs(Object pkgname, Class clazz, Symbol indicator, Object export, Env env)
        throws IllegalAccessException {
    Lisp lisp = env.lisp();/* ww w.  ja v  a2  s.c  o m*/
    Field[] fields = clazz.getFields();
    Class subrClass = Subr.class;
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        int m = f.getModifiers();
        if (!Modifier.isStatic(m))
            continue;
        if (!subrClass.isAssignableFrom(f.getType()))
            continue;
        Subr subr = Data.subr(f.get(null));
        Symbol sym = Data.symbol(lisp.getObarray().intern(pkgname, subr.name()).nth(0));
        if (!Data.isNot(export))
            lisp.getObarray().exp(pkgname, sym);
        Object old = lisp.getProp(sym, indicator, Symbols.NIL);
        if (!Data.isNot(old))
            throw new LispException(
                    "conflict detected while importing subrs " + "defined in ~S: name=~S indicator=~S",
                    Lists.list(clazz, sym, indicator));
        lisp.setProp(sym, indicator, subr);
        if (subr instanceof Prop)
            lisp.setProp((Prop) subr, SysSymbols.SUBR_FIELD, f);
    }
    return Symbols.T;
}

From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java

public static ASMClass getClass(ExtendableClassLoader pcl, Resource classRoot, Class clazz)
        throws IOException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        SecurityException, InvocationTargetException, NoSuchMethodException {
    Type type = Type.getType(clazz);

    // Fields/*from ww  w.j  a  va2  s  .  c o m*/
    Field[] fields = clazz.getFields();
    for (int i = 0; i < fields.length; i++) {
        if (Modifier.isPrivate(fields[i].getModifiers()))
            continue;
        createField(type, fields[i]);
    }

    // Methods
    Method[] methods = clazz.getMethods();
    Map<String, ASMMethod> amethods = new HashMap<String, ASMMethod>();
    for (int i = 0; i < methods.length; i++) {
        if (Modifier.isPrivate(methods[i].getModifiers()))
            continue;
        amethods.put(methods[i].getName(), getMethod(pcl, classRoot, type, clazz, methods[i]));
    }

    return new ASMClass(clazz.getName(), amethods);

}

From source file:com.screenslicer.common.CommonUtil.java

public static <T extends Object> List<T> constructListFromMap(Class<T> classOf, Map<String, Object> args) {
    Field[] fields = classOf.getFields();
    int len = 0;/*from  w ww.  java  2  s  .c om*/
    for (int i = 0; i < fields.length; i++) {
        for (int j = 0; j < args.size() * 2; j++) {
            if (args.containsKey(fields[i].getName() + j) && j > len) {
                len = j;
            }
        }
    }
    ++len;
    List<Map<String, Object>> argsList = new ArrayList<Map<String, Object>>(len);
    for (int i = 0; i < fields.length; i++) {
        for (int j = 0; j < len; j++) {
            if (argsList.size() == j || argsList.get(j) == null) {
                argsList.add(new HashMap<String, Object>());
            }
            String name = fields[i].getName();
            String name0 = name + "0";
            String key = name + j;
            if (args.containsKey(name0)) {
                if (args.containsKey(key)) {
                    argsList.get(j).put(name, args.get(key));
                } else if (args.containsKey(name)) {
                    argsList.get(j).put(name, args.get(name));
                } else {
                    argsList.get(j).put(name, args.get(name0));
                }
            } else {
                argsList.get(j).put(name, args.get(name));
            }
        }
    }
    List<T> list = new ArrayList<T>();
    for (Map<String, Object> map : argsList) {
        list.add(CommonUtil.gson.fromJson(CommonUtil.gson.toJson(args), classOf));
    }
    return list;
}