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:com.zb.app.common.core.lang.BeanUtils.java

/**
 * ?Beanmap,map/*from w ww  .ja v  a 2s.  com*/
 * 
 * @param bean
 * @return
 */
public static Map<String, String> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, String> valueMap = new LinkedHashMap<String, String>();
    Field[] fields = getAllFields(new ArrayList<Field>(), cls);

    for (Field field : fields) {
        try {
            if (field == null || field.getName() == null) {
                continue;
            }
            if (StringUtils.equals("serialVersionUID", field.getName())) {
                continue;
            }

            Object fieldVal = PropertyUtils.getProperty(bean, field.getName());

            String result = null;
            if (null != fieldVal) {
                if (StringUtils.equals("Date", field.getType().getSimpleName())) {
                    result = DateViewTools.formatFullDate((Date) fieldVal);
                } else {
                    result = String.valueOf(fieldVal);
                }
            }
            valueMap.put(field.getName(), result == null ? StringUtils.EMPTY : result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            continue;
        }
    }
    return valueMap;
}

From source file:com.harshad.linconnectclient.NotificationUtilities.java

@SuppressLint("DefaultLocale")
public static ArrayList<String> getNotificationText(Notification notification) {
    RemoteViews views = notification.contentView;
    Class<?> secretClass = views.getClass();

    try {/*w  w w.j a v  a  2s .com*/
        ArrayList<String> notificationData = new ArrayList<String>();

        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {
            if (!outerFields[i].getName().equals("mActions"))
                continue;

            outerFields[i].setAccessible(true);

            @SuppressWarnings("unchecked")
            ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views);
            for (Object action : actions) {
                Field innerFields[] = action.getClass().getDeclaredFields();

                Object value = null;
                for (Field field : innerFields) {
                    field.setAccessible(true);
                    // Value field could possibly contain text
                    if (field.getName().equals("value")) {
                        value = field.get(action);
                    }
                }

                // Check if value is a String
                if (value != null && value.getClass().getName().toUpperCase().contains("STRING")) {

                    notificationData.add(value.toString());
                }
            }

            return notificationData;
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:kina.utils.UtilMongoDB.java

/**
 * converts from an entity class with kina's anotations to BsonObject.
 *
 * @param t   an instance of an object of type T to convert to BSONObject.
 * @param <T> the type of the object to convert.
 * @return the provided object converted to BSONObject.
 * @throws IllegalAccessException//w  w  w. jav a2 s.  c om
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static <T extends KinaType> BSONObject getBsonFromObject(T t)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Field[] fields = filterKinaFields(t.getClass());

    BSONObject bson = new BasicBSONObject();

    for (Field field : fields) {
        Method method = Utils.findGetter(field.getName(), t.getClass());
        Object object = method.invoke(t);
        if (object != null) {
            if (Collection.class.isAssignableFrom(field.getType())) {
                Collection c = (Collection) object;
                Iterator iterator = c.iterator();
                List<BSONObject> innerBsonList = new ArrayList<>();

                while (iterator.hasNext()) {
                    innerBsonList.add(getBsonFromObject((KinaType) iterator.next()));
                }
                bson.put(kinaFieldName(field), innerBsonList);
            } else if (KinaType.class.isAssignableFrom(field.getType())) {
                bson.put(kinaFieldName(field), getBsonFromObject((KinaType) object));
            } else {
                bson.put(kinaFieldName(field), object);
            }
        }
    }

    return bson;
}

From source file:kilim.Fiber.java

static private void stateToString(StringBuilder sb, State s) {
    if (s == PAUSE_STATE) {
        sb.append("PAUSE\n");
        return;//from  ww  w  . j  a v a 2  s .  com
    }
    Field[] fs = s.getClass().getFields();
    for (int i = 0; i < fs.length; i++) {
        Field f = fs[i];
        sb.append(f.getName()).append(" = ");
        Object v;
        try {
            v = f.get(s);
        } catch (IllegalAccessException iae) {
            v = "?";
        }
        sb.append(' ').append(v).append(' ');
    }
    sb.append('\n');
}

From source file:kina.utils.UtilMongoDB.java

/**
 * converts from BsonObject to an entity class with kina's anotations
 *
 * @param classEntity the entity name.//from   w  ww . j  a va 2 s  .  com
 * @param bsonObject  the instance of the BSONObjet to convert.
 * @param <T>         return type.
 * @return the provided bsonObject converted to an instance of T.
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static <T> T getObjectFromBson(Class<T> classEntity, BSONObject bsonObject)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    T t = classEntity.newInstance();

    Field[] fields = filterKinaFields(classEntity);

    Object insert;

    for (Field field : fields) {
        Method method = Utils.findSetter(field.getName(), classEntity, field.getType());

        Class<?> classField = field.getType();

        Object currentBson = bsonObject.get(kinaFieldName(field));
        if (currentBson != null) {

            if (Iterable.class.isAssignableFrom(classField)) {
                Type type = field.getGenericType();

                insert = subDocumentListCase(type, (List) bsonObject.get(kinaFieldName(field)));

            } else if (KinaType.class.isAssignableFrom(classField)) {
                insert = getObjectFromBson(classField, (BSONObject) bsonObject.get(kinaFieldName(field)));
            } else {
                insert = currentBson;
            }

            method.invoke(t, insert);
        }
    }

    return t;
}

From source file:Main.java

private static void init() {
    try {/*from   w  ww .  j a va 2s  . c  om*/
        // read all com.android.internal.R
        Class clazz = Class.forName("com.android.internal.R$layout");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // public static final
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
                    && Modifier.isFinal(field.getModifiers())) {
                try {
                    int id = field.getInt(null);
                    sSystemLayoutResIds.put(id, field.getName());
                } catch (IllegalAccessException e) {
                }
            }
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

/**
 * Get all fields' value and put them to a map.
 * /*w  w w.  ja v  a  2s.  c o  m*/
 * @param bean
 * @return Map
 */
public static Map<String, Object> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, Object> valueMap = new HashMap<String, Object>();
    // Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        try {
            field.setAccessible(true);
            Object value = field.get(bean);
            //            if(value == null) {
            //               valueMap.put(field.getName(), "");
            //               continue;
            //            }
            valueMap.put(field.getName(), value);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return valueMap;
}

From source file:com.taobao.itest.spring.context.SpringContextManager.java

public static void findAnnotatedFieldsAndInjectBeanByName(Class<? extends Annotation> annotationType,
        Object object, ApplicationContext applicationContext) {
    Set<Field> fields = getFieldsAnnotatedWith(object.getClass(), annotationType);

    for (Field field : fields) {
        if (!hasBeanInjected(field, object)) {
            // applicationContext = determineApplicationContext(
            // applicationContext, field);
            setFieldValue(object, field, getBean(field.getName(), applicationContext));
        }/*www. j  a v  a2 s .  c  om*/
    }
}

From source file:net.buffalo.protocal.util.ClassUtil.java

private static HashMap getFieldMap(Class cl) {
    HashMap fieldMap = new HashMap();
    for (; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers()))
                continue;
            field.setAccessible(true);//w  ww.ja va 2 s .com
            fieldMap.put(field.getName(), field);
        }
    }

    return fieldMap;
}

From source file:Main.java

static Map<String, Object> objectToMap(Object object) {
    Map<String, Object> map = new HashMap<>();

    Class cls = object.getClass();
    while (cls != null) {
        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);/*from ww  w  . ja va 2  s .c  o  m*/

            Object value = null;
            try {
                value = field.get(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (value != null)
                map.put(field.getName(), value);
        }

        cls = cls.getSuperclass();
    }

    return map;
}