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
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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.
|
|
|
|
*
|
2020-08-25 01:31:47 +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 General Public License for more details.
|
2011-12-25 06:34:53 +00:00
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2011-12-25 06:34:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-07-23 15:48:51 +00:00
|
|
|
//FAWE start
|
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) {
|
2020-10-05 17:41:41 +00:00
|
|
|
if (from instanceof Class) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return getField((Class) from, null, name);
|
2020-10-05 17:41:41 +00:00
|
|
|
} else {
|
2018-08-12 14:03:07 +00:00
|
|
|
return getField(from.getClass(), from, name);
|
2020-10-05 17:41:41 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2021-07-23 15:48:51 +00:00
|
|
|
//FAWE end
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
@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
|
|
|
}
|