package treeroot.reflect;
import java.util.List;
public interface Node {
int CLASS=0x10000;
int INTERFACE=0x01000;
int CONSTRUCTOR=0x00100;
int METHOD=0x00010;
int FIELD=0x00001;
void setChilds(List childs);
boolean hasChild();
List getChilds();
String getName();
String getDetail();
int getType();
//in order to sort
int getOrder();
}
package treeroot.reflect;
import treeroot.reflect.Node;
import java.util.List;
import java.lang.reflect.Method;
public class MethodNode implements Node {
private int order;
private Method m;
public MethodNode(Method m){
this(m,0);
}
public MethodNode(Method m,int order){
this.m=m;
this.order=order;
}
/**
* Method setChilds
*
*
* @param parm1
*
*/
public void setChilds(List parm1) {
throw new UnsupportedOperationException("MothodNode has no child!");
// TODO: Add your code here
}
/**
* Method hasChild
*
*
* @return
*
*/
public boolean hasChild() {
return false;
// TODO: Add your code here
}
/**
* Method getChilds
*
*
* @return
*
*/
public List getChilds() {
throw new UnsupportedOperationException("MothodNode has no child!");
// TODO: Add your code here
}
/**
* Method getName
*
*
* @return
*
*/
public String getName() {
return m.getName()+ Helper.getParams(getDetail());
// TODO: Add your code here
}
/**
* Method getDetail
*
*
* @return
*
*/
public String getDetail() {
return m.toString();
// TODO: Add your code here
}
/**
* Method getType
*
*
* @return
*
*/
public int getType() {
return METHOD;
// TODO: Add your code here
}
/**
* Method getOrder
*
*
* @return
*
*/
public int getOrder() {
return order;
// TODO: Add your code here
}
public String toString(){
return getName();
}
}
package treeroot.reflect;
import treeroot.reflect.Node;
import java.util.List;
import java.lang.reflect.*;
public class InterfaceNode implements Node {
private Class c;
private int order;
private List childs;
public InterfaceNode(Class c){
this(c,0);
}
public InterfaceNode(Class c,int order){
if(!c.isInterface()){
throw new RuntimeException("InterfaceNode must accept a Interface!");
}
this.c=c;
this.order=order;
}
/**
* Method setChilds
*
*
* @param parm1
*
*/
public void setChilds(List parm1) {
this.childs=parm1;
// TODO: Add your code here
}
/**
* Method hasChild
*
*
* @return
*
*/
public boolean hasChild() {
return (childs!=null)&&(childs.size()>0);
// TODO: Add your code here
}
/**
* Method getChilds
*
*
* @return
*
*/
public List getChilds() {
return childs;
// TODO: Add your code here
}
/**
* Method getName
*
*
* @return
*
*/
public String getName() {
return c.getName();
// TODO: Add your code here
}
/**
* Method toString
*
*
* @return
*
*/
public String toString() {
return "<html><i>"+getName()+"</i></html>";
// TODO: Add your code here
}
/**
* Method getDetail
*
*
* @return
*
*/
public String getDetail() {
StringBuffer sb=new StringBuffer(Modifier.toString(c.getModifiers()));
sb.append(c.getName());
Class[] inters=c.getInterfaces();
if(inters.length>0){
sb.append(" implements ");
for(int i=0;i<inters.length;i++){
sb.append(inters[i].getName());
sb.append(',');
}
sb.setLength(sb.length()-1);
}
return sb.toString();
// TODO: Add your code here
}
/**
* Method getType
*
*
* @return
*
*/
public int getType() {
return INTERFACE;
// TODO: Add your code here
}
public int getOrder(){
return order;
}
}
package treeroot.reflect;
import treeroot.reflect.Node;
import java.util.List;
import java.lang.reflect.Modifier;
import java.lang.reflect.Field;
public class FieldNode implements Node {
private int order;
private int mode;
private Field field;
public FieldNode(Field field){
this(field,0);
}
public FieldNode(Field field,int order){
this.field=field;
this.order=order;
}
/**
* Method setChilds
*
*
* @param parm1
*
*/
public void setChilds(List parm1) {
throw new UnsupportedOperationException("Field can hava Child Nodes!");
// TODO: Add your code here
}
/**
* Method hasChild
*
*
* @return
*
*/
public boolean hasChild() {
return false;
// TODO: Add your code here
}
/**
* Method getChilds
*
*
* @return
*
*/
public List getChilds() {
//you should call the hasChild() to test.
throw new UnsupportedOperationException("Field can hava Child Nodes!");
// TODO: Add your code here
}
/**
* Method getName
*
*
* @return
*
*/
public String getName() {
return field.getName();
// TODO: Add your code here
}
/**
* Method getDetail
*
*
* @return
*
*/
public String getDetail() {
return field.toString();
// TODO: Add your code here
}
/**
* Method getType
*
*
* @return
*
*/
public int getType() {
return FIELD;
// TODO: Add your code here
}
public int getOrder(){
return order;
}
public String toString(){
return getName();
}
}
package treeroot.reflect;
import treeroot.reflect.Node;
import java.util.List;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class ConstructorNode implements Node {
private Constructor con;
private int order;
public ConstructorNode(Constructor con){
this(con,0);
}
public ConstructorNode(Constructor con,int order){
this.con=con;
this.order=order;
}
/**
* Method setChilds
*
*
* @param parm1
*
*/
public void setChilds(List parm1) {
throw new UnsupportedOperationException("ConstructorNode has no child!");
// TODO: Add your code here
}
/**
* Method hasChild
*
*
* @return
*
*/
public boolean hasChild() {
return false;
// TODO: Add your code here
}
/**
* Method getChilds
*
*
* @return
*
*/
public List getChilds() {
throw new UnsupportedOperationException("ConstructorNode has no child!");
// TODO: Add your code here
}
/**
* Method getName
*
*
* @return
*
*/
public String getName() {
return con.getName()+ Helper.getParams(getDetail());
// TODO: Add your code here
}
/**
* Method getDetail
*
*
* @return
*
*/
public String getDetail() {
return con.toString();
// TODO: Add your code here
}
/**
* Method getType
*
*
* @return
*
*/
public int getType() {
return CONSTRUCTOR;
// TODO: Add your code here
}
/**
* Method getOrder
*
*
* @return
*
*/
public int getOrder() {
return order;
// TODO: Add your code here
}
public String toString(){
return getName();
}
}
package treeroot.reflect;
import treeroot.reflect.Node;
import java.util.List;
import java.lang.reflect.*;
public class ClassNode implements Node {
private Class c;
private int order;
private List childs;
public ClassNode(Class c){
this(c,0);
}
public ClassNode(Class c,int order){
if(c.isInterface()||c.isArray()||c.isPrimitive()){
throw new RuntimeException("ClassNode must accept a Class!");
}
this.c=c;
this.order=order;
}
/**
* Method setChilds
*
*
* @param parm1
*
*/
public void setChilds(List parm1) {
this.childs=parm1;
// TODO: Add your code here
}
/**
* Method hasChild
*
*
* @return
*
*/
public boolean hasChild() {
return (childs!=null)&&(childs.size()>0);
// TODO: Add your code here
}
/**
* Method getChilds
*
*
* @return
*
*/
public List getChilds() {
return childs;
// TODO: Add your code here
}
/**
* Method getName
*
*
* @return
*
*/
public String getName() {
return c.getName();
// TODO: Add your code here
}
/**
* Method toString
*
*
* @return
*
*/
public String toString() {
return "<html><b>"+getName()+"</b></html>";// TODO: Add your code here
}
/**
* Method getDetail
*
*
* @return
*
*/
public String getDetail() {
StringBuffer sb=new StringBuffer(Modifier.toString(c.getModifiers()));
sb.append(" ");
sb.append(c.getName());
Class superClass=c.getSuperclass();
if(superClass!=null){
sb.append(" extends ");
sb.append(superClass.getName());
}
Class[] inters=c.getInterfaces();
if(inters.length>0){
sb.append(" implements ");
for(int i=0;i<inters.length;i++){
sb.append(inters[i].getName());
sb.append(',');
}
sb.setLength(sb.length()-1);
}
return sb.toString();
}
/**
* Method getType
*
*
* @return
*
*/
public int getType() {
return CLASS;
// TODO: Add your code here
}
public int getOrder(){
return order;
}
}
package treeroot.reflect;
class Helper {
static String getParams(String s){
return s.substring(s.indexOf('('),s.indexOf(')')+1);
}
}
package treeroot.reflect;
import java.util.*;
import java.lang.reflect.*;
public class ClassMeta {
public static Node getClassTree(Class c){
//
Node root;
if(c.isInterface()){
root=new InterfaceNode(c);
}
else{
root=new ClassNode(c);
}
List childs=new ArrayList();
//add super class as first node,if the class if Object.class,break recursion.
//only Class has the superClass.
Class superClass=c.getSuperclass();
if(superClass!=null)
childs.add(getClassTree(superClass));
//add the inteferaces
//Class or Interface must have the Interfaces
Class[] inters= c.getInterfaces();
for(int i=0;i<inters.length;i++){
childs.add(getClassTree(inters[i]));
}
//add the constructors
//Only Class have the Constructors.
Constructor[] cons=c.getConstructors();
for(int i=0;i<cons.length;i++){
childs.add(new ConstructorNode(cons[i]));
}
//add the methods
Method[] methods=c.getDeclaredMethods();
for(int i=0;i<methods.length;i++){
childs.add(new MethodNode(methods[i]));
}
//add the fields
Field[] fields=c.getDeclaredFields();
for(int i=0;i<fields.length;i++){
childs.add(new FieldNode(fields[i]));
}
root.setChilds(childs);
return root;
}
}
//以下是图形界面了
package treeroot.classviewer;
import treeroot.reflect.Node;
import treeroot.reflect.ClassMeta;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JScrollPane;
public class ClassViewer extends JPanel {
JTree tree;
DefaultTreeModel model;
JPanel detail=new JPanel();
JLabel lbl=new JLabel("the Detail Message");
private DefaultMutableTreeNode getRoot(Node node){
DefaultMutableTreeNode root=new DefaultMutableTreeNode(node);
if(node.hasChild()){
List childs=node.getChilds();
for(int i=0;i<childs.size();i++){
root.add(getRoot((Node)childs.get(i)));
}
}
return root;
}
public ClassViewer(){
DefaultMutableTreeNode root=getRoot(ClassMeta.getClassTree(Object.class));
model=new DefaultTreeModel(root);
tree=new JTree(model);
TreeSelectionModel selectModel =new DefaultTreeSelectionModel();
selectModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setSelectionModel(selectModel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node!=null){
Node n=(Node)node.getUserObject();
lbl.setText(n.getDetail());
}
}
}
);
this.setLayout(new BorderLayout());
this.add(new JScrollPane(tree),BorderLayout.CENTER);
detail.add(lbl);
this.add(detail,BorderLayout.SOUTH);
}
void setTree(Node treeRoot){
DefaultMutableTreeNode root=getRoot(treeRoot);
model=new DefaultTreeModel(root);
tree.setModel(model);
this.updateUI();
}
}
package treeroot.classviewer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import treeroot.reflect.*;
import javax.swing.tree.*;
public class MainFrame extends JFrame{
private JTextField txtClass=new JTextField("java.lang.Object",20);
private JButton btnClass=new JButton("confirm");
private JPanel topPanel=new JPanel();
ClassViewer tree=new ClassViewer();
public MainFrame(){
this.setTitle("Class Viewer");
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension d=kit.getScreenSize();
int width=d.width;
int height=d.height;
this.setLocation((width-WIDTH)/2,(height-HEIGHT)/2);
this.setSize(WIDTH,HEIGHT);
//this.setResizable(false);
topPanel.add(txtClass);
topPanel.add(btnClass);
btnClass.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Class c=null;
try{
String cls=txtClass.getText().trim();
c=Class.forName(cls);
}
catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,"can find the class!");
return;
}
Node root=ClassMeta.getClassTree(c);
tree.setTree(root);
}
}
);
this.getContentPane().add(topPanel,BorderLayout.NORTH);
this.getContentPane().add(tree,BorderLayout.CENTER);
}
public static void main(String[] args){
MainFrame main=new MainFrame();
//main.pack();
main.show();
}
private static int WIDTH=800;
private static int HEIGHT=600;
}