Friday, June 9, 2023
HomeJavaReflection for the trendy Java programmer

Reflection for the trendy Java programmer

Make the most of the wealthy metadata the Java compiler supplies to the JVM.

Reflection is likely one of the most versatile and highly effective methods Java builders have at their command. Nonetheless, it’s often misunderstood or misused, and sure features of its API and design are fairly clunky and, in locations, outdated.

Java Programmer, Oracle Java Career, Oracle Java Jobs, Oracle Java Prep, Oracle Java Preparation, Oracle Java Tutorial and Material, Oracle Java

However, to change into a complicated Java developer, gaining a strong understanding of reflection (also referred to as the Core Reflection API) is a necessary step. Let’s begin with an essential level of historical past that typically goes unnoticed.

When Java was first launched over 25 years in the past, the 2 most dominant programming languages have been most likely C and C++. As most Java builders know, the syntax of Java is closely influenced by these languages, particularly C++. Nonetheless, regardless of the same syntax, as programming environments go, Java could be very completely different from C++. The character of this distinction is in how applications are constructed and executed.

◉ C++ compiles to a local machine code binary that may be straight executed.

◉ Java compiles to a transportable, intermediate format (bytecode) that requires a runtime container to execute.

The Java runtime execution container, that’s, the Java Digital Machine, supplies exterior help for sure considerations that Java applications delegate to the runtime. The same old instance of those considerations is rubbish assortment: The Java programmer has no management over allocating and recovering reminiscence throughout the heap. As a substitute, the reclamation course of is below the entire management of the JVM.

That is an instance of what’s meant by describing the JVM as a managed setting. Java programmers surrender management of the exact low-level particulars of dealing with reminiscence and in return the JVM manages these (boring, pedantic, exacting) particulars on their behalf.

In the case of reflection, there may be one other side of the JVM’s managed execution setting to deal with: the wealthy runtime sort info that’s current in each executing Java program. Think about a bit of code like the next:

Record<String> ls = getSomeStrings();


With out data at runtime of the inheritance and interface implementation hierarchy of this system, how can the JVM work out which dimension() technique to name? In spite of everything, the Record interface doesn’t even include a way implementation.

The small print of the hierarchy are contained within the class information as a result of every particular person class refers back to the varieties it relies upon upon. The JVM, throughout the class loading course of, assembles this information right into a illustration of a graph that describes the category inheritance and interface implementation of each sort within the system. This consists of JDK varieties, third-party libraries, and customized, user-defined lessons of the applying.

This inheritance metadata is at all times current for each sort, and nothing a Java programmer can do will forestall it from being created and accessible at runtime. This stands in distinction to C++, which by default doesn’t protect sort metadata till runtime. As a substitute, C++ makes use of key phrases equivalent to digital and override to permit the programmer to selectively choose in to restricted runtime habits (equivalent to technique overriding).

In Java the runtime metadata is at all times current on each object, though the precise particulars of that metadata will rely upon the JVM implementation. For the remainder of this text, once I’m speaking about internals, I’ll focus on solely the HotSpot JVM, which is the implementation utilized by OpenJDK and Oracle’s JDK.

A dialogue of the Core Reflection API is, nonetheless, utterly relevant to any JVM implementation, because the API is a part of the java.* namespace and thus is a regular.

Within the HotSpot JVM, the metadata is held in an object header, which separates the metadata into type-specific and instance-specific metadata.

The instance-specific metadata is named the mark phrase, and it’s used to maintain observe of assorted essential items of data equivalent to whether or not an object’s intrinsic lock (also referred to as the monitor or synchronization lock) is being held by a thread. The metadata additionally incorporates info used throughout rubbish assortment (which is the place the identify comes from, after the idea of mark sweep rubbish assortment).
However, the shared, type-specific metadata is saved in an space referred to as metaspace and each Java object header incorporates a pointer to the metadata for the category that the thing belongs to. This is named the klass phrase of the header.

The truth that each object has a pointer to the shared class metadata signifies that throughout code execution, the JVM can at all times traverse the pointer and entry the runtime sort info of the category that the thing belongs to.

Due to this fact, one mind-set about reflection is that it’s posing the next query: “What if the execution setting uncovered the runtime metadata (which is assured to exist) and allowed Java programmers to entry and use it throughout the execution of their applications?”

A quick historical past of reflection

Within the first model of Java, the Core Reflection API didn’t exist, however there have been already strategies on java.lang.Class for accessing some metadata in regards to the runtime forms of objects, such because the identify of the category. For instance, code equivalent to the next would already work in Java 1.0:

Object o = getObject();


The following launch of Java, model 1.1, launched full reflection capabilities for the primary time. The package deal java.lang.mirror grew to become a part of the usual distribution and allowed builders to entry a full vary of reflective operations. This included things like calling strategies on objects of unknown sort that had been reflectively created.

