1 /*
2  * Copyright (C) 2010 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 com.android.dexgen.util;
18 
19 import dalvik.system.DexClassLoader;
20 
21 /**
22  * Class used indirectly for loading generated dex classes. It allows the caller
23  * to obtain appropriate {@code DexClassLoader} instance, which can be then used for
24  * loading classes.
25  */
26 public class DexClassLoaderHelper {
27 
28     private static class DexClassLoaderHelperHolder {
29         private static final DexClassLoaderHelper INSTANCE = new DexClassLoaderHelper();
30     }
31 
DexClassLoaderHelper()32     private DexClassLoaderHelper() {
33         // intentionally empty to disable direct instantiation
34     }
35 
36     /**
37      * Returns the sole instance of {@code DexClassLoaderHelper}.
38      *
39      * @return dex {@code DexClassLoaderHelper} sole instance
40      */
getInstance()41     public static DexClassLoaderHelper getInstance() {
42         return DexClassLoaderHelperHolder.INSTANCE;
43     }
44 
45     /**
46      * Creates and returns DexClassLoader instance with its classpath
47      * set to {@code pathHolder}.
48      *
49      * @param pathHolder {@code non-null;} location of jar archive containing dex
50      * classes canned into a working PathHolder instance.
51      * @return dex class loader instance with its classpath set to location
52      * indicated by {@code pathHolder}
53      */
getDexClassLoader(PathHolder pathHolder)54     public ClassLoader getDexClassLoader(PathHolder pathHolder) {
55         ClassLoader myLoader = DexClassLoaderHelper.class.getClassLoader();
56         return new DexClassLoader(pathHolder.getJarFilePath(), pathHolder.getDirLocation(),
57                 null, myLoader);
58     }
59 }