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 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.testutils.smoketest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.slf4j.LoggerFactory;
/**
* Utility methods for doing reflection.
*/
public final class ReflectionUtils {
private static final Random RANDOM = new Random();
private ReflectionUtils() {
}
public static Class loadClass(Class> base, String name) {
return loadClass(base.getClassLoader(), name);
}
public static Class loadClass(ClassLoader classloader, String name) {
try {
@SuppressWarnings("unchecked")
Class loaded = (Class) classloader.loadClass(name);
return loaded;
} catch (ClassNotFoundException exception) {
throw new IllegalStateException(
"Cannot find class " + name,
exception);
}
}
public static T newInstance(Class clazz, Object... params) {
Constructor constructor = findConstructor(clazz, params);
try {
return constructor.newInstance(params);
} catch (InstantiationException | IllegalAccessException ex) {
throw new IllegalStateException(
"Could not invoke " + constructor.toGenericString(),
ex);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new IllegalStateException(
"Unexpected checked exception thrown from "
+ constructor.toGenericString(),
ex);
}
}
private static Constructor findConstructor(
Class clazz,
Object[] params) {
for (Constructor> constructor : clazz.getConstructors()) {
Class>[] paramTypes = constructor.getParameterTypes();
if (matches(paramTypes, params)) {
@SuppressWarnings("unchecked")
Constructor rval = (Constructor) constructor;
return rval;
}
}
throw new IllegalStateException(
"No appropriate constructor found for "
+ clazz.getCanonicalName());
}
private static boolean matches(Class>[] paramTypes, Object[] params) {
if (paramTypes.length != params.length) {
return false;
}
for (int i = 0; i < params.length; ++i) {
if (!paramTypes[i].isAssignableFrom(params[i].getClass())) {
return false;
}
}
return true;
}
/**
* Evaluates the given path expression on the given object and returns the
* object found.
*
* @param target the object to reflect on
* @param path the path to evaluate
* @return the result of evaluating the path against the given object
*/
public static Object getByPath(Object target, List path) {
Object obj = target;
for (String field : path) {
if (obj == null) {
return null;
}
obj = evaluate(obj, trimType(field));
}
return obj;
}
/**
* Evaluates the given path expression and returns the list of all matching
* objects. If the path expression does not contain any wildcards, this
* will return a list of at most one item. If the path contains one or more
* wildcards, the returned list will include the full set of values
* obtained by evaluating the expression with all legal value for the
* given wildcard.
*
* @param target the object to evaluate the expression against
* @param path the path expression to evaluate
* @return the list of matching values
*/
public static List