Home / Class/ ReflectionUtil Class — netty Architecture

ReflectionUtil Class — netty Architecture

Architecture documentation for the ReflectionUtil class in ReflectionUtil.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  256133bf_a518_6230_b946_c6d646a25eb5["ReflectionUtil"]
  71d1e3d8_b3dc_8824_6aba_a6b82aac563a["ReflectionUtil.java"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|defined in| 71d1e3d8_b3dc_8824_6aba_a6b82aac563a
  df90c1eb_13a6_e8b2_49de_d92ac2d5f7a6["ReflectionUtil()"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|method| df90c1eb_13a6_e8b2_49de_d92ac2d5f7a6
  9a82bf1e_e7f1_ec4a_d974_807a5c397183["Throwable()"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|method| 9a82bf1e_e7f1_ec4a_d974_807a5c397183
  e08bd0e0_189a_55b5_4567_ed4e30b6657e["RuntimeException()"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|method| e08bd0e0_189a_55b5_4567_ed4e30b6657e
  d3ba72cb_b3af_6669_8eb0_62d09acd9ab8["fail()"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|method| d3ba72cb_b3af_6669_8eb0_62d09acd9ab8
  a5bea1aa_8419_848e_9649_5391157d2623["resolveTypeParameter()"]
  256133bf_a518_6230_b946_c6d646a25eb5 -->|method| a5bea1aa_8419_848e_9649_5391157d2623

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/ReflectionUtil.java lines 25–139

public final class ReflectionUtil {

    private ReflectionUtil() { }

    /**
     * Try to call {@link AccessibleObject#setAccessible(boolean)} but will catch any {@link SecurityException} and
     * {@link java.lang.reflect.InaccessibleObjectException} and return it.
     * The caller must check if it returns {@code null} and if not handle the returned exception.
     */
    public static Throwable trySetAccessible(AccessibleObject object, boolean checkAccessible) {
        if (checkAccessible && !PlatformDependent0.isExplicitTryReflectionSetAccessible()) {
            return new UnsupportedOperationException("Reflective setAccessible(true) disabled");
        }
        try {
            object.setAccessible(true);
            return null;
        } catch (SecurityException e) {
            return e;
        } catch (RuntimeException e) {
            return handleInaccessibleObjectException(e);
        }
    }

    private static RuntimeException handleInaccessibleObjectException(RuntimeException e) {
        // JDK 9 can throw an inaccessible object exception here; since Netty compiles
        // against JDK 7 and this exception was only added in JDK 9, we have to weakly
        // check the type
        if ("java.lang.reflect.InaccessibleObjectException".equals(e.getClass().getName())) {
            return e;
        }
        throw e;
    }

    private static Class<?> fail(Class<?> type, String typeParamName) {
        throw new IllegalStateException(
                "cannot determine the type of the type parameter '" + typeParamName + "': " + type);
    }

    /**
     * Resolve a type parameter of a class that is a subclass of the given parametrized superclass.
     * @param object The object to resolve the type parameter for
     * @param parametrizedSuperclass The parametrized superclass
     * @param typeParamName The name of the type parameter to resolve
     * @return The resolved type parameter
     * @throws IllegalStateException if the type parameter could not be resolved
     * */
    public static Class<?> resolveTypeParameter(final Object object,
                                                Class<?> parametrizedSuperclass,
                                                String typeParamName) {
        final Class<?> thisClass = object.getClass();
        Class<?> currentClass = thisClass;
        for (;;) {
            if (currentClass.getSuperclass() == parametrizedSuperclass) {
                int typeParamIndex = -1;
                TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
                for (int i = 0; i < typeParams.length; i ++) {
                    if (typeParamName.equals(typeParams[i].getName())) {
                        typeParamIndex = i;
                        break;
                    }
                }

                if (typeParamIndex < 0) {
                    throw new IllegalStateException(
                            "unknown type parameter '" + typeParamName + "': " + parametrizedSuperclass);
                }

                Type genericSuperType = currentClass.getGenericSuperclass();
                if (!(genericSuperType instanceof ParameterizedType)) {
                    return Object.class;
                }

                Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();

                Type actualTypeParam = actualTypeParams[typeParamIndex];
                if (actualTypeParam instanceof ParameterizedType) {
                    actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
                }
                if (actualTypeParam instanceof Class) {
                    return (Class<?>) actualTypeParam;
                }

Frequently Asked Questions

What is the ReflectionUtil class?
ReflectionUtil is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/ReflectionUtil.java.
Where is ReflectionUtil defined?
ReflectionUtil is defined in common/src/main/java/io/netty/util/internal/ReflectionUtil.java at line 25.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free