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.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.brooklyn.util.javalang;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
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.lang.reflect.TypeVariable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import javax.annotation.Nullable;
import org.apache.brooklyn.util.collections.MutableList;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.javalang.coerce.TypeCoercer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;
/**
* Reflection utilities
*
* @author aled
*/
public class Reflections {
private static final Logger LOG = LoggerFactory.getLogger(Reflections.class);
public static class ReflectionNotFoundException extends RuntimeException {
private static final long serialVersionUID = 9032835250796708037L;
public ReflectionNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ReflectionNotFoundException(String message) {
super(message);
}
}
public static class ReflectionAccessException extends RuntimeException {
private static final long serialVersionUID = 6569605861192432009L;
public ReflectionAccessException(String message, Throwable cause) {
super(message, cause);
}
}
private final ClassLoader classLoader;
private final Map classRenameMap = MutableMap.of();
public Reflections(ClassLoader classLoader) {
this.classLoader = classLoader!=null ? classLoader : getClass().getClassLoader();
}
/** supply a map of known renames, of the form "old-class -> new-class" */
public Reflections applyClassRenames(Map newClassRenames) {
this.classRenameMap.putAll(newClassRenames);
return this;
}
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public Object loadInstance(String classname, Object...argValues) throws ReflectionNotFoundException, ReflectionAccessException {
Class clazz = loadClass(classname);
Maybe v = null;
try {
v = invokeConstructorFromArgs(clazz, argValues);
if (v.isPresent()) return v.get();
} catch (Exception e) {
throw new IllegalStateException("Error invoking constructor for "+clazz+Arrays.toString(argValues) + ": " + Exceptions.collapseText(e));
}
throw new IllegalStateException("No suitable constructor for "+clazz+Arrays.toString(argValues));
}
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public Object loadInstance(String classname, Class[] argTypes, Object[] argValues) throws ReflectionNotFoundException, ReflectionAccessException {
Class clazz = loadClass(classname);
Constructor constructor = loadConstructor(clazz, argTypes);
return loadInstance(constructor, argValues);
}
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public Object loadInstance(String classname) throws ReflectionNotFoundException, ReflectionAccessException {
Class clazz = loadClass(classname);
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new ReflectionAccessException("Failed to create instance of class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e);
} catch (IllegalAccessException e) {
throw new ReflectionAccessException("Failed to create instance of class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e);
}
}
/** instantiates the given class from its binary name */
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public Class loadClass(String classname) throws ReflectionNotFoundException {
try {
classname = findMappedNameAndLog(classRenameMap, classname);
return classLoader.loadClass(classname);
} catch (ClassNotFoundException e) {
throw new ReflectionNotFoundException("Failed to load class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e);
} catch (NoClassDefFoundError e) {
throw new ReflectionNotFoundException("Failed to load class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e);
} catch (UnsupportedClassVersionError e) {
throw new ReflectionNotFoundException("Failed to load class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e);
}
}
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
@SuppressWarnings("unchecked")
public Class loadClass(String classname, Class superType) throws ReflectionNotFoundException {
return (Class) loadClass(classname);
}
/** given a nested part, e.g. Inner$VeryInner, this will recurse through clazz.Inner, looking for VeryInner,
* then looking in each supertype (interface) of clazz for Inner.VeryInner;
*
* so it will find Clazz.Inner.VeryInner wherever in the hierarchy it is defined
*
* (as opposed to ClassLoader which requires Inner.VeryInner to be _declared_ in clazz, not in any supertype
*
* returns null if not found
*/
public static Class loadInnerClassPossiblyInheritted(Class clazz, String nestedPart) throws ReflectionNotFoundException {
Set visited = new HashSet();
Class result = loadInnerClassPossiblyInheritted(visited, clazz, nestedPart);
if (result!=null) return result;
throw new ReflectionNotFoundException("Inner class " + nestedPart + " could not be found in " + clazz + " or any of its super-types");
}
/** as 2-arg, but maintains set of visited elements, and returns null if not found */
private static Class loadInnerClassPossiblyInheritted(Set visited, Class clazz, String nestedPart) throws ReflectionNotFoundException {
if (clazz==null) return null;
if (nestedPart==null || nestedPart.length()==0) return clazz;
int i1 = nestedPart.indexOf('$');
int i2 = nestedPart.indexOf('.');
int idx = (i2 > -1 && (i2 < i1 || i1==-1) ? i2 : i1);
String thisClassToFind = nestedPart;
String nextClassesToFind = "";
if (idx>=0) {
thisClassToFind = nestedPart.substring(0, idx);
nextClassesToFind = nestedPart.substring(idx+1);
}
if (!visited.add(clazz.getCanonicalName()+"!"+nestedPart)) {
//already visited
return null;
}
Class[] members = clazz.getClasses();
for (int i = 0; i < members.length; i++) {
if (members[i].getSimpleName().equals(thisClassToFind)) {
Class clazzI = loadInnerClassPossiblyInheritted(visited, members[i], nextClassesToFind);
if (clazzI!=null) return clazzI;
}
}
//look in supertype first (not sure if necessary)
Class result = loadInnerClassPossiblyInheritted(visited, clazz.getSuperclass(), nestedPart);
if (result!=null) return result;
for (Class iface : clazz.getInterfaces()) {
result = loadInnerClassPossiblyInheritted(visited, iface, nestedPart);
if (result!=null) return result;
}
return null;
}
/** does not look through ancestors of outer class */
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public Class loadInnerClassNotInheritted(String outerClassname, String innerClassname) throws ReflectionNotFoundException {
return loadClass(outerClassname + "$" + innerClassname);
}
/** does not look through ancestors of outer class
*
* uses the classloader set in this class, not in the clazz supplied */
public Class loadInnerClassNotInheritted(Class outerClazz, String innerClassname) throws ReflectionNotFoundException {
return loadClass(outerClazz.getName() + "$" + innerClassname);
}
public Constructor loadConstructor(Class clazz, Class[] argTypes) throws ReflectionAccessException {
try {
return clazz.getConstructor(argTypes);
} catch (SecurityException e) {
throw new ReflectionAccessException("Failed to load constructor of class '" + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e);
} catch (NoSuchMethodException e) {
throw new ReflectionAccessException("Failed to load constructor of class '" + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e);
}
}
/** @deprecated since 0.10.0 use {@link #invokeConstructorFromArgs(Class, Object...)} or one of the variants;
* this allows null field values */ @Deprecated
public static Optional invokeConstructorWithArgs(ClassLoader classLoader, String className, Object...argsArray) {
return Reflections.invokeConstructorFromArgsUntyped(classLoader, className, argsArray).toOptional();
}
/** @deprecated since 0.10.0 use {@link #invokeConstructorFromArgs(Class, Object...)} or one of the variants */ @Deprecated
public static Optional invokeConstructorWithArgs(ClassLoader classLoader, Class clazz, Object[] argsArray, boolean setAccessible) {
return invokeConstructorFromArgs(classLoader, clazz, argsArray, setAccessible).toOptional();
}
/** @deprecated since 0.10.0 use {@link #invokeConstructorFromArgs(Class, Object...)} or one of the variants */ @Deprecated
public static Optional invokeConstructorWithArgs(Class clazz, Object...argsArray) {
return Reflections.invokeConstructorFromArgs(clazz, argsArray).toOptional();
}
/** @deprecated since 0.10.0 use {@link #invokeConstructorFromArgs(Class, Object...)} or one of the variants */ @Deprecated
public static Optional invokeConstructorWithArgs(Class clazz, Object[] argsArray, boolean setAccessible) {
return Reflections.invokeConstructorFromArgs(clazz, argsArray, setAccessible).toOptional();
}
/** @deprecated since 0.10.0 use {@link #invokeConstructorFromArgs(Class, Object...)} or one of the variants */ @Deprecated
public static Optional invokeConstructorWithArgs(Reflections reflections, Class clazz, Object[] argsArray, boolean setAccessible) {
return Reflections.invokeConstructorFromArgs(reflections, clazz, argsArray, setAccessible).toOptional();
}
/** Finds and invokes a suitable constructor, supporting varargs and primitives, boxing and looking at compatible supertypes in the constructor's signature */
public static Maybe invokeConstructorFromArgs(Class clazz, Object...argsArray) {
return invokeConstructorFromArgs(clazz, argsArray, false);
}
/** As {@link #invokeConstructorFromArgs(Class, Object...)} but allowing more configurable input */
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */
public static Maybe