Java Code Examples for sun.reflect.misc.ReflectUtil#checkPackageAccess()

The following examples show how to use sun.reflect.misc.ReflectUtil#checkPackageAccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Proxy.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the invocation handler for the specified proxy instance.
 *
 * @param   proxy the proxy instance to return the invocation handler for
 * @return  the invocation handler for the proxy instance
 * @throws  IllegalArgumentException if the argument is not a
 *          proxy instance
 * @throws  SecurityException if a security manager, <em>s</em>, is present
 *          and the caller's class loader is not the same as or an
 *          ancestor of the class loader for the invocation handler
 *          and invocation of {@link SecurityManager#checkPackageAccess
 *          s.checkPackageAccess()} denies access to the invocation
 *          handler's class.
 */
@CallerSensitive
public static InvocationHandler getInvocationHandler(Object proxy)
    throws IllegalArgumentException
{
    /*
     * Verify that the object is actually a proxy instance.
     */
    if (!isProxyClass(proxy.getClass())) {
        throw new IllegalArgumentException("not a proxy instance");
    }

    final Proxy p = (Proxy) proxy;
    final InvocationHandler ih = p.h;
    if (System.getSecurityManager() != null) {
        Class<?> ihClass = ih.getClass();
        Class<?> caller = Reflection.getCallerClass();
        if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                ihClass.getClassLoader()))
        {
            ReflectUtil.checkPackageAccess(ihClass);
        }
    }

    return ih;
}
 
Example 2
Source File: ObjectOutputStream.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes class descriptor representing a dynamic proxy class to stream.
 */
private void writeProxyDesc(ObjectStreamClass desc, boolean unshared)
    throws IOException
{
    bout.writeByte(TC_PROXYCLASSDESC);
    handles.assign(unshared ? null : desc);

    Class<?> cl = desc.forClass();
    Class<?>[] ifaces = cl.getInterfaces();
    bout.writeInt(ifaces.length);
    for (int i = 0; i < ifaces.length; i++) {
        bout.writeUTF(ifaces[i].getName());
    }

    bout.setBlockDataMode(true);
    if (cl != null && isCustomSubclass()) {
        ReflectUtil.checkPackageAccess(cl);
    }
    annotateProxyClass(cl);
    bout.setBlockDataMode(false);
    bout.writeByte(TC_ENDBLOCKDATA);

    writeClassDesc(desc.getSuperDesc(), false);
}
 
Example 3
Source File: MBeanInstantiator.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Load a class with the specified loader, or with this object
 * class loader if the specified loader is null.
 **/
static Class<?> loadClass(String className, ClassLoader loader)
    throws ReflectionException {
    Class<?> theClass;
    if (className == null) {
        throw new RuntimeOperationsException(new
            IllegalArgumentException("The class name cannot be null"),
                          "Exception occurred during object instantiation");
    }
    ReflectUtil.checkPackageAccess(className);
    try {
        if (loader == null)
            loader = MBeanInstantiator.class.getClassLoader();
        if (loader != null) {
            theClass = Class.forName(className, false, loader);
        } else {
            theClass = Class.forName(className);
        }
    } catch (ClassNotFoundException e) {
        throw new ReflectionException(e,
        "The MBean class could not be loaded");
    }
    return theClass;
}
 
Example 4
Source File: ObjectView.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create the component.  The classid is used
 * as a specification of the classname, which
 * we try to load.
 */
protected Component createComponent() {
    AttributeSet attr = getElement().getAttributes();
    String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
    try {
        ReflectUtil.checkPackageAccess(classname);
        Class c = Class.forName(classname, true,Thread.currentThread().
                                getContextClassLoader());
        Object o = c.newInstance();
        if (o instanceof Component) {
            Component comp = (Component) o;
            setParameters(comp, attr);
            return comp;
        }
    } catch (Throwable e) {
        // couldn't create a component... fall through to the
        // couldn't load representation.
    }

    return getUnloadableRepresentation();
}
 
Example 5
Source File: ObjectOutputStream.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes class descriptor representing a standard (i.e., not a dynamic
 * proxy) class to stream.
 */