Be aware that the Core Reflection API predates the arrival of Java’s Collections API (which appeared in Java 1.2). In consequence, varieties equivalent to Record don’t seem within the Core Reflection API; as an alternative, arrays are used. This makes some components of the API fairly extra awkward to make use of than they in any other case could be.

In fact, Java was not the primary language to offer reflective programming capabilities; others, equivalent to Smalltalk, had pioneered the reflective method. Nonetheless, Java was the language that basically introduced reflective programming into mainstream software program improvement.

The Java ecosystem warmly embraced reflection, and most of the hottest and highly effective Java frameworks depend upon these capabilities to implement their core performance.

Reflection in motion

Listed here are some duties you are able to do with reflection.

◉ Create an object reflectively.

◉ Find a way on the thing.

◉ Name the situated technique.

These duties are completed with code equivalent to the next. On this first instance, exception dealing with is ignored, for now, to enhance code readability.

Class<?> selfClazz = getClass();

Constructor<?> ctor = selfClazz.getConstructor();

Object o = ctor.newInstance();

Methodology toStr = selfClazz.getMethod(“toString”);

Object str = toStr.invoke(o);


The code begins from a category object, which you could have obtained through getClass() on this instance. In follow, this class object will also be obtained from a category loader or, extra hardly ever, as a category literal (equivalent to String.class). Reflection may be mixed with class loading to offer the potential for Java applications to work with code that was utterly unknown at compile time.

This method is the idea of plugin architectures and different very dynamic mechanisms that Java could make use of.

Regardless of the way you obtained it, upon getting a category object, it’s possible you’ll name getConstructor() on it. This technique is variadic—that’s, it might take a variable variety of arguments—to deal with the opportunity of a number of constructors for the category.

The instance above calls the default (void) constructor; this constructor takes no arguments. The result’s a constructor object from which you’ll acquire an occasion of the category, simply as if you happen to had referred to as the constructor straight.

From the constructor object, calling newInstance() causes a brand new object of the suitable sort to be created. This technique can be variadic, as a result of within the nondefault case you would want to provide the right parameters to the decision.

By the best way, the category object additionally declares a newInstance() technique that can be utilized to create new objects straight, with out creating an intermediate constructor object. (Class.newInstance() is deprecated resulting from its exception dealing with habits; this might be mentioned shortly.)

You may additionally name getMethod() to retrieve a way object from the category object. This represents the potential to name a way that belongs to the category that the strategy object was created from. After getting the Methodology object, you should use invoke() to name the strategy it represents.

Occasion strategies should be referred to as reflectively with the receiver object (the thing that the strategy is being referred to as upon) because the preliminary argument to invoke(), with any technique parameters following. Within the instance above, o is the receiver and there are not any parameters for the decision to toString().

It’s also attainable to entry fields reflectively; the API and syntax are similar to the case of strategies. The core class is java.lang.mirror.Area and the fields may be accessed through Class.getField() and different related strategies. Because of the similarity of the code, I’ll skip over this case.

Variadic strategies within the Core Reflection API

Right here’s a second and barely extra advanced instance, the place the constructors and technique calls require arguments.

public file Particular person(String firstName, String lastName, int age) {}

Class<?> selfClazz = Class.forName(“javamag.reflection.ex1.Particular person”);

Constructor<?> ctor = selfClazz.getConstructor(String.class, String.class, int.class);

Object o = ctor.newInstance(“Robert”, “Smith”, 63);

Methodology toStr = selfClazz.getMethod(“equals”, Object.class);

Object different = new Object();

Object isEqual = toStr.invoke(o, different);


Right here, the calls to getConstructor() and getMethod() are supplied with a lot of class objects. These signify the kind signature of the constructors or strategies.

The calls to Constructor.newInstance() and invoke() are, equally, supplied with objects which are the parameters to the calls. These parameters ought to, after all, match the anticipated forms of the arguments.

Along with this requirement to make use of variadic strategies to get reflection objects, the kind system and nature of Java are available to play in a number of locations throughout the reflective APIs. These considerations embrace

◉ Coping with exceptions

◉ Overloading

◉ Entry management

◉ Autoboxing and casting

Every of those considerations can complicate the writing of reflective Java code.

Coping with exceptions. Till now, I’ve ignored the necessity to deal with exceptions throughout the reflective code examples. To maneuver into the actual world, I’ll present a part of the primary instance with exception dealing with code, as follows:

