1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.android.internal.creation;
6 
7 import net.bytebuddy.android.AndroidClassLoadingStrategy;
8 import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
9 import org.mockito.exceptions.base.MockitoException;
10 import org.mockito.internal.creation.bytebuddy.SubclassLoader;
11 
12 import java.io.File;
13 
14 import static org.mockito.internal.util.StringUtil.join;
15 
16 class AndroidLoadingStrategy implements SubclassLoader {
17 
18     @Override
resolveStrategy(Class<?> mockedType, ClassLoader classLoader, boolean codegen)19     public ClassLoadingStrategy<ClassLoader> resolveStrategy(Class<?> mockedType, ClassLoader classLoader, boolean codegen) {
20         File target = AndroidTempFileLocator.target;
21         if (target == null) {
22             throw new MockitoException(join(
23                     "Could not look up implicit location for storing generated classes",
24                     "",
25                     "You can configure an explicit location by setting the system property",
26                     "'org.mockito.android.target' to a folder for storing generated class files",
27                     "This location must be in private scope for most API versions, for example:",
28                     "",
29                     "MyActivity.this.getDir(\"target\", Context.MODE_PRIVATE)",
30                     "or",
31                     "getInstrumentation().getTargetContext().getCacheDir().getPath()"
32             ));
33         }
34         return new AndroidClassLoadingStrategy.Injecting(target);
35     }
36 }
37