<commit_before>//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 <NAME>, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING

package com.samskivert.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static com.samskivert.Log.log;

/**
 * Runnable related utility methods.
 */
public class Runnables
{
    /** A runnable that does nothing. Useful for use as the default value for optional callbacks. */
    public static Runnable NOOP = new Runnable() {
        public void run () {
            // noop!
        }
    };

    /**
     * Creates a runnable that invokes the specified method on the specified instance via
     * reflection.
     *
     * <p> NOTE: if you specify a protected or private method this method will call {@link
     * Method#setAccessible} to make the method accessible. If you're writing secure code or need
     * to run in a sandbox, don't use this functionality.
     *
     * @throws IllegalArgumentException if the supplied method name does not correspond to a
     * non-static zero-argument method of the supplied instance's class.
     */
    public static Runnable asRunnable (Object instance, String methodName)
    {
        Class<?> clazz = instance.getClass();
        Method method = findMethod(clazz, methodName);
        if (Modifier.isStatic(method.getModifiers())) {
            throw new IllegalArgumentException(
                clazz.getName() + "." + methodName + "() must not be static");
        }
        return new MethodRunner(method, instance);
    }

    /**
     * Creates a runnable that invokes the specified static method via reflection.
     *
     * <p> NOTE: if you specify a protected or private method this method will call {@link
     * Method#setAccessible} to make the method accessible. If you're writing secure code or need
     * to run in a sandbox, don't use this functionality.
     *
     * @throws IllegalArgumentException if the supplied method name does not correspond to a static
     * zero-argument method of the supplied class.
     */
    public static Runnable asRunnable (Class<?> clazz, String methodName)
    {
        Method method = findMethod(clazz, methodName);
        if (!Modifier.isStatic(method.getModifiers())) {
            throw new IllegalArgumentException(
                clazz.getName() + "." + methodName + "() must be static");
        }
        return new MethodRunner(method, null);
    }

    protected static Method findMethod (Class<?> clazz, String methodName)
    {
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(methodName) && method.getParameterTypes().length == 0) {
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                return method;
            }<commit_msg>The isAccessible() check is unnecessary.<commit_after>//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 <NAME>, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING

package com.samskivert.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static com.samskivert.Log.log;

/**
 * Runnable related utility methods.
 */
public class Runnables
{
    /** A runnable that does nothing. Useful for use as the default value for optional callbacks. */
    public static Runnable NOOP = new Runnable() {
        public void run () {
            // noop!
        }
    };

    /**
     * Creates a runnable that invokes the specified method on the specified instance via
     * reflection.
     *
     * <p> NOTE: if you specify a protected or private method this method will call {@link
     * Method#setAccessible} to make the method accessible. If you're writing secure code or need
     * to run in a sandbox, don't use this functionality.
     *
     * @throws IllegalArgumentException if the supplied method name does not correspond to a
     * non-static zero-argument method of the supplied instance's class.
     */
    public static Runnable asRunnable (Object instance, String methodName)
    {
        Class<?> clazz = instance.getClass();
        Method method = findMethod(clazz, methodName);
        if (Modifier.isStatic(method.getModifiers())) {
            throw new IllegalArgumentException(
                clazz.getName() + "." + methodName + "() must not be static");
        }
        return new MethodRunner(method, instance);
    }

    /**
     * Creates a runnable that invokes the specified static method via reflection.
     *
     * <p> NOTE: if you specify a protected or private method this method will call {@link
     * Method#setAccessible} to make the method accessible. If you're writing secure code or need
     * to run in a sandbox, don't use this functionality.
     *
     * @throws IllegalArgumentException if the supplied method name does not correspond to a static
     * zero-argument method of the supplied class.
     */
    public static Runnable asRunnable (Class<?> clazz, String methodName)
    {
        Method method = findMethod(clazz, methodName);
        if (!Modifier.isStatic(method.getModifiers())) {
            throw new IllegalArgumentException(
                clazz.getName() + "." + methodName + "() must be static");
        }
        return new MethodRunner(method, null);
    }

    protected static Method findMethod (Class<?> clazz, String methodName)
    {
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(methodName) && method.getParameterTypes().length == 0) {
                method.setAccessible(true); // isAccessible() check is too slow
                return method;
            }
{'fraction_non_alphanumeric': 0.09270284061915292, 'fraction_numerical': 0.0030617451947610136, 'mean_word_length': 2.622920517560074, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '364441', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1507, 'n_words': 553}