diff --git a/library/src/com/orm/SugarDb.java b/library/src/com/orm/SugarDb.java index 0f859afc..ca7a2b47 100644 --- a/library/src/com/orm/SugarDb.java +++ b/library/src/com/orm/SugarDb.java @@ -15,7 +15,12 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; -import java.util.*; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; import static com.orm.SugarConfig.getDatabaseVersion; import static com.orm.SugarConfig.getDebugEnabled; @@ -32,11 +37,7 @@ public SugarDb(Context context) { private > List getDomainClasses(Context context) { List domainClasses = new ArrayList(); try { - Enumeration allClasses = getAllClasses(context); - - while (allClasses.hasMoreElements()) { - String className = (String) allClasses.nextElement(); - + for (String className : getAllClasses(context)) { if (className.startsWith(SugarConfig.getDomainPackageName(context))) { T domainClass = getDomainClass(className, context); if (domainClass != null) domainClasses.add(domainClass); @@ -84,10 +85,26 @@ private > T getDomainClass(String className, Context co } - private Enumeration getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { + private List getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { String path = getSourcePath(context); - DexFile dexfile = new DexFile(path); - return dexfile.entries(); + List classNames = new ArrayList(); + try { + DexFile dexfile = new DexFile(path); + Enumeration dexEntries = dexfile.entries(); + while (dexEntries.hasMoreElements()) { + classNames.add(dexEntries.nextElement()); + } + } catch (NullPointerException e) { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + Enumeration urls = classLoader.getResources(""); + while (urls.hasMoreElements()) { + String urlPath = urls.nextElement().getFile(); + if (urlPath.contains("bin") || urlPath.contains("classes")) { + classNames.add(urlPath); + } + } + } + return classNames; } private String getSourcePath(Context context) throws PackageManager.NameNotFoundException {