Example usage for java.lang.reflect Field isAccessible

List of usage examples for java.lang.reflect Field isAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field isAccessible.

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:org.seasar.mayaa.impl.cycle.script.rhino.RhinoUtil.java

/**
 * public??????//from   ww w. ja  va 2  s. co  m
 *
 * @param bean 
 * @param propertyName ??
 * @return ?????null
 */
protected static Object getFromPublicField(Object bean, String propertyName) {
    try {
        // TODO Field
        Class beanClass = bean.getClass();
        Field field = beanClass.getField(propertyName);
        if (Modifier.isPublic(field.getModifiers())) {
            if (field.isAccessible() == false) {
                field.setAccessible(true);
            }
            return field.get(bean);
        }
    } catch (SecurityException ignore) {
        // no-op
    } catch (NoSuchFieldException ignore) {
        // no-op
    } catch (IllegalArgumentException ignore) {
        // no-op
    } catch (IllegalAccessException eignore) {
        // no-op
    }

    return null;
}

From source file:org.fastmongo.odm.dbobject.mapping.core.ConverterHelper.java

/**
 * Make the given field accessible if it wasn't.
 *
 * @param field the field//  ww w .ja v  a2  s .com
 */
private static void makeFieldAccessible(Field field) {
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
}

From source file:io.github.benas.randombeans.util.ReflectionUtils.java

/**
 * Get the value (accessible or not accessible) of a field of a target object.
 *
 * @param object instance to get the field of
 * @param field  field to get the value of
 * @throws IllegalAccessException if field can not be accessed
 *///from   w w w.  j  a  v a2  s. c om
public static Object getFieldValue(final Object object, final Field field) throws IllegalAccessException {
    boolean access = field.isAccessible();
    field.setAccessible(true);
    Object value = field.get(object);
    field.setAccessible(access);
    return value;
}

From source file:org.openmrs.order.OrderUtilTest.java

public static void setDateStopped(Order targetOrder, Date dateStopped) throws Exception {
    Field field = null;
    Boolean isAccessible = null;//from   w w w .  j a  v a2 s  .  co  m
    try {
        field = Order.class.getDeclaredField("dateStopped");
        isAccessible = field.isAccessible();
        if (!isAccessible) {
            field.setAccessible(true);
        }
        field.set(targetOrder, dateStopped);
    } finally {
        if (field != null && isAccessible != null) {
            field.setAccessible(isAccessible);
        }
    }
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Fetches the fields on a class that are annotated with SpringBean. The first time it
 * is called for a particular class it will introspect the class and cache the results.
 * All non-overridden fields are examined, including protected and private fields.
 * If a field is not public an attempt it made to make it accessible - if it fails
 * it is removed from the collection and an error is logged.
 *
 * @param clazz the class on which to look for SpringBean annotated fields
 * @return the collection of methods with the annotation
 *///  ww w  .j  av  a  2  s.  c om
protected static Collection<Field> getFields(Class<?> clazz) {
    Collection<Field> fields = fieldMap.get(clazz);
    if (fields == null) {
        fields = ReflectUtil.getFields(clazz);
        Iterator<Field> iterator = fields.iterator();

        while (iterator.hasNext()) {
            Field field = iterator.next();
            if (!field.isAnnotationPresent(SpringBean.class)) {
                iterator.remove();
            } else if (!field.isAccessible()) {
                // If the field isn't public, try to make it accessible
                try {
                    field.setAccessible(true);
                } catch (SecurityException se) {
                    throw new StripesRuntimeException("Field " + clazz.getName() + "." + field.getName()
                            + "is marked " + "with @SpringBean and is not public. An attempt to call "
                            + "setAccessible(true) resulted in a SecurityException. Please "
                            + "either make the field public, annotate a public setter instead "
                            + "or modify your JVM security policy to allow Stripes to "
                            + "setAccessible(true).", se);
                }
            }
        }

        fieldMap.put(clazz, fields);
    }

    return fields;
}

From source file:com.siberhus.ngai.core.CrudHelper.java

/**
 * //from  w ww .ja va2 s  .co m
 * @param entityClass
 * @return
 */
private synchronized final static EntityInfo getEntityInfo(Class<?> entityClass) {

    EntityInfo entityInfo = ENTITY_INFO_CACHE.get(entityClass);
    if (entityInfo != null) {
        return entityInfo;
    }
    entityInfo = new EntityInfo();
    Entity entityAnnot = (Entity) entityClass.getAnnotation(Entity.class);
    if (!"".equals(entityAnnot.name())) {
        entityInfo.setEntityName(entityAnnot.name());
    } else {
        entityInfo.setEntityName(entityClass.getSimpleName());
    }
    Collection<Field> entityFields = ReflectUtil.getFields(entityClass);
    for (Field field : entityFields) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        if (field.getName().equals("id")) {
            continue;
        }
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        entityInfo.getFieldSet().add(field);
    }
    ENTITY_INFO_CACHE.put(entityClass, entityInfo);
    return entityInfo;
}

From source file:Main.java

private static boolean setProxyPreICS(WebView webView, String host, final int port) {
    HttpHost proxyServer;/*  ww w. jav  a 2s .  c  o  m*/
    if (null == host) {
        proxyServer = null;
    } else {
        proxyServer = new HttpHost(host, port);
    }
    Class networkClass = null;
    Object network = null;
    try {
        networkClass = Class.forName("android.webkit.Network");
        if (networkClass == null) {
            Log.e(LOG_TAG, "failed to get class for android.webkit.Network");
            return false;
        }
        Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class);
        if (getInstanceMethod == null) {
            Log.e(LOG_TAG, "failed to get getInstance method");
        }
        network = getInstanceMethod.invoke(networkClass, new Object[] { webView.getContext() });
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting network: " + ex);
        return false;
    }
    if (network == null) {
        Log.e(LOG_TAG, "error getting network: network is null");
        return false;
    }
    Object requestQueue = null;
    try {
        Field requestQueueField = networkClass.getDeclaredField("mRequestQueue");
        requestQueue = getFieldValueSafely(requestQueueField, network);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting field value");
        return false;
    }
    if (requestQueue == null) {
        Log.e(LOG_TAG, "Request queue is null");
        return false;
    }
    Field proxyHostField = null;
    try {
        Class requestQueueClass = Class.forName("android.net.http.RequestQueue");
        proxyHostField = requestQueueClass.getDeclaredField("mProxyHost");
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting proxy host field");
        return false;
    }

    boolean temp = proxyHostField.isAccessible();
    try {
        proxyHostField.setAccessible(true);
        proxyHostField.set(requestQueue, proxyServer);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error setting proxy host");
    } finally {
        proxyHostField.setAccessible(temp);
    }
    Log.d(LOG_TAG, "Setting proxy with <= 3.2 API successful!");
    return true;
}

From source file:com.kangyonggan.cms.util.Reflections.java

/**
 * ?private/protected???public?????JDKSecurityManager
 *///from w ww.  ja va2  s. c o m
public static void makeAccessible(Field field) {
    boolean temp = (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
            || Modifier.isFinal(field.getModifiers())) && !field.isAccessible();
    if (temp) {
        field.setAccessible(true);
    }
}

From source file:com.qhn.bhne.xhmusic.utils.MyUtils.java

/**
 * InputMethodManager/*from   w w  w. j  ava 2 s .  c  o  m*/
 */
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;
    Object obj_get;
    for (String param : arr) {
        try {
            f = imm.getClass().getDeclaredField(param);
            if (!f.isAccessible()) {
                f.setAccessible(true);
            } // author: sodino mail:sodino@qq.com
            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
                    /*if (QLog.isColorLevel()) {
                    QLog.d(ReflecterHelper.class.getSimpleName(), QLog.CLR, "fixInputMethodManagerLeak break, context is not suitable, get_context=" + v_get.getContext()+" dest_context=" + destContext);
                    }*/
                    break;
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

From source file:truco.plugin.utils.Utils.java

public static void setHeaderAndFooter(Player player, String headers, String footers) {
    CraftPlayer craftplayer = (CraftPlayer) player;
    PlayerConnection connection = craftplayer.getHandle().playerConnection;
    IChatBaseComponent header = ChatSerializer.a("{text:\"" + headers + "\"}");
    IChatBaseComponent footer = ChatSerializer.a("{text:\"" + footers + "\"}");
    PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();

    try {// w  w  w .  java  2 s .  co  m
        Field headerField = packet.getClass().getDeclaredField("a");
        headerField.setAccessible(true);
        headerField.set(packet, header);
        headerField.setAccessible(!headerField.isAccessible());

        Field footerField = packet.getClass().getDeclaredField("b");
        footerField.setAccessible(true);
        footerField.set(packet, footer);
        footerField.setAccessible(!footerField.isAccessible());
    } catch (Exception e) {
        e.printStackTrace();
    }

    connection.sendPacket(packet);
}