package com.activeandroid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

import com.activeandroid.serializer.TypeSerializer;
import com.activeandroid.util.Log;
import com.activeandroid.util.ReflectionUtils;

import dalvik.system.DexFile;

class ModelInfo {
	//////////////////////////////////////////////////////////////////////////////////////
	// PRIVATE METHODS
	//////////////////////////////////////////////////////////////////////////////////////

	private Map<Class<? extends Model>, TableInfo> mTableInfos = new HashMap<Class<? extends Model>, TableInfo>();
	private Map<Class<?>, TypeSerializer> mTypeSerializers = new HashMap<Class<?>, TypeSerializer>();

	//////////////////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS
	//////////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("unchecked")
	public ModelInfo(Context context) {
		try {
			final String path = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).sourceDir;
			final DexFile dexfile = new DexFile(path);
			final Enumeration<String> entries = dexfile.entries();

			while (entries.hasMoreElements()) {
				final String name = entries.nextElement();

				if (name.contains("com.activeandroid")) {
					continue;
				}

				try {
					Class<?> discoveredClass = Class.forName(name, false, context.getClass().getClassLoader());
					if (ReflectionUtils.isModel(discoveredClass)) {
						Class<? extends Model> modelClass = (Class<? extends Model>) discoveredClass;
						mTableInfos.put(modelClass, new TableInfo(modelClass));
					}
					else if (ReflectionUtils.isTypeSerializer(discoveredClass)) {
						TypeSerializer typeSerializer = (TypeSerializer) discoveredClass.newInstance();
						mTypeSerializers.put(typeSerializer.getClass(), typeSerializer);
					}
				}
				catch (ClassNotFoundException e) {
					Log.e("Couldn't create class.", e.getMessage());
				}
				catch (InstantiationException e) {
					Log.e("Couldn't instantiate TypeSerializer.", e.getMessage());
				}
				catch (IllegalAccessException e) {
					Log.e("IllegalAccessException: ", e.getMessage());
				}
			}
		}
		catch (IOException e) {
			Log.e("Couln't open source path.", e.getMessage());
		}
		catch (NameNotFoundException e) {
			Log.e("Couldn't find ApplicationInfo for package name.", e.getMessage());
		}
	}

	//////////////////////////////////////////////////////////////////////////////////////
	// PUBLIC METHODS
	//////////////////////////////////////////////////////////////////////////////////////

	public List<TableInfo> getTableInfos() {
		return new ArrayList<TableInfo>(mTableInfos.values());
	}

	public TableInfo getTableInfo(Class<? extends Model> type) {
		return mTableInfos.get(type);
	}

	public List<Class<? extends Model>> getModelClasses() {
		return new ArrayList<Class<? extends Model>>(mTableInfos.keySet());
	}

	public TypeSerializer getParser(Class<?> type) {
		return mTypeSerializers.get(type);
	}
}
@@ -53,36 +53,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -1422,16 +1422,66 @@
 %09%09%09%09if (
+!name.contains(%22com.activeandroid.serializer%22) && 
 name.con
@@ -2853,33 +2853,25 @@
  %7B%0A%09%09return 
-new Array
+(
 List%3CTableIn
@@ -2873,17 +2873,18 @@
 bleInfo%3E
-(
+) 
 mTableIn
@@ -2895,17 +2895,16 @@
 values()
-)
 ;%0A%09%7D%0A%0A%09p
@@ -2995,24 +2995,56 @@
 (type);%0A%09%7D%0A%0A
+%09@SuppressWarnings(%22unchecked%22)%0A
 %09public List
@@ -3101,17 +3101,9 @@
 urn 
-new Array
+(
 List
@@ -3126,17 +3126,18 @@
  Model%3E%3E
-(
+) 
 mTableIn
@@ -3148,17 +3148,16 @@
 keySet()
-)
 ;%0A%09%7D%0A%0A%09p
@@ -3184,12 +3184,20 @@
  get
-Pars
+TypeSerializ
 er(C

Fix skipping of type parsers.