本文整理匯總了Java中java.lang.reflect.Field.isAccessible方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.isAccessible方法的具體用法?Java Field.isAccessible怎麽用?Java Field.isAccessible使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.isAccessible方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: findField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Field findField(Class<?> originClazz, String name) throws NoSuchFieldException {
for (Class<?> clazz = originClazz; clazz != null; clazz = clazz.getSuperclass()) {
try {
Field field = clazz.getDeclaredField(name);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException e) {
// ignore and search next
}
}
throw new NoSuchFieldException("Field " + name + " not found in " + originClazz);
}
示例2: findField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Field findField(Object instance, String name) throws NoSuchFieldException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Field field = clazz.getDeclaredField(name);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException e) {
// ignore and search next
}
}
throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}
示例3: updateFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* This method is for internal user only.
*/
public void updateFields(T object)
{
long version = getObjectVersion();
RemoteObject<?> other = (RemoteObject<?>)object;
if (version < 0 || other.getObjectVersion() > version) {
//We have invalid version value or the other one is newer.
for (Field f : mFields) {
if (!f.isAccessible()) {
f.setAccessible(true);
}
try {
Object fieldValue = f.get(object);
f.set(this, fieldValue);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
saveSnapshotState();
}
}
示例4: getValueByFieldName
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 獲取obj對象fieldName的屬性值
*
* @param obj
* @param fieldName
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static Object getValueByFieldName(Object obj, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Field field = getFieldByFieldName(obj, fieldName);
Object value = null;
if (field != null) {
if (field.isAccessible()) {
value = field.get(obj);
} else {
field.setAccessible(true);
value = field.get(obj);
field.setAccessible(false);
}
}
return value;
}
示例5: setDevEnvironment
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void setDevEnvironment()
{
try
{
Class<CoreModManager> clazz = CoreModManager.class;
Field f = clazz.getDeclaredField("deobfuscatedEnvironment");
boolean accessibilityFlag = f.isAccessible();
f.setAccessible(true);
isDevEnvironment = f.getBoolean(null);
f.setAccessible(accessibilityFlag);
if (isDevEnvironment)
{
ExPMisc.modLogger.log(LogLevel.Fine, "ExPetrum has detected dev environment! Additional debug features enabled!");
ExPMisc.modLogger.setLevel(LogLevel.Debug);
}
else
{
ExPMisc.modLogger.log(LogLevel.Fine, "ExPetrum has detected normal minecraft environment. No debug features enabled.");
}
}
catch (Exception ex)
{
ExPMisc.modLogger.log(LogLevel.Warning, "ExPetrum was unable to determine the environment it is located in! Assuming normal minecraft instance.");
}
}
示例6: readValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Object readValue(Object instance, Field field) throws ReflectorException {
if (instance == null) {
return null;
}
boolean originalAccessibility = field.isAccessible();
field.setAccessible(true);
try {
return field.get(instance);
} catch (IllegalAccessException e) {
throw new ReflectorException("Unable to manipulate field accessibility.", e);
} finally {
field.setAccessible(originalAccessibility);
}
}
示例7: lookupFieldResource
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Inject resources in specified field.
*
* @param context
* jndi context to extract value from
* @param instance
* object to inject into
* @param field
* field target for injection
* @param name
* jndi name value is bound under
* @param clazz
* class annotation is defined in
* @throws IllegalAccessException
* if field is inaccessible
* @throws javax.naming.NamingException
* if value is not accessible in naming context
*/
protected static void lookupFieldResource(Context context, Object instance, Field field, String name,
Class<?> clazz) throws NamingException, IllegalAccessException {
Object lookedupResource;
boolean accessibility;
String normalizedName = normalize(name);
if ((normalizedName != null) && (normalizedName.length() > 0)) {
lookedupResource = context.lookup(normalizedName);
} else {
lookedupResource = context.lookup(clazz.getName() + "/" + field.getName());
}
synchronized (field) {
accessibility = field.isAccessible();
field.setAccessible(true);
field.set(instance, lookedupResource);
field.setAccessible(accessibility);
}
}
示例8: writeField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static void writeField(@Nullable Object obj, Field field, Object value) {
final boolean wasAccessible = field.isAccessible();
try {
field.setAccessible(true);
field.set(obj, value);
} catch(IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
field.setAccessible(wasAccessible);
}
}
示例9: getFieldEnumOrdinal
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 獲取枚舉類型的字段值-ordinal
*
* @param target the target object from which to get the field
* @param field the field to get
* @return enum.ordinal
* @throws Exception IllegalArgumentException, IllegalAccess
*/
@SuppressWarnings("rawtypes")
public static int getFieldEnumOrdinal(Object target, Field field) throws Exception {
if (field.getType().isEnum()) {
if (!field.isAccessible()) {
ReflectionUtils.makeAccessible(field);
}
return ((Enum) field.get(target)).ordinal();
} else {
throw new ReflectionException(target.getClass().getName() + "." + field.getName()
+ ":field type is not Enum, can not convertToEnum");
}
}
示例10: fixInputMethodManagerLeak
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* InputMethodManager導致的內存泄露
* from: http://blog.csdn.net/sodino/article/details/32188809
* @param destContext
*/
public static void fixInputMethodManagerLeak(Context destContext) {
if (destContext == null) {
return;
}
InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
String[] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"};
Field f = null;
Object obj_get = null;
for (int i = 0;i < arr.length;i ++) {
String param = arr[i];
try{
f = imm.getClass().getDeclaredField(param);
if (f.isAccessible() == false) {
f.setAccessible(true);
} // author: sodino mail:[email protected]
obj_get = f.get(imm);
if (obj_get != null && obj_get instanceof View) {
View v_get = (View) obj_get;
if (v_get.getContext() == destContext) { // 被InputMethodManager持有引用的context是想要目標銷毀的
f.set(imm, null); // 置空,破壞掉path to gc節點
} else {
// 不是想要目標銷毀的,即為又進了另一層界麵了,不要處理,避免影響原邏輯,也就不用繼續for循環了
break;
}
}
}catch(Throwable t){
t.printStackTrace();
}
}
}
示例11: processFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
protected void processFields ( final FieldProcessor processor )
{
for ( final Field field : Reflections.findAllFields ( this.target.getClass () ) )
{
final Message message = field.getAnnotation ( Message.class );
if ( message == null )
{
continue;
}
if ( !field.isAccessible () )
{
field.setAccessible ( true );
try
{
processor.process ( this.target, field, message );
}
finally
{
field.setAccessible ( false );
}
}
else
{
processor.process ( this.target, field, message );
}
}
}
示例12: setFieldValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void setFieldValue(Field field, T object, Object value) throws IllegalArgumentException, IllegalAccessException {
if (field.isAccessible()) {
field.set(object, value);
} else {
field.setAccessible(true);
field.set(object, value);
field.setAccessible(false);
}
}
示例13: makeAccessible
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 改變private/protected的成員變量為public,盡量不調用實際改動的語句,避免JDK的SecurityManager抱怨。
*
* @param field the field
*/
public static void makeAccessible(Field field) {
if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier
.isFinal(field.getModifiers())) && !field.isAccessible()) {
field.setAccessible(true);
}
}
示例14: getField
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Gets the value of a field from an Object
*
* @param object Object that field belongs to
* @param field Field that is being retrieved
* @return The value of the field
*/
public static Object getField(Object object, Field field) {
Object value = null;
try {
boolean accessible = field.isAccessible();
field.setAccessible(true);
value = field.get(object);
field.setAccessible(accessible);
} catch (NullPointerException | IllegalAccessException ignored) {}
return value;
}
示例15: setField
import java.lang.reflect.Field; //導入方法依賴的package包/類
protected void setField(Field field) {
if (!field.isAccessible()) {
try {
field.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
field = null;
}
}
this.field = field;
this.isStatic = Modifier.isStatic(field.getModifiers());
}