Class Meta Information
-
By using Java Reflection you can inspect Java classes at runtime.
Inspecting classes is often the first thing you do when using
Reflection. From the classes you can obtain information about the
following :-
- Class Name
- Class Modifies (public, private, synchronized etc.)
- Package Info
- Superclass
- Implemented Interfaces
- Constructors
- Methods
- Fields
- Annotations For a full list you should consult the
JavaDoc for java.lang.Class.
The Class Object:
Before any inspection on a class you need to obtain its java.lang.Classobject. All types in Java including the primitive types (int, long, float etc.) including arrays have an associated Class object. If you know the name of the class at compile time you can obtain a Class object like this:Class myObjectClass = MyObject.class
If you don't know the name at compile time, but have the class name
as a string at runtime, you can do like this:-
String className =
... //obtain class name as string at runtime Class
class = Class.forName(className);
When using the Class.forName() method you must supply the fully qualified class name. That is the class name including all package names.
For instance, if MyObject is located in package com.jenkov.myapp then the fully qualified class name iscom.jenkov.myapp.MyObject
The
Class.forName()
method may throw a
ClassNotFoundException
if the class cannot be found on the classpath at runtime.
Class Name:
From a Class object you can obtain its name in two versions. The
fully qualified class name (including package name) is obtained
using the getName() method like this:-
If you want the class name without the pacakge name you can obtain it using thegetSimpleName() method, like this:-
Class aClass = ... //obtain Class object.
See prev. section String className = aClass.getName();
If you want the class name without the pacakge name you can obtain it using thegetSimpleName() method, like this:-
Class aClass = ... //obtain Class object.
See prev. section String simpleClassName = aClass.getSimpleName();
Modifiers:
You can access the modifiers of a class via the Class object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:Class aClass = ... //obtain Class object.
See prev. section int modifiers = aClass.getModifiers();
The modifiers are packed into an int where each modifier is a flag bit that is either set or cleared. You can check the modifiers using these methods in the classjava.lang.reflect.Modifier:-
Modifier.isAbstract(int modifiers)
Modifier.isFinal(int modifiers)
Modifier.isInterface(int modifiers)
Modifier.isNative(int modifiers)
Modifier.isPrivate(int modifiers)
Modifier.isProtected(int modifiers)
Modifier.isPublic(int modifiers)
Modifier.isStatic(int modifiers)
Modifier.isStrict(int modifiers)
Modifier.isSynchronized(int modifiers)
Modifier.isTransient(int modifiers)
Modifier.isVolatile(int modifiers)
Modifier.isFinal(int modifiers)
Modifier.isInterface(int modifiers)
Modifier.isNative(int modifiers)
Modifier.isPrivate(int modifiers)
Modifier.isProtected(int modifiers)
Modifier.isPublic(int modifiers)
Modifier.isStatic(int modifiers)
Modifier.isStrict(int modifiers)
Modifier.isSynchronized(int modifiers)
Modifier.isTransient(int modifiers)
Modifier.isVolatile(int modifiers)


