Java Code Examples for sun.reflect.misc.ReflectUtil

The following examples show how to use sun.reflect.misc.ReflectUtil. These examples are extracted from open source projects. 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 Project: jdk8u60   Source File: ObjectOutputStream.java    License: 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 2
Source Project: Bytecoder   Source File: ObjectOutputStream.java    License: Apache License 2.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 Project: jdk8u-jdk   Source File: Proxy.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void checkNewProxyPermission(Class<?> caller, Class<?> proxyClass) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        if (ReflectUtil.isNonPublicProxyClass(proxyClass)) {
            ClassLoader ccl = caller.getClassLoader();
            ClassLoader pcl = proxyClass.getClassLoader();

            // do permission check if the caller is in a different runtime package
            // of the proxy class
            int n = proxyClass.getName().lastIndexOf('.');
            String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);

            n = caller.getName().lastIndexOf('.');
            String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);

            if (pcl != ccl || !pkg.equals(callerPkg)) {
                sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
            }
        }
    }
}
 
Example 4
Source Project: TencentKona-8   Source File: ORB.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static ORB create_impl(String className) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null)
        cl = ClassLoader.getSystemClassLoader();

    try {
        ReflectUtil.checkPackageAccess(className);
        Class<org.omg.CORBA.ORB> orbBaseClass = org.omg.CORBA.ORB.class;
        Class<?> orbClass = Class.forName(className, true, cl).asSubclass(orbBaseClass);
        return (ORB)orbClass.newInstance();
    } catch (Throwable ex) {
        SystemException systemException = new INITIALIZE(
           "can't instantiate default ORB implementation " + className);
        systemException.initCause(ex);
        throw systemException;
    }
}
 
Example 5
Source Project: Java8CN   Source File: InvokerBytecodeGenerator.java    License: Apache License 2.0 6 votes vote down vote up
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 6
Source Project: jdk1.8-source-analysis   Source File: ObjectOutputStream.java    License: Apache License 2.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 7
Source Project: jdk1.8-source-analysis   Source File: ORB.java    License: Apache License 2.0 6 votes vote down vote up
private static ORB create_impl(String className) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null)
        cl = ClassLoader.getSystemClassLoader();

    try {
        ReflectUtil.checkPackageAccess(className);
        Class<org.omg.CORBA.ORB> orbBaseClass = org.omg.CORBA.ORB.class;
        Class<?> orbClass = Class.forName(className, true, cl).asSubclass(orbBaseClass);
        return (ORB)orbClass.newInstance();
    } catch (Throwable ex) {
        SystemException systemException = new INITIALIZE(
           "can't instantiate default ORB implementation " + className);
        systemException.initCause(ex);
        throw systemException;
    }
}
 
Example 8
Source Project: jdk1.8-source-analysis   Source File: MBeanSupport.java    License: Apache License 2.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 9
public MethodAccessor newMethodAccessor(Method method) {
    checkInitted();

    if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
        return new MethodAccessorGenerator().
            generateMethod(method.getDeclaringClass(),
                           method.getName(),
                           method.getParameterTypes(),
                           method.getReturnType(),
                           method.getExceptionTypes(),
                           method.getModifiers());
    } else {
        NativeMethodAccessorImpl acc =
            new NativeMethodAccessorImpl(method);
        DelegatingMethodAccessorImpl res =
            new DelegatingMethodAccessorImpl(acc);
        acc.setParent(res);
        return res;
    }
}
 
Example 10
Source Project: hottub   Source File: Proxy.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void checkNewProxyPermission(Class<?> caller, Class<?> proxyClass) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        if (ReflectUtil.isNonPublicProxyClass(proxyClass)) {
            ClassLoader ccl = caller.getClassLoader();
            ClassLoader pcl = proxyClass.getClassLoader();

            // do permission check if the caller is in a different runtime package
            // of the proxy class
            int n = proxyClass.getName().lastIndexOf('.');
            String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);

            n = caller.getName().lastIndexOf('.');
            String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);

            if (pcl != ccl || !pkg.equals(callerPkg)) {
                sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
            }
        }
    }
}
 
