Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:lite.flow.runtime.kiss.ComponentUtil.java

public static void injectOutput(String outputName, Output<?> output, Object componentInstance)
        throws IllegalArgumentException, IllegalAccessException {

    Class<?> componentClazz = componentInstance.getClass();

    // find activity all Output type fields
    for (Field field : FieldUtils.getAllFields(componentClazz)) {
        Class<?> decl = field.getType();
        if (Output.class.isAssignableFrom(decl)) {

            String name = field.getName();
            if (name != null && name.equals(outputName)) {
                field.setAccessible(true);
                field.set(componentInstance, output);
                return;
            }//from  w w w  .ja va  2 s.  c o m
        }
    }

    throw new IllegalArgumentException(
            format("Class '%s' do not contain output '%s'", componentClazz.getName(), outputName));
}

From source file:com.job.portal.utils.BeanUtils.java

public static Map<String, Object> convertToMap(Object bean) {
    Map<String, Object> m = new HashMap<String, Object>();
    try {//from   w ww.j a  v  a  2 s  . co  m
        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true); // You might want to set modifier to public first.
            Object value = field.get(bean);
            if (value != null) {
                m.put(field.getName(), value);
            }
        }
    } catch (Exception e) {
        LogOut.log.error("In " + new Object() {
        }.getClass().getEnclosingClass().getName() + "." + new Object() {
        }.getClass().getEnclosingMethod().getName() + " " + e);
    }
    return m;
}

From source file:com.job.portal.utils.BeanUtils.java

public static JSONObject convertToJSON(Object bean) {
    JSONObject obj = new JSONObject();
    try {//  ww  w  .  j  a  v a  2s.  com
        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true); // You might want to set modifier to public first.
            Object value = field.get(bean);
            if (value != null) {
                obj.put(field.getName(), value);
            }
        }
    } catch (Exception e) {
        LogOut.log.error("In " + new Object() {
        }.getClass().getEnclosingClass().getName() + "." + new Object() {
        }.getClass().getEnclosingMethod().getName() + " " + e);
    }
    return obj;
}

From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java

static String getPropertyName(Field field, Configuration c) {
    String name = c.value();//from   w ww  .  j  av a 2 s  . c  om
    if (name.isEmpty()) {
        return field.getName();
    }
    return name;
}

From source file:com.vivekpanyam.evolve.Utils.java

private static Field getField(Class<?> cls, String name) {
    for (Field field : cls.getDeclaredFields()) {
        if (!field.isAccessible()) {
            field.setAccessible(true);//from ww w .  ja  va 2  s  .com
        }
        if (field.getName().equals(name)) {
            return field;
        }
    }
    return null;
}

From source file:de.micromata.genome.util.runtime.config.LocalSettingsConfigUtils.java

/**
 * Inits the from local settings.//w  w w .ja va 2 s . co  m
 *
 * @param bean the bean
 * @param localSettings the local settings
 */
public static void initFromLocalSettings(LocalSettingsConfigModel bean, LocalSettings localSettings) {
    List<Field> fields = PrivateBeanUtils.findAllFields(bean.getClass(),
            FieldMatchers.hasAnnotation(ALocalSettingsPath.class));
    for (Field field : fields) {
        ALocalSettingsPath lsp = field.getAnnotation(ALocalSettingsPath.class);
        String key = lsp.key();
        if ("<fieldName>".equals(key) == true) {
            key = field.getName();
        }
        PrivateBeanUtils.writeField(bean, field, localSettings.get(bean.buildKey(key), lsp.defaultValue()));
    }
}

From source file:Main.java

public static Map<String, Object> optPublicFieldKeyValueMap(Object obj) {
    Map<String, Object> map = new HashMap<String, Object>();
    if (obj != null) {
        Field[] fields = obj.getClass().getFields();
        for (Field f : fields) {
            try {
                boolean isStatic = Modifier.isStatic(f.getModifiers());
                if (!isStatic) {
                    Object value = f.get(obj);
                    if (value != null)
                        map.put(f.getName(), value);
                }//  w  ww  . j a v  a  2s. c  o m
            } catch (Exception e) {

            }
        }
    }
    return map;
}

From source file:de.micromata.genome.util.runtime.config.LocalSettingsConfigUtils.java

/**
 * To properties in section./*from w w w  .j  a  va2  s  . c  o m*/
 *
 * @param bean the bean
 * @param writer the writer
 */
public static void toPropertiesInSection(LocalSettingsConfigModel bean, LocalSettingsWriter writer) {
    List<Field> fields = PrivateBeanUtils.findAllFields(bean.getClass(),
            FieldMatchers.hasAnnotation(ALocalSettingsPath.class));
    for (Field field : fields) {
        ALocalSettingsPath lsp = field.getAnnotation(ALocalSettingsPath.class);
        String key = lsp.key();
        if ("<fieldName>".equals(key) == true) {
            key = field.getName();
        }
        String val = (String) PrivateBeanUtils.readField(bean, field);
        writer.put(bean.buildKey(key), val, lsp.comment());
    }
}

From source file:edu.mayo.cts2.framework.webapp.rest.view.jsp.Beans.java

/**
 * Inspect.//from ww w  .j  a va 2s. c  o  m
 *
 * @param bean the bean
 * @return the list
 */
public static List<Map.Entry<String, Object>> inspect(Object bean) {
    Map<String, Object> props = new LinkedHashMap<String, Object>();

    Class<?> clazz = bean.getClass();

    while (clazz != null) {
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String name = field.getName();

            Object value;
            try {
                value = field.get(bean);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            if (value != null) {
                props.put(name, value);
            }
        }
        clazz = clazz.getSuperclass();
    }

    List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(props.entrySet());

    Collections.sort(list, BEAN_COMPARATOR);

    return list;
}

From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java

private static MockedField translateField(Field field, Object container, String className, String contextPath) {
    Class<?> fieldType = field.getType();
    MockedField out = buildMockedField(className, field.getName(), contextPath, fieldType.getName());
    Object value = safeGet(field, container);
    if (value == null || isBlackListed(fieldType)) {
        out.setFieldValue(null);/*  w w  w . j a va2s. co  m*/
        return out;
    }
    int hashCode = System.identityHashCode(value);
    String existingClassMapping = CACHED_IDS.putIfAbsent(hashCode, out.getFieldType());
    if (StringUtils.isNotBlank(existingClassMapping)) {
        out.setLink(String.valueOf(hashCode));
        return out;
    } else if (field.isEnumConstant() || fieldType.isPrimitive()) {
        out.setFieldValue(String.valueOf(value));
    } else {
        out.setExpression(translateValue(value));
    }
    out.setRecordedObjectHashCode(hashCode);
    return out;
}