<commit_before>package com.wombatnation.privateer;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Privateer is a utility class for unit testing. Privateer allows you to access
 * all fields on an object, regardless of whether their accessibility is set to
 * public, package, protected or private. Privateer also allows you to invoke
 * any method on an object, regardless of accessibility.
 * <p>
 * For convenience in writing unit tests, security is automatically disabled when a
 * new Privateer object is constructed.
 * 
 * @author rstewart
 */
public class Privateer {
  private SecurityManager secman = System.getSecurityManager();
  private SecurityManager insecman = new InsecurityManager();
  
  /**
   * Installs a SecurityManager that is completely unsecure.
   */
  public Privateer() {
    secureSystem(false);
  }
  
  /**
   * Sets whether system is secure, i.e., uses the default SecurityManager or
   * uses an unsecure SecurityManager.
   * 
   * @param secure Set to true to secure the system and to false to unsecure the system
   */
  public void secureSystem(boolean secure) {
    if (secure)
      System.setSecurityManager(secman);
    else
      System.setSecurityManager(insecman);
  }
  
  /**
   * Returns all declared fields on Object o as accessible.
   * 
   * @param o
   * @return all declared fields on Object o as accessible
   */
  public Field[] getAllFields(Object o) {
    Field[] fields = o.getClass().getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);
    return fields;
  }
  
  /**
   * Returns all declared methods on Object o as accessible.
   * 
   * @param o
   * @return all declared methods on Object o as accessible
   */
  public Method[] getAllMethods(Object o) {
    Method[] methods = o.getClass().getDeclaredMethods();
    AccessibleObject.setAccessible(methods, true);
    return methods;
  }
  
  /**
   * Gets the specified field on Object o, even if that field is not normally accessible.
   * 
   * @param o
   * @param fieldName
   * @param value
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public Object getField(Object o, String fieldName)
      throws NoSuchFieldException, IllegalAccessException {
    
    Field field = o.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(o);
  }
  
  /**
   * Sets the specified field on Object o to the specified value, even if that field is not normally accessible.
   * 
   * @param o
   * @param fieldName
   * @param value
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public void setField(Object o, String fieldName, Object value)
      throws NoSuchFieldException, IllegalAccessException {
    
    Field field = o.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(o, value);
  }
  
  /**
   * Calls the specified method on the Object o with the specified arguments. Returns the
   * result as an Object.
   * 
   * @param o
   * @param methodName
   * @param args
   * @return Object that is the result of calling the named method
   * @throws NoSuchMethodException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public Object callMethod(Object o, String methodName, Object... args)
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    Method method = null;
    if (args == null || args.length == 0) {<commit_msg>Javadoc updates<commit_after>package com.wombatnation.privateer;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Privateer is a utility class for unit testing. Privateer allows you to access
 * all fields on an object, regardless of whether their accessibility is set to
 * public, package, protected or private. Privateer also allows you to invoke
 * any method on an object, regardless of accessibility.
 * <p>
 * For convenience in writing unit tests, security is automatically disabled when a
 * new Privateer object is constructed.
 * 
 * @author rstewart
 */
public class Privateer {
  private SecurityManager secman = System.getSecurityManager();
  private SecurityManager insecman = new InsecurityManager();
  
  /**
   * Constructs a Privateer object that allows you to access normally inaccessible
   * fields and methods. It does this by installing a SecurityManager that is completely unsecure.
   */
  public Privateer() {
    secureSystem(false);
  }
  
  /**
   * Sets whether system is secure, i.e., whether it uses the default SecurityManager or
   * unsecure SecurityManager.
   * 
   * @param secure Set to true to secure the system and to false to unsecure the system
   */
  public void secureSystem(boolean secure) {
    if (secure)
      System.setSecurityManager(secman);
    else
      System.setSecurityManager(insecman);
  }
  
  /**
   * Returns all declared Fields on Object o as accessible.
   * 
   * @param o Object to access
   * @return all declared Fields on Object o as accessible
   */
  public Field[] getAllFields(Object o) {
    Field[] fields = o.getClass().getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);
    return fields;
  }
  
  /**
   * Returns all declared Methods on Object o as accessible.
   * 
   * @param o Object to access
   * @return all declared Methods on Object o as accessible
   */
  public Method[] getAllMethods(Object o) {
    Method[] methods = o.getClass().getDeclaredMethods();
    AccessibleObject.setAccessible(methods, true);
    return methods;
  }
  
  /**
   * Gets the specified field on Object o, even if that field is not normally accessible.
   * 
   * @param o Object to access
   * @param fieldName Name of field whose value will be returned
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public Object getField(Object o, String fieldName)
      throws NoSuchFieldException, IllegalAccessException {
    
    Field field = o.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(o);
  }
  
  /**
   * Sets the specified field on Object o to the specified value, even if that field is not normally accessible.
   * 
   * @param o Object to access
   * @param fieldName Name of field whose value will be set
   * @param value Object value that will be set for the field
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  public void setField(Object o, String fieldName, Object value)
      throws NoSuchFieldException, IllegalAccessException {
    
    Field field = o.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(o, value);
  }
  
  /**
   * Calls the specified method on the Object o with the specified arguments. Returns the
   * result as an Object.
   * <p>
   * The length of the vararg list of arguments to be passed to the method must match
   * what the method expects. If no arguments are expected, null can be passed. If one
   * argument is expected, that argument can be passed as an object. If multiple
   * arguments are expected, they can be passed as an Object array or as a comma-separated
   * list.
   * 
   * @param o Object to access
   * @param methodName Name of method to call
   * @param args Vararg list of arguments to pass to method
   * @return Object that is the result of calling the named method
   * @throws NoSuchMethodException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public Object callMethod(Object o, String methodName, Object... args)
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    Method method = null;
    if (args == null || args.length == 0) {
{'fraction_non_alphanumeric': 0.07757422615287429, 'fraction_numerical': 0.00025268477574226153, 'mean_word_length': 3.3831672203765226, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '7146999', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2113, 'n_words': 823}