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:GetColor.java

public static void setObjectColor(Object obj, Color color) {
    Class cls = obj.getClass(); //#1

    try {//from  w  w w.  j a v a2 s .c  o  m
        Method method = cls.getMethod("setColor", //#2
                new Class[] { Color.class });

        method.invoke(obj, new Object[] { color }); //#3
    } catch (NoSuchMethodException ex) { //#4
        throw new IllegalArgumentException(cls.getName() + " does not support" + "method setColor(:Color)");
    } catch (IllegalAccessException ex) { //#5
        throw new IllegalArgumentException(
                "Insufficient access permissions to call" + "setColor(:Color) in class " + cls.getName());
    } catch (InvocationTargetException ex) { //#6
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static String classNameForTarget(Object target) {
    String packageName = target.getClass().getPackage().getName();
    String className = target.getClass().getName();
    className = className.substring(packageName.length() + 1).replace(DOT, NOT_A_DOT);
    return packageName + DOT + className;
}

From source file:Main.java

public static String getClassName(Object obj) {
    String arrays[] = obj.getClass().getName().split("\\.");
    return arrays[arrays.length - 1];
}

From source file:Main.java

private static boolean isPrimitiveBoxingType(Object obj) {
    String simplyName = obj.getClass().getSimpleName();
    if (simplyName.equals("Integer") || simplyName.equals("Double") || simplyName.equals("Float")
            || simplyName.equals("Boolean") || simplyName.equals("Long") || simplyName.equals("Byte")
            || simplyName.equals("Short") || simplyName.equals("Byte")) {
        return true;
    } else {//from  w ww. jav a 2 s. co m
        return false;
    }
}

From source file:Main.java

public static Method[] getSelfMethod(Object o) {
    Class c = o.getClass();
    Method[] ms = o.getClass().getMethods();
    List list = new ArrayList();
    for (Method m : ms) {
        int mod = m.getModifiers();
        if (m.getDeclaringClass().equals(c) && Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
            list.add(m);//w ww  .j av a  2 s .c o  m
        }

    }
    return (Method[]) list.toArray(new Method[0]);
}

From source file:MainClass.java

static Object doubleArray(Object original) {
    Object returnValue = null;/*from ww w.  ja  va  2  s .c  o m*/
    Class type = original.getClass();
    if (type.isArray()) {
        int length = Array.getLength(original);
        Class elementType = type.getComponentType();
        returnValue = Array.newInstance(elementType, length * 2);
        System.arraycopy(original, 0, returnValue, 0, length);
    }
    return returnValue;
}

From source file:Main.java

public static Object expand(Object a) {
    Class cl = a.getClass();

    if (!cl.isArray()) {
        return null;
    }/*from   w w  w. java 2 s. c  o m*/

    int length = Array.getLength(a);
    int newLength = length + 1; // 50% more
    Class componentType = a.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);

    System.arraycopy(a, 0, newArray, 0, length);
    return newArray;
}

From source file:Main.java

public static void classInspector(Object object) {
    Class cls = object.getClass();
    System.out.println(cls.toString());
    Field[] fieldlist = cls.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];

        System.out.println("name = " + fld.getName());
        System.out.println("decl class = " + fld.getDeclaringClass());
        System.out.println("type = " + fld.getType());
        System.out.println("-----");
    }/* w  ww .  ja v  a 2  s. c o  m*/
}

From source file:Main.java

public static void autoMappingJsonToObject(JSONObject json, Object obj) {
    Field[] fields = obj.getClass().getFields();
    for (Field field : fields) {
        if (json.optString(field.getName()) != null) {
            try {
                field.set(obj, json.optString(field.getName()));
            } catch (Exception e) {
            }/*from   w w  w . ja  va 2 s.c o  m*/
        }
    }
}

From source file:Main.java

public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }/*w w  w  . ja  v  a  2  s.  c  om*/
    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;
}