非常简单的应用,直接上代码。
0. 获取域的代码
/**
* @author jeremysong
*/
public class AccessChecker {
public static boolean check(Object obj, String tag) {
if (obj == null || tag == null || tag.isEmpty()) {
return false;
}
try {
Class clz = obj.getClass();
// Field field = clz.getField("tags");
Field field = clz.getDeclaredField("tags");
field.setAccessible(true);
String[] tags = (String[]) field.get(obj);
for (String t : tags) {
if (tag.equals(t)) return true;
}
} catch (NoSuchFieldException e) {
System.out.println("指定对象无tags属性");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("获取不到指定对象的tags属性");
e.printStackTrace();
}
r