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.querybyexample.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnFields(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Field field : entityClass.getFields()) {
        Column column = field.getAnnotation(Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            values.put(field.getName(), getValueFromField(field, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, field.getName()));
            }//w w  w . ja  v  a  2s . co m
        }
    }
    return errors;
}

From source file:com.dbs.sdwt.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnFields(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Field field : entityClass.getFields()) {
        Column column = field.getAnnotation(Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            values.put(field.getName(), jpaUtil.getValueFromField(field, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, field.getName()));
            }// w  ww  . j av a 2  s  .  c o m
        }
    }
    return errors;
}

From source file:com.zimbra.cs.localconfig.LocalConfigCLI.java

private void loadExtensionLC(String className) {
    try {/*  www.j  a va  2s.co  m*/
        Class<?> lcClass = Class.forName(className);
        Field[] fields = lcClass.getFields();
        for (Field field : fields) {
            try {
                if (field.getType() == KnownKey.class) {
                    KnownKey key = (KnownKey) field.get(null);
                    // Automatically set the key name with the variable name.
                    key.setKey(field.getName());
                    // process annotations
                    if (field.isAnnotationPresent(Supported.class))
                        key.setSupported(true);
                    if (field.isAnnotationPresent(Reloadable.class))
                        key.setReloadable(true);
                }
            } catch (Throwable never) {
                // ignore
            }
        }
    } catch (ClassNotFoundException e) {
        // ignore
    }
}

From source file:io.github.huherto.springyRecords.RecordMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class.//from w ww  .  j  ava2  s.com
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, Field>();

    Field fields[] = mappedClass.getFields();
    for (Field field : fields) {
        int mod = field.getModifiers();
        if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
            for (Annotation a : field.getAnnotations()) {
                if (a.annotationType().isAssignableFrom(Column.class)) {
                    Column c = (Column) a;
                    String columnName = c.name();
                    mappedFields.put(columnName, field);
                }
            }
        }
    }
}

From source file:org.openlaszlo.remote.json.LZReturnObject.java

/**
 * Create JSON for an instance//from   w  ww .j a v  a  2s  .co m
 */
void pushObjectPOJO(Object object) throws Exception {
    Class<?> cl = object.getClass();
    Field[] fields = cl.getFields();
    for (int i = 0; i < fields.length; i++) {
        if (!Modifier.isPublic(fields[i].getModifiers()))
            continue;

        String fieldName = fields[i].getName();
        Object value;
        try {
            value = fields[i].get(object);
        } catch (IllegalAccessException e) {
            mLogger.error("IllegalAccessException", e);
            continue;
        }
        if (mLogger.isDebugEnabled()) {
            mLogger.debug("add field name " + fieldName + ", " + (value != null ? value.getClass() : null));
        }
        body.append(",");
        body.append(ScriptCompiler.JSONquote(fieldName) + ": ");
        createReturnValue(value);
    }
}

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

public String create(Class<?> clazz) {
    StringBuilder sb = new StringBuilder("CREATE TABLE ");
    sb.append(SQLBuilder.getTableName(clazz));
    sb.append(" (");
    boolean first = true;
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Column.class)) {
            if (!first) {
                sb.append(", ");
            }/*  www .  j a  v  a  2s  . c  om*/
            sb.append(createFieldDef(clazz, field));
            first = false;
        }
    }
    sb.append(")");
    return sb.toString();
}

From source file:org.jannocessor.service.render.VelocityTemplateRenderer.java

private Map<String, Object> getStaticFields(Class<?> clazz) {
    Map<String, Object> map = Power.map();

    for (Field field : clazz.getFields()) {
        if (Modifier.isStatic(field.getModifiers())) {
            try {
                map.put(field.getName(), field.get(null));
            } catch (Exception e) {
                logger.error("Cannot access field: " + field.getName(), e);
            }/*from  w w w . java  2s .c o m*/
        }
    }

    return map;
}

From source file:org.artifactory.repo.RepositoryConfigurationBase.java

/**
 * Extract from an Enum the {@link javax.xml.bind.annotation.XmlEnumValue} that are associated with its fields.
 *
 * @param clazz The class that is to be introspected
 * @return A map that maps {@link javax.xml.bind.annotation.XmlEnumValue#value()} to the enum name itself.
 *//*from   w  ww . jav a2s  . c om*/
protected Map<String, String> extractXmlValueFromEnumAnnotations(Class clazz) {
    Map<String, String> annotationToName = Maps.newHashMap();
    Field[] fields = clazz.getFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(XmlEnumValue.class)) {
            XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);
            annotationToName.put(annotation.value(), field.getName());
        }
    }
    return annotationToName;
}

From source file:org.apache.camel.util.ObjectHelper.java

/**
 * Lookup the constant field on the given class with the given name
 *
 * @param clazz  the class/*from   w  w  w. j  a  v a2 s.com*/
 * @param name   the name of the field to lookup
 * @return the value of the constant field, or <tt>null</tt> if not found
 */
public static String lookupConstantFieldValue(Class clazz, String name) {
    if (clazz == null) {
        return null;
    }

    for (Field field : clazz.getFields()) {
        if (field.getName().equals(name)) {
            try {
                return (String) field.get(null);
            } catch (IllegalAccessException e) {
                // ignore
                return null;
            }
        }
    }

    return null;
}

From source file:cn.webwheel.ActionSetter.java

public List<SetterInfo> parseSetters(Class cls) {
    List<SetterInfo> list = new ArrayList<SetterInfo>();
    Field[] fields = cls.getFields();
    for (int i = fields.length - 1; i >= 0; i--) {
        Field field = fields[i];/* w  w w  .ja va  2 s  .  c o m*/
        if (Modifier.isFinal(field.getModifiers()))
            continue;
        if (Modifier.isStatic(field.getModifiers()))
            continue;
        WebParam param = field.getAnnotation(WebParam.class);
        String name = field.getName();
        if (field.getType().isArray())
            name += "[]";
        if (param != null && !param.value().isEmpty())
            name = param.value();
        SetterInfo si = getSetterInfo(field, name);
        if (si == null) {
            if (param != null) {
                logger.severe("wrong WebParam used at " + field);
            }
            continue;
        }
        list.add(si);
    }

    Method[] methods = cls.getMethods();
    for (int i = methods.length - 1; i >= 0; i--) {
        Method method = methods[i];
        WebParam param = method.getAnnotation(WebParam.class);
        String name = isSetter(method);
        if (name == null) {
            continue;
        }
        if (method.getParameterTypes()[0].isArray()) {
            name += "[]";
        }
        if (param != null && !param.value().isEmpty()) {
            name = param.value();
        }
        SetterInfo si = getSetterInfo(method, name);
        if (si == null) {
            if (param != null) {
                logger.severe("wrong WebParam used at " + method);
            }
            continue;
        }
        list.add(si);
    }
    return list;
}