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 2013 Sven Ewald
*
* Licensed 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.
*/
package org.xmlbeam.util.intern;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import javax.management.RuntimeErrorException;
import org.xmlbeam.exceptions.XBException;
/**
* A set of tiny helper methods internally used in the projection framework. This methods are
* not part of the public framework API and might change in minor version updates.
*
* @author Sven Ewald
*/
public final class ReflectionHelper {
private final static Method ISDEFAULT = findMethodByName(Method.class, "isDefault");
private final static Class> OPTIONAL_CLASS = findClass("java.util.Optional");
private final static Method OFNULLABLE = findMethodByName(OPTIONAL_CLASS, "ofNullable");
private final static Method GETPARAMETERS = findMethodByName(Method.class, "getParameters");
private final static int PUBLIC_STATIC_MODIFIER = Modifier.STATIC | Modifier.PUBLIC;
private final static Pattern VALID_FACTORY_METHOD_NAMES = Pattern.compile("valueOf|of|parse|getInstance");
private final static Method STREAM = findMethodByName(List.class, "stream");
public final static Class> LOCAL_DATE_CLASS = findClass("java.time.LocalDate");
public final static Class> LOCAL_DATE_TIME_CLASS = findClass("java.time.LocalDateTime");
public final static Class> LOCAL_DATE_TIME_FORMATTER_CLASS = findClass("java.time.format.DateTimeFormatter");
public static final int JAVA_VERSION = getJavaVersion();
/**
* @param
* @param content
* @return new array of T
*/
public static T[] array(T... content) {
return content;
};
private static Class> findClass(final String name) {
try {
return Class.forName(name, false, ReflectionHelper.class.getClassLoader());
} catch (ClassNotFoundException e) {
return null;
}
}
/**
* @param a
* @param b
* @return a set of all interfaces that a extends and b extends
*/
public static Set> findAllCommonSuperInterfaces(final Class> a, final Class> b) {
final Set> seta = new HashSet>(findAllSuperInterfaces(a));
final Set> setb = new HashSet>(findAllSuperInterfaces(b));
seta.retainAll(setb);
return seta;
}
/**
* Non exception throwing shortcut to find the first method with a given name.
*
* @param clazz
* @param name
* @return method with name "name" or null if it does not exist.
*/
public static Method findMethodByName(final Class> clazz, final String name) {
if (clazz == null) {
return null;
}
for (final Method m : clazz.getMethods()) {
if (name.equals(m.getName())) {
return m;
}
}
return null;
}
/**
* Returns list of super interfaces,sorted from the top (super) to the bottom (extended).
*
* @param a
* @return a set of all super interfaces of a
*/
public static List> findAllSuperInterfaces(final Class> a) {
LinkedList> list = new LinkedList>();
Queue> queue = new LinkedList>();
queue.add(a);
while (!queue.isEmpty()) {
Class> c = queue.poll();
if (c.isInterface()) {
list.addFirst(c);
}
queue.addAll(Arrays.asList(c.getInterfaces()));
}
return list;
};
/**
* Defensive implemented method to determine if method has a return type.
*
* @param method
* @return true if and only if it is not a void method.
*/
public static boolean hasReturnType(final Method method) {
if (method == null) {
return false;
}
if (method.getReturnType() == null) {
return false;
}
if (Void.class.equals(method.getReturnType())) {
return false;
}
return !Void.TYPE.equals(method.getReturnType());
}
/**
* @param method
* @return true if and only if the method has at least one parameter.
*/
public static boolean hasParameters(final Method method) {
return (method != null) && (method.getParameterTypes().length > 0);
}
/**
* Same as Arrays.asList(...), but does automatically conversion of primitive arrays.
*
* @param array
* @return List of objects representing the given array contents
*/
public static List