1 /*
2  * Copyright (C) 2016 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 // Class loader that returns Helper2.class when asked to load "Test".
18 public class MisbehavingLoader extends DefiningLoader {
19     private DefiningLoader defining_loader;
20 
MisbehavingLoader(ClassLoader parent, DefiningLoader defining_loader)21     public MisbehavingLoader(ClassLoader parent, DefiningLoader defining_loader) {
22         super(parent);
23         this.defining_loader = defining_loader;
24     }
25 
findClass(String name)26     protected Class<?> findClass(String name) throws ClassNotFoundException
27     {
28         if (name.equals("Helper1") || name.equals("Helper2")) {
29             return super.findClass(name);
30         } else if (name.equals("Test")) {
31             throw new Error("Unexpected MisbehavingLoader.findClass(\"Test\")");
32         }
33         return super.findClass(name);
34     }
35 
loadClass(String name, boolean resolve)36     protected Class<?> loadClass(String name, boolean resolve)
37         throws ClassNotFoundException
38     {
39         if (name.equals("Helper1") || name.equals("Helper2")) {
40             return super.loadClass(name, resolve);
41         } else if (name.equals("Test")) {
42             // Ask for a different class.
43             return defining_loader.loadClass("Helper2", resolve);
44         }
45         return super.loadClass(name, resolve);
46     }
47 }
48