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

/** Constructs a JToggleButton with an icon from the given file id. */
public static JToggleButton makeToggleButton(final Object owner, final String id, final String altText,
        final int wpad, final int hpad) {
    final URL url = owner.getClass().getResource(id);
    ImageIcon icon = null;//from   www  .  ja  va  2  s.  co  m
    if (url != null)
        icon = new ImageIcon(url);
    JToggleButton button;
    if (icon == null)
        button = new JToggleButton(altText);
    else {
        button = new JToggleButton(icon);
        button.setPreferredSize(new Dimension(icon.getIconWidth() + wpad, icon.getIconHeight() + hpad));
    }
    return button;
}

From source file:ClassUtil.java

/**
 * Report the full name of the object class, without the package information
 *
 * @param obj the object to name/*from w  w w. j a v a  2 s  .  c o  m*/
 * @return the concatenation of (enclosing) simple names
 */
public static String nameOf(Object obj) {
    StringBuilder sb = new StringBuilder();

    for (Class cl = obj.getClass(); cl != null; cl = cl.getEnclosingClass()) {
        if (sb.length() > 0) {
            sb.insert(0, "-");
        }

        sb.insert(0, cl.getSimpleName());
    }

    return sb.toString();
}

From source file:MethodInfoDemo.java

/**
 * __UNDOCUMENTED__//from  ww  w.  ja  v  a 2  s .c o  m
 * 
 * @param obj
 *          __UNDOCUMENTED__
 * 
 * @throws IllegalAccessException
 *           __UNDOCUMENTED__
 * @throws InvocationTargetException
 *           __UNDOCUMENTED__
 */
public static void emptyStrings(final Object obj) throws IllegalAccessException, InvocationTargetException {
    final String PREFIX = "set"; //$NON-NLS-1$
    Method[] methods = obj.getClass().getMethods();
    for (int idx = 0; idx < methods.length; idx++) {
        if (methods[idx].getName().startsWith(PREFIX)) {
            if (methods[idx].getParameterTypes()[0] == String.class) {
                methods[idx].invoke(obj, new Object[] { new String() });
            }
        }
    }
}

From source file:com.rdg.json.util.JSONUtil.java

/**
 * transform object to xml//  w w  w  .  j  a  v a 2s .  c  o  m
 *
 * @param object
 */
public static void objectToJSON(Object object) {
    try {
        objectMapper.readValue(new File(FILE_NAME), object.getClass());
    } catch (IOException e) {
        logger.error(Const.ERROR_IN_JSON_READ + e);
    }
}

From source file:easyrpc.client.serialization.jsonrpc.JSONCaller.java

private static void addType(Object value, ArrayNode arr) {
    switch (value.getClass().getName()) {
    case "java.lang.Integer":
    case "int":
        arr.add((Integer) value);
        break;/*from  w ww.  ja v a 2 s. c  o m*/
    case "java.lang.Long":
    case "long":
        arr.add((Long) value);
        break;
    case "java.lang.Character":
    case "char":
        arr.add((java.lang.Character) value);
        break;
    case "java.lang.Void":
    case "void":
        throw new IllegalArgumentException("A parameter cannot be of void type");
    case "java.lang.Float":
    case "float":
        arr.add((Float) value);
        break;
    case "java.lang.Double":
    case "double":
        arr.add((Double) value);
        break;
    case "java.lang.String":
        arr.add((String) value);
        break;
    default:
        // map an object
        arr.add(MAPPER.valueToTree(value));
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.logging.ToString.java

public static String simpleName(Object o) {
    return (o == null) ? "null" : o.getClass().getSimpleName();
}

From source file:ClassLoaderUtils.java

/**
 * Show the class loader hierarchy for this class.
 * @param obj object to analyze loader hierarchy for
 * @param role a description of the role of this class in the application
 * (e.g., "servlet" or "EJB reference")/* w w w .j a v a 2s  .c om*/
 * @param lineBreak line break
 * @param tabText text to use to set tabs
 * @return a String showing the class loader hierarchy for this class
 */
public static String showClassLoaderHierarchy(Object obj, String role, String lineBreak, String tabText) {
    String s = "object of " + obj.getClass() + ": role is " + role + lineBreak;
    return s + showClassLoaderHierarchy(obj.getClass().getClassLoader(), lineBreak, tabText, 0);
}

From source file:Main.java

/**
 * This method is a helper for classes to implement {@link java.lang.Object#equals(java.lang.Object)}
 * The method checks whether the two arguments are both null or both not null and
 * whether they are of the same class/*from w w w.  ja  va 2 s  . c  o  m*/
 * @param obj1 first object to compare
 * @param obj2 second object to compare
 * @return true if both objects are null or both are not null
 * and if both are of the same class if not null
 * false otherwise
 * 
 * source from: https://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/Utils.java
 */
public static boolean checkNullAndClass(Object obj1, Object obj2) {
    if (checkNullEquals(obj1, obj2, false)) {
        if (obj1 != null) {
            return obj1.getClass() == obj2.getClass();
        } else {
            return true; // both obj1 and obj2 should be null
        }
    } else {
        return false;
    }
}

From source file:Main.java

public static void cleanThreadLocals(Thread thread)
        throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
    if (thread == null)
        return;//  w ww  .j  ava  2 s . com
    Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
    threadLocalsField.setAccessible(true);

    Class<?> threadLocalMapKlazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
    Field tableField = threadLocalMapKlazz.getDeclaredField("table");
    tableField.setAccessible(true);

    Object fieldLocal = threadLocalsField.get(thread);
    if (fieldLocal == null) {
        return;
    }
    Object table = tableField.get(fieldLocal);

    int threadLocalCount = Array.getLength(table);

    for (int i = 0; i < threadLocalCount; i++) {
        Object entry = Array.get(table, i);
        if (entry != null) {
            Field valueField = entry.getClass().getDeclaredField("value");
            valueField.setAccessible(true);
            Object value = valueField.get(entry);
            if (value != null) {

                if ("java.util.concurrent.ConcurrentLinkedQueue".equals(value.getClass().getName())
                        || "java.util.concurrent.ConcurrentHashMap".equals(value.getClass().getName())) {
                    valueField.set(entry, null);
                }
            }

        }
    }
}