<commit_before>/* Copyright (C) 2010 Center for Computational Pharmacology, University of Colorado School of Medicine
 * 
 * This file is part of the CCP common library.
 * The CCP common library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package edu.ucdenver.ccp.common.reflection;

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

/**
 * Provides access to private members in classes. Modified from
 * http://snippets.dzone.com/posts/show/2242
 */
public class PrivateAccessor {

	/**
	 * 
	 * @param o
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public static Object getPrivateFieldValue(Object o, String fieldName) throws NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		return getPrivateField(o, fieldName).get(o);
	}

	/**
	 * If the field is not visible in the class provided, superclasses are searched.
	 * 
	 * @param o
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Field getPrivateField(Object o, String fieldName) throws NoSuchFieldException {
		Field field = getPrivateField(o.getClass(), fieldName);
		if (field == null)
			throw new NoSuchFieldException(String.format("No '%s' field can be found in class %s", fieldName, o
					.getClass().getName()));
		return field;
	}

	private static Field getPrivateField(Class<?> clazz, String fieldName) {
		for (Field field : clazz.getDeclaredFields()) {
			if (field.getName().equals(fieldName)) {
				field.setAccessible(true);
				return field;
			}
		}
		Class<?> superclass = clazz.getSuperclass();
		if (superclass == null)
			return null;
		return getPrivateField(superclass, fieldName);
	}

	public static Object invokePrivateMethod(Object o, String methodName, Object... params) throws SecurityException,
			NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
<commit_msg>KABOB-203: implemented recursive getAllFields (including inherited) method<commit_after>/* Copyright (C) 2010 Center for Computational Pharmacology, University of Colorado School of Medicine
 * 
 * This file is part of the CCP common library.
 * The CCP common library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package edu.ucdenver.ccp.common.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * Provides access to private members in classes. Modified from
 * http://snippets.dzone.com/posts/show/2242
 */
public class PrivateAccessor {

	/**
	 * 
	 * @param o
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public static Object getPrivateFieldValue(Object o, String fieldName) throws NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		return getPrivateField(o, fieldName).get(o);
	}

	/**
	 * If the field is not visible in the class provided, superclasses are searched.
	 * 
	 * @param o
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Field getPrivateField(Object o, String fieldName) throws NoSuchFieldException {
		Field field = getPrivateField(o.getClass(), fieldName);
		if (field == null)
			throw new NoSuchFieldException(String.format("No '%s' field can be found in class %s", fieldName, o
					.getClass().getName()));
		return field;
	}

	private static Field getPrivateField(Class<?> clazz, String fieldName) {
		for (Field field : clazz.getDeclaredFields()) {
			if (field.getName().equals(fieldName)) {
				field.setAccessible(true);
				return field;
			}
		}
		Class<?> superclass = clazz.getSuperclass();
		if (superclass == null)
			return null;
		return getPrivateField(superclass, fieldName);
	}

	/**
	 * Get all declared and inherited class fields recursively
	 * 
	 * @param clazz class to reflect on
	 * @param fields to hold return value
	 * @return all declared and inherited fields
	 */
	public static Set<Field> getAllFields(Class<?> clazz, Set<Field> fields) {
		for (Field field : clazz.getDeclaredFields()) {
			fields.add(field);
			field.setAccessible(true);
			fields.add(field);
		}
		
		Class<?> superclass = clazz.getSuperclass();
		if (superclass == null)
			return fields;
		
		return getAllFields(superclass, fields);
	}
	
	public static Object invokePrivateMethod(Object o, String methodName, Object... params) throws SecurityException,
			NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

{'fraction_non_alphanumeric': 0.08773098052272349, 'fraction_numerical': 0.007158315298818046, 'mean_word_length': 4.298059964726631, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1891469', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1587, 'n_words': 615}