反射の例


        !          reflect SDK
public class ViewClassInfoFrame extends JFrame implements ActionListener{
    JTextField classnameField = new JTextField();
    JButton  viewinfoButton = new JButton();
    JLabel hintLabel = new JLabel();
    JTextArea infoTextArea  = new JTextArea();
    JScrollPane infoScrollPane = new JScrollPane();
    TitledBorder titledBorder;
    
    JPanel  upPanel = new JPanel();
    JPanel  centerPanel  = new JPanel();
    
    BorderLayout mainFrameBorderLayout = new BorderLayout();
    BorderLayout centerPanelBorderLayout = new BorderLayout();
    BorderLayout upPanelBorderLayout = new BorderLayout();
   
    /**      */
    public ViewClassInfoFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        init();
        validate();
    }
    /**     */
    private void init(){
        
        classnameField.setFont(new Font("Dialog",0,15));
        classnameField.setSelectedTextColor(Color.white);
        classnameField.setText("");
        
        viewinfoButton.setFont(new Font("Dialog",0,13));
        viewinfoButton.setText("       ");
        viewinfoButton.addActionListener(this);
        hintLabel.setFont(new Font("Dialog",0,13));
        hintLabel.setText("   ");
        
        infoTextArea.setFont(new Font("Dialog",0,14));
        infoTextArea.setEditable(false);
        infoTextArea.setText("");
        
        titledBorder = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134,134,134)),"    ");
        infoScrollPane.setBorder(titledBorder);
        infoScrollPane.getViewport().add(infoTextArea,null);
        
        upPanel.setLayout(upPanelBorderLayout);
        centerPanel.setLayout(centerPanelBorderLayout);
        upPanel.add(hintLabel,BorderLayout.NORTH);
        upPanel.add(classnameField,BorderLayout.CENTER);
        upPanel.add(viewinfoButton,BorderLayout.SOUTH);
        
        centerPanel.add(infoScrollPane);
        
        this.getContentPane().setLayout(mainFrameBorderLayout);
        this.setSize(new Dimension(450,360));
        this.setTitle("           ");
        
        this.getContentPane().add(upPanel,BorderLayout.NORTH);
        this.getContentPane().add(centerPanel,BorderLayout.CENTER);
        
        this.getRootPane().setDefaultButton(viewinfoButton);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        String classname = classnameField.getText();
        StringBuffer  buffer  =  new StringBuffer();
        try {
            //            
            Class c = Class.forName(classname);
            buffer.append("/**   **/ 
"); buffer.append(getClassStatement(c)); buffer.append("
"); buffer.append("/** **/"); buffer.append(getFields(c)); buffer.append("/** **/"); buffer.append(getConstructors(c)); buffer.append("/** **/"); buffer.append(getMethods(c)); buffer.append("}
"); } catch (Exception e1) { JOptionPane.showMessageDialog(this, " :" + e1.getMessage()); } infoTextArea.setText(buffer.toString()); } public String getClassStatement(Class c){ StringBuffer buffer = new StringBuffer(); if(c.getName().equals("java.lang.object")){ buffer.append("public class Ojbect{"); return buffer.toString(); }else { String supername = c.getSuperclass().getName(); buffer.append("public class").append(c.getName()); buffer.append("extends").append(supername).append(";"); } return buffer.toString(); } /** * * @param c * @return */ public String getFields(Class c){ StringBuffer buffer = new StringBuffer(); Field field = null; Field [] fields = c.getFields(); for (int i = 0; i < fields.length; i++) { field = fields[i]; buffer.append(Modifier.toString(field.getModifiers())).append(" "); Class type = field.getType(); buffer.append(type.getName()).append(" "); buffer.append(field.getName()).append(";
");// } return buffer.toString(); } /** * * @param c * @return String */ public String getConstructors(Class c){ StringBuffer buffer = new StringBuffer(); Constructor [] constructors = c.getDeclaredConstructors(); Constructor constructor = null; for (int i = 0; i < constructors.length; i++) { constructor = constructors[i]; buffer.append(Modifier.toString(constructor.getModifiers())).append(" "); buffer.append(constructor.getName()).append("("); // Class[] paramsTypes= constructor.getParameterTypes(); Class class1= null; for (int j = 0; j < paramsTypes.length; j++) { class1 = paramsTypes[j]; if (j==(paramsTypes.length-1)) { buffer.append(class1.getName()); }else { buffer.append(class1.getName()).append(","); } } buffer.append(")"); // Class[] exceTypes = constructor.getExceptionTypes(); for (int j = 0; j < exceTypes.length; j++) { class1 = exceTypes[j]; if (j==0) { buffer.append("throws"); } if(j==(exceTypes.length-1)){ buffer.append(class1.getName()); } else { buffer.append(class1.getName()).append(", "); } } buffer.append("
"); } return buffer.toString(); } /** * * @param c * @return String */ public String getMethods(Class c){ StringBuffer buffer = new StringBuffer(); Method[] methods= c.getMethods(); Method method = null; for (int i = 0; i < methods.length; i++) { method = methods[i]; buffer.append(Modifier.toString(method.getModifiers())).append(" "); Class returnType = method.getReturnType(); buffer.append(returnType.getName()).append(" "); buffer.append(method.getName()).append("("); // Class[] paramTypes =method.getParameterTypes(); Class class1 = null; for (int j = 0; j < paramTypes.length; j++) { class1 = paramTypes[j]; if (j==(paramTypes.length-1)) { buffer.append(class1.getName()); } else { buffer.append(class1.getName()).append(", "); } } buffer.append(")"); // Class[] exceTypes = method.getExceptionTypes(); for (int j = 0; j < exceTypes.length; j++) { class1 = exceTypes[j]; if (j==0) { buffer.append("throws"); } if(j==(exceTypes.length-1)){ buffer.append(class1.getName()); } else { buffer.append(class1.getName()).append(", "); } } buffer.append("
"); } return buffer.toString(); } /** * @param args */ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } ViewClassInfoFrame frame = new ViewClassInfoFrame(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height>screenSize.height) { frameSize.height= screenSize.height; } if (frameSize.width>screenSize.width) { frameSize.width= screenSize.width; } frame.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2); frame.setVisible(true); } }