emul/src/main/java/java/lang/Class.java
changeset 122 0a582b5a2737
parent 93 a236a9f137ac
child 225 25e350c6385f
     1.1 --- a/emul/src/main/java/java/lang/Class.java	Mon Oct 08 17:10:27 2012 -0700
     1.2 +++ b/emul/src/main/java/java/lang/Class.java	Tue Oct 30 09:24:41 2012 +0100
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5  package java.lang;
     1.6  
     1.7 +import java.io.InputStream;
     1.8  import java.lang.annotation.Annotation;
     1.9  
    1.10  /**
    1.11 @@ -552,15 +553,15 @@
    1.12       * @throws  NullPointerException If {@code name} is {@code null}
    1.13       * @since  JDK1.1
    1.14       */
    1.15 -//     public InputStream getResourceAsStream(String name) {
    1.16 -//        name = resolveName(name);
    1.17 -//        ClassLoader cl = getClassLoader0();
    1.18 -//        if (cl==null) {
    1.19 -//            // A system class.
    1.20 -//            return ClassLoader.getSystemResourceAsStream(name);
    1.21 -//        }
    1.22 -//        return cl.getResourceAsStream(name);
    1.23 -//    }
    1.24 +     public InputStream getResourceAsStream(String name) {
    1.25 +        name = resolveName(name);
    1.26 +        ClassLoader cl = getClassLoader0();
    1.27 +        if (cl==null) {
    1.28 +            // A system class.
    1.29 +            return ClassLoader.getSystemResourceAsStream(name);
    1.30 +        }
    1.31 +        return cl.getResourceAsStream(name);
    1.32 +    }
    1.33  
    1.34      /**
    1.35       * Finds a resource with a given name.  The rules for searching resources
    1.36 @@ -596,18 +597,86 @@
    1.37       *              resource with this name is found
    1.38       * @since  JDK1.1
    1.39       */
    1.40 -//    public java.net.URL getResource(String name) {
    1.41 -//        name = resolveName(name);
    1.42 -//        ClassLoader cl = getClassLoader0();
    1.43 -//        if (cl==null) {
    1.44 -//            // A system class.
    1.45 -//            return ClassLoader.getSystemResource(name);
    1.46 -//        }
    1.47 -//        return cl.getResource(name);
    1.48 -//    }
    1.49 +    public java.net.URL getResource(String name) {
    1.50 +        name = resolveName(name);
    1.51 +        ClassLoader cl = getClassLoader0();
    1.52 +        if (cl==null) {
    1.53 +            // A system class.
    1.54 +            return ClassLoader.getSystemResource(name);
    1.55 +        }
    1.56 +        return cl.getResource(name);
    1.57 +    }
    1.58  
    1.59  
    1.60 +   /**
    1.61 +     * Add a package name prefix if the name is not absolute Remove leading "/"
    1.62 +     * if name is absolute
    1.63 +     */
    1.64 +    private String resolveName(String name) {
    1.65 +        if (name == null) {
    1.66 +            return name;
    1.67 +        }
    1.68 +        if (!name.startsWith("/")) {
    1.69 +            Class<?> c = this;
    1.70 +            while (c.isArray()) {
    1.71 +                c = c.getComponentType();
    1.72 +            }
    1.73 +            String baseName = c.getName();
    1.74 +            int index = baseName.lastIndexOf('.');
    1.75 +            if (index != -1) {
    1.76 +                name = baseName.substring(0, index).replace('.', '/')
    1.77 +                    +"/"+name;
    1.78 +            }
    1.79 +        } else {
    1.80 +            name = name.substring(1);
    1.81 +        }
    1.82 +        return name;
    1.83 +    }
    1.84 +    
    1.85 +    /**
    1.86 +     * Returns the class loader for the class.  Some implementations may use
    1.87 +     * null to represent the bootstrap class loader. This method will return
    1.88 +     * null in such implementations if this class was loaded by the bootstrap
    1.89 +     * class loader.
    1.90 +     *
    1.91 +     * <p> If a security manager is present, and the caller's class loader is
    1.92 +     * not null and the caller's class loader is not the same as or an ancestor of
    1.93 +     * the class loader for the class whose class loader is requested, then
    1.94 +     * this method calls the security manager's {@code checkPermission}
    1.95 +     * method with a {@code RuntimePermission("getClassLoader")}
    1.96 +     * permission to ensure it's ok to access the class loader for the class.
    1.97 +     *
    1.98 +     * <p>If this object
    1.99 +     * represents a primitive type or void, null is returned.
   1.100 +     *
   1.101 +     * @return  the class loader that loaded the class or interface
   1.102 +     *          represented by this object.
   1.103 +     * @throws SecurityException
   1.104 +     *    if a security manager exists and its
   1.105 +     *    {@code checkPermission} method denies
   1.106 +     *    access to the class loader for the class.
   1.107 +     * @see java.lang.ClassLoader
   1.108 +     * @see SecurityManager#checkPermission
   1.109 +     * @see java.lang.RuntimePermission
   1.110 +     */
   1.111 +    public ClassLoader getClassLoader() {
   1.112 +        throw new SecurityException();
   1.113 +    }
   1.114 +    
   1.115 +    // Package-private to allow ClassLoader access
   1.116 +    native ClassLoader getClassLoader0();    
   1.117  
   1.118 +    /**
   1.119 +     * Returns the {@code Class} representing the component type of an
   1.120 +     * array.  If this class does not represent an array class this method
   1.121 +     * returns null.
   1.122 +     *
   1.123 +     * @return the {@code Class} representing the component type of this
   1.124 +     * class if this class is an array
   1.125 +     * @see     java.lang.reflect.Array
   1.126 +     * @since JDK1.1
   1.127 +     */
   1.128 +    public native Class<?> getComponentType();
   1.129  
   1.130      /**
   1.131       * Returns true if and only if this class was declared as an enum in the