Search in sources :

Example 56 with Field

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);
}
Also used : Field(java.lang.reflect.Field)

Example 57 with Field

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;
}
Also used : Field(java.lang.reflect.Field) ArrayDeque(java.util.ArrayDeque)

Example 58 with Field

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;
}
Also used : Field(java.lang.reflect.Field)

Example 59 with Field

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) {
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field)

Example 60 with Field

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;
}
Also used : Field(java.lang.reflect.Field) SQLException(java.sql.SQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Field (java.lang.reflect.Field)5144 Method (java.lang.reflect.Method)613 Test (org.junit.Test)538 ArrayList (java.util.ArrayList)490 IOException (java.io.IOException)318 HashMap (java.util.HashMap)318 Map (java.util.Map)296 InvocationTargetException (java.lang.reflect.InvocationTargetException)176 List (java.util.List)171 Pattern (java.util.regex.Pattern)122 Matcher (java.util.regex.Matcher)117 HashSet (java.util.HashSet)109 File (java.io.File)107 Annotation (java.lang.annotation.Annotation)98 Support_Field (tests.support.Support_Field)82 Collection (java.util.Collection)81 Test (org.testng.annotations.Test)68 Type (java.lang.reflect.Type)67 SienaException (siena.SienaException)65 Before (org.junit.Before)62