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      */
getResource(String name)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      */
getResources(String name)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 
findLoadedClass(ClassLoader cl, String name)71     native static Class findLoadedClass(ClassLoader cl, String name);
72 
73     /**
74      * Boot class path manipulation, for getResources().
75      */
getBootClassPathSize()76     native private static int getBootClassPathSize();
getBootClassPathResource(String name, int index)77     native private static String getBootClassPathResource(String name, int index);
78 }
79