All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mobi.cangol.mobile.utils.ClassUtils Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
/**
 * Copyright (c) 2013 Cangol
 * 

* Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mobi.cangol.mobile.utils; import android.content.Context; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import dalvik.system.DexFile; import mobi.cangol.mobile.logging.Log; public class ClassUtils { private ClassUtils() { } /** * 加载类 * * @param context * @param className * @return */ public static Class loadClass(Context context, String className) { Class clazz = null; try { clazz = context.getClassLoader().loadClass(className); } catch (ClassNotFoundException e) { Log.d(e.getMessage()); } return clazz; } /** * 获取接口的所有实现类 * * @param c * @param context * @param packageName * @return */ @SuppressWarnings("unchecked") public static List> getAllClassByInterface(Class c, Context context, String packageName) { List> returnClassList = new ArrayList>(); if (c.isInterface()) { List> allClass = getAllClassByPackage(packageName, context); for (int i = 0; i < allClass.size(); i++) { if (c.isAssignableFrom(allClass.get(i)) && !allClass.get(i).isInterface()) { returnClassList.add((Class) allClass.get(i)); } } allClass = null; //System.gc(); } else { Log.e("class " + c + " is not Interface"); } return returnClassList; } /** * 获取包内所有类 * * @param packageName * @param context * @return */ public static List> getAllClassByPackage(String packageName, Context context) { List> classList = new ArrayList>(); List list = getAllClassNameFromDexFile(context, packageName); Class clazz = null; try { for (String classNane : list) { if (classNane.startsWith(packageName)) { clazz = (Class) context.getClassLoader().loadClass(classNane); classList.add(clazz); clazz = null; } } list = null; //System.gc(); } catch (ClassNotFoundException e) { Log.e("ClassNotFoundException " + e.getMessage()); } return classList; } /** * 获取dexFile所有类 * * @param context * @return */ public static List getAllClassNameFromDexFile(Context context, String packageName) { List classList = new ArrayList(); try { DexFile df = new DexFile(context.getPackageCodePath()); String str = null; if (df != null) { for (Enumeration iter = df.entries(); iter.hasMoreElements(); ) { str = iter.nextElement(); if (packageName != null && str.startsWith(packageName)) { classList.add(str); } else if (packageName == null || "".equals(packageName)) { classList.add(str); } } } else { Log.e("DexFile " + context.getPackageCodePath() + " is null"); } df.close(); //System.gc(); } catch (IOException e) { Log.e("IOException " + e.getMessage()); } return classList; } /** * 获取dexFile所有类 * * @param context * @return */ public static List> getAllClassFromDexFile(Context context, String packageName) { List> classList = new ArrayList>(); try { DexFile df = new DexFile(context.getPackageCodePath()); Class clazz = null; String str = null; if (df != null) { for (Enumeration iter = df.entries(); iter.hasMoreElements(); ) { str = iter.nextElement(); if (packageName != null && str.startsWith(packageName)) { clazz = (Class) context.getClassLoader().loadClass(str); classList.add(clazz); } else if (packageName == null || "".equals(packageName)) { clazz = (Class) context.getClassLoader().loadClass(str); classList.add(clazz); } clazz = null; } } else { Log.e("DexFile " + context.getPackageCodePath() + " is null"); } df.close(); //System.gc(); } catch (IOException e) { Log.e("IOException " + e.getMessage()); } catch (ClassNotFoundException e) { Log.e("ClassNotFoundException " + e.getMessage()); } return classList; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy