Example usage for java.lang.reflect AccessibleObject setAccessible

List of usage examples for java.lang.reflect AccessibleObject setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect AccessibleObject setAccessible.

Prototype

@CallerSensitive 
public void setAccessible(boolean flag) 

Source Link

Document

Set the accessible flag for this reflected object to the indicated boolean value.

Usage

From source file:Utils.java

public static void makeAccessible(final AccessibleObject object) {
    if (!object.isAccessible()) {
        if (System.getSecurityManager() == null) {
            object.setAccessible(true);
        } else {//from  w w  w.  j ava  2  s  .  c  om
            AccessController.doPrivileged(new PrivilegedAction<Object>() {

                public Object run() {
                    object.setAccessible(true);
                    return null;
                }
            });
        }
    }
}

From source file:Main.java

public static void checkAndFixAccess(Member paramMember) {
    AccessibleObject localAccessibleObject = (AccessibleObject) paramMember;
    try {/*w  ww.ja v  a2 s  .  c o m*/
        localAccessibleObject.setAccessible(true);
        return;
    } catch (SecurityException localSecurityException) {
        while (localAccessibleObject.isAccessible())
            ;
        Class localClass = paramMember.getDeclaringClass();
        throw new IllegalArgumentException("Can not access " + paramMember + " (from class "
                + localClass.getName() + "; failed to set access: " + localSecurityException.getMessage());
    }
}

From source file:ch.algotrader.util.FieldUtil.java

private static void setAccessible(final AccessibleObject object) {

    if (object.isAccessible())
        return;//from ww  w .j  ava  2s  .  co  m

    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        object.setAccessible(true);
        return null;
    });
}

From source file:Main.java

/**
 * Method called to check if we can use the passed method or constructor
 * (wrt access restriction -- public methods can be called, others
 * usually not); and if not, if there is a work-around for
 * the problem.//w w w  .  j a  v  a 2  s.c o m
 */
public static void checkAndFixAccess(Member member) {
    // We know all members are also accessible objects...
    AccessibleObject ao = (AccessibleObject) member;

    /* 14-Jan-2009, tatu: It seems safe and potentially beneficial to
     *   always to make it accessible (latter because it will force
     *   skipping checks we have no use for...), so let's always call it.
     */
    //if (!ao.isAccessible()) {
    try {
        ao.setAccessible(true);
    } catch (SecurityException se) {
        /* 17-Apr-2009, tatu: Related to [JACKSON-101]: this can fail on
         *    platforms like EJB and Google App Engine); so let's
         *    only fail if we really needed it...
         */
        if (!ao.isAccessible()) {
            Class<?> declClass = member.getDeclaringClass();
            throw new IllegalArgumentException("Can not access " + member + " (from class "
                    + declClass.getName() + "; failed to set access: " + se.getMessage());
        }
    }
    //}
}

From source file:com.panet.imeta.core.config.KettleConfig.java

private <E extends AccessibleObject> void inject(E[] elems, TempConfig cfg, ConfigManager<?> parms)
        throws IllegalAccessException, InvocationTargetException {
    for (AccessibleObject elem : elems) {
        Inject inj = elem.getAnnotation(Inject.class);
        if (inj != null) {
            // try to inject property from map.
            elem.setAccessible(true);
            String property = inj.property();
            // Can't think of any other way
            if (elem instanceof Method) {
                Method meth = (Method) elem;
                // find out what we are going to inject 1st
                property = property.equals("") ? Introspector.decapitalize(meth.getName().substring(3))
                        : property;/*w  w  w .  j ava 2 s  .  c o m*/
                meth.invoke(parms, cfg.parms.get(property));

            } else if (elem instanceof Field) {
                Field field = (Field) elem;
                field.set(parms, cfg.parms.get(property.equals("") ? field.getName() : property));
            }
        }
    }
}

From source file:com.github.tddts.jet.config.spring.postprocessor.MessageAnnotationBeanPostProcessor.java

private String preprocess(AccessibleObject accessibleObject) throws NoSuchMessageException {
    Message messageAnnotation = accessibleObject.getAnnotation(Message.class);
    String messageKey = messageAnnotation.value();
    String message = messageSource.getMessage(messageKey, EMPTY_ARGS, Locale.getDefault());
    accessibleObject.setAccessible(true);
    return message;
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

private static void setAccessible(AccessibleObject object) {
    object.setAccessible(true);
}

From source file:com.mylife.hbase.mapper.HBaseEntityMapper.java

private byte[] getRowKeyFrom(final Object hbasePersistableObject) throws Exception {
    /*//from   w  w  w .j  a va  2s . c o  m
     * Get the field or method that has the @HBaseRowKey annotation.
     */
    AccessibleObject accessibleObject = annotatedClassToAnnotatedHBaseRowKey
            .get(hbasePersistableObject.getClass());
    accessibleObject.setAccessible(true);
    /*
     * Either invoke a method to get the key value from the overall hbasePersistableObject or simple field value.
     */
    if (accessibleObject.getClass().isAssignableFrom(Method.class)) {
        return toBytes(((Method) accessibleObject).invoke(hbasePersistableObject, (Object[]) null));
    } else {
        return toBytes(((Field) accessibleObject).get(hbasePersistableObject));
    }
}

From source file:com.clark.func.Functions.java

/**
 * XXX Default access superclass workaround
 * //from   www  . j a v  a 2  s  . c o  m
 * 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
        }
    }
}