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:service.GroupService.java

private List<String> getRolesList() throws ClassNotFoundException {
    Class cl = Class.forName("roles.Roles");
    Field[] fields = cl.getFields();
    List<String> roles = new ArrayList();
    for (Field field : fields) {
        roles.add(field.getName());//from   www  .j  a v a  2 s  .  c  om
    }
    return roles;
}

From source file:loxia.support.json.JSONObject.java

/**
 * Get an array of field names from an Object.
 *
 * @return An array of field names, or null if there are no names.
 *///from  ww  w .ja  va  2 s .  com
@Deprecated
public static String[] getNames(Object object) {
    if (object == null) {
        return null;
    }
    Class<? extends Object> klass = object.getClass();
    Field[] fields = klass.getFields();
    int length = fields.length;
    if (length == 0) {
        return null;
    }
    String[] names = new String[length];
    for (int i = 0; i < length; i += 1) {
        names[i] = fields[i].getName();
    }
    return names;
}

From source file:org.cosmo.common.record.Meta.java

public static Meta Instance(Class<? extends Record> clazz) {
    Meta meta = ClassMetaMap.get(clazz);
    // this call  possiblely be made before the this "clazz" has actually
    // kicked off the static initializer to make RecordMeta initialized.
    // ie. Class A has a member in Class B, and accessed a Instance A with
    // member B before Class B is referenced. In that case DefnRecord.readImpl() would
    // call this method before it's init. To resolve that, this method iterate through
    // all method and call get() to kick of the static initaizer.
    if (meta == null) {
        for (Field field : clazz.getFields()) {
            try {
                field.get(null);//from ww w. j  a  v a 2 s. co m
            } catch (Exception e) {
            }
        }
        meta = ClassMetaMap.get(clazz);
        if (meta == null) {
            throw new RuntimeException(New.str("Invalid Meta class: ", clazz.getName()));
        }
    }
    return meta;
}

From source file:net.eledge.android.toolkit.db.internal.TableBuilder.java

private void collectFieldUpdatesAnnotations(SparseArray<List<String>> versionUpdates, Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(ModelUpdate.class)) {
            addUpdateIfNeeded(versionUpdates, field.getAnnotation(ModelUpdate.class));
        } else if (field.isAnnotationPresent(ModelUpdates.class)) {
            ModelUpdates updates = field.getAnnotation(ModelUpdates.class);
            for (ModelUpdate update : updates.value()) {
                addUpdateIfNeeded(versionUpdates, update);
            }/* ww  w . ja  v  a 2s.  co  m*/
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.ext.factory.stylekey.AbstractStyleKeyFactory.java

/**
 * Loads all public static stylekeys which are declared in the given class.
 *
 * @param c/*from   www  .  j  av a  2s  .c  om*/
 *          the class from where to load the stylekeys.
 * @throws SecurityException
 *           if the current security settings deny class access.
 */
protected void loadFromClass(final Class c) {
    final Field[] fields = c.getFields();
    for (int i = 0; i < fields.length; i++) {
        final Field f = fields[i];
        if (StyleKey.class.isAssignableFrom(f.getType()) == false) {
            // is no instance of stylekey...
            continue;
        }

        if (Modifier.isPublic(f.getModifiers()) && Modifier.isStatic(f.getModifiers())) {
            try {
                addKey((StyleKey) f.get(null));
            } catch (IllegalAccessException ex) {
                AbstractStyleKeyFactory.logger.warn("Unexpected Exception while loading stylekeys", ex);
            }
        }
    }
}

From source file:net.kseek.http.ModInternationalization.java

public ModInternationalization(TinyHttpServer server) {
    super();/*from w w  w .  j a  va2s .  co  m*/

    StringBuilder builder = new StringBuilder();

    try {

        String sClassName = server.mContext.getPackageName() + ".R.string";

        Class classToInvestigate = Class.forName(sClassName);

        Field[] fields = classToInvestigate.getFields();

        // Retrieves R.string
        //Class<?> String = Class.forName(server.mContext.getPackageName())+".R$string");
        //Field[] fields = String.getFields();

        // Constructs a JSON with all members starting with PREFIX
        builder.append("{\"");
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getName().startsWith(PREFIX)) {
                builder.append(fields[i].getName());
                builder.append("\":\"");
                builder.append((String) server.getString(fields[i].getInt(null)));
                builder.append("\",\"");
            }
        }

        if (builder.length() > 2) {
            builder.setLength(builder.length() - 2);
        } else {
            builder.setLength(builder.length() - 1);
        }
        builder.append("}");

        mJSON = builder.toString();

    } catch (Exception e) {
        Log.e(TAG, "Little problem with ModInternationalization !");
        e.printStackTrace();
    }

}

