本文整理匯總了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;
}