package com.ronsoft.books.nio.appendix;
import java.lang.reflect.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;
/**
* Extract information about a class. This class was used to generate
* the quick reference for the NIO book for O'Reilly. It's kind of ugly
* and there are a lot of hacks in here. The most obvious one is the
* wrapping of <emphasis> tags around class and member names in the
* toString() output.
* With a little work this could be made into a generic class introspector,
* but it currently has some warts on it.
*
* Created May 2002
* @author Ron Hitchens (ron@ronsoft.com)
* @version $Id: ClassInfo.java,v 1.2 2002/05/31 04:16:13 ron Exp $
*/
public class ClassInfo
{
private static final String SRC_DIR = "/usr/local/java/java1.4/src";
private Package packg;
private Class thisClass;
private Class superClass;
private Class declaringClass;
private Class [] interfaces;
private Field [] fields;
private Constructor [] constructors;
private Method [] methods;
private Class [] classes;
private ClassInfo [] internalClasses;
private int mods = Modifier.PUBLIC;
public ClassInfo (String className)
throws Exception
{
this (Class.forName (className));
}
public ClassInfo (String className, boolean prot)
throws Exception
{
this (Class.forName (className),
(prot) ? Modifier.PUBLIC | Modifier.PROTECTED : Modifier.PUBLIC);
}
public ClassInfo (Class clas)
throws Exception
{
this (clas, Modifier.PUBLIC);
}
public ClassInfo (Class clas, int mods)
throws Exception
{
thisClass = clas;
packg = clas.getPackage();
superClass = clas.getSuperclass();
interfaces = clas.getInterfaces();
declaringClass = clas.getDeclaringClass();
fields = trimFields (clas.getDeclaredFields(), mods);
constructors = trimConstructors (clas.getConstructors(), mods);
methods = trimMethods (clas.getDeclaredMethods(), mods);
classes = trimClasses (clas.getDeclaredClasses(), mods);
if (classes.length == 0) {
internalClasses = new ClassInfo [0];
} else {
List list = new LinkedList();
for (int i = 0; i < classes.length; i++) {
list.add (new ClassInfo (classes [i]));
}
internalClasses = new ClassInfo [list.size()];
list.toArray (internalClasses);
}
}
// ---------------------------------------------------------------
public boolean isInterface()
{
return (thisClass.isInterface());
}
public Class getSuperClass()
{
if ((superClass == null) || (superClass == Object.class)) {
return (null);
}
return (superClass);
}
// ---------------------------------------------------------------
// Given an array of Class or Member, return a new array containing
// only those elements with the matching modifier bits set.
// This is to filter out private and protected methods for example.
private Field [] trimFields (Field [] item, int mods)
{
List list = new LinkedList();
for (int i = 0; i < item.length; i++) {
if ((item [i].getModifiers() & mods) != 0) {
list.add (item [i]);
}
}
Field [] trimmed = new Field [list.size()];
list.toArray (trimmed);
Arrays.sort (trimmed, comper);
return (trimmed);
}
private Constructor [] trimConstructors (Constructor [] item, int mods)
{
List list = new LinkedList();
for (int i = 0; i < item.length; i++) {
if ((item [i].getModifiers() & mods) != 0) {
list.add (item [i]);
}
}
Constructor [] trimmed = new Constructor [list.size()];
list.toArray (trimmed);
Arrays.sort (trimmed, comper);
return (trimmed);
}
private Method [] trimMethods (Method [] item, int mods)
{
List list = new LinkedList();
for (int i = 0; i < item.length; i++) {
if ((item [i].getModifiers() & mods) != 0) {
list.add (item [i]);
}
}
Method [] trimmed = new Method [list.size()];
list.toArray (trimmed);
Arrays.sort (trimmed, comper);
return (trimmed);
}
private Class [] trimClasses (Class [] item, int mods)
{
List list = new LinkedList();
for (int i = 0; i < item.length; i++) {
if ((item [i].getModifiers() & mods) != 0) {
list.add (item [i]);
}
}
Class [] trimmed = new Class [list.size()];
list.toArray (trimmed);
Arrays.sort (trimmed, comper);
return (trimmed);
}
private Comper comper = new Comper();
private class Comper implements Comparator
{
public int compare (Object o1, Object o2)
{
if (o1 instanceof Class) {
Class c1 = (Class) o1;
Class c2 = (Class) o2;
String n1 = c1.getName();
String n2 = c2.getName();
if (c1.isArray()) {
n1 = c1.getComponentType().getName();
}
if (c2.isArray()) {
n2 = c2.getComponentType().getName();
}
return (n1.compareTo (n2));
}
Member m1 = (Member) o1;
Member m2 = (Member) o2;
String n1 = m1.getName();
String n2 = m2.getName();
if (( ! n1.equals (n2)) || (m1 instanceof Field)) {
return (n1.compareTo (n2));
}
if (m1 instanceof Constructor) {
return (compareArgs (
((Constructor) m1).getParameterTypes(),
((Constructor) m2).getParameterTypes()));
}
return (compareArgs (((Method) m1).getParameterTypes(),
((Method) m2).getParameterTypes()));
}
private int compareArgs (Class [] p1, Class [] p2)
{
int n = p1.length - p2.length;
if (n != 0) {
return (n);
}
for (int i = 0; i < p1.length; i++) {
n = comper.compare (p1 [i], p2 [i]);
if (n != 0) {
return (n);
}
}
return (0);
}
public boolean equals (Object o1)
{
return (false);
}
}
// ---------------------------------------------------------------
public String getSimpleClassName (String name)
{
String rep = name.replace ('$', '.');
int n = rep.lastIndexOf (".");
if (n == -1) {
return (rep);
}
return (rep.substring (n + 1));
}
public String getSimpleClassName (Class clas)
{
return (getSimpleClassName (clas.getName()));
}
public String getClassName (String name)
{
String pkgname = packg.getName();
if (name.startsWith ("java.lang")) {
return (name.substring ("java.lang".length() + 1));
}
if (name.startsWith (pkgname)) {
String simple = name.substring (pkgname.length() + 1);
// is the package name the whole prefix?
if (simple.indexOf (".") == -1) {
return (simple.replace ('$', '.'));
}
}
return (name.replace ('$', '.'));
}
public String getClassName (Class clas)
{
return (getClassName (clas.getName()));
}
public String getClassName()
{
return (getClassName (thisClass));
}
public String getModifierNames (int mods)
{
if (thisClass.isInterface()) {
mods &= ~Modifier.ABSTRACT;
}
return (Modifier.toString (mods));
}
public String getModifierNames()
{
return (getModifierNames (thisClass.getModifiers()));
}
private void indent (StringBuffer sb, String indent, int level)
{
for (int i = 0; i < level; i++) {
sb.append (indent);
}
}
// ---------------------------------------------------------------
public String toString()
{
return (toString ("\t"));
}
public String toString (String indent)
{
return (toString (indent, true));
}
public String toString (String indent, boolean listPackage)
{
StringBuffer sb = new StringBuffer();
stringify (sb, indent, 0, listPackage);
return (sb.toString());
}
void stringify (StringBuffer sb, String indent,
int indentLevel, boolean listPackage)
{
if (listPackage) {
indent (sb, indent, indentLevel);
sb.append ("package ").append (packg.getName());
sb.append ("\n\n");
}
indent (sb, indent, indentLevel);
sb.append (getModifierNames());
if (thisClass.isInterface()) {
sb.append (" ");
} else {
sb.append (" class ");
}
// HACK! This should be better parameterized
sb.append ("<emphasis>");
sb.append (getClassName());
sb.append ("</emphasis>");
Class sc = getSuperClass();
if (sc != null) {
sb.append ("\n");
indent (sb, indent, indentLevel);
sb.append (indent).append ("extends ");
sb.append (get
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
nio.rar (63个子文件)
nio
buffers
BufferWrap.java 515B
BufferDupe.java 828B
BufferSelf.java 350B
Unsigned.java 3KB
BufferAllocBenchmark.java 1KB
BufferFlip.java 651B
BufferWrap2.java 734B
BufferSlice.java 887B
BufferLongView.java 1KB
BufferFillDrain.java 1KB
Test.java 570B
BufferMap.java 670B
BufferCharView.java 1KB
appendix
ClassXml.java 1KB
genclass.sh 173B
ClassInfo.java 14KB
regrex
BackSlashes.java 1KB
Grep.java 9KB
poodle.xsl 1KB
Poodle.java 3KB
SimpleGrep.java 1KB
RegexAppend.java 2KB
RegexReplace.java 2KB
RegexMatches.java 591B
CharSeq.java 2KB
RegexSplit2.java 778B
EmailAddressFinder.java 2KB
Split.java 1KB
RegexSplit.java 507B
charset
RonsoftCharsetProvider.java 2KB
EncodeTest.java 2KB
CharsetDecode.java 4KB
Rot13Charset.java 8KB
channels
Marketing.java 3KB
LockOverlap.java 991B
MappedHttp.java 3KB
StreamFile.java 2KB
ConnectAsync.java 1KB
ChannelClose.java 5KB
FilePointer.java 985B
ChannelCopy.java 3KB
SystemInPipe.java 2KB
ChannelAccept.java 2KB
TimeService.java 8KB
DGKaboom.java 1KB
SelectTest.java 2KB
HttpServer.java 3KB
FileHole.java 1KB
TimeServer.java 3KB
SocketChannelCreate.java 890B
PipeTest.java 2KB
MapFile.java 4KB
SelectSocketsThreadPool.java 7KB
LockBug.java 700B
SelectSockets.java 5KB
TimeClientSelect.java 1KB
TimeServiceSelect.java 1KB
LockTest.java 4KB
TestStdin.java 1KB
ChannelTransfer.java 1KB
TimeClient.java 6KB
DGNonBlockBug.java 2KB
www.pudn.com.txt 218B
共 63 条
- 1
资源评论
JonSco
- 粉丝: 67
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功