From source file:com.baqr.baqrcam.http.ModInternationalization.java

public ModInternationalization(TinyHttpServer server) {
    super();/* ww w .j  av a  2 s.co  m*/

    StringBuilder builder = new StringBuilder();

    try {

        // Retrieves R.string
        Class<?> String = Class.forName(server.mContext.getPackageName() + ".R$string");
        Field[] fields = String.getFields();

        // Constructs a JSON with all members starting with PREFIX
        builder.append("{\"");
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getName().startsWith(PREFIX)) {
                builder.append(fields[i].getName());
                builder.append("\":\"");
                builder.append((String) server.getString(fields[i].getInt(null)));
                builder.append("\",\"");
            }
        }

        if (builder.length() > 2) {
            builder.setLength(builder.length() - 2);
        } else {
            builder.setLength(builder.length() - 1);
        }
        builder.append("}");

        mJSON = builder.toString();

    } catch (Exception e) {
        Log.e(TAG, "Little problem with ModInternationalization !");
        e.printStackTrace();
    }

}

From source file:com.anrisoftware.globalpom.reflection.annotations.AnnotationDiscoveryImpl.java

private Set<AnnotationBean> findFields(Set<AnnotationBean> result, Object bean) {
    Class<? extends Object> type = bean.getClass();
    return findAnnotations(result, bean, asList(type.getFields()));
}

From source file:net.facework.core.http.ModInternationalization.java

public ModInternationalization(TinyHttpServer server) {
    super();//  w  w w .  j a  v  a 2s  . c om

    StringBuilder builder = new StringBuilder();

    try {

        // Retrieves R.string
        Class<?> String = Class.forName(server.mContext.getPackageName() + ".R$string");
        Field[] fields = String.getFields();

        // Constructs a JSON with all members starting with PREFIX
        builder.append("{\"");
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getName().startsWith(PREFIX)) {
                builder.append(fields[i].getName());
                builder.append("\":\"");
                builder.append(server.getString(fields[i].getInt(null)));
                builder.append("\",\"");
            }
        }

        if (builder.length() > 2) {
            builder.setLength(builder.length() - 2);
        } else {
            builder.setLength(builder.length() - 1);
        }
        builder.append("}");

        mJSON = builder.toString();

    } catch (Exception e) {
        Log.e(TAG, "Little problem with ModInternationalization !");
        e.printStackTrace();
    }

}

From source file:com.google.feedserver.util.CommonsCliHelper.java

/**
 * For each class registered, we extract options based on the flags set within
 * the class./* w  ww.  ja  va 2 s. c  o  m*/
 * 
 * - Any class field ending in {prefix}_FLAG is turned into "--{prefix}" on
 * the command line. eg. "public String adminEmail_FLAG" becomes
 * "--adminEmail" - Any field defined with {prefix}_HELP will be used as the
 * help text.
 * 
 * @return Options all commandline options registered for parsing.
 */
@SuppressWarnings("unchecked")
private Options createOptions() {

    Options options = new Options();
    options.addOption(new Option("help", false, "Print out usage."));
    for (Class flagClass : classes) {
        for (Field field : flagClass.getFields()) {
            if (field.getName().endsWith("_FLAG")) {
                String argName = field.getName().substring(0, field.getName().length() - "_FLAG".length());
                String helpText = getHelpText(flagClass, argName);
                if (field.getType().getName().equals(Boolean.class.getName())) {
                    options.addOption(new Option(argName, false, helpText));
                    options.addOption(new Option("no" + argName, true, helpText));
                } else {
                    options.addOption(new Option(argName, true, helpText));
                }
            }
        }
    }
    return options;
}