1 package org.robolectric.annotation.processing; 2 3 import static com.google.testing.compile.JavaFileObjects.forResource; 4 import static org.robolectric.annotation.processing.RobolectricProcessor.JSON_DOCS_DIR; 5 import static org.robolectric.annotation.processing.RobolectricProcessor.PACKAGE_OPT; 6 import static org.robolectric.annotation.processing.RobolectricProcessor.SDK_CHECK_MODE; 7 8 import com.google.common.collect.ImmutableMap; 9 import com.google.common.io.Files; 10 import java.io.File; 11 import java.net.URI; 12 import java.net.URL; 13 import javax.tools.JavaFileObject; 14 15 public class Utils { 16 17 public static final ImmutableMap<String, String> DEFAULT_OPTS = 18 ImmutableMap.<String, String>builder() 19 .put(PACKAGE_OPT, "org.robolectric") 20 .put(JSON_DOCS_DIR, Files.createTempDir().toString()) 21 .put(SDK_CHECK_MODE, "OFF") 22 .build(); 23 24 public static final JavaFileObject SHADOW_PROVIDER_SOURCE = 25 forResource("mock-source/org/robolectric/internal/ShadowProvider.java"); 26 public static final JavaFileObject SHADOW_EXTRACTOR_SOURCE = 27 forResource("mock-source/org/robolectric/shadow/api/Shadow.java"); 28 toResourcePath(String clazzName)29 public static String toResourcePath(String clazzName) { 30 return clazzName.replace('.', '/') + ".java"; 31 } 32 getClassRootDir(Class<?> clazz)33 public static String getClassRootDir(Class<?> clazz) { 34 // Get the URL representation of the class location 35 URL resource = clazz.getResource(clazz.getSimpleName() + ".class"); 36 if (resource == null) { 37 return null; // dynamic class 38 } 39 if (resource.getProtocol().equals("file")) { 40 File path = new File(URI.create(resource.toString()).getPath()).getParentFile(); 41 int packagePartSize = clazz.getPackageName().split("\\.").length; 42 for (int i = 0; i < packagePartSize; i++) { 43 path = path.getParentFile(); 44 } 45 return path.getAbsolutePath(); 46 } else if (resource.getProtocol().equals("jar")) { 47 // Extract path to JAR and find root 48 String path = resource.getPath().substring(5, resource.getPath().indexOf("!")); 49 File jarFile = new File(URI.create(path).getPath()); 50 return jarFile.getAbsolutePath(); 51 } else { 52 return null; // Not a supported class location 53 } 54 } 55 } 56