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) 2018 Vikash Madhow
*/
package ma.vi.base.reflect;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import ma.vi.base.collections.Maps;
import ma.vi.base.lang.NotFoundException;
import ma.vi.base.tuple.T2;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import static ma.vi.base.lang.Errors.unchecked;
import static org.apache.commons.lang3.StringUtils.countMatches;
/**
* Utility functions for working reflectively with classes.
*
* @author [email protected]
*/
public class Classes {
/**
* Returns true if the supplied class is a sub-class of the super-type whose fully-qualified
* name has been supplied. This method uses reflection to test against super-types which might
* not be available or convenient to load.
*/
public static boolean isSubclassOf(Class> cls, String superType) {
T2, String> pair = new T2<>(cls, superType);
if (subclassCache.containsKey(pair)) {
return subclassCache.get(pair);
} else {
boolean subclass = false;
for (Class> component : Dissector.componentClasses(cls)) {
if (component.getName().equals(superType)) {
subclass = true;
break;
}
}
subclassCache.put(pair, subclass);
return subclass;
}
}
/**
* Utility for creating a new instance of the class using a constructor matching the types
* of the supplied parameters, wrapping any exceptions as RuntimeExceptions. For primitive
* types, an instance of the corresponding wrapper type is created instead.
*
* The type of the parameters of the constructor to used are determined from the parameters
* supplied. This requires that all parameters are non-null; null parameters are assumed to
* be of object type otherwise.
*/
public static Object instanceOf(Class> cls, Object... parameters) {
List>> paramsWithType = new ArrayList<>();
for (Object parameter : parameters) {
paramsWithType.add(T2.of(parameter, parameter == null ? Object.class : parameter.getClass()));
}
return instanceOfWithTypes(cls, paramsWithType.toArray(new T2[0]));
}
/**
* Utility for creating a new instance of the class using a constructor matching the types
* of the supplied parameters, wrapping any exceptions as RuntimeExceptions. For primitive
* types, an instance of the corresponding wrapper type is created instead.
*/
public static Object instanceOfWithTypes(Class> cls, T2