Meta Class in Java
A meta class is with the reflection library of Java. It has the ability to be used to establish a factory or a single instance, in which every class shares its constructor parameters. In order to give an IDE access to class-level information and postpone loading the class until a time when an instance of the class is actually needed, a MetaClass describes an actual class. A MetaClass uses the proper class loader to bind the Class object from its class name. The newInstance() method can be used to create new instances of the class once it has been bound.
As known, getting the metadata about classes at runtime is commonly referred to as "metaclass" in Java. With Java's reflection API, you may view classes, interfaces, fields, methods, and other runtime objects.
public class MetaClass extends java.lang.Object
Declaration of a Constructor:
MetaClass(java.lang.ClassLoader classLoader, java.lang.String className)
Methods in Meta class:
Method | Description |
java.lang.ClassLoader getClassLoader() | For this meta class, obtain the classloader. |
java.lang.String getClassName() | Get the class name that is completely qualified. |
newInstance() | Establishes a new instance of the class that this MetaClass object is intended to represent. |
java.lang.Class | toClass()Using the class name, create the Class object. |
java.lang.String | toString()Build the code's strings. |
static <E> E -cast (java.lang.Stringvalue, java.lang.reflect.Type type) | Cast an object's String representation into the actual object. |
static <E> E -castWithoutKnowingType(java.lang.String value) | Boolean and checkConstructor(java.lang.Object... parameters) are returned. |
static MetaClass create(java.lang.Class<?> class) | Generates a new helper method, or MetaClass |
static MetaClass create(java.lang.String classname) | Creates a fresh MetaClass (helper method). |
<E> MetaClass.ClassFactory<E> createFactory(java.lang.Class<?>... classes) | Builds a factory that generates instances of this class from a constructor that accepts the specified types as parameters. |
<E> MetaClass.ClassFactory<E> createFactory(java.lang.Object... objects) | Generates a factory that creates instances of this class from a constructor that accepts the specified object types. |
<E> MetaClass.ClassFactory<E> createFactory(java.lang.String... classes) | Builds a factory that generates instances of this class from a constructor that accepts the specified types as parameters. |
<E,F extends E> F createInstance(java.lang.Class<E> type, java.lang.Object... params) | Makes an instance of the class, passing an array of objects as constructor parameters and requiring a cast to a specific type. NOTE: instead of calling the constructor that matches the signature supplied to this function, the resultant instance will [unlike Java] call the narrowest constructor. |
<E> E createInstance(java.lang.Object... objects) | Construct an instance of the class with an array of objects passed as constructor parameters, with the class automatically determining its type. NOTE: instead of calling the constructor that matches the signature supplied to this function, the resultant instance will [unlike Java] call the narrowest constructor. |
boolean | equals(java.lang.Object o) |
int | hashCode() |
java.lang.String | toString() |
MetaClass:
For the given class name, create a meta class.
Syntax:
public MetaClass(java.lang.ClassLoader classLoader, java.lang.String className)
Parameters:
classLoader - the loader of the class from which to load it. It cannot be empty.
className - the class's fully qualified name. It cannot be empty.
getClassName:
Get the completely qualified class name. The actual Class instance is created using this name.
Syntax:
public java.lang.String getClassName()
Returns: The fully qualified name of the class.
getClassLoader:
For this meta class, obtain the classloader.
public java.lang.ClassLoader getClassLoader()
Returns: the class loader that needs to load this meta class.
toclass:
Using the class name, create the Class object. Make use of the appropriate class loader for this.
Syntax:
public java.lang.Class toClass() throws java.lang.ClassNotFoundException
Returns: The original created class object
Throws:
java.lang.ClassNotFoundException - if the specified class was not found.
newInstance:
Establishes a new instance of the class that this MetaClass object is meant to represent.
Syntax:
public T newInstance() throwsjava.lang.InstantiationException java.lang.IllegalAccessException, java.lang.ClassNotFoundException
Returns: A newly generated instance
Throws:
java.lang.IllegalAccessException - if access to the Class or its initializer is denied.
java.lang.InstantiationException - if the class fails to instantiate for any other reason, or if the class is an interface, an array class, a primitive type, an abstract class, or void.
java.lang.ClassNotFoundException - if the MetaClass name is not able to locate the Class.
java.lang.ClassCastException - if the incorrect type of class is used
toString:
It overrides in class java.lang.Object
Syntax:
public java.lang.String toString()
hashCode:
It overrides in class java.lang.Object
Syntax:
public int hashCode()
equals:
It overrides and equals in class java.lang.Object
Syntax:
public boolean equals(java.lang.Object object)
Implementation:
FileName: MetaMainClass.java
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.io.*; class MetaClass { private int metaField; public void metaMethod() { // "Implementing the methods of meta class." } } public class MetaMainClass { public static void main(String[] args) { try { // Getting the Class object for MetaClass Class<?> metaClass = MetaClass.class; // obtaining the class name String className = metaClass.getName(); System.out.println("The Class Name is given by: " + className); // Getting the declared and specified fields Field[] fieldobj = metaClass.getDeclaredFields(); System.out.println("The Fields are:"); for (Field myfield : fieldobj) { System.out.println(myfield.getName()); } // obtaining the specifed declared methods Method[] methodobj = metaClass.getDeclaredMethods(); System.out.println("The Methods are:"); for (Method mymethod : methodobj) { System.out.println(mymethod.getName()); } //Various metadata operations can be implemented....... } catch (SecurityException | IllegalArgumentException e) { // To Handle the exceptions like SecurityException and IllegalArgumentException catch block is used e.printStackTrace(); } } }
Output:
The Class Name is given by: MetaClass The Fields are: metaField The Methods are: metaMethod