當前位置: 首頁>>代碼示例>>Java>>正文


Java Field.isAccessible方法代碼示例

本文整理匯總了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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:ShareReflectUtil.java

示例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());
}
 
開發者ID:alibaba,項目名稱:atlas,代碼行數:18,代碼來源:NClassLoader.java

示例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();
    }
}
 
開發者ID:suomenriistakeskus,項目名稱:oma-riista-android,代碼行數:26,代碼來源:RemoteObject.java

示例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;
}
 
開發者ID:PekingGo,項目名稱:ipayquery,代碼行數:27,代碼來源:ReflectHelper.java

示例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.");
	}
}
 
開發者ID:V0idWa1k3r,項目名稱:ExPetrum,代碼行數:26,代碼來源:ExPetrum.java

示例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);
    }
}
 
開發者ID:iFanie,項目名稱:ChocoPie,代碼行數:17,代碼來源:Reflector.java

示例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);
	}
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:40,代碼來源:DefaultInstanceManager.java

示例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);
    }
}
 
開發者ID:OvercastNetwork,項目名稱:ProjectAres,代碼行數:12,代碼來源:Fields.java

示例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");
    }
}
 
開發者ID:geeker-lait,項目名稱:tasfe-framework,代碼行數:21,代碼來源:EnumFieldReflectUtil.java

示例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();
        }
    }
}
 
開發者ID:weimin96,項目名稱:shareNote,代碼行數:41,代碼來源:LeakUtil.java

示例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 );
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:29,代碼來源:AnnotationMessageProcessor.java

示例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);
    }
}
 
開發者ID:stoiandan,項目名稱:OnlineShop,代碼行數:10,代碼來源:CSVProcessor.java

示例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);
    }
}
 
開發者ID:ruyangit,項目名稱:angit,代碼行數:12,代碼來源:Reflections.java

示例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;
}
 
開發者ID:ImpactDevelopment,項目名稱:ClientAPI,代碼行數:18,代碼來源:ReflectionUtils.java

示例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());
}
 
開發者ID:Borlea,項目名稱:EchoPet,代碼行數:13,代碼來源:SafeField.java


注:本文中的java.lang.reflect.Field.isAccessible方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。