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:Main.java

public static Object toSerializableObject(Object obj) {
    // Case for objects have no specified method to JSON string.
    if (obj.getClass().isArray()) {
        JSONArray result = new JSONArray();
        Object[] arr = (Object[]) obj;
        for (int i = 0; i < arr.length; ++i) {
            try {
                result.put(i, toSerializableObject(arr[i]));
            } catch (Exception e) {
                e.printStackTrace();/*from   w w  w.j  a  v  a 2s.c o  m*/
            }
        }
        return result;
    }
    // The original serializable object.
    if (isSerializable(obj))
        return obj;

    // Customized serializable object.
    //
    // If the object is not directly serializable, we check if it is customised
    // serializable. That means the developer implemented the public serialization
    // method "toJSONString".
    try {
        Method m = obj.getClass().getMethod("toJSONString", new Class<?>[0]);
        String jsonStr = (String) (m.invoke(obj, new Object[0]));
        if (jsonStr.trim().charAt(0) == '[') {
            return new JSONArray(jsonStr);
        } else {
            return new JSONObject(jsonStr);
        }
    } catch (Exception e) {
        Log.w(TAG, "No serialization method: \"toJSONString\", or errors happened.");
    }

    /*
     * For ordinary objects, we will just serialize the accessible fields.
     */
    try {
        Class<?> c = obj.getClass();
        JSONObject json = new JSONObject();
        Field[] fields = c.getFields();
        for (Field f : fields) {
            json.put(f.getName(), f.get(obj));
        }
        return json;
    } catch (Exception e) {
        Log.e(TAG, "Field to serialize object to JSON.");
        e.printStackTrace();
        return null;
    }
}

From source file:de.cbb.mplayer.mapping.MappingUtil.java

private static void mapFieldToPresenter(Field field, Object entity, Object presenter) {
    if (MappingFactory.EXCLUSIONS.contains(field.getName())) {
        return;/*from   ww  w . ja  v a2  s .c o  m*/
    }
    try {
        Object value1 = PropertyUtils.getSimpleProperty(entity, field.getName());
        String fieldname = field.getName();

        MappingFactory.buildMapper(presenter, fieldname, value1).map(value1);

    } catch (Exception ex) {
        log.debug("Unmapped attribute: " + field.getName() + " [" + ex.toString() + "]");
    }
}

From source file:Main.java

static String getElementName(Field field) {
    XmlElement element = (XmlElement) field.getAnnotation(XmlElement.class);
    return !DEFAULT_NAME.equals(element.name()) ? element.name() : field.getName();
}

From source file:ReflectionTest.java

/**
 * Prints all fields of a class//from w  w  w .  j a  v a 2s  .  c  o  m
 * @param cl a class
 */
public static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for (Field f : fields) {
        Class type = f.getType();
        String name = f.getName();
        System.out.print("   ");
        String modifiers = Modifier.toString(f.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.println(type.getName() + " " + name + ";");
    }
}

From source file:Main.java

public static Method findDoSelectMethodForField(Field field, Class<?> objectClass, Class<?> paramClass)
        throws NoSuchMethodException {
    String methodName = "select" + field.getName().toUpperCase().substring(0, 1) + field.getName().substring(1);
    return findMethodForField(methodName, objectClass, paramClass);
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotJsonSerializer.java

private static Object invokeGetterMethod(Object target, Field field) throws IOException {
    String fieldName = Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
    String getter = "get" + fieldName;

    Method method;/*from  www.  ja  v  a 2 s . c  o  m*/
    try {
        method = target.getClass().getMethod(getter);
    } catch (NoSuchMethodException | SecurityException e) {
        if (e instanceof NoSuchMethodException && boolean.class.equals(field.getType())) {
            getter = "is" + fieldName;
            try {
                method = target.getClass().getMethod(getter);
            } catch (NoSuchMethodException | SecurityException ie) {
                throw new IllegalArgumentException(ie);
            }
        } else {
            throw new IllegalArgumentException(e);
        }
    }

    Object value;
    try {
        value = method.invoke(target);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new IOException(e);
    }
    return value;
}

From source file:com.arconsis.android.datarobot.BasePersistenceTest.java

private static Collection<String> getAllFieldNames(EntityData entityData) {
    Collection<String> assoFieldNames = Collections2.transform(entityData.allAssociations,
            new Function<Field, String>() {

                @Override/*from   w  w  w .jav a 2s  . c om*/
                public String apply(Field field) {
                    return field.getName();
                }
            });
    return assoFieldNames;
}

From source file:be.bittich.dynaorm.core.StringQueryBuilder.java

/**
 * Return the request to get a row value mapped with the parameters
 *
 * @param <T>//from   w  ww . j a v  a2s . co m
 * @param t
 * @param fieldPrimary
 * @param dialect
 * @return
 * @throws RequestInvalidException
 */
public static <T> KeyValue<String, List<String>> conditionPrimaryKeysBuilder(T t,
        Map<Field, PrimaryKey> fieldPrimary, Dialect dialect) throws RequestInvalidException {
    boolean firstIteration = true;
    String req = "";
    List<String> parameters = new LinkedList();
    for (Field field : fieldPrimary.keySet()) {
        String label = "".equals(fieldPrimary.get(field).label()) ? field.getName()
                : fieldPrimary.get(field).label();
        String value = AnnotationProcessor.getFieldValue(field.getName(), t);
        if (firstIteration) {
            req = dialect.where(req);
            firstIteration = false;
        } else {
            req = dialect.andWhere(req);
        }
        req = dialect.equalTo(req, label);
        parameters.add(value);
    }
    return new DefaultKeyValue(req, parameters);
}

From source file:com.us.reflect.ClassFinder.java

/**
 *  before/*w w  w. java2 s . c o m*/
 * @param invocation
 */
public static void beforeAction(ActionInvocation invc, String actionPath) throws Exception {
    ActionInvocation invocation = (ActionInvocation) invc;
    final ActionProxy proxy = invocation.getProxy();
    Class<?> classFromPath = findClass(actionPath);// ?Class
    Class<?> targetClass = classFromPath == null ? proxy.getAction().getClass() : classFromPath;
    String methodName = findMethodName(actionPath);// ??? checkExist(sessionKey)
    String[] paramers = findParameters(actionPath);
    Class<?>[] paramersType = findParameterTypes(targetClass, paramers);
    Method method = targetClass.getDeclaredMethod(methodName, paramersType);
    Object runner = targetClass.newInstance();
    Field[] allFiled = targetClass.getDeclaredFields();
    for (Field field : allFiled) {
        String beanName = field.getName();
        Object bean = DaoHelper.getBeanById(beanName);
        if (bean != null) {
            BeanUtils.setProperty(runner, beanName, bean);
        }
    }
    method.invoke(runner, getRuntimeArgs(invocation, paramers));
}

From source file:Main.java

static String getAttributeName(Field field) {
    XmlAttribute attribute = (XmlAttribute) field.getAnnotation(XmlAttribute.class);
    return !DEFAULT_NAME.equals(attribute.name()) ? attribute.name() : field.getName();
}