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.
A bundle containing code of all jar-based modules that provide
JAX-RS and Jersey-related features. Such a bundle is *only intended* for
developers that do not use Maven's dependency system.
The bundle does not include code for contributes, tests and samples.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2011 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 com.sun.jersey.core.reflection;
import com.sun.jersey.impl.ImplMessages;
import java.lang.reflect.AccessibleObject;
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.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
/**
* Utility methods for Java reflection.
*
* @author [email protected]
*/
public class ReflectionHelper {
/**
*
*/
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 RuntimeException();
}
}
/**
* 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 '@', 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";
StringBuffer sb = new StringBuffer();
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 '@',
* the unsigned hexadecimal representation of the hash code of the
* object, the character '.', the name of the method,
* the character '(', the list of method parameters, and
* the character ')'. In other words, thos method returns a
* string equal to the value of:
*
*
* @param o the object whose class implements m.
* @param m the method.
* @return the string representation of the method and instance.
*/
public static String methodInstanceToString(Object o, Method m) {
StringBuffer sb = new StringBuffer();
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();
}
StringBuffer sb = new StringBuffer();
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 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 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.
*/
public static Class classForName(String name, ClassLoader cl) {
if (cl != null) {
try {
return Class.forName(name, false, cl);
} catch (ClassNotFoundException ex) {
}
}
try {
return Class.forName(name);
} catch (ClassNotFoundException 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 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 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.
*/
public static Class classForNameWithException(String name, ClassLoader cl)
throws ClassNotFoundException {
if (cl != null) {
try {
return Class.forName(name, false, cl);
} catch (ClassNotFoundException ex) {
}
}
return Class.forName(name);
}
/**
* Get the context class loader.
*
* @return the context class loader, otherwise null security privilages
* are not set.
*/
public static ClassLoader getContextClassLoader() {
return AccessController.doPrivileged(
new PrivilegedAction() {
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