<commit_before>/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * 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.springframework.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.core.NestedRuntimeException;

/**
 * Invoker and cache for dispatch methods that all share the same target object.
 * The dispatch methods typically share the same form, but multiple exist per target
 * object, and they only differ in name.
 * @author <NAME>
 */
public class DispatchMethodInvoker {

	/**
	 * The target object to cache methods on.
	 */
	private Object target;

	/**
	 * The method parameter types describing the form of the dispatcher methods.
	 */
	private Class[] parameterTypes;

	/**
	 * The dispatch method return type
	 */
	private Class returnType;

	/**
	 * A description of the method type.
	 */
	private String typeCaption = "dispatcher";

	/**
	 * The resolved method cache.
	 */
	private Map methodCache = new CachingMapTemplate() {
		public Object create(Object key) {
			String methodName = (String)key;
			try {
				return getTarget().getClass().getMethod(methodName, parameterTypes);
			} catch (NoSuchMethodException e) {
				throw new MethodLookupException("Unable to resolve " + getTypeCaption() + " method with name '"
						+ methodName + "' and signature '" + getSignature(methodName)
						+ "'; make sure the method name is correct "
						+ "and such a public method is defined on targetClass " + getTarget().getClass().getName(), e);
			}
		}
	};

	/**
	 * Creates a still-to-be-configured dispatch method invoker. 
	 */
	public DispatchMethodInvoker() {

	}

	/**
	 * Creates a dispatch method invoker.
	 * @param the parameter types defining the form of the dispatch method
	 */
	public DispatchMethodInvoker(Class[] parameterTypes, Class returnType, String typeCaption) {
		setParameterTypes(parameterTypes);
		setReturnType(returnType);
		setTypeCaption(typeCaption);
	}

	/**
	 * Creates a dispatch method invoker.
	 * @param target the object
	 * @param the parameter types defining the form of the dispatch method
	 */
	public DispatchMethodInvoker(Object target, Class[] parameterTypes, Class returnType, String typeCaption) {
		setTarget(target);
		setParameterTypes(parameterTypes);
		setReturnType(returnType);
		setTypeCaption(typeCaption);
	}

	/**
	 * Set the target object holding the methods.
	 * @param target the delegate to set
	 */
	public void setTarget(Object target) {
		Assert.notNull(target, "The target object is required");
		this.target = target;
		this.methodCache.clear();
	}

	/**
	 * @param parameterTypes
	 */
	public void setParameterTypes(Class[] parameterTypes) {
		this.parameterTypes = parameterTypes;
		this.methodCache.clear();
	}

	/**
	 * Sets the expected return type for the dispatch methods.
	 * @param returnType The expected return type
	 */
	public void setReturnType(Class returnType) {
		this.returnType = returnType;
	}

	/**
	 * @param methodTypeCaption The methodTypeCaption to set.
	 */
	public void setTypeCaption(String typeCaption) {
		this.typeCaption = typeCaption;
	}

	/**
	 * Returns the target object holding the methods.
	 * Defaults to this object.
	 */
	public Object getTarget() {
		return target;
	}

	/**
	 * Returns the parameter types of describing the form of the dispatch method.
	 * @return the parameter types
	 */
	public Class[] getParameterTypes() {
		return parameterTypes;
	}

	/**
	 * Returns the expected return type of the dispatch methods.
	 * @return the expected return type, or null if void
	 */
	public Class getReturnType() {
		return returnType;
	}

	/**
	 * Returns a optional description of the type of method resolved by this cache.
	 * @return the method type description
	 */
	public String getTypeCaption() {
		return typeCaption;
	}

	protected String getSignature(String methodName) {
		StringBuffer parameterTypesString = new StringBuffer();
		for (int i = 0; i < parameterTypes.length; i++) {
			parameterTypesString.append(parameterTypes[i]);
			if (i < parameterTypes.length) {
				parameterTypesString.append(',');
			}
		}
		return "public " + getReturnType() + " " + methodName + "(" + parameterTypesString + ");";
	}
	
