All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.commons.ReflectionUtils Maven / Gradle / Ivy

Go to download

Various utility classes. Except for very rare exceptions (annotation-based validation) this will not require any dependencies beyond the JRE

The newest version!
/*
 * Copyright (c) 2014 Kloudtek Ltd
 */

package com.aeontronix.commons;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Various reflection related utility functions
 */
public class ReflectionUtils {
    public static String toString(Method method) {
        return "Method " + method.getDeclaringClass().getName() + "#" + method.getName();
    }

    public static String toString(Field field) {
        return field.getDeclaringClass().getName() + "#" + field.getName();
    }

    public static Object invoke(Method method, Object obj) throws Throwable {
        try {
            return method.invoke(obj);
        } catch (IllegalAccessException e) {
            throw new IllegalAccessException("Method " + toString(method) + " cannot be invoked: " + e.getMessage());
        } catch (InvocationTargetException e) {
            throw e.getCause() != null ? e.getCause() : e;
        }
    }

    public static void set(Object obj, String name, Object value) {
        try {
            findField(obj, name).set(obj, value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void set(Class clazz, String name, Object value) {
        try {
            findField(clazz, name).set(null, value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object get(Object obj, String name) {
        try {
            return findField(obj, name).get(obj);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object get(Class clazz, String name) {
        try {
            return findField(clazz, name).get(null);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Field findField(Object obj, String name) {
        return findField(obj.getClass(), name);
    }

    public static Field findField(@NotNull Class cl, String name) {
        Class c = cl;
        while (c != null) {
            try {
                Field field = c.getDeclaredField(name);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                return field;
            } catch (NoSuchFieldException e) {
                c = c.getSuperclass();
            }
        }
        throw new IllegalArgumentException("Field " + name + " not found in " + cl.getName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy