• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 package tests.support;
17 
18 import java.io.File;
19 import java.net.URL;
20 import java.net.URLClassLoader;
21 
22 /**
23  * Support class for creating a file-based ClassLoader. Delegates to either
24  * Dalvik's PathClassLoader or the RI's URLClassLoader, but does so by-name.
25  * This allows us to run corresponding tests in both environments.
26  */
27 public abstract class Support_ClassLoader {
28 
getClassLoader(URL url, ClassLoader parent)29     public abstract ClassLoader getClassLoader(URL url, ClassLoader parent);
30 
getInstance(URL url, ClassLoader parent)31     public static ClassLoader getInstance(URL url, ClassLoader parent) {
32         try {
33             Support_ClassLoader factory;
34 
35             String packageName = Support_ClassLoader.class.getPackage().getName();
36             if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
37                 factory = (Support_ClassLoader)Class.forName(
38                     packageName + ".Support_ClassLoaderDalvik").newInstance();
39             } else {
40                 factory = (Support_ClassLoader)Class.forName(
41                     packageName + ".Support_ClassLoader$RefImpl").newInstance();
42             }
43 
44             return factory.getClassLoader(url, parent);
45         } catch (Exception ex) {
46             throw new RuntimeException("Unable to create ClassLoader", ex);
47         }
48     }
49 
50     /**
51      * Implementation for the reference implementation. Nothing interesting to
52      * see here. Please get along.
53      */
54     static class RefImpl extends Support_ClassLoader {
55         @Override
getClassLoader(URL url, ClassLoader parent)56         public ClassLoader getClassLoader(URL url, ClassLoader parent) {
57             return new URLClassLoader(new URL[] { url }, parent);
58         }
59     }
60 }
61