<commit_before>/*
 * Copyright 2004-2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *  
 */

package org.apache.jorphan.reflect;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * @author mstover
 */
public class Functor {
	private static Logger log = LoggingManager.getLoggerForClass();

	Object invokee;

	String methodName;

	Object[] args;

	Class[] types;

	Method methodToInvoke;

	/**
	 * Create a functor with the invokee and a method name.
	 * 
	 * @param invokee
	 * @param methodName
	 */
	public Functor(Object invokee, String methodName) {
		this(methodName);
		this.invokee = invokee;
	}

	/**
	 * Create a functor with the invokee, method name, and argument class types.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param types
	 */
	public Functor(Object invokee, String methodName, Class[] types) {
		this(invokee, methodName);
		this.types = types;
	}

	/**
	 * Create a functor with just the method name.
	 * 
	 * @param methodName
	 */
	public Functor(String methodName) {
		this.methodName = methodName;
	}

	/**
	 * Create a functor with the method name and argument class types.
	 * 
	 * @param methodName
	 * @param types
	 */
	public Functor(String methodName, Class[] types) {
		this(methodName);
		this.types = types;
	}

	/**
	 * Create a functor with an invokee, method name, and argument values.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param args
	 */
	public Functor(Object invokee, String methodName, Object[] args) {
		this(invokee, methodName);
		this.args = args;
	}

	public Functor(String methodName, Object[] args) {
		this(methodName);
		this.args = args;
	}

	/**
	 * Create a functor with an invokee, method name, argument values, and
	 * argument class types.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param args
	 * @param types
	 */
	public Functor(Object invokee, String methodName, Object[] args, Class[] types) {
		this(invokee, methodName, args);
		this.types = types;
	}

	public Object invoke() {
		try {
			return createMethod(getTypes()).invoke(invokee, getArgs());
		} catch (Exception e) {
			log.warn("Trouble functing method: ", e);
			throw new org.apache.jorphan.util.JMeterError(e); // JDK1.4
		}
	}

	public Object invoke(Object p_invokee) {
		this.invokee = p_invokee;
		return invoke();
	}

	public Object invoke(Object[] p_args) {
		this.args = p_args;
		return invoke();
	}

	public Object invoke(Object p_invokee, Object[] p_args) {
		this.args = p_args;
		this.invokee = p_invokee;
		return invoke();
	}

	private Method createMethod(Class[] p_types) {
		log.debug("Trying to functorize invokee: " + invokee.getClass().getName() + " method: " + methodName
				+ " types: " + Arrays.asList(p_types));
		if (methodToInvoke == null) {
			try {
				methodToInvoke = invokee.getClass().getMethod(methodName, p_types);
			} catch (Exception e) {
				for (int i = 0; i < p_types.length; i++) {
					Class primitive = getPrimitive(p_types[i]);<commit_msg>Log more info when the call fails<commit_after>/*
 * Copyright 2004-2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *  
 */

package org.apache.jorphan.reflect;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * @author mstover
 */
public class Functor {
	private static Logger log = LoggingManager.getLoggerForClass();

	Object invokee;

	String methodName;

	Object[] args;

	Class[] types;

	Method methodToInvoke;

	/**
	 * Create a functor with the invokee and a method name.
	 * 
	 * @param invokee
	 * @param methodName
	 */
	public Functor(Object invokee, String methodName) {
		this(methodName);
		this.invokee = invokee;
	}

	/**
	 * Create a functor with the invokee, method name, and argument class types.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param types
	 */
	public Functor(Object invokee, String methodName, Class[] types) {
		this(invokee, methodName);
		this.types = types;
	}

	/**
	 * Create a functor with just the method name.
	 * 
	 * @param methodName
	 */
	public Functor(String methodName) {
		this.methodName = methodName;
	}

	/**
	 * Create a functor with the method name and argument class types.
	 * 
	 * @param methodName
	 * @param types
	 */
	public Functor(String methodName, Class[] types) {
		this(methodName);
		this.types = types;
	}

	/**
	 * Create a functor with an invokee, method name, and argument values.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param args
	 */
	public Functor(Object invokee, String methodName, Object[] args) {
		this(invokee, methodName);
		this.args = args;
	}

	public Functor(String methodName, Object[] args) {
		this(methodName);
		this.args = args;
	}

	/**
	 * Create a functor with an invokee, method name, argument values, and
	 * argument class types.
	 * 
	 * @param invokee
	 * @param methodName
	 * @param args
	 * @param types
	 */
	public Functor(Object invokee, String methodName, Object[] args, Class[] types) {
		this(invokee, methodName, args);
		this.types = types;
	}

	public Object invoke() {
		try {
			return createMethod(getTypes()).invoke(invokee, getArgs());
		} catch (Exception e) {
			log.warn("Trouble functing method: "+methodName+" invokee: "+invokee.getClass().getName(), e);
			throw new org.apache.jorphan.util.JMeterError(e); // JDK1.4
		}
	}

	public Object invoke(Object p_invokee) {
		this.invokee = p_invokee;
		return invoke();
	}

	public Object invoke(Object[] p_args) {
		this.args = p_args;
		return invoke();
	}

	public Object invoke(Object p_invokee, Object[] p_args) {
		this.args = p_args;
		this.invokee = p_invokee;
		return invoke();
	}

	private Method createMethod(Class[] p_types) {
		log.debug("Trying to functorize invokee: " + invokee.getClass().getName() + " method: " + methodName
				+ " types: " + Arrays.asList(p_types));
		if (methodToInvoke == null) {
			try {
				methodToInvoke = invokee.getClass().getMethod(methodName, p_types);
			} catch (Exception e) {
				for (int i = 0; i < p_types.length; i++) {
					Class primitive = getPrimitive(p_types[i]);
{'fraction_non_alphanumeric': 0.11548556430446194, 'fraction_numerical': 0.004144218814753419, 'mean_word_length': 3.5678233438485805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7584288', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2217, 'n_words': 718}