	/**
	 * Get a handle to the method of the specified name, with the signature defined by the
	 * configured parameterTypes.
	 * @param methodName the method name
	 * @return the method
	 */
	public Method getDispatchMethod(String methodName) {
		return (Method)this.methodCache.get(methodName);<commit_msg>*** empty log message ***<commit_after>/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * 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.springframework.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.core.NestedRuntimeException;

/**
 * Invoker and cache for dispatch methods that all share the same target object.
 * The dispatch methods typically share the same form, but multiple exist per target
 * object, and they only differ in name.
 * @author <NAME>
 */
public class DispatchMethodInvoker {

	/**
	 * The target object to cache methods on.
	 */
	private Object target;

	/**
	 * The method parameter types describing the form of the dispatcher methods.
	 */
	private Class[] parameterTypes;

	/**
	 * The dispatch method return type
	 */
	private Class returnType;

	/**
	 * A description of the method type.
	 */
	private String typeCaption = "dispatcher";

	/**
	 * The resolved method cache.
	 */
	private Map methodCache = new CachingMapTemplate() {
		public Object create(Object key) {
			String methodName = (String)key;
			try {
				return getTarget().getClass().getMethod(methodName, parameterTypes);
			} catch (NoSuchMethodException e) {
				throw new MethodLookupException("Unable to resolve " + getTypeCaption() + " method with name '"
						+ methodName + "' and signature '" + getSignature(methodName)
						+ "'; make sure the method name is correct "
						+ "and such a public method is defined on targetClass " + getTarget().getClass().getName(), e);
			}
		}
	};

	/**
	 * Creates a still-to-be-configured dispatch method invoker. 
	 */
	public DispatchMethodInvoker() {

	}

	/**
	 * Creates a dispatch method invoker.
	 * @param the parameter types defining the form of the dispatch method
	 */
	public DispatchMethodInvoker(Class[] parameterTypes, Class returnType, String typeCaption) {
		setParameterTypes(parameterTypes);
		setReturnType(returnType);
		setTypeCaption(typeCaption);
	}

	/**
	 * Creates a dispatch method invoker.
	 * @param target the object
	 * @param the parameter types defining the form of the dispatch method
	 */
	public DispatchMethodInvoker(Object target, Class[] parameterTypes, Class returnType, String typeCaption) {
		setTarget(target);
		setParameterTypes(parameterTypes);
		setReturnType(returnType);
		setTypeCaption(typeCaption);
	}

	/**
	 * Set the target object holding the methods.
	 * @param target the delegate to set
	 */
	public void setTarget(Object target) {
		Assert.notNull(target, "The target object is required");
		this.target = target;
		this.methodCache.clear();
	}

	/**
	 * @param parameterTypes
	 */
	public void setParameterTypes(Class[] parameterTypes) {
		this.parameterTypes = parameterTypes;
		this.methodCache.clear();
	}

	/**
	 * Sets the expected return type for the dispatch methods.
	 * @param returnType The expected return type
	 */
	public void setReturnType(Class returnType) {
		this.returnType = returnType;
	}

	/**
	 * @param methodTypeCaption The methodTypeCaption to set.
	 */
	public void setTypeCaption(String typeCaption) {
		this.typeCaption = typeCaption;
	}

	/**
	 * Returns the target object holding the methods.
	 * Defaults to this object.
	 */
	public Object getTarget() {
		return target;
	}

	/**
	 * Returns the parameter types of describing the form of the dispatch method.
	 * @return the parameter types
	 */
	public Class[] getParameterTypes() {
		return parameterTypes;
	}

	protected String getParameterTypesString() {
		StringBuffer parameterTypesString = new StringBuffer();
		for (int i = 0; i < parameterTypes.length; i++) {
			parameterTypesString.append(parameterTypes[i]);
			if (i < parameterTypes.length - 1) {
				parameterTypesString.append(',');
			}
		}
		return parameterTypesString.toString();
	}
	
	/**
	 * Returns the expected return type of the dispatch methods.
	 * @return the expected return type, or null if void
	 */
	public Class getReturnType() {
		return returnType;
	}

	protected String getReturnTypeString() {
		return (returnType != null ? returnType.getName() : "void");
	}
	/**
	 * Returns a optional description of the type of method resolved by this cache.
	 * @return the method type description
	 */
	public String getTypeCaption() {
		return typeCaption;
	}

	protected String getSignature(String methodName) {
		return "public " + getReturnTypeString() + " " + methodName + "(" + getParameterTypesString() + ");";
	}
	
	/**
	 * Get a handle to the method of the specified name, with the signature defined by the
	 * configured parameterTypes.
	 * @param methodName the method name
	 * @return the method
	 */
	public Method getDispatchMethod(String methodName) {
		return (Method)this.methodCache.get(methodName);
{'fraction_non_alphanumeric': 0.09657230887733487, 'fraction_numerical': 0.0025996533795493936, 'mean_word_length': 3.8311627906976744, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '5395229', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2758, 'n_words': 1027}