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

org.d2ab.util.Classes Maven / Gradle / Ivy

The newest version!
package org.d2ab.util;

import java.lang.reflect.Field;
import java.util.Optional;

/**
 * Created by Daniel on 2017-02-19.
 */
public abstract class Classes {
	Classes() {
	}

	@SuppressWarnings("unchecked")
	public static > Optional classByName(String className) {
		try {
			return Optional.of((C) Class.forName(className));
		} catch (ClassNotFoundException | RuntimeException e) {
			return Optional.empty();
		}
	}

	public static Optional accessibleField(Class cls, String fieldName) {
		try {
			Field f = cls.getDeclaredField(fieldName);
			f.setAccessible(true);
			return Optional.of(f);
		} catch (NoSuchFieldException | RuntimeException e) {
			return Optional.empty();
		}
	}

	@SuppressWarnings("unchecked")
	public static  Optional getValue(Field field, Object object) {
		try {
			return Optional.of((T) field.get(object));
		} catch (IllegalAccessException | RuntimeException e) {
			return Optional.empty();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy