Home | History | Annotate | Download | only in lang
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package java.lang;
     18 
     19 import java.net.MalformedURLException;
     20 import java.net.URL;
     21 import java.util.ArrayList;
     22 import java.util.List;
     23 
     24 class VMClassLoader {
     25 
     26     /**
     27      * Get a resource from a file in the bootstrap class path.
     28      *
     29      * It would be simpler to just walk through the class path elements
     30      * ourselves, but that would require reopening Jar files.
     31      *
     32      * We assume that the bootclasspath can't change once the VM has
     33      * started.  This assumption seems to be supported by the spec.
     34      */
     35     static URL getResource(String name) {
     36         int numEntries = getBootClassPathSize();
     37         for (int i = 0; i < numEntries; i++) {
     38             String urlStr = getBootClassPathResource(name, i);
     39             if (urlStr != null) {
     40                 try {
     41                     return new URL(urlStr);
     42                 } catch (MalformedURLException mue) {
     43                     mue.printStackTrace();
     44                     // unexpected; keep going
     45                 }
     46             }
     47         }
     48         return null;
     49     }
     50 
     51     /*
     52      * Get an enumeration with all matching resources.
     53      */
     54     static List<URL> getResources(String name) {
     55         ArrayList<URL> list = new ArrayList<URL>();
     56         int numEntries = getBootClassPathSize();
     57         for (int i = 0; i < numEntries; i++) {
     58             String urlStr = getBootClassPathResource(name, i);
     59             if (urlStr != null) {
     60                 try {
     61                     list.add(new URL(urlStr));
     62                 } catch (MalformedURLException mue) {
     63                     mue.printStackTrace();
     64                     // unexpected; keep going
     65                 }
     66             }
     67         }
     68         return list;
     69     }
     70 
     71     /**
     72      * Load class with bootstrap class loader.
     73      */
     74     native static Class loadClass(String name, boolean resolve) throws ClassNotFoundException;
     75 
     76     native static Class getPrimitiveClass(char type);
     77 
     78     native static Class defineClass(ClassLoader cl, String name, byte[] data, int offset, int len)
     79             throws ClassFormatError;
     80 
     81     native static Class defineClass(ClassLoader cl, byte[] data, int offset, int len)
     82             throws ClassFormatError;
     83 
     84     native static Class findLoadedClass(ClassLoader cl, String name);
     85 
     86     /**
     87      * Boot class path manipulation, for getResources().
     88      */
     89     native private static int getBootClassPathSize();
     90     native private static String getBootClassPathResource(String name, int index);
     91 }
     92