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

com.github.bloodshura.x.utilities.reflect.EasyField Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 *
 * https://www.github.com/BloodShura
 */

package com.github.bloodshura.x.utilities.reflect;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Field;

public class EasyField {
	private final Field field;

	public EasyField(@Nonnull Field field) {
		this.field = field;
	}

	@Nonnull
	public Field get() {
		return field;
	}

	@Nonnull
	public String getName() {
		return get().getName();
	}

	@Nullable
	public E getValue(@Nullable Object instance) {
		try {
			get().setAccessible(true);

			return (E) get().get(instance);
		}
		catch (IllegalAccessException | IllegalArgumentException | NullPointerException | SecurityException exception) {
			EasyReflection.getExceptionProcessor().process(exception);
		}
		finally {
			try {
				get().setAccessible(false);
			}
			catch (SecurityException exception) {
				EasyReflection.getExceptionProcessor().process(exception);
			}
		}

		return null;
	}

	public void setValue(@Nullable Object instance, @Nullable E value) {
		try {
			get().setAccessible(true);
			get().set(instance, value);
		}
		catch (IllegalAccessException | IllegalArgumentException | NullPointerException | SecurityException exception) {
			EasyReflection.getExceptionProcessor().process(exception);
		}
		finally {
			try {
				get().setAccessible(false);
			}
			catch (SecurityException exception) {
				EasyReflection.getExceptionProcessor().process(exception);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy