use of java.lang.reflect.Field in project stargate-core by tuplejump.
the class SearchSupport method getPrivateProperty.
private Object getPrivateProperty(Object instance, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(instance);
}
use of java.lang.reflect.Field in project bazel by bazelbuild.
the class MoreAsserts method isRetained.
private static boolean isRetained(Predicate<Object> predicate, Object start) {
Map<Object, Object> visited = Maps.newIdentityHashMap();
visited.put(start, start);
Queue<Object> toScan = new ArrayDeque<>();
toScan.add(start);
while (!toScan.isEmpty()) {
Object current = toScan.poll();
if (current.getClass().isArray()) {
if (current.getClass().getComponentType().isPrimitive()) {
continue;
}
for (Object ref : (Object[]) current) {
if (ref != null) {
if (predicate.apply(ref)) {
return true;
}
if (visited.put(ref, ref) == null) {
toScan.add(ref);
}
}
}
} else {
// iterate *all* fields (getFields() returns only accessible ones)
for (Class<?> clazz = current.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
for (Field f : clazz.getDeclaredFields()) {
if (f.getType().isPrimitive() || ALL_STRONG_REFS.apply(f)) {
continue;
}
f.setAccessible(true);
try {
Object ref = f.get(current);
if (ref != null) {
if (predicate.apply(ref)) {
return true;
}
if (visited.put(ref, ref) == null) {
toScan.add(ref);
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Error when scanning the heap", e);
}
}
}
}
}
return false;
}
use of java.lang.reflect.Field in project CoreNLP by stanfordnlp.
the class StatisticalCorefTrainer method fieldValues.
public static String fieldValues(Object o) {
String s = "";
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
s += field.getName() + " = " + field.get(o) + "\n";
} catch (Exception e) {
throw new RuntimeException("Error getting field value for " + field.getName(), e);
}
}
return s;
}
use of java.lang.reflect.Field in project CoreNLP by stanfordnlp.
the class Units method registerUnit.
public static void registerUnit(Env env, Class clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
boolean isUnit = Unit.class.isAssignableFrom(field.getType());
if (isStatic && isUnit) {
try {
Unit unit = ErasureUtils.uncheckedCast(field.get(null));
registerUnit(env, unit);
} catch (IllegalAccessException ex) {
}
}
}
}
use of java.lang.reflect.Field in project CoreNLP by stanfordnlp.
the class GetPatternsFromDataMultiClass method getAllOptions.
public Map<String, String> getAllOptions() {
Map<String, String> values = new HashMap<>();
props.forEach((x, y) -> values.put(x.toString(), y.toString()));
values.putAll(constVars.getAllOptions());
//StringBuilder sb = new StringBuilder();
Class<?> thisClass;
try {
thisClass = Class.forName(this.getClass().getName());
Field[] aClassFields = thisClass.getDeclaredFields();
//sb.append(this.getClass().getSimpleName() + " [ ");
for (Field f : aClassFields) {
if (f.getGenericType().getClass().isPrimitive() || Arrays.binarySearch(printOptionClass, f.getType().getClass()) >= 0) {
String fName = f.getName();
Object fvalue = f.get(this);
values.put(fName, fvalue == null ? "null" : fvalue.toString());
//sb.append("(" + f.getType() + ") " + fName + " = " + f.get(this) + ", ");
}
}
} catch (Exception e) {
log.warn(e);
}
return values;
}
Aggregations