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.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.jersey.internal.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.internal.OsgiRegistry;
import org.glassfish.jersey.internal.util.collection.ClassTypePair;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
* Utility methods for Java reflection.
*
* @author Paul Sandoz
*/
public class ReflectionHelper {
private static final Logger LOGGER = Logger.getLogger(ReflectionHelper.class.getName());
/**
* Get the declaring class of an accessible object. Supported are {@link Method},
* {@link Field} and {@link Constructor} accessible object types.
*
* @param ao an accessible object.
* @return the declaring class of an accessible object.
* @throws IllegalArgumentException in case the type of the accessible object
* is not supported.
*/
public static Class> getDeclaringClass(AccessibleObject ao) {
if (ao instanceof Method) {
return ((Method) ao).getDeclaringClass();
} else if (ao instanceof Field) {
return ((Field) ao).getDeclaringClass();
} else if (ao instanceof Constructor) {
return ((Constructor) ao).getDeclaringClass();
} else {
throw new IllegalArgumentException("Unsupported accessible object type: " + ao.getClass().getName());
}
}
/**
* Create a string representation of an object.
*
* Returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character {@code '@'}, and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
*
*
* @param o the object.
* @return the string representation of the object.
*/
public static String objectToString(Object o) {
if (o == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).
append('@').append(Integer.toHexString(o.hashCode()));
return sb.toString();
}
/**
* Create a string representation of a method and an instance whose
* class implements the method.
*
* Returns a string consisting of the name of the class of which the object
* is an instance, the at-sign character {@code '@'},
* the unsigned hexadecimal representation of the hash code of the
* object, the character {@code '.'}, the name of the method,
* the character {@code '('}, the list of method parameters, and
* the character {@code ')'}. In other words, those method returns a
* string equal to the value of:
*
*
* @param o the object whose class implements {@code m}.
* @param m the method.
* @return the string representation of the method and instance.
*/
public static String methodInstanceToString(Object o, Method m) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).
append('@').append(Integer.toHexString(o.hashCode())).
append('.').append(m.getName()).append('(');
Class[] params = m.getParameterTypes();
for (int i = 0; i < params.length; i++) {
sb.append(getTypeName(params[i]));
if (i < (params.length - 1)) {
sb.append(",");
}
}
sb.append(')');
return sb.toString();
}
/**
* @param type
* @return
*/
private static String getTypeName(Class> type) {
if (type.isArray()) {
try {
Class> cl = type;
int dimensions = 0;
while (cl.isArray()) {
dimensions++;
cl = cl.getComponentType();
}
StringBuilder sb = new StringBuilder();
sb.append(cl.getName());
for (int i = 0; i < dimensions; i++) {
sb.append("[]");
}
return sb.toString();
} catch (Throwable e) { /*FALLTHRU*/ }
}
return type.getName();
}
/**
* Get the Class from the class name.
*
* The context class loader will be utilized if accessible and non-null.
* Otherwise the defining class loader of this class will
* be utilized.
*
* @param class type.
* @param name the class name.
* @return the Class, otherwise null if the class cannot be found.
*/
public static Class classForName(String name) {
return classForName(name, getContextClassLoader());
}
/**
* Get the Class from the class name.
*
* @param class type.
* @param name the class name.
* @param cl the class loader to use, if null then the defining class loader
* of this class will be utilized.
* @return the Class, otherwise null if the class cannot be found.
*/
@SuppressWarnings("unchecked")
public static Class classForName(String name, ClassLoader cl) {
if (cl != null) {
try {
return (Class) Class.forName(name, false, cl);
} catch (ClassNotFoundException ex) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unable to load class " + name + " using the supplied classloader " + cl.getClass().getName() + ".", ex);
}
}
}
try {
return (Class) Class.forName(name);
} catch (ClassNotFoundException ex) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unable to load class " + name + " using the current classloader.", ex);
}
}
return null;
}
/**
* Get the Class from the class name.
*
* The context class loader will be utilized if accessible and non-null.
* Otherwise the defining class loader of this class will
* be utilized.
*
* @param class type.
* @param name the class name.
* @return the Class, otherwise null if the class cannot be found.
* @throws ClassNotFoundException if the class cannot be found.
*/
public static Class classForNameWithException(String name)
throws ClassNotFoundException {
return classForNameWithException(name, getContextClassLoader());
}
/**
* Get the Class from the class name.
*
* @param class type.
* @param name the class name.
* @param cl the class loader to use, if null then the defining class loader
* of this class will be utilized.
* @return the Class, otherwise null if the class cannot be found.
* @throws ClassNotFoundException if the class cannot be found.
*/
@SuppressWarnings("unchecked")
public static Class classForNameWithException(String name, ClassLoader cl)
throws ClassNotFoundException {
if (cl != null) {
try {
return (Class) Class.forName(name, false, cl);
} catch (ClassNotFoundException ex) {
}
}
return (Class) Class.forName(name);
}
/**
* Get the context class loader.
*
* @return the context class loader, otherwise null security privileges
* are not set.
*/
public static ClassLoader getContextClassLoader() {
return AccessController.doPrivileged(
new PrivilegedAction() {
@Override
public ClassLoader run() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
}
return cl;
}
});
}
/**
* Set a method to be accessible.
*
* @param m the method to be set as accessible
*/
public static void setAccessibleMethod(final Method m) {
if (Modifier.isPublic(m.getModifiers())) {
return;
}
AccessController.doPrivileged(new PrivilegedAction