strive {

    Class<?> selfClazz = getClass();

    Constructor<?> ctor = selfClazz.getConstructor();

    Object o = ctor.newInstance();

    // …

} catch (NoSuchMethodException e) {

    // May very well be thrown by getConstructor()


} catch (IllegalAccessException e) {

    // May very well be thrown by newInstance()


} catch (InstantiationException e) {

    // May very well be thrown by newInstance()


} catch (InvocationTargetException e) {

    // May very well be thrown by newInstance()



Of those 4 attainable exceptions, NoSuchMethodException may be thrown by getConstructor() if the category doesn’t have a constructor with a signature that matches the category objects handed in. The remaining exceptions may very well be thrown by Constructor.newInstance() as follows:

◉ IllegalAccessException: Entry management is enforced, and the caller doesn’t have the right entry.

◉ InstantiationException: The constructor may be accessed, however an object can’t be created, for instance, as a result of the underlying class is summary.

◉ InvocationTargetException: The execution of the constructor physique threw an exception.

The ultimate exception, InvocationTargetException, incorporates a reference to the unique exception that was thrown by the constructor physique, and this underlying exception may be retrieved by calling getTargetException(). It’s this function that explains why this method is most well-liked over the deprecated Class.newInstance() technique: The deprecated technique has no strategy to retrieve the underlying exception.

Of the remaining reflective calls on this instance, getMethod() can throw NoSuchMethodException, and invoke() can throw IllegalAccessException and InvocationTargetException.

Overloading. The existence of technique overloading within the Java language signifies that when a way (or constructor) is appeared up through getMethod(), passing the identify is just not adequate; as an alternative, the strategy signature, represented as a sequence of sophistication objects (which might be transformed to a Class[]) should even be supplied.

I contemplate utilization of Class[] (and Object[] when a way known as reflectively) as one thing of a design flaw, and it does make the API tougher to work with. That is actually simply an unlucky accident of historical past as a result of the Core Reflection API was added earlier than the Collections libraries existed, so the API should be maintained in its authentic kind for backwards compatibility causes.

Entry management. One other downside pertains to entry management: The API supplies two completely different strategies, getMethod() and getDeclaredMethod(), to entry strategies reflectively.

The primary of those two potentialities, getMethod(), is used for wanting up public strategies. Against this, getDeclaredMethod() can be utilized to seek out any technique declared on a category, even personal strategies.

By default, reflective code nonetheless respects the entry management semantics of the Java language, however it’s attainable to override these semantics as a result of the API supplies the setAccessible() technique, which may be referred to as on strategies, constructors, and fields. As soon as this technique has been referred to as on an accessible object, the entry management modifiers might be ignored.

For my part, the setAccessible() technique is basically harmful, as a result of it permits programmers to selectively flip off components of the entry management system when they’re working reflectively.

It represents a compromise within the reflective subsystem: Generally there may be simply no different strategy to get the entry that’s required, however when it’s overused it will probably trigger all types of safety and security points.

It may be argued that the compromise is just not too unhealthy, as a result of by the point you could have obtained a Methodology object or one other accessible object, the corresponding class has undoubtedly already been loaded, which signifies that the bytecode of the category has already handed verification by the point you make your reflective name.

However, utilizing setAccessible() to realize entry to strategies which are in any other case inaccessible represents a violation of encapsulation and a utilization that the unique code creator didn’t intend.

Autoboxing and casting. The Core Reflection API incorporates a number of variadic strategies for lookup and invocation. For instance, Constructor.newInstance() and invoke() each take Object… as their variadic parameter, and this instantly raises the query of what to do about primitive values while you name code reflectively.

First off, the lookup strategies (equivalent to getConstructor() and getMethod()) take Class<?> objects as parameters, so you’ll be able to merely move the category literals similar to primitive varieties, equivalent to int.class.

It’s price noting right here that the majority class literals are singletons. For instance, String.class is the one occasion of the kind Class<String>. Nonetheless, there are two cases of Class which are parameterized by every wrapper sort (equivalent to Integer): one for the wrapper sort and one for the corresponding primitive.

That’s, the next code prints false:

Class<Integer> intClz = int.class;

Class<Integer> integerClz = Integer.class;

System.out.println(intClz == integerClz);

The second instance above supplied a clue as to how this is applicable to reflective code in follow.

Class<?> selfClazz = Class.forName(“javamag.reflection.ex1.Particular person”);

Constructor<?> ctor = selfClazz.getConstructor(String.class, String.class, int.class);

Object o = ctor.newInstance(“Robert”, “Smith”, 63);

This code seems up the first constructor for the file sort after which instantiates an object by passing a primitive worth because the second argument. This argument will should be boxed to match the signature of Constructor.newInstance() (which takes Object[]), so the decision is de facto the next:

Object o = ctor.newInstance(“Robert”, “Smith”, Integer.valueOf(63));

The ultimate argument will then be unboxed within the reflection implementation code previous to the precise name to the constructor being made. This method is pretty easy when you get used to it, however it’s a little clumsy, and it does require further pointless boxing operations.

The return worth of reflective calls additionally requires cautious dealing with; the return sort of invoke() is Object, so any return worth must be downcast to an anticipated, extra helpful, sort.

This forged operation, after all, could fail with a ClassCastException (which is a runtime exception).




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments