Skip to content

io microsphere reflect ReflectionUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

ReflectionUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.reflect | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/reflect/ReflectionUtils.java

Overview

Reflection Utility class , generic methods are defined from FieldUtils , MethodUtils , ConstructorUtils

Declaration

public abstract class ReflectionUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isSupportedSunReflectReflection

if (ReflectionUtils.isSupportedSunReflectReflection()) {
    System.out.println("sun.reflect.Reflection is supported.");
} else {
    System.out.println("sun.reflect.Reflection is NOT supported.");
}

getCallerClassName

public class Example {
    public void exampleMethod() {
        String callerClassName = ReflectionUtils.getCallerClassName();
        System.out.println("Caller class: " + callerClassName);
    }
}

getCallerClass

public class Example {
    public void testMethod() {
        Class<?> callerClass = ReflectionUtils.getCallerClass();
        System.out.println("Caller class: " + callerClass.getName());
    }
}

toList

String[] stringArray = {"apple", "banana", "cherry"};
List<String> stringList = ReflectionUtils.toList(stringArray);
System.out.println(stringList);  // Output: [apple, banana, cherry]

Integer[][] nestedArray = {{1, 2}, {3, 4}};
List<List<Integer>> nestedList = ReflectionUtils.toList(nestedArray);
System.out.println(nestedList);  // Output: [[1, 2], [3, 4]]

isInaccessibleObjectException

class Person {
    private String name;
    private int age;
    private Address address; // Assume Address is another POJO class

    // constructor, getters, setters...
}

class Address {
    private String city;
    private String street;

    // constructor, getters, setters...
}

Person person = new Person("John", 30, new Address("New York", "5th Avenue"));
Map<String, Object> result = ReflectionUtils.readFieldsAsMap(person);

// Sample output:
// {
//   "name": "John",
//   "age": 30,
//   "address": {
//     "city": "New York",
//     "street": "5th Avenue"
//   }
// }
try {
    Field field = MyClass.class.getDeclaredField("myField");
    field.setAccessible(true); // This might throw InaccessibleObjectException
} catch (Throwable t) {
    if (ReflectionUtils.isInaccessibleObjectException(t)) {
        System.err.println("Caught InaccessibleObjectException: " + t.getMessage());
    } else {
        throw new RuntimeException("Unexpected error", t);
    }
}

isInaccessibleObjectException

Class<?> exceptionClass = SomeException.class;
if (ReflectionUtils.isInaccessibleObjectException(exceptionClass)) {
    System.err.println("The class represents InaccessibleObjectException");
} else {
    System.out.println("The class does not represent InaccessibleObjectException");
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.reflect.ReflectionUtils;

API Reference

Public Methods

Method Description
isSupportedSunReflectReflection Current Type
getCallerClassName Retrieves the fully qualified name of the class that called the method invoking this method.
getCallerClass Gets the Class of the method caller.
toList Get caller class In SUN HotSpot JVM
isInaccessibleObjectException Reads all non-static fields of the given object and returns them as a map.
isInaccessibleObjectException Checks whether the specified Class represents

Method Details

isSupportedSunReflectReflection

public static boolean isSupportedSunReflectReflection()

Current Type / private static final Class TYPE = ReflectionUtils.class;

private static final Logger logger = getLogger(TYPE);

/** Sun JDK implementation class: full name of sun.reflect.Reflection / public static final String SUN_REFLECT_REFLECTION_CLASS_NAME = "sun.reflect.Reflection";

/** The Class of sun.reflect.Reflection /

getCallerClassName

public static String getCallerClassName()

Retrieves the fully qualified name of the class that called the method invoking this method.

This method attempts to use the internal Sun JDK class sun.reflect.Reflection for high-performance caller class detection if available. If not supported (e.g., non-Sun/HotSpot JVM), it falls back to using the StackTraceElement approach.

Example Usage

`public class Example {
    public void exampleMethod() {
        String callerClassName = ReflectionUtils.getCallerClassName();
        System.out.println("Caller class: " + callerClassName);
    `
}
}

Performance Consideration

On Sun/HotSpot JVMs, this method is highly efficient as it leverages internal JVM mechanisms. On other JVMs, a stack trace-based approach is used, which may be less performant but ensures compatibility.

getCallerClass

public static Class<?> getCallerClass()

Gets the Class of the method caller.

This method attempts to retrieve the calling class using the sun.reflect.Reflection class if supported by the current JVM. If not supported (e.g., non-Sun/HotSpot JVM), it falls back to using the StackTraceElement approach.

Example Usage

`public class Example {
    public void testMethod() {
        Class callerClass = ReflectionUtils.getCallerClass();
        System.out.println("Caller class: " + callerClass.getName());
    `
}
}

isInaccessibleObjectException

public static boolean isInaccessibleObjectException(Throwable failure)

Reads all non-static fields of the given object and returns them as a map.

This method recursively processes nested objects, converting them into maps as well, provided they are not primitive or simple types (e.g., String, Number).

Example Usage

`class Person {
    private String name;
    private int age;
    private Address address; // Assume Address is another POJO class

    // constructor, getters, setters...
`

class Address {
    private String city;
    private String street;

    // constructor, getters, setters...
}

Person person = new Person("John", 30, new Address("New York", "5th Avenue"));
Map result = ReflectionUtils.readFieldsAsMap(person);

// Sample output:
// {
//   "name": "John",
//   "age": 30,
//   "address": {
//     "city": "New York",
//     "street": "5th Avenue"
//   }
// }
}

isInaccessibleObjectException

public static boolean isInaccessibleObjectException(Class<?> throwableClass)

Checks whether the specified Class represents java.lang.reflect.InaccessibleObjectException.

This method is useful when dealing with reflection operations that may fail due to module system restrictions introduced in JDK 9+. It avoids direct dependency on the presence of the class, which may not be available in earlier JDK versions.

Example Usage

`Class exceptionClass = SomeException.class;
if (ReflectionUtils.isInaccessibleObjectException(exceptionClass)) {
    System.err.println("The class represents InaccessibleObjectException");
` else {
    System.out.println("The class does not represent InaccessibleObjectException");
}
}

See Also

  • Method
  • Field
  • Constructor
  • Array
  • MethodUtils
  • FieldUtils
  • ConstructorUtils

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally