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

com.llamalad7.mixinextras.utils.InternalField Maven / Gradle / Ivy

Go to download

Companion library to Mixin with lots of features to improve the compatibility and concision of your mixins!

There is a newer version: 0.5.0-beta.4
Show newest version
package com.llamalad7.mixinextras.utils;

import java.lang.reflect.Field;

@SuppressWarnings("unchecked")
interface InternalField {
    T get(O owner);

    void set(O owner, T newValue);

    static  InternalField of(Class clazz, String name) {
        Field impl;
        try {
            impl = clazz.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(
                    String.format(
                            "Failed to find field %s::%s! Please report to LlamaLad7!",
                            clazz, name
                    ), e
            );
        }
        impl.setAccessible(true);
        return new InternalField() {
            @Override
            public T get(O owner) {
                try {
                    return (T) impl.get(owner);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                            String.format(
                                    "Failed to get %s::%s on %s! Please report to LlamaLad7!",
                                    clazz, name, owner
                            ), e
                    );
                }
            }

            @Override
            public void set(O owner, T newValue) {
                try {
                    impl.set(owner, newValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                            String.format(
                                    "Failed to set %s::%s to %s on %s! Please report to LlamaLad7!",
                                    clazz, name, newValue, owner
                            ), e
                    );
                }
            }
        };
    }

    static  InternalField of(String clazz, String name) {
        try {
            return of(Class.forName(clazz), name);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(String.format("Failed to find class %s! Please report to LlamaLad7!", clazz), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy