/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

/*
 * This is a modified version of the original Apache class.  It has had unused
 * members removed.
 */
package org.ehcache.impl.internal.classes.commonslang.reflect;

import org.ehcache.impl.internal.classes.commonslang.ArrayUtils;
import org.ehcache.impl.internal.classes.commonslang.ClassUtils;

import java.lang.reflect.Array;
import java.lang.reflect.Method;

Utility reflection methods focused on Methods, originally from Commons BeanUtils. Differences from the BeanUtils version may be noted, especially where similar functionality already existed within Lang.

Known Limitations

Accessing Public Methods In A Default Access Superclass

There is an issue when invoking public methods contained in a default access superclass on JREs prior to 1.4. Reflection locates these methods fine and correctly assigns them as public. However, an IllegalAccessException is thrown if the method is invoked.

MethodUtils contains a workaround for this situation. It will attempt to call AccessibleObject.setAccessible(boolean) on this method. If this call succeeds, then the method can be invoked as normal. This call will only succeed when the application has sufficient security privileges. If this call fails then the method may fail.

Since:2.5
/** * <p>Utility reflection methods focused on {@link Method}s, originally from Commons BeanUtils. * Differences from the BeanUtils version may be noted, especially where similar functionality * already existed within Lang. * </p> * * <h3>Known Limitations</h3> * <h4>Accessing Public Methods In A Default Access Superclass</h4> * <p>There is an issue when invoking {@code public} methods contained in a default access superclass on JREs prior to 1.4. * Reflection locates these methods fine and correctly assigns them as {@code public}. * However, an {@link IllegalAccessException} is thrown if the method is invoked.</p> * * <p>{@link MethodUtils} contains a workaround for this situation. * It will attempt to call {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} on this method. * If this call succeeds, then the method can be invoked as normal. * This call will only succeed when the application has sufficient security privileges. * If this call fails then the method may fail.</p> * * @since 2.5 */
public class MethodUtils {

Given an arguments array passed to a varargs method, return an array of arguments in the canonical form, i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type.

Params:
  • args – the array of arguments passed to the varags method
  • methodParameterTypes – the declared array of method parameter types
Returns:an array of the variadic arguments passed to the method
Since:3.5
/** * <p>Given an arguments array passed to a varargs method, return an array of arguments in the canonical form, * i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type. * </p> * * @param args the array of arguments passed to the varags method * @param methodParameterTypes the declared array of method parameter types * @return an array of the variadic arguments passed to the method * @since 3.5 */
static Object[] getVarArgs(final Object[] args, final Class<?>[] methodParameterTypes) { if (args.length == methodParameterTypes.length && args[args.length - 1].getClass().equals(methodParameterTypes[methodParameterTypes.length - 1])) { // The args array is already in the canonical form for the method. return args; } // Construct a new array matching the method's declared parameter types. final Object[] newArgs = new Object[methodParameterTypes.length]; // Copy the normal (non-varargs) parameters System.arraycopy(args, 0, newArgs, 0, methodParameterTypes.length - 1); // Construct a new array for the variadic parameters final Class<?> varArgComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType(); final int varArgLength = args.length - methodParameterTypes.length + 1; Object varArgsArray = Array.newInstance(ClassUtils.primitiveToWrapper(varArgComponentType), varArgLength); // Copy the variadic arguments into the varargs array. System.arraycopy(args, methodParameterTypes.length - 1, varArgsArray, 0, varArgLength); if(varArgComponentType.isPrimitive()) { // unbox from wrapper type to primitive type varArgsArray = ArrayUtils.toPrimitive(varArgsArray); } // Store the varargs array in the last position of the array to return newArgs[methodParameterTypes.length - 1] = varArgsArray; // Return the canonical varargs array. return newArgs; } }