private void writeNonProxyDesc(ObjectStreamClass desc, boolean unshared)
    throws IOException
{
    bout.writeByte(TC_CLASSDESC);
    handles.assign(unshared ? null : desc);

    if (protocol == PROTOCOL_VERSION_1) {
        // do not invoke class descriptor write hook with old protocol
        desc.writeNonProxy(this);
    } else {
        writeClassDescriptor(desc);
    }

    Class<?> cl = desc.forClass();
    bout.setBlockDataMode(true);
    if (cl != null && isCustomSubclass()) {
        ReflectUtil.checkPackageAccess(cl);
    }
    annotateClass(cl);
    bout.setBlockDataMode(false);
    bout.writeByte(TC_ENDBLOCKDATA);

    writeClassDesc(desc.getSuperDesc(), false);
}
 
Example 6
Source File: MBeanSupport.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
<T> MBeanSupport(T resource, Class<T> mbeanInterfaceType)
        throws NotCompliantMBeanException {
    if (mbeanInterfaceType == null)
        throw new NotCompliantMBeanException("Null MBean interface");
    if (!mbeanInterfaceType.isInstance(resource)) {
        final String msg =
            "Resource class " + resource.getClass().getName() +
            " is not an instance of " + mbeanInterfaceType.getName();
        throw new NotCompliantMBeanException(msg);
    }
    ReflectUtil.checkPackageAccess(mbeanInterfaceType);
    this.resource = resource;
    MBeanIntrospector<M> introspector = getMBeanIntrospector();
    this.perInterface = introspector.getPerInterface(mbeanInterfaceType);
    this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface);
}
 
Example 7
Source File: LoaderHandler.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Class<?> loadClassForName(String name,
                                          boolean initialize,
                                          ClassLoader loader)
        throws ClassNotFoundException
{
    if (loader == null) {
        ReflectUtil.checkPackageAccess(name);
    }
    return Class.forName(name, initialize, loader);
}
 
Example 8
Source File: DefaultFormatter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts the passed in String into an instance of
 * <code>getValueClass</code> by way of the constructor that
 * takes a String argument. If <code>getValueClass</code>
 * returns null, the Class of the current value in the
 * <code>JFormattedTextField</code> will be used. If this is null, a
 * String will be returned. If the constructor throws an exception, a
 * <code>ParseException</code> will be thrown. If there is no single
 * argument String constructor, <code>string</code> will be returned.
 *
 * @throws ParseException if there is an error in the conversion
 * @param string String to convert
 * @return Object representation of text
 */
public Object stringToValue(String string) throws ParseException {
    Class<?> vc = getValueClass();
    JFormattedTextField ftf = getFormattedTextField();

    if (vc == null && ftf != null) {
        Object value = ftf.getValue();

        if (value != null) {
            vc = value.getClass();
        }
    }
    if (vc != null) {
        Constructor cons;

        try {
            ReflectUtil.checkPackageAccess(vc);
            SwingUtilities2.checkAccess(vc.getModifiers());
            cons = vc.getConstructor(new Class[]{String.class});

        } catch (NoSuchMethodException nsme) {
            cons = null;
        }

        if (cons != null) {
            try {
                SwingUtilities2.checkAccess(cons.getModifiers());
                return cons.newInstance(new Object[] { string });
            } catch (Throwable ex) {
                throw new ParseException("Error creating instance", 0);
            }
        }
    }
    return string;
}
 
Example 9
Source File: ObjectInputStreamWithLoader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Class<?> resolveClass(ObjectStreamClass aClass)
        throws IOException, ClassNotFoundException {
    if (loader == null) {
        return super.resolveClass(aClass);
    } else {
        String name = aClass.getName();
        ReflectUtil.checkPackageAccess(name);
        // Query the class loader ...
        return Class.forName(name, false, loader);
    }
}
 
