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

public static <E> E callWithDefault(Object target, String methodName, E defaultValue) {
    try {/*from www  .  j a va 2 s. c o m*/
        //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:Util.java

/**
 * Is the given {@code object} a primitive type or wrapper for a primitive
 * type?/*from   w ww.j  a va2  s . c  o m*/
 * 
 * @param object
 *            The object to check for primitive-ness.
 * @return {@code true} if {@code object} is a primitive type or wrapper for
 *         a primitive type, {@code false} otherwise.
 */
public static boolean isPrimitive(Object object) {
    if (object == null)
        return false;

    Class<?> type = object.getClass();

    return object instanceof String || (object instanceof Integer || Integer.TYPE.equals(type))
            || (object instanceof Boolean || Boolean.TYPE.equals(type))
            || (object instanceof Long || Long.TYPE.equals(type))
            || (object instanceof Double || Double.TYPE.equals(type))
            || (object instanceof Float || Float.TYPE.equals(type))
            || (object instanceof Byte || Byte.TYPE.equals(type))
            || (object instanceof Short || Short.TYPE.equals(type))
            || (object instanceof Character || Character.TYPE.equals(type));
}

From source file:Main.java

public static Object expandBy(Object oldArray, int increment) {
    Object newArray = null;//w ww  .  java 2  s.  c  o  m
    int oldLength = Array.getLength(oldArray);
    int newLength = oldLength + increment;
    Class<?> c = oldArray.getClass();
    newArray = Array.newInstance(c.getComponentType(), newLength);
    System.arraycopy(oldArray, 0, newArray, 0, oldLength);
    return newArray;
}

From source file:Main.java

/**
 * Create and return the name of the <code>ManagedBean</code> that
 * corresponds to this Catalina component.
 *
 * @param component The component for which to create a name
 *///  w  ww  . ja  v a  2  s .c o m
static String createManagedName(Object component) {

    // Deal with exceptions to the standard rule
    String className = component.getClass().getName();
    for (int i = 0; i < exceptions.length; i++) {
        if (className.equals(exceptions[i][0])) {
            return (exceptions[i][1]);
        }
    }

    // Perform the standard transformation
    int period = className.lastIndexOf('.');
    if (period >= 0)
        className = className.substring(period + 1);
    return (className);

}

From source file:Main.java

public static void pojoToXml(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(o, file);/*from  w  w w . j a  v  a  2s  .  c o  m*/
}

From source file:Main.java

public static String getAvailableStoragePath(Context context) {
    if (BASE_DIR == null) {
        try {/*from  w  ww  . j av a  2s  . c o  m*/
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
            StorageManager sStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

            Class<?> smClazz = sStorageManager.getClass();
            Method listMethod = smClazz.getDeclaredMethod("getVolumeList");
            Object vlObject = listMethod.invoke(sStorageManager);

            if (vlObject.getClass().isArray()) {
                String state = null;
                String path = null;
                // Class svClazz =
                // Class.forName("android.os.storage.StorageVolume");
                Object svObject = Array.get(vlObject, 1);
                if (svObject != null) {
                    Method pathMethod = svObject.getClass().getMethod("getPath");
                    path = (String) pathMethod.invoke(svObject);

                    Method stateMethod = smClazz.getMethod("getVolumeState", new Class[] { String.class });
                    state = (String) stateMethod.invoke(sStorageManager, path);
                }

                if (path != null && state != null && state.equals(Environment.MEDIA_MOUNTED)) {
                    BASE_DIR = path;
                } else {
                    BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
                }
            }
        } catch (Exception e) {
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

        return BASE_DIR;
    } else {
        return BASE_DIR;
    }

}

From source file:com.dangdang.ddframe.job.lite.spring.util.AopTargetUtils.java

private static Object getTargetObject(final Object object) {
    try {/*from ww  w.  j  av a  2  s . c o  m*/
        Field advised = object.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(object)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobSystemException(ex);
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean setProxyLollipop(final Context context, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {//from ww w  . j  av a  2 s.  c om
        Context appContext = context.getApplicationContext();
        Class applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:Main.java

/**
 * get the method start with 'get' or 'is'.
 *///from w ww .  ja v  a 2 s .c  o  m
public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<>();
        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);
                }
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:Main.java

/**
 * Returns all objects of given type and its subtypes
 * @param clazz The class you want to return all of the instances of.
 * @param objects The collection you'd like to search.
 * @return A List of all of the instances of a specific Class found in a Collection.
 *//*from   ww  w .  j  a  v a 2  s  . com*/
public static List getAllInstances(Class clazz, Collection objects) {
    List allInstances = new ArrayList();
    if (objects != null && clazz != null) {
        Iterator objectsIterator = objects.iterator();
        while (objectsIterator.hasNext()) {
            Object instance = objectsIterator.next();
            if (instance != null) {
                if (clazz.isAssignableFrom(instance.getClass())) {
                    allInstances.add(instance);
                }
            }
        }
    }
    return allInstances;
}