Example 11
public Object invoke(Object obj, Object[] args)
    throws IllegalArgumentException, InvocationTargetException
{
    // We can't inflate methods belonging to vm-anonymous classes because
    // that kind of class can't be referred to by name, hence can't be
    // found from the generated bytecode.
    if (++numInvocations > ReflectionFactory.inflationThreshold()
            && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
        MethodAccessorImpl acc = (MethodAccessorImpl)
            new MethodAccessorGenerator().
                generateMethod(method.getDeclaringClass(),
                               method.getName(),
                               method.getParameterTypes(),
                               method.getReturnType(),
                               method.getExceptionTypes(),
                               method.getModifiers());
        parent.setDelegate(acc);
    }

    return invoke0(method, obj, args);
}
 
Example 12
Source Project: JDKSourceCode1.8   Source File: Introspector.java    License: MIT License 6 votes vote down vote up
/**
 * Introspect on a Java Bean and learn about all its properties, exposed
 * methods, and events.
 * <p>
 * If the BeanInfo class for a Java Bean has been previously Introspected
 * then the BeanInfo class is retrieved from the BeanInfo cache.
 *
 * @param beanClass  The bean class to be analyzed.
 * @return  A BeanInfo object describing the target bean.
 * @exception IntrospectionException if an exception occurs during
 *              introspection.
 * @see #flushCaches
 * @see #flushFromCaches
 */
public static BeanInfo getBeanInfo(Class<?> beanClass)
    throws IntrospectionException
{
    if (!ReflectUtil.isPackageAccessible(beanClass)) {
        return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
    }
    ThreadGroupContext context = ThreadGroupContext.getContext();
    BeanInfo beanInfo;
    synchronized (declaredMethodCache) {
        beanInfo = context.getBeanInfo(beanClass);
    }
    if (beanInfo == null) {
        beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
        synchronized (declaredMethodCache) {
            context.putBeanInfo(beanClass, beanInfo);
        }
    }
    return beanInfo;
}
 
Example 13
Source Project: openjdk-jdk8u   Source File: MBeanSupport.java    License: 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 14
Source Project: jdk1.8-source-analysis   Source File: SerialJavaObject.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns an array of <code>Field</code> objects that contains each
 * field of the object that this helper class is serializing.
 *
 * @return an array of <code>Field</code> objects
 * @throws SerialException if an error is encountered accessing
 * the serialized object
 * @throws  SecurityException  If a security manager, <i>s</i>, is present
 * and the caller's class loader is not the same as or an
 * ancestor of the class loader for the class of the
 * {@linkplain #getObject object} being serialized
 * and invocation of {@link SecurityManager#checkPackageAccess
 * s.checkPackageAccess()} denies access to the package
 * of that class.
 * @see Class#getFields
 */
@CallerSensitive
public Field[] getFields() throws SerialException {
    if (fields != null) {
        Class<?> c = this.obj.getClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            /*
             * Check if the caller is allowed to access the specified class's package.
             * If access is denied, throw a SecurityException.
             */
            Class<?> caller = sun.reflect.Reflection.getCallerClass();
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                    c.getClassLoader())) {
                ReflectUtil.checkPackageAccess(c);
            }
        }
        return c.getFields();
    } else {
        throw new SerialException("SerialJavaObject does not contain" +
            " a serialized object instance");
    }
}
 
Example 15
Source Project: openjdk-8   Source File: MBeanInstantiator.java    License: 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 16
Source Project: openjdk-8   Source File: SerialJavaObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an array of <code>Field</code> objects that contains each
 * field of the object that this helper class is serializing.
 *
 * @return an array of <code>Field</code> objects
 * @throws SerialException if an error is encountered accessing
 * the serialized object
 * @throws  SecurityException  If a security manager, <i>s</i>, is present
 * and the caller's class loader is not the same as or an
 * ancestor of the class loader for the class of the
 * {@linkplain #getObject object} being serialized
 * and invocation of {@link SecurityManager#checkPackageAccess
 * s.checkPackageAccess()} denies access to the package
 * of that class.
 * @see Class#getFields
 */
@CallerSensitive
public Field[] getFields() throws SerialException {
    if (fields != null) {
        Class<?> c = this.obj.getClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            /*
             * Check if the caller is allowed to access the specified class's package.
             * If access is denied, throw a SecurityException.
             */
            Class<?> caller = sun.reflect.Reflection.getCallerClass();
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                    c.getClassLoader())) {
                ReflectUtil.checkPackageAccess(c);
            }
        }
        return c.getFields();
    } else {
        throw new SerialException("SerialJavaObject does not contain" +
            " a serialized object instance");
    }
}
 
Example 17
/**
 * Returns an array of <code>Field</code> objects that contains each
 * field of the object that this helper class is serializing.
 *
 * @return an array of <code>Field</code> objects
 * @throws SerialException if an error is encountered accessing
 * the serialized object
 * @throws  SecurityException  If a security manager, <i>s</i>, is present
 * and the caller's class loader is not the same as or an
 * ancestor of the class loader for the class of the
 * {@linkplain #getObject object} being serialized
 * and invocation of {@link SecurityManager#checkPackageAccess
 * s.checkPackageAccess()} denies access to the package
 * of that class.
 * @see Class#getFields
 */
@CallerSensitive
public Field[] getFields() throws SerialException {
    if (fields != null) {
        Class<?> c = this.obj.getClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            /*
             * Check if the caller is allowed to access the specified class's package.
             * If access is denied, throw a SecurityException.
             */
            Class<?> caller = sun.reflect.Reflection.getCallerClass();
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                    c.getClassLoader())) {
                ReflectUtil.checkPackageAccess(c);
            }
        }
        return c.getFields();
    } else {
        throw new SerialException("SerialJavaObject does not contain" +
            " a serialized object instance");
    }
}
 
Example 18
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 19
Source Project: JDKSourceCode1.8   Source File: MBeanSupport.java    License: MIT License 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 20
Source Project: Bytecoder   Source File: ClassLoader.java    License: Apache License 2.0 6 votes vote down vote up
private void checkPackageAccess(Class<?> cls, ProtectionDomain pd) {
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        if (ReflectUtil.isNonPublicProxyClass(cls)) {
            for (Class<?> intf: cls.getInterfaces()) {
                checkPackageAccess(intf, pd);
            }
            return;
        }

        final String packageName = cls.getPackageName();
        if (!packageName.isEmpty()) {
            AccessController.doPrivileged(new PrivilegedAction<>() {
                public Void run() {
                    sm.checkPackageAccess(packageName);
                    return null;
                }
            }, new AccessControlContext(new ProtectionDomain[] {pd}));
        }
    }
}
 
Example 21
Source Project: jdk8u-jdk   Source File: ObjectView.java    License: 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 22
/**
 * Loads the class with the specified name using this object's
 * Default Loader Repository.
 **/
public Class<?> findClassWithDefaultLoaderRepository(String className)
    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 (clr == null) throw new ClassNotFoundException(className);
        theClass = clr.loadClass(className);
    }
    catch (ClassNotFoundException ee) {
        throw new ReflectionException(ee,
   "The MBean class could not be loaded by the default loader repository");
    }

    return theClass;
}
 
Example 23
Source Project: jdk8u_jdk   Source File: SerialJavaObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an array of <code>Field</code> objects that contains each
 * field of the object that this helper class is serializing.
 *
 * @return an array of <code>Field</code> objects
 * @throws SerialException if an error is encountered accessing
 * the serialized object
 * @throws  SecurityException  If a security manager, <i>s</i>, is present
 * and the caller's class loader is not the same as or an
 * ancestor of the class loader for the class of the
 * {@linkplain #getObject object} being serialized
 * and invocation of {@link SecurityManager#checkPackageAccess
 * s.checkPackageAccess()} denies access to the package
 * of that class.
 * @see Class#getFields
 */
@CallerSensitive
public Field[] getFields() throws SerialException {
    if (fields != null) {
        Class<?> c = this.obj.getClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            /*
             * Check if the caller is allowed to access the specified class's package.
             * If access is denied, throw a SecurityException.
             */
            Class<?> caller = sun.reflect.Reflection.getCallerClass();
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                    c.getClassLoader())) {
                ReflectUtil.checkPackageAccess(c);
            }
        }
        return c.getFields();
    } else {
        throw new SerialException("SerialJavaObject does not contain" +
            " a serialized object instance");
    }
}
 
Example 24
Source Project: hottub   Source File: MBeanSupport.java    License: 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 25
/**
 * Loads the class with the specified name using this object's
 * Default Loader Repository.
 **/
public Class<?> findClassWithDefaultLoaderRepository(String className)
    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 (clr == null) throw new ClassNotFoundException(className);
        theClass = clr.loadClass(className);
    }
    catch (ClassNotFoundException ee) {
        throw new ReflectionException(ee,
   "The MBean class could not be loaded by the default loader repository");
    }

    return theClass;
}
 
Example 26
Source Project: Java8CN   Source File: MethodHandles.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Perform necessary <a href="MethodHandles.Lookup.html#secmgr">access checks</a>.
 * Determines a trustable caller class to compare with refc, the symbolic reference class.
 * If this lookup object has private access, then the caller class is the lookupClass.
 */
void checkSecurityManager(Class<?> refc, MemberName m) {
    SecurityManager smgr = System.getSecurityManager();
    if (smgr == null)  return;
    if (allowedModes == TRUSTED)  return;

    // Step 1:
    boolean fullPowerLookup = hasPrivateAccess();
    if (!fullPowerLookup ||
        !VerifyAccess.classLoaderIsAncestor(lookupClass, refc)) {
        ReflectUtil.checkPackageAccess(refc);
    }

    // Step 2:
    if (m.isPublic()) return;
    if (!fullPowerLookup) {
        smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
    }

    // Step 3:
    Class<?> defc = m.getDeclaringClass();
    if (!fullPowerLookup && defc != refc) {
        ReflectUtil.checkPackageAccess(defc);
    }
}
 
Example 27
Source Project: JDKSourceCode1.8   Source File: MBeanInstantiator.java    License: MIT License 6 votes vote down vote up
/**
 * Loads the class with the specified name using this object's
 * Default Loader Repository.
 **/
public Class<?> findClassWithDefaultLoaderRepository(String className)
    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 (clr == null) throw new ClassNotFoundException(className);
        theClass = clr.loadClass(className);
    }
    catch (ClassNotFoundException ee) {
        throw new ReflectionException(ee,
   "The MBean class could not be loaded by the default loader repository");
    }

    return theClass;
}
 
Example 28
/**
 * Returns an array of <code>Field</code> objects that contains each
 * field of the object that this helper class is serializing.
 *
 * @return an array of <code>Field</code> objects
 * @throws SerialException if an error is encountered accessing
 * the serialized object
 * @throws  SecurityException  If a security manager, <i>s</i>, is present
 * and the caller's class loader is not the same as or an
 * ancestor of the class loader for the class of the
 * {@linkplain #getObject object} being serialized
 * and invocation of {@link SecurityManager#checkPackageAccess
 * s.checkPackageAccess()} denies access to the package
 * of that class.
 * @see Class#getFields
 */
@CallerSensitive
public Field[] getFields() throws SerialException {
    if (fields != null) {
        Class<?> c = this.obj.getClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            /*
             * Check if the caller is allowed to access the specified class's package.
             * If access is denied, throw a SecurityException.
             */
            Class<?> caller = Reflection.getCallerClass();
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                    c.getClassLoader())) {
                ReflectUtil.checkPackageAccess(c);
            }
        }
        return c.getFields();
    } else {
        throw new SerialException("SerialJavaObject does not contain" +
            " a serialized object instance");
    }
}
 
Example 29
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 30
Source Project: jdk8u60   Source File: Introspector.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Introspect on a Java Bean and learn about all its properties, exposed
 * methods, and events.
 * <p>
 * If the BeanInfo class for a Java Bean has been previously Introspected
 * then the BeanInfo class is retrieved from the BeanInfo cache.
 *
 * @param beanClass  The bean class to be analyzed.
 * @return  A BeanInfo object describing the target bean.
 * @exception IntrospectionException if an exception occurs during
 *              introspection.
 * @see #flushCaches
 * @see #flushFromCaches
 */
public static BeanInfo getBeanInfo(Class<?> beanClass)
    throws IntrospectionException
{
    if (!ReflectUtil.isPackageAccessible(beanClass)) {
        return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
    }
    ThreadGroupContext context = ThreadGroupContext.getContext();
    BeanInfo beanInfo;
    synchronized (declaredMethodCache) {
        beanInfo = context.getBeanInfo(beanClass);
    }
    if (beanInfo == null) {
        beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
        synchronized (declaredMethodCache) {
            context.putBeanInfo(beanClass, beanInfo);
        }
    }
    return beanInfo;
}