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

/**
 * Returns a setter method for the given property, or null if there is none.
 *
 * @param target       Target exposing the setter.
 * @param propertyName The setter's property.
 * @return a setter method for the given property, or null if there is none.
 *//*from   www .  j av  a 2s  . c o  m*/
public static synchronized Method getSetter(final Object target, final String propertyName) {
    return getSetter(target.getClass(), propertyName);
}

From source file:ArrayDemo.java

/** 
 * Copy an array and return the copy.//from   w  w  w.j  av a2s  . c  o m
 *
 * @param input The array to copy.
 *
 * @return The coppied array.
 *
 * @throws IllegalArgumentException If input is not an array.
 */
public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }
    final int length = Array.getLength(input);
    final Class componentType = type.getComponentType();

    final Object result = Array.newInstance(componentType, length);
    for (int idx = 0; idx < length; idx++) {
        Array.set(result, idx, Array.get(input, idx));
    }
    return result;
}

From source file:Main.java

public static Object setAdditionalStaticField(Object obj, String key, Object value) {
    return setAdditionalInstanceField(obj.getClass(), key, value);
}

From source file:Main.java

public static <T> T getVar(@NonNull Object obj, @NonNull String name) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Field f : cls.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                f.setAccessible(true);//from  w  w  w. j a  v  a2 s  .  c o  m
                //noinspection unchecked
                return (T) f.get(obj);
            }
        }
    }
    throw new RuntimeException("no var matching " + name);
}

From source file:Main.java

public static Class<?> findFirstElementType(Collection<?> collection) {
    for (Object object : collection) {
        if (object != null) {
            return object.getClass();
        }/*  www. j a  va2 s  .  c o  m*/
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> void addAll(Collection<T> dest, Collection<? super T> src, Class<? extends T> type) {
    if (dest == null || src == null || type == null) {
        throw new IllegalArgumentException("Arguments must not be null.");
    }//from www.  j  av  a 2  s .  c o  m
    for (Object element : src) {
        if (element != null && type.isAssignableFrom(element.getClass())) {
            dest.add((T) element);
        }
    }
}

From source file:Main.java

private static void marshalToFile(Object objJAXB, String fullFileName) throws Exception {
    newMarshaller(objJAXB.getClass()).marshal(objJAXB, new File(fullFileName));
}

From source file:Main.java

/**
 * Returns the first object of given type or its subtypes
 * @param clazz The class you want to return the first instance found of.
 * @param objects The collection you'd like to search.
 * @return The first instance of a specific Class found in a Collection.  Returns null if no instance is found.
 *///from  w  w w  .j av  a2  s  .c  o m
public static Object getFirstInstance(Class clazz, Collection objects) {
    if (objects != null && clazz != null) {
        Iterator objectsIterator = objects.iterator();
        while (objectsIterator.hasNext()) {
            Object instance = objectsIterator.next();
            if (clazz.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}

From source file:Main.java

public static void Object2XmlFile(Object ob, String path) throws JAXBException, FileNotFoundException {
    Class local = ob.getClass();
    JAXBContext context = JAXBContext.newInstance(new Class[] { local });
    Marshaller marshaller = context.createMarshaller();
    // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
    // paramNamespacePrefixMapper);
    // marshaller.setProperty("jaxb.formatted.output",
    // Boolean.valueOf(true));
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(ob, new FileOutputStream(new File(path)));
}

From source file:Main.java

private static boolean isListenerRegistered(Object[] objects, Class t, EventListener l) {
    for (int i = 0; i < objects.length; i++) {
        Object listener = objects[i];

        if (t.isAssignableFrom(listener.getClass()) && (listener == l)) {
            return true;
        }/* w w w.  j a  v a 2 s.  co  m*/
    }

    return false;
}