@SuppressWarnings({ "unchecked", "rawtypes" }) public static <A> List<Class<A>> getClassesAsList(String pckgname, Class<A> type) throws ClassNotFoundException
//package com.java2s; /****************************************************************************** * Copyright (C) 2009-2015 BIMserver.org * /*ww w. j a va 2s . co m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *****************************************************************************/ import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.List; public class Main { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <A> List<Class<A>> getClassesAsList(String pckgname, Class<A> type) throws ClassNotFoundException { ArrayList<Class<A>> classes = new ArrayList<Class<A>>(); File directory = null; try { ClassLoader cld = Thread.currentThread() .getContextClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); } String path = pckgname.replace('.', '/'); URL resource = cld.getResource(path); if (resource == null) { throw new ClassNotFoundException("No resource for " + path); } directory = new File(resource.getFile()); } catch (NullPointerException x) { throw new ClassNotFoundException(pckgname + " (" + directory + ") does not appear to be a valid package"); } if (directory.exists()) { String[] files = directory.list(); for (int i = 0; i < files.length; i++) { if (files[i].endsWith(".class")) { Class forName = Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6)); if (type.isAssignableFrom(forName) && type != forName) { classes.add(forName); } } } } else { throw new ClassNotFoundException(pckgname + " does not appear to be a valid package"); } return classes; } }