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

panda.lang.reflect.FieldInjector Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.lang.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

import panda.lang.Exceptions;
import panda.lang.Injector;
import panda.lang.Objects;

public class FieldInjector implements Injector {
	private Field field;

	/**
	 * @param field the field
	 */
	public FieldInjector(Field field) {
		this.field = field;
		
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}
	}

	@Override
	public Type type(Object obj) {
		return Fields.getFieldType(field);
	}
	
	@Override
	public void inject(Object obj, Object value) {
		try {
			field.set(obj, value);
		}
		catch (Exception e) {
			throw Exceptions.wrapThrow(e);
		}
	}

	@Override
	public String toString() {
		return Objects.toStringBuilder()
				.append("field", field)
				.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy