1 package com.xtremelabs.robolectric.internal; 2 3 public class ClassNameResolver<T> { 4 private String packageName; 5 private String className; 6 ClassNameResolver(String packageName, String className)7 public ClassNameResolver(String packageName, String className) { 8 this.packageName = packageName; 9 this.className = className; 10 } 11 resolve()12 public Class<? extends T> resolve() { 13 Class<? extends T> aClass; 14 if (looksFullyQualified(className)) { 15 aClass = safeClassForName(className); 16 } else { 17 aClass = safeClassForName(packageName + "." + className); 18 if (aClass == null) { 19 aClass = safeClassForName(packageName + className); 20 } 21 } 22 23 if (aClass == null) { 24 throw new RuntimeException("Could not find a class for package: " 25 + packageName + " and class name: " + className); 26 } 27 return aClass; 28 } 29 looksFullyQualified(String className)30 private boolean looksFullyQualified(String className) { 31 return className.contains(".") && !className.startsWith("."); 32 } 33 safeClassForName(String classNamePath)34 private Class<? extends T> safeClassForName(String classNamePath) { 35 try { 36 return (Class<? extends T>) Class.forName(classNamePath); 37 } catch (ClassNotFoundException e) { 38 return null; 39 } 40 } 41 } 42