本文整理匯總了Java中java.lang.reflect.AccessibleObject.setAccessible方法的典型用法代碼示例。如果您正苦於以下問題:Java AccessibleObject.setAccessible方法的具體用法?Java AccessibleObject.setAccessible怎麽用?Java AccessibleObject.setAccessible使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.AccessibleObject
的用法示例。
在下文中一共展示了AccessibleObject.setAccessible方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: referenceFieldsOf
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
Collection<Field> referenceFieldsOf(Class<?> k) {
Collection<Field> fields = classFields.get(k);
if (fields == null) {
fields = new ArrayDeque<Field>();
ArrayDeque<Field> allFields = new ArrayDeque<>();
for (Class<?> c = k; c != null; c = c.getSuperclass())
for (Field field : c.getDeclaredFields())
if (!Modifier.isStatic(field.getModifiers())
&& !field.getType().isPrimitive())
fields.add(field);
AccessibleObject.setAccessible(
fields.toArray(new AccessibleObject[0]), true);
classFields.put(k, fields);
}
return fields;
}
示例2: 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");
}
}
}
}
示例3: 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();
}
}
示例4: 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();
}
示例5: trySetAccessible
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
static void trySetAccessible(AccessibleObject ao, boolean shouldSucceed) {
try {
ao.setAccessible(true);
assertTrue(shouldSucceed);
} catch (Exception e) {
assertFalse(shouldSucceed);
}
}
示例6: getAllConstructorsOfClass
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* 獲取某個類型的所有構造方法
*
* @param clazz 類型
* @param accessible 是否可以訪問
* @return 構造方法數組
*/
private static Constructor<?>[] getAllConstructorsOfClass(final Class<?> clazz, boolean accessible) {
if (clazz == null) {
return null;
}
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors != null && constructors.length > 0) {
AccessibleObject.setAccessible(constructors, accessible);
}
return constructors;
}
示例7: tryToMakeAccessible
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
@Override
protected boolean tryToMakeAccessible(AccessibleObject accessible)
{
if (accessible.isAccessible()) {
return true;
}
try {
accessible.setAccessible(true);
} catch (Exception ex) { }
return accessible.isAccessible();
}
示例8: testForwarding
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* Tests that the forwarding wrapper returned by {@code wrapperFunction} properly forwards
* method calls with parameters passed as is, return value returned as is, and exceptions
* propagated as is.
*/
public <T> void testForwarding(
Class<T> interfaceType, Function<? super T, ? extends T> wrapperFunction) {
checkNotNull(wrapperFunction);
checkArgument(interfaceType.isInterface(), "%s isn't an interface", interfaceType);
Method[] methods = getMostConcreteMethods(interfaceType);
AccessibleObject.setAccessible(methods, true);
for (Method method : methods) {
// Under java 8, interfaces can have default methods that aren't abstract.
// No need to verify them.
// Can't check isDefault() for JDK 7 compatibility.
if (!Modifier.isAbstract(method.getModifiers())) {
continue;
}
// The interface could be package-private or private.
// filter out equals/hashCode/toString
if (method.getName().equals("equals")
&& method.getParameterTypes().length == 1
&& method.getParameterTypes()[0] == Object.class) {
continue;
}
if (method.getName().equals("hashCode")
&& method.getParameterTypes().length == 0) {
continue;
}
if (method.getName().equals("toString")
&& method.getParameterTypes().length == 0) {
continue;
}
testSuccessfulForwarding(interfaceType, method, wrapperFunction);
testExceptionPropagation(interfaceType, method, wrapperFunction);
}
if (testsEquals) {
testEquals(interfaceType, wrapperFunction);
}
testToString(interfaceType, wrapperFunction);
}
示例9: setAccessible
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
static void setAccessible(AccessibleObject obj) {
if (setAccessibleEnable && !obj.isAccessible()) {
try {
obj.setAccessible(true);
} catch (AccessControlException e) {
setAccessibleEnable = false;
}
}
}
示例10: setAccessibleWorkaround
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* XXX Default access superclass workaround
*
* When a public class has a default access superclass with public members,
* these members are accessible. Calling them from compiled code works fine.
* Unfortunately, on some JVMs, using reflection to invoke these members
* seems to (wrongly) to prevent access even when the modifer is public.
* Calling setAccessible(true) solves the problem but will only work from
* sufficiently privileged code. Better workarounds would be gratefully
* accepted.
* @param o the AccessibleObject to set as accessible
*/
static void setAccessibleWorkaround(AccessibleObject o) {
if (o == null || o.isAccessible()) {
return;
}
Member m = (Member) o;
if (Modifier.isPublic(m.getModifiers())
&& isPackageAccess(m.getDeclaringClass().getModifiers())) {
try {
o.setAccessible(true);
} catch (SecurityException e) {
// ignore in favor of subsequent IllegalAccessException
}
}
}
示例11: reflectionAppend
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* <p>
* Appends the fields and values defined by the given object of the given <code>Class</code>.
* </p>
*
* @param object
* the object to append details of
* @param clazz
* the class to append details of
* @param builder
* the builder to append to
* @param useTransients
* whether to use transient fields
* @param excludeFields
* Collection of String field names to exclude from use in calculation of hash code
*/
private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder builder, boolean useTransients,
String[] excludeFields) {
if (isRegistered(object)) {
return;
}
try {
register(object);
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (!ArrayUtils.contains(excludeFields, field.getName())
&& (field.getName().indexOf('$') == -1)
&& (useTransients || !Modifier.isTransient(field.getModifiers()))
&& (!Modifier.isStatic(field.getModifiers()))) {
try {
Object fieldValue = field.get(object);
builder.append(fieldValue);
} 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");
}
}
}
} finally {
unregister(object);
}
}
示例12: appendFieldsIn
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* <p>
* Appends the fields and values defined by the given object of the given Class.
* </p>
*
* <p>
* If a cycle is detected as an object is "toString()'ed", such an object is rendered as if
* <code>Object.toString()</code> had been called and not implemented by the object.
* </p>
*
* @param clazz
* The class of object parameter
*/
protected void appendFieldsIn(Class clazz) {
if (clazz.isArray()) {
this.reflectionAppendArray(this.getObject());
return;
}
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
if (this.accept(field)) {
try {
// Warning: Field.get(Object) creates wrappers objects
// for primitive types.
Object fieldValue = this.getValue(field);
this.append(fieldName, fieldValue);
} catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception
// instead
//throw a runtime exception in case the impossible
// happens.
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
}
}
}
}
示例13: reflectionAppend
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* <p>Appends to <code>builder</code> the comparison of <code>lhs</code>
* to <code>rhs</code> using the fields defined in <code>clazz</code>.</p>
*
* @param lhs left-hand object
* @param rhs right-hand object
* @param clazz <code>Class</code> that defines fields to be compared
* @param builder <code>CompareToBuilder</code> to append to
* @param useTransients whether to compare transient fields
* @param excludeFields fields to exclude
*/
private static void reflectionAppend(
Object lhs,
Object rhs,
Class clazz,
CompareToBuilder builder,
boolean useTransients,
String[] excludeFields) {
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length && builder.comparison == 0; 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");
}
}
}
}
示例14: setAccessible
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
static void setAccessible(AccessibleObject obj) {
if (!setAccessibleEnable) {
return;
}
if (obj.isAccessible()) {
return;
}
try {
obj.setAccessible(true);
} catch (AccessControlException error) {
setAccessibleEnable = false;
}
}
示例15: resultSingle
import java.lang.reflect.AccessibleObject; //導入方法依賴的package包/類
/**
* 根據類型將請求中的參數封裝成一個對象並返回
* @param clazz httpBean中方法參數的類型
* @param index 用於resultList()方法標識索引位置
* @return
* @throws Exception
*/
private Object resultSingle(Class<?> clazz, int index) throws Exception {
if (cycleNumber >= 10) {
cycleNumber++;
return null;
}
Object instance = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (Field field : fields) {
if (BasicUtil.isCollection(clazz)) {
instance = resultList(field.getGenericType());
continue;
}
String[] vals = paramMap.get(field.getName());
if (vals == null || index >= vals.length)
continue;
String param = vals[index];
if (BasicUtil.isCommonType(field))
field.set(instance, BasicUtil.convert(field, param));
else if (field.getType() == Date.class) {
dateFormatCheck(format);
Date date = DateUtil.resolve(param, format);
field.set(instance, date);
}
else {
resultSingle(field.getType(), 0);
}
}
return instance;
}