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

/**
 * Get a class or interface from an object, if available.  This is useful
 * when an object may implement optional interfaces, and we want to
 * get those interfaces./*  w  ww  .  j a va  2 s  .  c o m*/
 * 
 * As an example, see {@link AudioPlayerWithEvents}.
 * 
 * @param object the object to inspect
 * @param desiredClass the class to get
 * @return
 */
public static <T> T getIfAvailable(Object object, Class<T> desiredClass) {
    return (desiredClass.isAssignableFrom(object.getClass())) ? desiredClass.cast(object) : null;
}

From source file:com.idealista.solrmeter.util.ReflectionUtils.java

/**
 * This method will invoke the getter method of the attribute with the attributeName
 * passed as parameter.//from  w  w  w. j a  v a 2  s . c o  m
 * @param object
 * @param attributeName
 * @return
 */
public static Object getAttribute(Object object, String attributeName) {
    try {
        return object.getClass().getMethod("get" + StringUtils.capitalize(attributeName)).invoke(object);
    } catch (Exception e) {
        throw new RuntimeException("Cant invoke the getter method of the attribute " + attributeName
                + " on the object " + object.toString() + ". Generated method name was " + "get"
                + StringUtils.capitalize(attributeName), e);
    }
}

From source file:framework.ReloadingServer.java

private static void run(String className, String method) {
    try {//www  .  jav a  2s  . c om
        Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        Object obj = clazz.newInstance();
        obj.getClass().getMethod(method).invoke(obj);
    } catch (Exception e) {
        Loggers.SERVER.error(e.getMessage(), e);
    }
}

From source file:Main.java

private static final List<Class<?>> objectsToClasses(List<Object> actualArgs) {
    final List<Class<?>> actualClasses = new ArrayList<Class<?>>(actualArgs.size());
    for (final Object actualArg : actualArgs)
        actualClasses.add(actualArg == null ? null : actualArg.getClass());
    return actualClasses;
}

From source file:Main.java

/**
 * Filters Views based on the given class type.
 * /*from  w  w  w.j a  va2 s .  c  om*/
 * @param classToFilterBy the class to filter
 * @param viewList the Iterable to filter from
 * @return an ArrayList with filtered views
 */

public static <T> ArrayList<T> filterViews(Class<T> classToFilterBy, Iterable<?> viewList) {
    ArrayList<T> filteredViews = new ArrayList<T>();
    for (Object view : viewList) {
        if (view != null && classToFilterBy.isAssignableFrom(view.getClass())) {
            filteredViews.add(classToFilterBy.cast(view));
        }
    }
    viewList = null;
    return filteredViews;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed./*from  w w  w. j  ava  2  s .c om*/
 */
public static <T> T[] emptyArray(Class kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed./* ww w .j a va 2s. c  o m*/
 */
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:com.googlecode.psiprobe.controllers.threads.ListThreadsController.java

private static String toUID(Object o) {
    return o.getClass().getName() + "@" + o.hashCode();
}

From source file:Main.java

private static Marshaller createMarshaller(Object o) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return jaxbMarshaller;
}

From source file:Main.java

public static String toXML(Object obj) {
    try {//from   ww w.  j a  v a 2s .  c  o  m
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}