mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
moved 2 files to ensure history is saved
This commit is contained in:
44
forge-core/src/main/java/forge/util/ReflectionUtil.java
Normal file
44
forge-core/src/main/java/forge/util/ReflectionUtil.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package forge.util;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
*/
|
||||
public class ReflectionUtil {
|
||||
|
||||
public static <T> T makeDefaultInstanceOf(Class<? extends T> cls) {
|
||||
if ( null == cls )
|
||||
throw new IllegalArgumentException("Class<? extends T> cls must not be null");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Constructor<? extends T>[] cc = (Constructor<? extends T>[]) cls.getConstructors();
|
||||
for (Constructor<? extends T> c : cc) {
|
||||
Class<?>[] pp = c.getParameterTypes();
|
||||
if (pp.length == 0) {
|
||||
try {
|
||||
T res = c.newInstance();
|
||||
return res;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("No default constructor found in class " + cls.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast object to a given type if possible, returning null if not possible
|
||||
* @param obj
|
||||
* @param type
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T safeCast(Object obj, Class<T> type) {
|
||||
if (type.isInstance(obj)) {
|
||||
return (T) obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user