Java Reference
In-Depth Information
public static String getClassInterfaces(Class c) {
// Get a comma-separated list of interfaces implemented by the class
Class[] interfaces = c.getInterfaces();
String interfacesList = null;
if (interfaces.length > 0) {
String[] interfaceNames = new String[interfaces.length];
for(int i = 0; i < interfaces.length; i++) {
interfaceNames[i] = interfaces[i].getSimpleName();
}
interfacesList = String.join(", ", interfaceNames);
}
return interfacesList;
}
public static String getGenericTypeParams(Class c) {
StringBuilder sb = new StringBuilder();
TypeVariable<?>[] typeParms = c.getTypeParameters();
if (typeParms.length > 0) {
String[] paramNames = new String[typeParms.length];
for(int i = 0; i < typeParms.length; i++) {
paramNames[i] = typeParms[i].getTypeName();
}
sb.append('<');
String parmsList = String.join(",", paramNames);
sb.append(parmsList);
sb.append('>');
}
return sb.toString();
}
}
public class Person extends Object implements Cloneable, Serializable
To get the simple class name, use the
getSimpleName()
method of the
Class
class, like so:
String simpleName = c.getSimpleName();
The modifiers of a class are the keywords that appear before the keyword
class
in the class declaration. In the
following example,
public
and
abstract
are the modifiers for the
MyClass
class:
public abstract
class MyClass {
// Code goes here
}
The
getModifiers()
method of the
Class
class returns all modifiers for the class. Note that the
getModifiers()
method returns an integer. To get the textual form of the modifiers, you need to call the
toString(int modifiers)
static method of the
java.lang.reflect.Modifier
class passing the modifiers value in an integer form. Assuming
c
is
the reference of a
Class
object, you get the modifiers of the class as shown: