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:com.yanzhenjie.recyclerview.swipe.SwipeAdapterWrapper.java

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (mHeaderViews.get(viewType) != null) {
        return new ViewHolder(mHeaderViews.get(viewType));
    } else if (mFootViews.get(viewType) != null) {
        return new ViewHolder(mFootViews.get(viewType));
    }// ww w  .  j ava  2  s .  c  om
    final RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(parent, viewType);

    if (mSwipeItemClickListener != null) {
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeItemClickListener.onItemClick(v, viewHolder.getAdapterPosition());
            }
        });
    }
    if (mSwipeItemLongClickListener != null) {
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mSwipeItemLongClickListener.onItemLongClick(v, viewHolder.getAdapterPosition());
                return true;
            }
        });
    }

    if (mSwipeMenuCreator == null)
        return viewHolder;

    final SwipeMenuLayout swipeMenuLayout = (SwipeMenuLayout) mInflater
            .inflate(R.layout.recycler_swipe_view_item, parent, false);
    SwipeMenu swipeLeftMenu = new SwipeMenu(swipeMenuLayout, viewType);
    SwipeMenu swipeRightMenu = new SwipeMenu(swipeMenuLayout, viewType);

    mSwipeMenuCreator.onCreateMenu(swipeLeftMenu, swipeRightMenu, viewType);

    int leftMenuCount = swipeLeftMenu.getMenuItems().size();
    if (leftMenuCount > 0) {
        SwipeMenuView swipeLeftMenuView = (SwipeMenuView) swipeMenuLayout.findViewById(R.id.swipe_left);
        // noinspection WrongConstant
        swipeLeftMenuView.setOrientation(swipeLeftMenu.getOrientation());
        swipeLeftMenuView.createMenu(swipeLeftMenu, swipeMenuLayout, mSwipeMenuItemClickListener,
                SwipeMenuRecyclerView.LEFT_DIRECTION);
    }

    int rightMenuCount = swipeRightMenu.getMenuItems().size();
    if (rightMenuCount > 0) {
        SwipeMenuView swipeRightMenuView = (SwipeMenuView) swipeMenuLayout.findViewById(R.id.swipe_right);
        // noinspection WrongConstant
        swipeRightMenuView.setOrientation(swipeRightMenu.getOrientation());
        swipeRightMenuView.createMenu(swipeRightMenu, swipeMenuLayout, mSwipeMenuItemClickListener,
                SwipeMenuRecyclerView.RIGHT_DIRECTION);
    }

    ViewGroup viewGroup = (ViewGroup) swipeMenuLayout.findViewById(R.id.swipe_content);
    viewGroup.addView(viewHolder.itemView);

    try {
        Field itemView = getSupperClass(viewHolder.getClass()).getDeclaredField("itemView");
        if (!itemView.isAccessible())
            itemView.setAccessible(true);
        itemView.set(viewHolder, swipeMenuLayout);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return viewHolder;
}

From source file:org.onehippo.forge.utilities.hst.simpleocm.build.NodeBuilderImpl.java

/**
 * Build property/*  ww  w .  java2  s . co  m*/
 *
 * @param node    the node to set / append the property / child node
 * @param relPath the relative path for the new property / child node
 * @param field   the field to get the data / information from to build
 * @param obj     the object the field is bound to
 * @throws ContentNodeBindingException
 */
public void buildProperty(Node node, String relPath, Field field, Object obj)
        throws ContentNodeBindingException {
    try {
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        final List<Class<?>> actualTypeParameters = GenericsUtil.getActualTypeParameters(field);
        final Object value = field.get(obj);
        final Class<?> type = field.getType();
        buildProperty(node, relPath, value, type, actualTypeParameters);
    } catch (IllegalAccessException accessException) {
        throw new ContentNodeBindingException(
                "Error building property for object '" + obj.getClass() + "' relative path '" + relPath + "'",
                accessException);
    } catch (RepositoryException repositoryException) {
        throw new ContentNodeBindingException(
                "Error building property for object '" + obj.getClass() + "' relative path '" + relPath + "'",
                repositoryException);
    }
}

From source file:com.exam.mserver.common.persistence.util.ReflectHelper.java

/**
 * objfieldName//ww w .ja  va  2s. co m
 * 
 * @param obj
 * @param fieldName
 * @param value
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static boolean setValueByFieldName(Object obj, String fieldName, Object value) {
    try {
        // java.lang.Class.getDeclaredField() - Field??Class?
        // Field
        Field field = obj.getClass().getDeclaredField(fieldName);
        /**
         * public void setAccessible(boolean flag)
         * throws SecurityException accessible  true ??? Java  false
         * ?? Java 
         * ? ReflectPermission("suppressAccessChecks") ?? checkPermission 
         *  flag  true?? Class  Constructor ? SecurityException
         *  java.lang.Class  Constructor  flag  true? SecurityException
         * ?
         * flag - accessible 
         * 
         * SecurityException - ?
         */
        if (field.isAccessible()) {// ? accessible 
            field.set(obj, value);// ?? Field 
        } else {
            field.setAccessible(true);
            field.set(obj, value);
            field.setAccessible(false);
        }
        return true;
    } catch (Exception e) {
    }
    return false;
}

From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java

private void setLastAttempt(final long lastAttempt) {
    try {//from  w  ww  . j  a v a 2 s.c  o  m
        // try reflection to be safe for the parent class changing the location
        final Field field = UpdateSite.class.getDeclaredField("lastAttempt");
        final boolean accessible = field.isAccessible();
        try {
            field.setLong(this, lastAttempt);
        } finally {
            if (!accessible) {
                field.setAccessible(false);
            }
        }
    } catch (final Throwable e) {
        // ignore
    }
    this.lastAttempt = lastAttempt;
}

From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java

/**
 * Sets the data timestamp (and tries to propagate the change to {@link UpdateSite#dataTimestamp}
 *
 *//*  www. ja v a 2 s . c om*/
private void setDataTimestamp(final long dataTimestamp) {
    try {
        // try reflection to be safe for the parent class changing the location
        final Field field = UpdateSite.class.getDeclaredField("dataTimestamp");
        final boolean accessible = field.isAccessible();
        try {
            field.setLong(this, dataTimestamp);
        } finally {
            if (!accessible) {
                field.setAccessible(false);
            }
        }
    } catch (final Throwable e) {
        // ignore
    }
    this.dataTimestamp = dataTimestamp;
}

From source file:org.ngrinder.model.BaseEntity.java

/**
 * Merge source entity into current entity.
 *
 * Only not null value is merged./*from   w  ww  . j  a  v a  2 s . c  o m*/
 *
 * @param source merge source
 * @return merged entity
 */
@SuppressWarnings("unchecked")
public M merge(M source) {
    Field forDisplay = null;
    try {
        Field[] fields = getClass().getDeclaredFields();
        // Iterate over all the attributes
        for (Field each : fields) {
            if (each.isSynthetic()) {
                continue;
            }
            final int modifiers = each.getModifiers();
            if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
                continue;
            }
            forDisplay = each;
            if (!each.isAccessible()) {
                each.setAccessible(true);
            }
            final Object value = each.get(source);
            if (value != null) {
                each.set(this, value);
            }
        }
        return (M) this;
    } catch (Exception e) {
        String displayName = (forDisplay == null) ? "Empty" : forDisplay.getName();
        throw processException(
                displayName + " - Exception occurred while merging an entity from " + source + " to " + this,
                e);
    }
}

From source file:com.nonninz.robomodel.RoboModel.java

@Override
public String toString() {
    final List<Field> fields = getSavedFields();

    final StringBuilder b = new StringBuilder();
    b.append(getTableName() + " {id: " + getId() + ", ");
    String fieldName;/*from   w  w w. j a  v a 2 s . c  o m*/
    for (final Field f : fields) {
        fieldName = f.getName();
        final boolean accessible = f.isAccessible();
        f.setAccessible(true);
        try {
            b.append(fieldName + ": " + f.get(this) + ", ");
        } catch (final IllegalAccessException e) {
            b.append(fieldName + ": (INACCESSIBLE), ");
        }
        f.setAccessible(accessible);
    }
    b.append("}");

    return b.toString();
}

From source file:com.palantir.ptoss.cinch.core.BindingContext.java

/**
 * Look through all of the declared, static, final fields of the context object, grab the value,
 * and insert a mapping from the field's name to the object.
 *
 * Note that this will index non-public fields.
 *
 * @return the bindable constants map/*  www .  j a v  a  2 s  .  c  o m*/
 * @throws IllegalArgumentException on reflection error
 * @throws IllegalAccessException on reflection error
 */
private Map<String, Object> indexBindableConstants() throws IllegalArgumentException, IllegalAccessException {
    Map<String, Object> map = Maps.newHashMap();
    for (Field field : object.getClass().getDeclaredFields()) {
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        if (Reflections.isFieldFinal(field) && Reflections.isFieldStatic(field)) {
            map.put(field.getName(), field.get(object));
        }
        field.setAccessible(accessible);
    }
    return map;
}

From source file:de.codesourcery.eve.skills.util.SpringBeanInjector.java

private void doInjectDependencies(Object obj)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    Class<?> currentClasz = obj.getClass();
    do {//from   www.  j  a  v a 2 s. c  om
        // inject fields
        for (Field f : currentClasz.getDeclaredFields()) {

            final int m = f.getModifiers();
            if (Modifier.isStatic(m) || Modifier.isFinal(m)) {
                continue;
            }

            final Resource annot = f.getAnnotation(Resource.class);
            if (annot != null) {
                if (!f.isAccessible()) {
                    f.setAccessible(true);
                }

                if (log.isTraceEnabled()) {
                    log.trace("doInjectDependencies(): Setting field " + f.getName() + " with bean '"
                            + annot.name() + "'");
                }
                f.set(obj, getBean(annot.name()));
            }
        }

        // inject methods
        for (Method method : currentClasz.getDeclaredMethods()) {

            final int m = method.getModifiers();
            if (Modifier.isStatic(m) || Modifier.isAbstract(m)) {
                continue;
            }

            if (method.getParameterTypes().length != 1) {
                continue;
            }

            if (!method.getName().startsWith("set")) {
                continue;
            }

            final Resource annot = method.getAnnotation(Resource.class);
            if (annot != null) {
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }

                if (log.isTraceEnabled()) {
                    log.trace("doInjectDependencies(): Invoking setter method " + method.getName()
                            + " with bean '" + annot.name() + "'");
                }

                method.invoke(obj, getBean(annot.name()));
            }
        }

        currentClasz = currentClasz.getSuperclass();

    } while (currentClasz != null);
}

From source file:com.cloudbees.jenkins.plugins.enterpriseplugins.CloudBeesUpdateSite.java

private void setLastAttempt(long lastAttempt) {
    try {//from  www  . j a v  a2 s. c  om
        // try reflection to be safe for the parent class changing the location
        Field field = UpdateSite.class.getDeclaredField("lastAttempt");
        boolean accessible = field.isAccessible();
        try {
            field.setLong(this, lastAttempt);
        } finally {
            if (!accessible) {
                field.setAccessible(false);
            }
        }
    } catch (Throwable e) {
        // ignore
    }
    this.lastAttempt = lastAttempt;
}