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

private static void printType(Object object) {
    Class type = object.getClass();
    if (type.isArray()) {
        Class elementType = type.getComponentType();
        System.out.println("Array of: " + elementType);
        System.out.println("Array size: " + Array.getLength(object));
    }/*from   w  w  w  . j  av a 2s  .  c  om*/
}

From source file:Main.java

public static String getTag(Object o) {
    return o.getClass().getSimpleName();
}

From source file:SampleSuper.java

static void printSuperclasses(Object o) {
    Class subclass = o.getClass();
    Class superclass = subclass.getSuperclass();
    while (superclass != null) {
        String className = superclass.getName();
        System.out.println(className);
        subclass = superclass;//from  ww  w  . j a  v  a2s  . c  o  m
        superclass = subclass.getSuperclass();
    }
}

From source file:SampleConstructor.java

static void showConstructors(Object o) {
    Class c = o.getClass();
    Constructor[] theConstructors = c.getConstructors();
    for (int i = 0; i < theConstructors.length; i++) {
        System.out.print("( ");
        Class[] parameterTypes = theConstructors[i].getParameterTypes();
        for (int k = 0; k < parameterTypes.length; k++) {
            String parameterString = parameterTypes[k].getName();
            System.out.print(parameterString + " ");
        }//  w ww.j  a  v  a2 s.c o m
        System.out.println(")");
    }
}

From source file:Main.java

public static int getDim(Object array) {
    int dim = 0;//from ww  w .  ja  v a2  s . c  om
    Class cls = array.getClass();
    while (cls.isArray()) {
        dim++;
        cls = cls.getComponentType();
    }
    return dim;
}

From source file:Main.java

public static boolean isArray(Object obj) {
    return obj.getClass().isArray();
}

From source file:SampleComponentReflection.java

static void printComponentType(Object array) {
    Class arrayClass = array.getClass();
    String arrayName = arrayClass.getName();
    Class componentClass = arrayClass.getComponentType();
    String componentName = componentClass.getName();
    System.out.println("Array: " + arrayName + ", Component: " + componentName);
}

From source file:Main.java

private static boolean isArray(Object object) {
    return object.getClass().getName().startsWith("[");
}

From source file:Main.java

public static int getArrayDimension(Object array) {
    int dimension = 0;
    Class c = array.getClass();
    if (!c.isArray()) {
        throw new IllegalArgumentException("Object is not  an  array");
    }// w w  w  .j  a  va2  s . co  m
    while (c.isArray()) {
        dimension++;
        c = c.getComponentType();
    }
    return dimension;
}

From source file:SampleMethod.java

static void showMethods(Object o) {
    Class c = o.getClass();
    Method[] theMethods = c.getMethods();
    for (int i = 0; i < theMethods.length; i++) {
        String methodString = theMethods[i].getName();
        System.out.println("Name: " + methodString);
        String returnString = theMethods[i].getReturnType().getName();
        System.out.println("   Return Type: " + returnString);
        Class[] parameterTypes = theMethods[i].getParameterTypes();
        System.out.print("   Parameter Types:");
        for (int k = 0; k < parameterTypes.length; k++) {
            String parameterString = parameterTypes[k].getName();
            System.out.print(" " + parameterString);
        }/*from w ww . j  a  v a2  s  .  c o m*/
        System.out.println();
    }
}