Search in sources :

Example 31 with Field

use of java.lang.reflect.Field in project flink by apache.

the class TaskTest method setState.

private void setState(Task task, ExecutionState state) {
    try {
        Field f = Task.class.getDeclaredField("executionState");
        f.setAccessible(true);
        f.set(task, state);
    } catch (Exception e) {
        throw new RuntimeException("Modifying the task state failed", e);
    }
}
Also used : Field(java.lang.reflect.Field) TimeoutException(java.util.concurrent.TimeoutException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) PartitionProducerDisposedException(org.apache.flink.runtime.jobmanager.PartitionProducerDisposedException) IOException(java.io.IOException)

Example 32 with Field

use of java.lang.reflect.Field in project flink by apache.

the class TestJvmProcess method getProcessId.

/**
	 * Gets the process ID, if possible. This method currently only work on UNIX-based
	 * operating systems. On others, it returns {@code -1}.
	 * 
	 * @return The process ID, or -1, if the ID cannot be determined.
	 */
public long getProcessId() {
    checkState(process != null, "process not started");
    try {
        Class<? extends Process> clazz = process.getClass();
        if (clazz.getName().equals("java.lang.UNIXProcess")) {
            Field pidField = clazz.getDeclaredField("pid");
            pidField.setAccessible(true);
            return pidField.getLong(process);
        } else {
            return -1;
        }
    } catch (Throwable ignored) {
        return -1;
    }
}
Also used : Field(java.lang.reflect.Field)

Example 33 with Field

use of java.lang.reflect.Field in project hadoop by apache.

the class TestConfigurationFieldsBase method extractMemberVariablesFromConfigurationFields.

/**
   * Utility function to extract &quot;public static final&quot; member
   * variables from a Configuration type class.
   *
   * @param fields The class member variables
   * @return HashMap containing <StringValue,MemberVariableName> entries
   */
private HashMap<String, String> extractMemberVariablesFromConfigurationFields(Field[] fields) {
    // Sanity Check
    if (fields == null)
        return null;
    HashMap<String, String> retVal = new HashMap<String, String>();
    // Setup regexp for valid properties
    String propRegex = "^[A-Za-z][A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)+$";
    Pattern p = Pattern.compile(propRegex);
    // Iterate through class member variables
    int totalFields = 0;
    String value;
    for (Field f : fields) {
        if (configDebug) {
            System.out.println("Field: " + f);
        }
        // Filter out anything that isn't "public static final"
        if (!Modifier.isStatic(f.getModifiers()) || !Modifier.isPublic(f.getModifiers()) || !Modifier.isFinal(f.getModifiers())) {
            continue;
        }
        // default values
        if (!f.getType().getName().equals("java.lang.String")) {
            continue;
        }
        // filter out default-value fields
        if (isFieldADefaultValue(f)) {
            continue;
        }
        // Convert found member into String
        try {
            value = (String) f.get(null);
        } catch (IllegalAccessException iaException) {
            continue;
        }
        if (configDebug) {
            System.out.println("  Value: " + value);
        }
        //               or file properties (ending in .xml)
        if (value.endsWith(".xml") || value.endsWith(".") || value.endsWith("-"))
            continue;
        // Ignore known configuration props
        if (configurationPropsToSkipCompare != null) {
            if (configurationPropsToSkipCompare.contains(value)) {
                continue;
            }
        }
        // Ignore known configuration prefixes
        boolean skipPrefix = false;
        if (configurationPrefixToSkipCompare != null) {
            for (String cfgPrefix : configurationPrefixToSkipCompare) {
                if (value.startsWith(cfgPrefix)) {
                    skipPrefix = true;
                    break;
                }
            }
        }
        if (skipPrefix) {
            continue;
        }
        // Positive Filter: Look only for property values.  Expect it to look
        //                  something like: blah.blah2(.blah3.blah4...)
        Matcher m = p.matcher(value);
        if (!m.find()) {
            if (configDebug) {
                System.out.println("  Passes Regex: false");
            }
            continue;
        }
        if (configDebug) {
            System.out.println("  Passes Regex: true");
        }
        // Save member variable/value as hash
        if (!retVal.containsKey(value)) {
            retVal.put(value, f.getName());
        } else {
            if (configDebug) {
                System.out.println("ERROR: Already found key for property " + value);
            }
        }
    }
    return retVal;
}
Also used : Pattern(java.util.regex.Pattern) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher)

Example 34 with Field

use of java.lang.reflect.Field in project flink by apache.

the class DataSourceTaskTest method testDataSourceTask.

