//可以动态查找出工作空间中已知父类的所有子类。 package util; import java.io.File; import java.io.IOException; import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Comparator; import java.util.Enumeration; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.jar.JarEntry; import java.util.jar.JarFile; /** * <code>ClassUtil</code> file contains code used for loading all * implementations of specified <em>interface</em> or <em>abstract class</em> * found in classpath. As a result of calling some functions you can have * <code>Set</code> containing all required classes. * * <p> * </p> */ public class ClassUtil { private static final String[] SKIP_CONTAINS = { ".ui.", ".swing", ".awt", ".sql.", ".xml.", ".terracotta." }; private static final String[] SKIP_STARTS = { "com.mysql", "tigase.pubsub.Utils", "org.apache.derby", "org.apache.xml", "org.postgresql", "com.sun", "groovy", "org.codehaus.groovy", "org.netbeans", "org.python" }; /** * Method description * * * @param fileName * * @return */ public static String getClassNameFromFileName(String fileName) { String class_name = null; if (fileName.endsWith(".class")) { // class_name = fileName.substring(0, // fileName.length()-6).replace(File.separatorChar, '.'); // Above code does not works on MS Windows if we load // files from jar file. Jar manipulation code always returns // file names with unix style separators String tmp_class_name = fileName.substring(0, fileName.length() - 6).replace('\\', '.'); class_name = tmp_class_name.replace('/', '.'); } // end of if (entry_name.endsWith(".class")) return class_name; } /** * Method description * * * @param dir * * @return */ public static Set<String> getClassNamesFromDir(File dir) { Set<String> tmp_set = getFileListDeep(dir); Set<String> result = new TreeSet<String>(); for (String elem : tmp_set) { String class_name = getClassNameFromFileName(elem); if (class_name != null) { result.add(class_name); // System.out.println("class name: "+class_name); } // end of if (class_name != null) } // end of for () return result; } /** * Method description * * * @param jarFile * * @return * * @throws IOException */ public static Set<String> getClassNamesFromJar(File jarFile) throws IOException { Set<String> result = new TreeSet<String>(); JarFile jar = new JarFile(jarFile); Enumeration<JarEntry> jar_entries = jar.entries(); while (jar_entries.hasMoreElements()) { JarEntry jar_entry = jar_entries.nextElement(); String class_name = getClassNameFromFileName(jar_entry.getName()); if (class_name != null) { result.add(class_name); // System.out.println("class name: "+class_name); } // end of if (entry_name.endsWith(".class")) } // end of while (jar_entries.hasMoreElements()) return result; } /** * Method description * * * @return * * @throws ClassNotFoundException * @throws IOException */ public static Set<Class<?>> getClassesFromClassPath() throws IOException, ClassNotFoundException { Set<Class<?>> classes_set = new TreeSet<Class<?>>(new ClassComparator()); String classpath = System.getProperty("java.class.path"); // System.out.println("classpath: "+classpath); StringTokenizer stok = new StringTokenizer(classpath, File.pathSeparator, false); while (stok.hasMoreTokens()) { String path = stok.nextToken(); File file = new File(path); if (file.exists()) { if (file.isDirectory()) { // System.out.println("directory: "+path); Set<String> class_names = getClassNamesFromDir(file); classes_set.addAll(getClassesFromNames(class_names)); } // end of if (file.isDirectory()) if (file.isFile()) { // System.out.println("jar file: "+path); Set<String> class_names = getClassNamesFromJar(file); classes_set.addAll(getClassesFromNames(class_names)); // System.out.println("Loaded jar file: "+path); } // end of if (file.isFile()) } // end of if (file.exists()) } // end of while (stok.hasMoreTokens()) return classes_set; } /** * Method description * * * @param names * * @return * * @throws ClassNotFoundException */ public static Set<Class<?>> getClassesFromNames(Set<String> names) throws ClassNotFoundException { Set<Class<?>> classes = new TreeSet<Class<?>>(new ClassComparator()); for (String name : names) { try { boolean skip_class = false; for (String test_str : SKIP_CONTAINS) { skip_class = name.contains(test_str); if (skip_class) { break; } } if (!skip_class) { for (String test_str : SKIP_STARTS) { skip_class = name.startsWith(test_str); if (skip_class) { break; } } } if (!skip_class) { // System.out.println(new Date() + " - Class name: " + name); Class cls = Class.forName(name, false, ClassLoader.getSystemClassLoader()); classes.add(cls); } } catch (SecurityException e) { } catch (NoClassDefFoundError e) { } catch (UnsatisfiedLinkError e) { } catch (Throwable e) { Throwable cause = e.getCause(); System.out.println("Class name: " + name); e.printStackTrace(); if (cause != null) { cause.printStackTrace(); } } } // end of for () return classes; } /** * Method description * * * @param classes * @param cls * @param <T> * * @return */ @SuppressWarnings({ "unchecked" }) public static <T extends Class> Set<T> getClassesImplementing(Collection<Class<?>> classes, T cls) { Set<T> classes_set = new TreeSet<T>(new ClassComparator()); for (Class c : classes) { // System.out.println("Checking class: " + c.getName()); if (cls.isAssignableFrom(c)) { int mod = c.getModifiers(); if (!Modifier.isAbstract(mod) && !Modifier.isInterface(mod)) { classes_set.add((T) c); } // end of if (!Modifier.isAbstract(mod) && !Modifier.isInterface(mod)) } // end of if (cls.isAssignableFrom(c)) } // end of for () return classes_set; } /** * Method description * * * @param cls * @param <T> * * @return * * @throws ClassNotFoundException * @throws IOException */ public static <T extends Class> Set<T> getClassesImplementing(T cls) throws IOException, ClassNotFoundException { return getClassesImplementing(getClassesFromClassPath(), cls); } /** * Method description * * * @param path * * @return */ public static Set<String> getFileListDeep(File path) { Set<String> set = new TreeSet<String>(); if (path.isDirectory()) { String[] files = path.list(); for (String file : files) { walkInDirForFiles(path, file, set); } // end of for () } else { set.add(path.toString()); } // end of if (file.isDirectory()) else return set; } /** * Method description * * * @param obj * @param <T> * * @return * * @throws ClassNotFoundException * @throws IOException * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings("unchecked") public static <T> Set<T> getImplementations(Class<T> obj) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { Set<T> result = new TreeSet<T>(new ObjectComparator()); for (Class cls : getClassesImplementing(obj)) { result.add((T) cls.newInstance()); } // end of for () return result; } /** * Method description * * * @param base_dir * @param path * @param set */ public static void walkInDirForFiles(File base_dir, String path, Set<String> set) { File tmp_file = new File(base_dir, path); if (tmp_file.isDirectory()) { String[] files = tmp_file.list(); for (String file : files) { walkInDirForFiles(base_dir, new File(path, file).toString(), set); } // end of for () } else { // System.out.println("File: " + path.toString()); set.add(path); } // end of if (file.isDirectory()) else } public static void main(String[] args) { try { Set<Class<Comparator>> procs= ClassUtil.getClassesImplementing(Comparator.class); System.out.println(procs.size()); for (Class<Comparator> c:procs) { System.out.println(c.getName()); } //2 //util.ClassComparator //util.ObjectComparator } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // ClassUtil
/* * Tigase Jabber/XMPP Utils * Copyright (C) 2004-2012 "Artur Hefczyc" <artur.hefczyc@tigase.org> * * 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. * * 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. Look for COPYING file in the top folder. * If not, see http://www.gnu.org/licenses/. * * $Rev$ * Last modified by $Author$ * $Date$ */ package util; import java.util.Comparator; import java.io.Serializable; /** * In a few cases classes have to be kept in <code>Set</code>. This * <code>Comparator</code> implementation has been created to return proper * value for <code>compare</code> method and to make it possible to store * classes in any <code>Set</code>. * * <p> * Created: Sat Oct 9 22:27:54 2004 * </p> * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a> * @version $Rev$ */ public class ClassComparator implements Comparator<Class>, Serializable { private static final long serialVersionUID = 1L; // Implementation of java.util.Comparator /** * Method <code>compare</code> is used to perform * * @param c1 an <code>Object</code> value * @param c2 an <code>Object</code> value * @return an <code>int</code> value */ // @Override public int compare(Class c1, Class c2) { return c1.getName().compareTo(c2.getName()); } }// ClassComparator
/* * Tigase Jabber/XMPP Utils * Copyright (C) 2004-2012 "Artur Hefczyc" <artur.hefczyc@tigase.org> * * 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. * * 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. Look for COPYING file in the top folder. * If not, see http://www.gnu.org/licenses/. * * $Rev$ * Last modified by $Author$ * $Date$ */ package util; import java.util.Comparator; import java.io.Serializable; /** * Describe class ObjectComparator here. * * * Created: Tue May 17 23:53:20 2005 * * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a> * @version $Rev$ */ public class ObjectComparator implements Comparator<Object>, Serializable { private static final long serialVersionUID = 1L; /** * Describe <code>compare</code> method here. * * @param object an <code>Object</code> value * @param object1 an <code>Object</code> value * @return an <code>int</code> value */ public int compare(final Object o1, final Object o2) { return o1.getClass().getName().compareTo(o2.getClass().getName()); } } // ObjectComparator
相关推荐
实验四 查找 1. 实验目的或任务 通过指导学生上机实践,对...建立有序表,采用折半查找实现某一已知的关键字的查找(采用顺序表存储结构) 选择1,用折半查找法查找一个关键字,输入关键字长度,输入元素,输入要查找的
基于opencv的已知左右图像坐标求空间中三维坐标,以及已知空间坐标求其在左右图像中的二维坐标。
合肥工业大学数据结构试验七查找 包括完整的实验要求、实验预习报告、实验最终报告 实验要求: ...<5> 已知整型数组A[1..26]递增有序,设计算法以构造一棵平衡的二叉排序树来存放该数组中的所有元素。
在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。(C语言)
已知机械臂的D_H参数,求工作空间,使用matlab运行代码可以生成四个图像,有三维图
已知Q是一个非空队列,S是一个空栈。仅用队列和栈的ADT函数和少量工作变量,编写一个算法,将队列Q中的所有元素逆置
7.28⑤ 已知有向图和图中两个顶点u和v,试编写算法求有向图中从u到v的所有简单路径。 void AllPath(ALGraph g, VertexType sv, VertexType tv,StrARR &path, int &i);
合肥工业大学数据结构 查找实验 编写算法实现下列问题的求解。 (1) 对下列数据表,分别采用二分查找算法实现查找...(5) 已知整型数组A[1..26]递增有序,设计算法以构造一棵平衡的二叉排序树来存放该数组中的所有元素。
遍历已知父窗里子窗所有句柄 的实例
ArcGIS已知经纬度绘制点图层和已知空间点计算经纬度的方法
已知PID VID,自动查找USB串口号, 先增加using System.IO.Ports; using System.Management; using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices;
清楚说明了java三大特性: 抽象:处理...由于接口的信息中只有已知不可变值以及抽象方法,所以信息的绑定方式单一,从实现上可以满足继承但也不会出现网状的信息绑定效果,不会增加访问时的节点数。因此接口允许多继承。
继承:从一个已知的类中派生出一个新的类,新类可以拥有已知类的行为和属性,并且可以通过重写来增强已知类的能力。 多态:多态的本质就是一个程序中存在多个同名的不同方法,主要通过三种方式来实现: 通过子类对...
已知空间坐标和对应的属性,利用python的matplotlib绘制等值线图,可进行不规则的凹边界约束。是本文原创博客https://blog.csdn.net/csubai07/article/details/106482015源文件
rsa解密(已知nec).py
在P4P问题中,当空间4个点共面时,不仅摄像机坐标系与物体坐标系之间 的旋转矩阵R和平移向量t可以线性求解,而且可以同时确定摄像机的有效焦距f和像素比例,该程序设计模拟已知相机矩阵M情况下由已知三维空间点和图像...
已知不相同的数不超过10000个,现在需要在其中查找某个自然数,如找到则输出并统计这个自然数出现的次数,如没找到则输出NO。 Input 输入由多组测试数据组成。 每组测试数据输入包含n+1行; 第一行是两个整数n...
根据当前点距离空间三个确定点(x,y,z坐标已知)的距离,计算出当前点空间坐标{x,y,z}
3. 用顺序查找法在长度为n的线性表中进行查找,在等概率情况下,查找成功的平均比较次数是 。 4. 二分查找算法描述如下: intSearch_Bin(SST ST, KT key) { low=1 ; high=ST. length; while(low) { mid=(low+high)...
使用线性表的间接寻址( class IndirectList )方法,实现二分查找; 查找的数据由键盘输入; 输出线性表和查找的结果(包括次数);