2011-12-25 06:34:53 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2011-12-25 06:34:53 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2011-12-25 06:34:53 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2011-12-25 06:34:53 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2011-12-25 06:34:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.util;
|
|
|
|
|
2019-06-06 22:39:51 +00:00
|
|
|
import java.lang.reflect.Field;
|
2011-12-25 06:34:53 +00:00
|
|
|
|
2014-04-04 21:35:12 +00:00
|
|
|
public final class ReflectionUtil {
|
|
|
|
|
2018-12-20 16:00:27 +00:00
|
|
|
private ReflectionUtil() {
|
|
|
|
}
|
2014-04-04 21:35:12 +00:00
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2011-12-25 06:34:53 +00:00
|
|
|
public static <T> T getField(Object from, String name) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (from instanceof Class)
|
|
|
|
return getField((Class) from, null, name);
|
|
|
|
else
|
|
|
|
return getField(from.getClass(), from, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2019-06-25 17:07:47 +00:00
|
|
|
public static <T> T getField(Class checkClass, Object from, String name) {
|
2011-12-25 06:34:53 +00:00
|
|
|
do {
|
|
|
|
try {
|
|
|
|
Field field = checkClass.getDeclaredField(name);
|
|
|
|
field.setAccessible(true);
|
2019-06-25 17:07:47 +00:00
|
|
|
return (T) field.get(from);
|
2018-06-16 06:36:55 +00:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ignored) {
|
2011-12-25 06:34:53 +00:00
|
|
|
}
|
|
|
|
} while (checkClass.getSuperclass() != Object.class && ((checkClass = checkClass.getSuperclass()) != null));
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-20 16:00:27 +00:00
|
|
|
|
2011-12-25 06:34:53 +00:00
|
|
|
}
|