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 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.commons.util;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.junit.gen5.commons.meta.API.Usage.Internal;
import java.io.File;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.junit.gen5.commons.meta.API;
/**
* Collection of utilities for working with the Java reflection APIs.
*
*
DISCLAIMER
*
*
These utilities are intended solely for usage within the JUnit framework
* itself. Any usage by external parties is not supported.
* Use at your own risk!
*
* @since 5.0
*/
@API(Internal)
public final class ReflectionUtils {
public enum MethodSortOrder {
HierarchyDown, HierarchyUp
}
private ReflectionUtils() {
/* no-op */
}
public static ClassLoader getDefaultClassLoader() {
try {
return Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
/* ignore */
}
return ClassLoader.getSystemClassLoader();
}
public static boolean isPublic(Class> clazz) {
return Modifier.isPublic(clazz.getModifiers());
}
public static boolean isPublic(Member member) {
return Modifier.isPublic(member.getModifiers());
}
public static boolean isPrivate(Class> clazz) {
return Modifier.isPrivate(clazz.getModifiers());
}
public static boolean isPrivate(Member member) {
return Modifier.isPrivate(member.getModifiers());
}
public static boolean isAbstract(Class> clazz) {
return Modifier.isAbstract(clazz.getModifiers());
}
public static boolean isAbstract(Member member) {
return Modifier.isAbstract(member.getModifiers());
}
public static boolean isStatic(Class> clazz) {
return Modifier.isStatic(clazz.getModifiers());
}
public static boolean isStatic(Member member) {
return Modifier.isStatic(member.getModifiers());
}
/**
* Create a new instance of the specified {@link Class} by invoking
* the constructor whose argument list matches the types of the supplied
* arguments.
*
*
The constructor will be made accessible if necessary, and any checked
* exception will be {@linkplain ExceptionUtils#throwAsUncheckedException masked}
* as an unchecked exception.
*
* @param clazz the class to instantiate; never {@code null}
* @param args the arguments to pass to the constructor none of which may be {@code null}
* @return the new instance
* @see ExceptionUtils#throwAsUncheckedException(Throwable)
*/
public static T newInstance(Class clazz, Object... args) {
Preconditions.notNull(clazz, "class must not be null");
Preconditions.notNull(args, "none of the arguments may be null");
try {
Class>[] parameterTypes = Arrays.stream(args).map(Object::getClass).toArray(Class[]::new);
Constructor constructor = makeAccessible(clazz.getDeclaredConstructor(parameterTypes));
return constructor.newInstance(args);
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
/**
* Invoke the supplied method, making it accessible if necessary and
* {@linkplain ExceptionUtils#throwAsUncheckedException masking} any
* checked exception as an unchecked exception.
*
* @param method the method to invoke; never {@code null}
* @param target the object on which to invoke the method; may be
* {@code null} if the method is {@code static}
* @param args the arguments to pass to the method
* @return the value returned by the method invocation or {@code null}
* if the return type is {@code void}
* @see ExceptionUtils#throwAsUncheckedException(Throwable)
*/
public static Object invokeMethod(Method method, Object target, Object... args) {
Preconditions.notNull(method, "method must not be null");
Preconditions.condition((target != null || isStatic(method)),
() -> String.format("Cannot invoke non-static method [%s] on a null target.", method.toGenericString()));
try {
return makeAccessible(method).invoke(target, args);
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
public static Optional> loadClass(String name) {
return loadClass(name, getDefaultClassLoader());
}
public static Optional> loadClass(String name, ClassLoader classLoader) {
Preconditions.notBlank(name, "class name must not be null or empty");
Preconditions.notNull(classLoader, "ClassLoader must not be null");
try {
// TODO Add support for primitive types and arrays.
return Optional.of(classLoader.loadClass(name.trim()));
}
catch (ClassNotFoundException e) {
return Optional.empty();
}
}
/**
* Try to load a method by its fully qualified name (if such a thing exists for methods).
* @param fullyQualifiedMethodName In the form 'package.subpackage.ClassName#methodName'
* @return Optional of Method
*/
public static Optional loadMethod(String fullyQualifiedMethodName) {
Preconditions.notBlank(fullyQualifiedMethodName, "full method name must not be null or empty");
// TODO Handle overloaded and inherited methods
Optional testMethodOptional = Optional.empty();
int hashPosition = fullyQualifiedMethodName.lastIndexOf('#');
if (hashPosition >= 0 && hashPosition < fullyQualifiedMethodName.length()) {
String className = fullyQualifiedMethodName.substring(0, hashPosition);
String methodName = fullyQualifiedMethodName.substring(hashPosition + 1);
Optional> methodClassOptional = loadClass(className);
if (methodClassOptional.isPresent()) {
try {
testMethodOptional = Optional.of(methodClassOptional.get().getDeclaredMethod(methodName));
}
catch (NoSuchMethodException ignore) {
}
}
}
return testMethodOptional;
}
public static Optional