@Test
public void testDataSourceTask() {
    int keyCnt = 100;
    int valCnt = 20;
    this.outList = new ArrayList<Record>();
    try {
        InputFilePreparator.prepareInputFile(new UniformRecordGenerator(keyCnt, valCnt, false), this.tempTestPath, true);
    } catch (IOException e1) {
        Assert.fail("Unable to set-up test input file");
    }
    super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE);
    super.addOutput(this.outList);
    DataSourceTask<Record> testTask = new DataSourceTask<>();
    super.registerFileInputTask(testTask, MockInputFormat.class, new File(tempTestPath).toURI().toString(), "\n");
    try {
        testTask.invoke();
    } catch (Exception e) {
        System.err.println(e);
        Assert.fail("Invoke method caused exception.");
    }
    try {
        Field formatField = DataSourceTask.class.getDeclaredField("format");
        formatField.setAccessible(true);
        MockInputFormat inputFormat = (MockInputFormat) formatField.get(testTask);
        Assert.assertTrue("Invalid status of the input format. Expected for opened: true, Actual: " + inputFormat.opened, inputFormat.opened);
        Assert.assertTrue("Invalid status of the input format. Expected for closed: true, Actual: " + inputFormat.closed, inputFormat.closed);
    } catch (Exception e) {
        System.err.println(e);
        Assert.fail("Reflection error while trying to validate inputFormat status.");
    }
    Assert.assertTrue("Invalid output size. Expected: " + (keyCnt * valCnt) + " Actual: " + this.outList.size(), this.outList.size() == keyCnt * valCnt);
    HashMap<Integer, HashSet<Integer>> keyValueCountMap = new HashMap<>(keyCnt);
    for (Record kvp : this.outList) {
        int key = kvp.getField(0, IntValue.class).getValue();
        int val = kvp.getField(1, IntValue.class).getValue();
        if (!keyValueCountMap.containsKey(key)) {
            keyValueCountMap.put(key, new HashSet<Integer>());
        }
        keyValueCountMap.get(key).add(val);
    }
    Assert.assertTrue("Invalid key count in out file. Expected: " + keyCnt + " Actual: " + keyValueCountMap.keySet().size(), keyValueCountMap.keySet().size() == keyCnt);
    for (Integer mapKey : keyValueCountMap.keySet()) {
        Assert.assertTrue("Invalid value count for key: " + mapKey + ". Expected: " + valCnt + " Actual: " + keyValueCountMap.get(mapKey).size(), keyValueCountMap.get(mapKey).size() == valCnt);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) IOException(java.io.IOException) Field(java.lang.reflect.Field) Record(org.apache.flink.types.Record) UniformRecordGenerator(org.apache.flink.runtime.operators.testutils.UniformRecordGenerator) File(java.io.File) IntValue(org.apache.flink.types.IntValue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Field

use of java.lang.reflect.Field in project groovy by apache.

the class DefaultGroovyMethods method dump.

/**
     * Generates a detailed dump string of an object showing its class,
     * hashCode and fields.
     *
     * @param self an object
     * @return the dump representation
     * @since 1.0
     */
public static String dump(Object self) {
    if (self == null) {
        return "null";
    }
    StringBuilder buffer = new StringBuilder("<");
    Class klass = self.getClass();
    buffer.append(klass.getName());
    buffer.append("@");
    buffer.append(Integer.toHexString(self.hashCode()));
    boolean groovyObject = self instanceof GroovyObject;
    /*jes this may be rewritten to use the new getProperties() stuff
         * but the original pulls out private variables, whereas getProperties()
         * does not. What's the real use of dump() here?
         */
    while (klass != null) {
        for (final Field field : klass.getDeclaredFields()) {
            if ((field.getModifiers() & Modifier.STATIC) == 0) {
                if (groovyObject && field.getName().equals("metaClass")) {
                    continue;
                }
                AccessController.doPrivileged(new PrivilegedAction() {

                    public Object run() {
                        field.setAccessible(true);
                        return null;
                    }
                });
                buffer.append(" ");
                buffer.append(field.getName());
                buffer.append("=");
                try {
                    buffer.append(InvokerHelper.toString(field.get(self)));
                } catch (Exception e) {
                    buffer.append(e);
                }
            }
        }
        klass = klass.getSuperclass();
    }
    /* here is a different implementation that uses getProperties(). I have left
         * it commented out because it returns a slightly different list of properties;
         * i.e. it does not return privates. I don't know what dump() really should be doing,
         * although IMO showing private fields is a no-no
         */
    /*
        List props = getProperties(self);
            for(Iterator itr = props.keySet().iterator(); itr.hasNext(); ) {
            String propName = itr.next().toString();

            // the original skipped this, so I will too
            if(pv.getName().equals("class")) continue;
            if(pv.getName().equals("metaClass")) continue;

            buffer.append(" ");
            buffer.append(propName);
            buffer.append("=");
            try {
                buffer.append(InvokerHelper.toString(props.get(propName)));
            }
            catch (Exception e) {
                buffer.append(e);
            }
        }
        */
    buffer.append(">");
    return buffer.toString();
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) URISyntaxException(java.net.URISyntaxException) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) MalformedURLException(java.net.MalformedURLException)

Aggregations

Field (java.lang.reflect.Field)4517 Method (java.lang.reflect.Method)500 Test (org.junit.Test)475 ArrayList (java.util.ArrayList)434 IOException (java.io.IOException)276 HashMap (java.util.HashMap)263 Map (java.util.Map)253 List (java.util.List)146 InvocationTargetException (java.lang.reflect.InvocationTargetException)136 Pattern (java.util.regex.Pattern)121 Matcher (java.util.regex.Matcher)115 File (java.io.File)93 HashSet (java.util.HashSet)91 Support_Field (tests.support.Support_Field)82 Annotation (java.lang.annotation.Annotation)77 Collection (java.util.Collection)70 SienaException (siena.SienaException)65 Test (org.testng.annotations.Test)64 Before (org.junit.Before)61 Type (java.lang.reflect.Type)57