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

com.g2forge.alexandria.reflection.object.implementations.JavaFieldReflection Maven / Gradle / Ivy

Go to download

Generic type safe reflection library, which adds significant usability over the standard Java runtime.

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.reflection.object.implementations;

import java.lang.reflect.Field;

import com.g2forge.alexandria.generic.type.java.member.IJavaFieldType;
import com.g2forge.alexandria.generic.type.java.type.implementations.ReflectionException;
import com.g2forge.alexandria.java.adt.tuple.ITuple1GS;
import com.g2forge.alexandria.reflection.object.AJavaMemberReflection;
import com.g2forge.alexandria.reflection.object.IJavaFieldReflection;
import com.g2forge.alexandria.reflection.object.IJavaTypeReflection;
import com.g2forge.alexandria.reflection.object.HReflection;

public class JavaFieldReflection extends AJavaMemberReflectionimplements IJavaFieldReflection {
	public JavaFieldReflection(IJavaFieldType type) {
		super(type);
	}

	@Override
	public ITuple1GS getAccessor(final T object) {
		final Field field = getType().getJavaMember();
		field.setAccessible(true);
		return new ITuple1GS() {
			@Override
			public F get0() {
				try {
					@SuppressWarnings("unchecked")
					final F retVal = (F) field.get(object);
					return retVal;
				} catch (IllegalArgumentException | IllegalAccessException exception) {
					throw new ReflectionException(exception);
				}
			}

			@Override
			public ITuple1GS set0(F value) {
				try {
					field.set(object, value);
				} catch (IllegalArgumentException | IllegalAccessException exception) {
					throw new ReflectionException(exception);
				}
				return this;
			}
		};
	}

	@Override
	public IJavaTypeReflection getFieldType() {
		return HReflection.toReflection(getType().getJavaMember().getGenericType());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy