本文整理汇总了Java中java.lang.reflect.AccessibleObject类的典型用法代码示例。如果您正苦于以下问题:Java AccessibleObject类的具体用法?Java AccessibleObject怎么用?Java AccessibleObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AccessibleObject类属于java.lang.reflect包,在下文中一共展示了AccessibleObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setAndGetAccessible
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
/**
* 放回一个可以访问的字段或者方法
*
*/
public static <T extends AccessibleObject> T setAndGetAccessible(T accessible) {
if (accessible == null) {
return null;
}
if (accessible instanceof Member) {
Member member = (Member) accessible;
if (Modifier.isPublic(member.getModifiers())
&& Modifier.isPublic(member.getDeclaringClass().getModifiers())) {
return accessible;
}
}
if (!accessible.isAccessible()) {
accessible.setAccessible(true);
}
return accessible;
}
示例2: setAccessibleWorkaround
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
static boolean setAccessibleWorkaround(final AccessibleObject o) {
if (o == null || o.isAccessible()) {
return false;
}
final Member m = (Member) o;
if (!o.isAccessible() && Modifier.isPublic(m.getModifiers()) && isPackageAccess(m
.getDeclaringClass().getModifiers())) {
try {
o.setAccessible(true);
return true;
} catch (final SecurityException e) { // NOPMD
// ignore in favor of subsequent IllegalAccessException
}
}
return false;
}
示例3: cloneFields
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
protected void cloneFields(Class<? extends WizardControl> clazz, WizardControl control)
{
Field[] declaredFields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(declaredFields, true);
for( Field field : declaredFields )
{
try
{
if( (field.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) == 0 )
{
field.set(control, field.get(this));
}
}
catch( Exception e )
{
throw new RuntimeException(e);
}
}
}
示例4: reflectionAppend
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
/**
* <p>Appends the fields and values defined by the given object of the
* given Class.</p>
*
* @param lhs the left hand object
* @param rhs the right hand object
* @param clazz the class to append details of
* @param builder the builder to append to
* @param useTransients whether to test transient fields
* @param excludeFields array of field names to exclude from testing
*/
private static void reflectionAppend(
Object lhs,
Object rhs,
Class clazz,
EqualsBuilder builder,
boolean useTransients,
String[] excludeFields) {
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length && builder.isEquals; i++) {
Field f = fields[i];
if (!ArrayUtils.contains(excludeFields, f.getName())
&& (f.getName().indexOf('$') == -1)
&& (useTransients || !Modifier.isTransient(f.getModifiers()))
&& (!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
}
}
}
}
示例5: getClassLoader
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
/**
* Enable sharing of the class-loader with 3rd party.
*
* @return the class-loader user be the helper.
*/
public ClassLoader getClassLoader() {
// To follow the same behavior of Class.forName(...) I had to play
// dirty (Supported by Sun, IBM & BEA JVMs)
try {
// Get a reference to this class' class-loader
ClassLoader cl = this.getClass().getClassLoader();
// Create a method instance representing the protected
// getCallerClassLoader method of class ClassLoader
Method mthd = ClassLoader.class.getDeclaredMethod(
"getCallerClassLoader", new Class<?>[0]);
// Make the method accessible.
AccessibleObject.setAccessible(new AccessibleObject[] {mthd}, true);
// Try to get the caller's class-loader
return (ClassLoader)mthd.invoke(cl, new Object[0]);
} catch (Throwable all) {
// Use this class' class-loader
return this.getClass().getClassLoader();
}
}
示例6: getClassLoader
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
/**
* Enable sharing of the class-loader with 3rd party.
*
* @return the class-loader user be the helper.
*/
public ClassLoader getClassLoader() {
// To follow the same behavior of Class.forName(...) I had to play
// dirty (Supported by Sun, IBM & BEA JVMs)
try {
// Get a reference to this class' class-loader
ClassLoader cl = this.getClass().getClassLoader();
// Create a method instance representing the protected
// getCallerClassLoader method of class ClassLoader
Method mthd = ClassLoader.class.getDeclaredMethod(
"getCallerClassLoader", new Class[0]);
// Make the method accessible.
AccessibleObject.setAccessible(new AccessibleObject[] {mthd}, true);
// Try to get the caller's class-loader
return (ClassLoader)mthd.invoke(cl, new Object[0]);
} catch (Exception all) {
// Use this class' class-loader
return this.getClass().getClassLoader();
}
}
示例7: checkAccess
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public static void checkAccess(AccessibleObject accessibleObject, Object obj)
throws IllegalAccessException,
InvocationTargetException,
InstantiationException
{
if (accessibleObject instanceof Field) {
Field field = (Field) accessibleObject;
field.set(obj, 42);
field.get(obj);
} else if (accessibleObject instanceof Method) {
Method method = (Method) accessibleObject;
method.invoke(obj);
} else if (accessibleObject instanceof Constructor) {
Constructor<?> constructor = (Constructor<?>) accessibleObject;
Object[] params = new Object[constructor.getParameterCount()];
constructor.newInstance(params);
}
}
示例8: getReadMember
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
Member getReadMember(Class targetClass) throws NameNotFoundException {
// mapping: public field (same name), method (getAttribute()),
// method (attribute())
List key = new ArrayList();
key.add(targetClass);
key.add(_name);
Member m = (Member) _cache.get(key);
if (m != null)
return m;
m = getReadField(targetClass);
if (m == null)
m = getReadMethod(targetClass);
if (m != null)
_cache.putIfAbsent(key, m);
else
throw new NameNotFoundException(
LocalizedStrings.AttributeDescriptor_NO_PUBLIC_ATTRIBUTE_NAMED_0_WAS_FOUND_IN_CLASS_1
.toLocalizedString(new Object[] {_name, targetClass.getName()}));
// override security for nonpublic derived classes with public members
((AccessibleObject) m).setAccessible(true);
return m;
}
示例9: setAccessibility
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public static void setAccessibility(boolean b) {
if (!b) {
accessibility = false;
} else {
String.class.getDeclaredMethods(); // test basic access
try {
final AccessibleObject member = String.class.getDeclaredField("value");
member.setAccessible(true);
member.setAccessible(false);
} catch (NoSuchFieldException e) {
// ignore
}
accessibility = true;
}
KrineClassManager.clearResolveCache();
}
示例10: getParameterNames
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
private String[] getParameterNames(AccessibleObject a, String name, Class<?>[] types, String raw) {
if (types.length == 0)
return new String[0];
StringBuilder regex = new StringBuilder();
regex.append(format(">\\Q%s\\E</A></(?:B|strong)>\\(", name));
for (Class<?> klass : types) {
regex.append(format(
",?\\s*(?:<A[^>]+>)?[\\w.]*\\Q%s\\E(?:</A>)?(?:<[^&]+>)? ([^),\\s]+)",
klass.getSimpleName()
));
}
regex.append(format("\\)</CODE>"));
Pattern pattern = Pattern.compile(regex.toString(), Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(raw);
if (!matcher.find())
throw new ParameterNamesNotFoundException(a + ", " + regex);
String[] names = new String[types.length];
for (int i = 0 ; i < names.length ; i++)
names[i] = matcher.group(1 + i).trim();
return names;
}
示例11: annotatedName
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
private static String annotatedName(AccessibleObject obj) {
if (obj.isAnnotationPresent(PropertyName.class)) {
PropertyName annotation = obj.getAnnotation(PropertyName.class);
return annotation.value();
}
return null;
}
示例12: lookupParameterNames
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
for (int i = 0; i < paranamers.length; i++) {
Paranamer paranamer = paranamers[i];
String[] names = paranamer.lookupParameterNames(methodOrCtor, i+1 < paranamers.length ? false : throwExceptionIfMissing);
if (names != Paranamer.EMPTY_NAMES) {
return names;
}
}
return Paranamer.EMPTY_NAMES;
}
示例13: MethodIntrospector
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public MethodIntrospector(AccessibleObject methodOrCtor) {
super();
this.methodOrCtor = methodOrCtor;
this.classes = getClasses(methodOrCtor);
this.types = getGenericParameterTypes(methodOrCtor);
this.names = getParameterNames(methodOrCtor);
this.parameterDescriptions = mergeToParameters(names, types);
}
示例14: getJpaAnnotatedMembers
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public List<AccessibleObject> getJpaAnnotatedMembers(Class<?> c, Class<? extends Annotation> annotation) {
final List<AccessibleObject> jpaAnnotated = new ArrayList<AccessibleObject>();
for (Class<?> cl = c; cl != Object.class && cl != null; cl = cl.getSuperclass()) {
parseClass(annotation, jpaAnnotated, cl);
}
return jpaAnnotated;
}
示例15: lookupParameterNames
import java.lang.reflect.AccessibleObject; //导入依赖的package包/类
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
String[] names = methodCache.get(methodOrCtor);
// refer PARANAMER-19
if(names == null) {
names = delegate.lookupParameterNames(methodOrCtor, throwExceptionIfMissing);
methodCache.put(methodOrCtor, names);
}
return names;
}