Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (C) 2014, 2015, Danilo Pianini and contributors
* listed in the project's build.gradle or pom.xml file.
*
* This file is part of Protelis, and is distributed under the terms of
* the GNU General Public License, with a linking exception, as described
* in the file LICENSE.txt in this project's top directory.
*******************************************************************************/
package org.protelis.lang.util;
import static java8.util.stream.StreamSupport.stream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.commons.math3.util.Pair;
import org.danilopianini.lang.PrimitiveUtils;
import org.protelis.lang.datatype.Field;
import org.protelis.lang.datatype.Fields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java8.util.J8Arrays;
import java8.util.Optional;
import java8.util.stream.Collectors;
import java8.util.stream.Stream;
/**
* Utilities that make easier to cope with Java Reflection.
*/
public final class ReflectionUtils {
private static final Logger L = LoggerFactory.getLogger(ReflectionUtils.class);
private static final int CACHE_MAX_SIZE = 1000;
private static final LoadingCache, String, List>>, Method> METHOD_CACHE = CacheBuilder
.newBuilder().maximumSize(CACHE_MAX_SIZE).expireAfterAccess(1, TimeUnit.HOURS)
.build(new CacheLoader, String, List>>, Method>() {
@Override
public Method load(final Triple, String, List>> key) {
final List> al = key.getRight();
final Class>[] args = new Class>[al.size()];
return loadBestMethod(key.getLeft(), key.getMiddle(), al.toArray(args));
}
});
private ReflectionUtils() {
}
/**
* @param methodName
* the method to be invoked
* @param target
* the target object. It can not be null
* @param args
* the arguments for the method
* @return the result of the invocation, or an {@link IllegalStateException}
* if something goes wrong.
*/
public static Object invokeBestNotStatic(final Object target, final String methodName, final Object[] args) {
Objects.requireNonNull(target);
return invokeBestMethod(target.getClass(), methodName, target, args);
}
/**
* @param clazz
* the class where to search for suitable methods
* @param methodName
* the method to be invoked
* @param args
* the arguments for the method
* @return the result of the invocation, or an {@link IllegalStateException}
* if something goes wrong.
*/
public static Object invokeBestStatic(final Class> clazz, final String methodName, final Object... args) {
return invokeBestMethod(clazz, methodName, null, args);
}
/**
* @param clazz
* the class where to search for suitable methods
* @param methodName
* the method to be invoked
* @param args
* the arguments for the method
* @return the result of the invocation, or an {@link IllegalStateException}
* if something goes wrong.
*/
public static Method searchBestMethod(final Class> clazz, final String methodName, final Object... args) {
return searchBestMethod(clazz, methodName, Arrays.asList(args));
}
/**
* @param clazz
* the class where to search for suitable methods
* @param methodName
* the method to be invoked
* @param args
* the arguments for the method. If a {@link Field} is passed,
* then the expected type of the field is used.
* @return the result of the invocation, or an {@link IllegalStateException}
* if something goes wrong.
*/
public static Method searchBestMethod(final Class> clazz, final String methodName, final List