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 static void setAccessible(AccessibleObject[] array, boolean flag) 

Source Link

Document

Convenience method to set the accessible flag for an array of reflected objects with a single security check (for efficiency).

Usage

From source file:ubic.gemma.web.taglib.ConstantsTag.java

@Override
public int doStartTag() throws JspException {
    // Using reflection, get the available field names in the class
    Class<?> c;//from  w  w  w .j  ava  2 s  .co m
    int toScope = PageContext.PAGE_SCOPE;

    if (scope != null) {
        toScope = getScope(scope);
    }

    try {
        c = Class.forName(clazz);
    } catch (ClassNotFoundException cnf) {
        log.error("ClassNotFound - maybe a typo?");
        throw new JspException(cnf.getMessage());
    }

    try {
        // if var is null, expose all variables
        if (var == null) {
            Field[] fields = c.getDeclaredFields();

            AccessibleObject.setAccessible(fields, true);

            for (Field field : fields) {
                /*
                 * if (log.isDebugEnabled()) { log.debug("putting '" + fields[i].getName() + "=" +
                 * fields[i].get(this) + "' into " + scope + " scope"); }
                 */
                pageContext.setAttribute(field.getName(), field.get(this), toScope);
            }
        } else {
            try {
                String value = (String) c.getField(var).get(this);
                pageContext.setAttribute(c.getField(var).getName(), value, toScope);
            } catch (NoSuchFieldException nsf) {
                log.error(nsf.getMessage());
                throw new JspException(nsf);
            }
        }
    } catch (IllegalAccessException iae) {
        log.error("Illegal Access Exception - maybe a classloader issue?");
        throw new JspException(iae);
    }

    // Continue processing this page
    return (SKIP_BODY);
}

From source file:ubic.gemma.web.taglib.ConstantsTei.java

/**
 * Return information about the scripting variables to be created.
 *//*w  ww. j a  va2s .c om*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
    // loop through and expose all attributes
    List<VariableInfo> vars = new ArrayList<VariableInfo>();

    try {
        String clazz = data.getAttributeString("className");

        if (clazz == null) {
            clazz = Constants.class.getName();
        }

        Class<?> c = Class.forName(clazz);

        // if no var specified, get all
        if (data.getAttributeString("var") == null) {
            Field[] fields = c.getDeclaredFields();

            AccessibleObject.setAccessible(fields, true);

            for (int i = 0; i < fields.length; i++) {
                vars.add(new VariableInfo(fields[i].getName(), "java.lang.String", true, VariableInfo.AT_END));
            }
        } else {
            String var = data.getAttributeString("var");
            vars.add(
                    new VariableInfo(c.getField(var).getName(), "java.lang.String", true, VariableInfo.AT_END));
        }
    } catch (Exception cnf) {
        log.error(cnf.getMessage());
        cnf.printStackTrace();
    }

    return vars.toArray(new VariableInfo[] {});
}