private boolean loadModelFromMetaData(Configuration configuration) { if (!configuration.isValid()) { return false; } final List<Class<? extends Model>> models = configuration.getModelClasses(); if (models != null) { for (Class<? extends Model> model : models) { mTableInfos.put(model, new TableInfo(model)); } } final List<Class<? extends TypeSerializer>> typeSerializers = configuration.getTypeSerializers(); if (typeSerializers != null) { for (Class<? extends TypeSerializer> typeSerializer : typeSerializers) { try { TypeSerializer instance = typeSerializer.newInstance(); mTypeSerializers.put(instance.getDeserializedType(), instance); } catch (InstantiationException e) { Log.e("Couldn't instantiate TypeSerializer.", e); } catch (IllegalAccessException e) { Log.e("IllegalAccessException", e); } } } return true; }
public ModelInfo(Configuration configuration) { if (!loadModelFromMetaData(configuration)) { try { scanForModel(configuration.getContext()); } catch (IOException e) { Log.e("Couldn't open source path.", e); } } Log.i("ModelInfo loaded."); }
private void scanForModelClasses(File path, String packageName, ClassLoader classLoader) { if (path.isDirectory()) { for (File file : path.listFiles()) { scanForModelClasses(file, packageName, classLoader); } } else { String className = path.getName(); // Robolectric fallback if (!path.getPath().equals(className)) { className = path.getPath(); if (className.endsWith(".class")) { className = className.substring(0, className.length() - 6); } else { return; } className = className.replace(System.getProperty("file.separator"), "."); int packageNameIndex = className.lastIndexOf(packageName); if (packageNameIndex < 0) { return; } className = className.substring(packageNameIndex); } try { Class<?> discoveredClass = Class.forName(className, false, classLoader); if (ReflectionUtils.isModel(discoveredClass)) { @SuppressWarnings("unchecked") Class<? extends Model> modelClass = (Class<? extends Model>) discoveredClass; mTableInfos.put(modelClass, new TableInfo(modelClass)); } else if (ReflectionUtils.isTypeSerializer(discoveredClass)) { TypeSerializer instance = (TypeSerializer) discoveredClass.newInstance(); mTypeSerializers.put(instance.getDeserializedType(), instance); } } catch (ClassNotFoundException e) { Log.e("Couldn't create class.", e); } catch (InstantiationException e) { Log.e("Couldn't instantiate TypeSerializer.", e); } catch (IllegalAccessException e) { Log.e("IllegalAccessException", e); } } }