Example 10
Source File: ClassLoaderRepositorySupport.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Class<?> loadClass(final LoaderEntry list[],
                           final String className,
                           final ClassLoader without,
                           final ClassLoader stop)
        throws ClassNotFoundException {
    ReflectUtil.checkPackageAccess(className);
    final int size = list.length;
    for(int i=0; i<size; i++) {
        try {
            final ClassLoader cl = list[i].loader;
            if (cl == null) // bootstrap class loader
                return Class.forName(className, false, null);
            if (cl == without)
                continue;
            if (cl == stop)
                break;
            if (MBEANSERVER_LOGGER.isLoggable(Level.TRACE)) {
                MBEANSERVER_LOGGER.log(Level.TRACE, "Trying loader = " + cl);
            }
            /* We used to have a special case for "instanceof
               MLet" here, where we invoked the method
               loadClass(className, null) to prevent infinite
               recursion.  But the rule whereby the MLet only
               consults loaders that precede it in the CLR (via
               loadClassBefore) means that the recursion can't
               happen, and the test here caused some legitimate
               classloading to fail.  For example, if you have
               dependencies C->D->E with loaders {E D C} in the
               CLR in that order, you would expect to be able to
               load C.  The problem is that while resolving D, CLR
               delegation is disabled, so it can't find E.  */
            return Class.forName(className, false, cl);
        } catch (ClassNotFoundException e) {
            // OK: continue with next class
        }
    }

    throw new ClassNotFoundException(className);
}
 
Example 11
Source File: AquaUtils.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
T getInstance() {
    try {
        ReflectUtil.checkPackageAccess(clazz);
        return clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException ignored) {
    }
    return null;
}
 
Example 12
Source File: MBeanIntrospector.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the methods to be analyzed to build the MBean interface.
 */
final List<Method> getMethods(final Class<?> mbeanType) {
    ReflectUtil.checkPackageAccess(mbeanType);
    return Arrays.asList(mbeanType.getMethods());
}
 
Example 13
Source File: ClassLoaderRepositorySupport.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private Class<?> loadClass(final LoaderEntry list[],
                           final String className,
                           final ClassLoader without,
                           final ClassLoader stop)
        throws ClassNotFoundException {
    ReflectUtil.checkPackageAccess(className);
    final int size = list.length;
    for(int i=0; i<size; i++) {
        try {
            final ClassLoader cl = list[i].loader;
            if (cl == null) // bootstrap class loader
                return Class.forName(className, false, null);
            if (cl == without)
                continue;
            if (cl == stop)
                break;
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
                MBEANSERVER_LOGGER.logp(Level.FINER,
                        ClassLoaderRepositorySupport.class.getName(),
                        "loadClass", "Trying loader = " + cl);
            }
            /* We used to have a special case for "instanceof
               MLet" here, where we invoked the method
               loadClass(className, null) to prevent infinite
               recursion.  But the rule whereby the MLet only
               consults loaders that precede it in the CLR (via
               loadClassBefore) means that the recursion can't
               happen, and the test here caused some legitimate
               classloading to fail.  For example, if you have
               dependencies C->D->E with loaders {E D C} in the
               CLR in that order, you would expect to be able to
               load C.  The problem is that while resolving D, CLR
               delegation is disabled, so it can't find E.  */
            return Class.forName(className, false, cl);
        } catch (ClassNotFoundException e) {
            // OK: continue with next class
        }
    }

    throw new ClassNotFoundException(className);
}
 
Example 14
Source File: DefaultMXBeanMappingFactory.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static <T extends Enum<T>> MXBeanMapping
        makeEnumMapping(Class<?> enumClass, Class<T> fake) {
    ReflectUtil.checkPackageAccess(enumClass);
    return new EnumMapping<T>(Util.<Class<T>>cast(enumClass));
}
 
Example 15
Source File: SwingUtilities.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
static Class<?> loadSystemClass(String className) throws ClassNotFoundException {
    ReflectUtil.checkPackageAccess(className);
    return Class.forName(className, true, Thread.currentThread().
                         getContextClassLoader());
}
 
Example 16
Source File: ClassLoaderRepositorySupport.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private Class<?> loadClass(final LoaderEntry list[],
                           final String className,
                           final ClassLoader without,
                           final ClassLoader stop)
        throws ClassNotFoundException {
    ReflectUtil.checkPackageAccess(className);
    final int size = list.length;
    for(int i=0; i<size; i++) {
        try {
            final ClassLoader cl = list[i].loader;
            if (cl == null) // bootstrap class loader
                return Class.forName(className, false, null);
            if (cl == without)
                continue;
            if (cl == stop)
                break;
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
                MBEANSERVER_LOGGER.logp(Level.FINER,
                        ClassLoaderRepositorySupport.class.getName(),
                        "loadClass", "Trying loader = " + cl);
            }
            /* We used to have a special case for "instanceof
               MLet" here, where we invoked the method
               loadClass(className, null) to prevent infinite
               recursion.  But the rule whereby the MLet only
               consults loaders that precede it in the CLR (via
               loadClassBefore) means that the recursion can't
               happen, and the test here caused some legitimate
               classloading to fail.  For example, if you have
               dependencies C->D->E with loaders {E D C} in the
               CLR in that order, you would expect to be able to
               load C.  The problem is that while resolving D, CLR
               delegation is disabled, so it can't find E.  */
            return Class.forName(className, false, cl);
        } catch (ClassNotFoundException e) {
            // OK: continue with next class
        }
    }

    throw new ClassNotFoundException(className);
}
 
Example 17
Source File: MBeanIntrospector.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the methods to be analyzed to build the MBean interface.
 */
final List<Method> getMethods(final Class<?> mbeanType) {
    ReflectUtil.checkPackageAccess(mbeanType);
    return Arrays.asList(mbeanType.getMethods());
}
 
Example 18
Source File: DefaultMXBeanMappingFactory.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
private static <T extends Enum<T>> MXBeanMapping
        makeEnumMapping(Class<?> enumClass, Class<T> fake) {
    ReflectUtil.checkPackageAccess(enumClass);
    return new EnumMapping<T>(Util.<Class<T>>cast(enumClass));
}
 
Example 19
Source File: NumberFormatter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Invoked to toggle the sign. For this to work the value class
 * must have a single arg constructor that takes a String.
 */
private Object toggleSign(boolean positive) throws ParseException {
    Object value = stringToValue(getFormattedTextField().getText());

    if (value != null) {
        // toString isn't localized, so that using +/- should work
        // correctly.
        String string = value.toString();

        if (string != null && string.length() > 0) {
            if (positive) {
                if (string.charAt(0) == '-') {
                    string = string.substring(1);
                }
            }
            else {
                if (string.charAt(0) == '+') {
                    string = string.substring(1);
                }
                if (string.length() > 0 && string.charAt(0) != '-') {
                    string = "-" + string;
                }
            }
            if (string != null) {
                Class<?> valueClass = getValueClass();

                if (valueClass == null) {
                    valueClass = value.getClass();
                }
                try {
                    ReflectUtil.checkPackageAccess(valueClass);
                    SwingUtilities2.checkAccess(valueClass.getModifiers());
                    Constructor cons = valueClass.getConstructor(
                                          new Class[] { String.class });
                    if (cons != null) {
                        SwingUtilities2.checkAccess(cons.getModifiers());
                        return cons.newInstance(new Object[]{string});
                    }
                } catch (Throwable ex) { }
            }
        }
    }
    return null;
}
 
Example 20
Source File: Introspector.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Basic method for testing if a given class is a JMX compliant
 * Standard MBean.  This method is only called by the legacy code
 * in com.sun.management.jmx.
 *
 * @param baseClass The class to be tested.
 *
 * @param mbeanInterface the MBean interface that the class implements,
 * or null if the interface must be determined by introspection.
 *
 * @return the computed {@link javax.management.MBeanInfo}.
 * @exception NotCompliantMBeanException The specified class is not a
 *            JMX compliant Standard MBean
 */
public static synchronized MBeanInfo
        testCompliance(final Class<?> baseClass,
                       Class<?> mbeanInterface)
        throws NotCompliantMBeanException {
    if (mbeanInterface == null)
        mbeanInterface = getStandardMBeanInterface(baseClass);
    ReflectUtil.checkPackageAccess(mbeanInterface);
    MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
    return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
}