• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import dalvik.system.BaseDexClassLoader;
8 
9 /**
10  * This class wraps two given ClassLoader objects and delegates findClass() and findLibrary() calls
11  * to the first one that returns a match.
12  */
13 public class WrappedClassLoader extends ClassLoader {
14     private final ClassLoader mPrimaryClassLoader;
15     private final ClassLoader mSecondaryClassLoader;
16 
WrappedClassLoader(ClassLoader primary, ClassLoader secondary)17     public WrappedClassLoader(ClassLoader primary, ClassLoader secondary) {
18         mPrimaryClassLoader = primary;
19         mSecondaryClassLoader = secondary;
20     }
21 
22     @Override
findClass(String name)23     protected Class<?> findClass(String name) throws ClassNotFoundException {
24         try {
25             return mPrimaryClassLoader.loadClass(name);
26         } catch (ClassNotFoundException e) {
27             try {
28                 return mSecondaryClassLoader.loadClass(name);
29             } catch (ClassNotFoundException e2) {
30                 e.addSuppressed(e2);
31                 throw e;
32             }
33         }
34     }
35 
36     @Override
findLibrary(String name)37     public String findLibrary(String name) {
38         String path = null;
39         // BaseDexClassLoader has a public findLibrary method, but ClassLoader's is protected
40         // so we can only do this for classloaders that actually do extend BaseDexClassLoader.
41         // findLibrary is rarely used so it's fine to just check this each time.
42         if (mPrimaryClassLoader instanceof BaseDexClassLoader) {
43             path = ((BaseDexClassLoader) mPrimaryClassLoader).findLibrary(name);
44             if (path != null) return path;
45         }
46         if (mSecondaryClassLoader instanceof BaseDexClassLoader) {
47             path = ((BaseDexClassLoader) mSecondaryClassLoader).findLibrary(name);
48         }
49         return path;
50     }
51 }
52