Example usage for java.lang Object getClass

List of usage examples for java.lang Object getClass

Introduction

In this page you can find the example usage for java.lang Object getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E callWithDefault(Object target, String methodName, E defaultValue) {
    try {/*from w  ww  . jav a 2s  .com*/
        //noinspection unchecked
        return (E) target.getClass().getMethod(methodName, (Class[]) null).invoke(target);
    } catch (NoSuchMethodException ignored) {
    } catch (IllegalAccessException ignored) {
    } catch (InvocationTargetException ignored) {
    }

    return defaultValue;
}

From source file:Main.java

public static String getStringByMap(Map<String, Object> map) {
    StringBuffer sb = new StringBuffer(100);

    for (Map.Entry<String, Object> m : map.entrySet()) {
        String key = m.getKey();//from   w  w  w.  j  av a  2s  . c om
        Object value = m.getValue();
        if (value != null) {
            String clsName = value.getClass().getName();
            if (value.getClass().isArray())
                sb.append("[").append(key).append(":").append("ARRAY").append(":")
                        .append(toStringByArray((Object[]) value)).append("]");
            else if ((("java.lang.String".equals(clsName)) || ("java.lang.Integer".equals(clsName))
                    || ("java.lang.Long".equals(clsName)) || ("java.lang.Boolean".equals(clsName))
                    || ("java.util.Date".equals(clsName))) && (!"".equals(value.toString()))) {
                sb.append("[").append(key).append(":").append(value.getClass().getName()).append(":")
                        .append(esc(value.toString())).append("]");
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

public static JSONArray object2Json(Object data) throws JSONException {
    if (!data.getClass().isArray()) {
        throw new JSONException("Not a primitive data: " + data.getClass());
    }//from  w w  w  .j a  v a  2s  .  c o m
    final int length = Array.getLength(data);
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < length; ++i) {
        jsonArray.put(wrap(Array.get(data, i)));
    }

    return jsonArray;
}

From source file:Main.java

public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<String, Method>();
        for (Method method : bean.getClass().getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
                    && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) {
                String name = method.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method);
                } else if (name.length() > 2 && name.startsWith("is")) {
                    cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method);
                }//from  www . j  a  v  a 2  s  .co  m
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:Main.java

public static Method discover(Object o, String method, Class<?> signature) {
    Method m = null;/*from ww w .j a va  2  s  .  co  m*/
    try {
        m = o.getClass().getMethod(method, signature);
    } catch (Exception ignore) {
    }
    return m;
}

From source file:Main.java

private static List<Field> getAllFieads(Object o) {
    ArrayList<Field> fields = new ArrayList<Field>();
    if (o != null) {
        Class<?> type = o.getClass();
        do {/*  w  ww  .  jav a 2  s  . c  o  m*/
            for (Field f : type.getDeclaredFields()) {
                fields.add(f);
            }
            type = type.getSuperclass();
        } while (type != null);
    }
    return fields;
}

From source file:com.mooregreatsoftware.gradle.license.ExtLicensePlugin.java

private static void addExclude(Object licenseExt, String exclusion) {
    try {//from w w  w .  j a v  a 2  s .  c o m
        licenseExt.getClass().getMethod("exclude", String.class).invoke(licenseExt, exclusion);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new PluginApplicationException(PLUGIN_ID, e);
    }
}

From source file:Main.java

public static void dumpBatteryStats(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

    Bundle bundle = batteryStatus.getExtras();
    for (String key : bundle.keySet()) {
        Object value = bundle.get(key);
        Log.d(TAG, String.format("Battery,%s=%s (%s)", key, value.toString(), value.getClass().getName()));
    }/*from   w w  w  .j a  va 2s .  co m*/
}

From source file:Main.java

public static void setField(Object obj, String fieldName, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    prepareField(obj.getClass(), fieldName).set(obj, value);
}

From source file:springfox.documentation.schema.Enums.java

private static Optional<Method> findJsonValueAnnotatedMethod(Object enumConstant) {
    for (Method each : enumConstant.getClass().getMethods()) {
        JsonValue jsonValue = AnnotationUtils.findAnnotation(each, JsonValue.class);
        if (jsonValue != null && jsonValue.value()) {
            return Optional.of(each);
        }//from   w w w .j a  v a2s .co  m
    }
    return Optional.absent();
}