org.d2ab.util.Classes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sequence Show documentation
Show all versions of sequence Show documentation
A lightweight alternative to Java 8 sequential Stream
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();
}
}
}