org.bouncycastle.jsse.provider.ReflectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bctls-lts8on Show documentation
Show all versions of bctls-lts8on Show documentation
The Bouncy Castle Java APIs for the TLS, including a JSSE provider. The APIs are designed primarily to be used in conjunction with the BC LTS provider but may also be used with other providers providing cryptographic services.
package org.bouncycastle.jsse.provider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
class ReflectionUtil
{
static Method findMethod(Method[] methods, String name)
{
if (methods != null)
{
for (Method m : methods)
{
if (m.getName().equals(name))
{
return m;
}
}
}
return null;
}
static boolean hasMethod(Method[] methods, String name)
{
return null != findMethod(methods, name);
}
static Class> getClass(final String className)
{
if (null == className)
{
return null;
}
return AccessController.doPrivileged(new PrivilegedAction>()
{
public Class> run()
{
try
{
ClassLoader classLoader = ReflectionUtil.class.getClassLoader();
Class> clazz = (null == classLoader)
? Class.forName(className)
: classLoader.loadClass(className);
return clazz;
}
catch (Exception e)
{
}
return null;
}
});
}
static Constructor getDeclaredConstructor(final String className, final Class>... parameterTypes)
{
if (null == className)
{
return null;
}
return AccessController.doPrivileged(new PrivilegedAction>()
{
public Constructor run()
{
try
{
ClassLoader classLoader = ReflectionUtil.class.getClassLoader();
@SuppressWarnings("unchecked")
Class clazz = (Class)((null == classLoader)
? Class.forName(className)
: classLoader.loadClass(className));
if (null != clazz)
{
return clazz.getDeclaredConstructor(parameterTypes);
}
}
catch (Exception e)
{
}
return null;
}
});
}
static Method getMethod(final String className, final String methodName, final Class>... parameterTypes)
{
if (null == className || null == methodName)
{
return null;
}
return AccessController.doPrivileged(new PrivilegedAction()
{
public Method run()
{
try
{
ClassLoader classLoader = ReflectionUtil.class.getClassLoader();
Class> clazz = (null == classLoader)
? Class.forName(className)
: classLoader.loadClass(className);
if (null != clazz)
{
return clazz.getMethod(methodName, parameterTypes);
}
}
catch (Exception e)
{
}
return null;
}
});
}
static Method[] getMethods(final String className)
{
if (null == className)
{
return null;
}
return AccessController.doPrivileged(new PrivilegedAction()
{
public Method[] run()
{
try
{
ClassLoader classLoader = ReflectionUtil.class.getClassLoader();
Class> clazz = (null == classLoader)
? Class.forName(className)
: classLoader.loadClass(className);
if (null != clazz)
{
return clazz.getMethods();
}
}
catch (Exception e)
{
}
return null;
}
});
}
static Integer getStaticInt(final String className, final String fieldName)
{
return AccessController.doPrivileged(new PrivilegedAction()
{
public Integer run()
{
try
{
ClassLoader classLoader = ReflectionUtil.class.getClassLoader();
Class> clazz = (null == classLoader)
? Class.forName(className)
: classLoader.loadClass(className);
if (null != clazz)
{
Field field = clazz.getField(fieldName);
if (null != field)
{
Class> fieldType = field.getType();
if (int.class == fieldType)
{
return field.getInt(null);
}
}
}
}
catch (Exception e)
{
}
return null;
}
});
}
static Integer getStaticIntOrDefault(final String className, final String fieldName, int defaultValue)
{
Integer value = getStaticInt(className, fieldName);
return null == value ? defaultValue : value.intValue();
}
static Object invokeGetter(final Object obj, final Method method)
{
return invokeMethod(obj, method);
}
static Object invokeMethod(final Object obj, final Method method, final Object... args)
{
return AccessController.doPrivileged(new PrivilegedAction
© 2015 - 2024 Weber Informatics LLC | Privacy Policy