📄 ognlruntime.java
字号:
}
/**
Returns the package name of the object's class.
*/
public static String getPackageName(Object o)
{
return (o == null) ? null : getClassPackageName(o.getClass());
}
/**
Returns the package name of the class given.
*/
public static String getClassPackageName(Class c)
{
String s = c.getName();
int i = s.lastIndexOf('.');
return (i < 0) ? null : s.substring(0, i);
}
/**
Returns a "pointer" string in the usual format for these
things - 0x<hex digits>.
*/
public static String getPointerString(int num)
{
StringBuffer result = new StringBuffer();
String hex = Integer.toHexString(num),
pad;
Integer l = new Integer(hex.length());
//result.append(HEX_PREFIX);
if ((pad = (String)HEX_PADDING.get(l)) == null) {
StringBuffer pb = new StringBuffer();
for (int i = hex.length(); i < HEX_LENGTH; i++) {
pb.append('0');
}
pad = new String(pb);
HEX_PADDING.put(l, pad);
}
result.append(pad);
result.append(hex);
return new String(result);
}
/**
Returns a "pointer" string in the usual format for these
things - 0x<hex digits> for the object given. This will
always return a unique value for each object.
*/
public static String getPointerString(Object o)
{
return getPointerString((o == null) ? 0 : System.identityHashCode(o));
}
/**
Returns a unique descriptor string that includes the object's
class and a unique integer identifier. If fullyQualified is
true then the class name will be fully qualified to include
the package name, else it will be just the class' base name.
*/
public static String getUniqueDescriptor(Object object, boolean fullyQualified)
{
StringBuffer result = new StringBuffer();
if (object != null) {
if (object instanceof Proxy) {
Class interfaceClass = object.getClass().getInterfaces()[0];
result.append(getClassName(interfaceClass, fullyQualified));
result.append('^');
object = Proxy.getInvocationHandler(object);
}
result.append(getClassName(object, fullyQualified));
result.append('@');
result.append(getPointerString(object));
} else {
result.append(NULL_OBJECT_STRING);
}
return new String(result);
}
/**
Returns a unique descriptor string that includes the object's
class' base name and a unique integer identifier.
*/
public static String getUniqueDescriptor(Object object)
{
return getUniqueDescriptor(object, false);
}
/**
Utility to convert a List into an Object[] array. If the list is zero
elements this will return a constant array; toArray() on List always
returns a new object and this is wasteful for our purposes.
*/
public static Object[] toArray(List list)
{
Object[] result;
int size = list.size();
if (size == 0) {
result = NoArguments;
} else {
result = getObjectArrayPool().create(list.size());
for (int i = 0; i < size; i++) {
result[i] = list.get(i);
}
}
return result;
}
/**
Returns the parameter types of the given method.
*/
public static Class[] getParameterTypes(Method m)
{
synchronized(methodParameterTypesCache) {
Class[] result;
if ((result = (Class[])methodParameterTypesCache.get(m)) == null) {
methodParameterTypesCache.put(m, result = m.getParameterTypes());
}
return result;
}
}
/**
Returns the parameter types of the given method.
*/
public static Class[] getParameterTypes(Constructor c)
{
synchronized(ctorParameterTypesCache) {
Class[] result;
if ((result = (Class[])ctorParameterTypesCache.get(c)) == null) {
ctorParameterTypesCache.put(c, result = c.getParameterTypes());
}
return result;
}
}
/**
* Gets the SecurityManager that OGNL uses to determine permissions for
* invoking methods.
*
* @return SecurityManager for OGNL
*/
public static SecurityManager getSecurityManager()
{
return securityManager;
}
/**
* Sets the SecurityManager that OGNL uses to determine permissions for
* invoking methods.
*
* @param value SecurityManager to set
*/
public static void setSecurityManager(SecurityManager value)
{
securityManager = value;
}
/**
Permission will be named "invoke.<declaring-class>.<method-name>".
*/
public static Permission getPermission(Method method)
{
Permission result = null;
Class mc = method.getDeclaringClass();
synchronized(invokePermissionCache) {
Map permissions = (Map)invokePermissionCache.get(mc);
if (permissions == null) {
invokePermissionCache.put(mc, permissions = new HashMap(101));
}
if ((result = (Permission)permissions.get(method.getName())) == null) {
result = new OgnlInvokePermission("invoke." + mc.getName() + "." + method.getName());
permissions.put(method.getName(), result);
}
}
return result;
}
public static Object invokeMethod( Object target, Method method, Object[] argsArray ) throws InvocationTargetException, IllegalAccessException
{
Object result;
boolean wasAccessible = true;
if (securityManager != null) {
try {
securityManager.checkPermission(getPermission(method));
} catch (SecurityException ex) {
throw new IllegalAccessException("Method [" + method + "] cannot be accessed.");
}
}
if (!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
if (!(wasAccessible = ((AccessibleObject)method).isAccessible())) {
((AccessibleObject)method).setAccessible(true);
}
}
result = method.invoke( target, argsArray );
if (!wasAccessible) {
((AccessibleObject)method).setAccessible(false);
}
return result;
}
/**
* Gets the class for a method argument that is appropriate for looking up methods
* by reflection, by looking for the standard primitive wrapper classes and
* exchanging for them their underlying primitive class objects. Other classes are
* passed through unchanged.
*
* @param arg an object that is being passed to a method
* @return the class to use to look up the method
*/
public static final Class getArgClass( Object arg )
{
if ( arg == null )
return null;
Class c = arg.getClass();
if ( c == Boolean.class )
return Boolean.TYPE;
else if ( c.getSuperclass() == Number.class ) {
if ( c == Integer.class )
return Integer.TYPE;
if ( c == Double.class )
return Double.TYPE;
if ( c == Byte.class )
return Byte.TYPE;
if ( c == Long.class )
return Long.TYPE;
if ( c == Float.class )
return Float.TYPE;
if ( c == Short.class )
return Short.TYPE;
}
else if ( c == Character.class )
return Character.TYPE;
return c;
}
/**
* Tells whether the given object is compatible with the given class
* ---that is, whether the given object can be passed as an argument
* to a method or constructor whose parameter type is the given class.
* If object is null this will return true because null is compatible
* with any type.
*/
public static final boolean isTypeCompatible( Object object, Class c )
{
boolean result = true;
if ( object != null ) {
if ( c.isPrimitive() ) {
if ( getArgClass(object) != c ) {
result = false;
}
} else if ( !c.isInstance(object) ) {
result = false;
}
}
return result;
}
/**
* Tells whether the given array of objects is compatible with the given array of
* classes---that is, whether the given array of objects can be passed as arguments
* to a method or constructor whose parameter types are the given array of classes.
*/
public static final boolean areArgsCompatible( Object[] args, Class[] classes )
{
boolean result = true;
if ( args.length != classes.length ) {
result = false;
} else {
for ( int index=0, count=args.length; result && (index < count); ++index ) {
result = isTypeCompatible(args[index], classes[index]);
}
}
return result;
}
/**
* Tells whether the first array of classes is more specific than the second.
* Assumes that the two arrays are of the same length.
*/
public static final boolean isMoreSpecific( Class[] classes1, Class[] classes2 )
{
for ( int index=0, count=classes1.length; index < count; ++index )
{
Class c1 = classes1[index], c2 = classes2[index];
if ( c1 == c2 )
continue;
else if ( c1.isPrimitive() )
return true;
else if ( c1.isAssignableFrom(c2) )
return false;
else if ( c2.isAssignableFrom(c1) )
return true;
}
// They are the same! So the first is not more specific than the second.
return false;
}
public static final String getModifierString(int modifiers)
{
String result;
if (Modifier.isPublic(modifiers))
result = "public";
else
if (Modifier.isProtected(modifiers))
result = "protected";
else
if (Modifier.isPrivate(modifiers))
result = "private";
else
result = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -