-1

in this code the classwhat we called class "filepanel" in below code.it is neither static inner nor inner then what we called it?i want to check for such type of classes from java code using AST parser

    public class choosefile extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;
    JButton file,go;
    private Dimension preferredDimensions;
    public choosefile(){
        preferredDimensions = new Dimension(800, 700);
            JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            filepanel filepnl=new filepanel();
            frame.setPreferredSize(preferredDimensions);
            frame.add(filepnl);
            frame.pack();
            frame.setLocation(50,0);
            frame.setVisible(true);


    }
    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
    public static void main(String[] args)
    {
        new choosefile();
    }

}
class filepanel extends JPanel implements ActionListener{

    private static final long serialVersionUID = 1L;
    JScrollPane scroll,scroll1;
     JScrollBar bar;
    JButton file,go;
    JTextField txtbx;
    JTextArea txtarea1,txtarea2;
    public filepanel()
    {
         try {
                initComponents();

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Could not load frame", "Main Menu Error: 0x004", JOptionPane.ERROR_MESSAGE);
                System.exit(4);
            }
    }
      private void initComponents() throws Exception {
          setLayout(new GridBagLayout());
           GridBagConstraints c = new GridBagConstraints();
          file=new JButton("Choose File..");
         // JButton button1 = new JButton("1");
           file.addActionListener(this);


           c.fill = GridBagConstraints.PAGE_START;
           c.gridx = 0;
           c.gridy = 0;
           add(file, c);

           txtbx = new JTextField(" ");
           txtbx.setPreferredSize(new Dimension(150, 20));
           c.fill = GridBagConstraints.CENTER;
           c.weightx = 0.0;
           c.gridx = 1;
           c.gridy = 0;

           add(txtbx , c);

           go = new JButton(" Go ");
           go.addActionListener(this);

           go.setPreferredSize(new Dimension(5, 20));
           c.fill = GridBagConstraints.HORIZONTAL;
           c.weightx = 0.5;
           c.gridx = 2;
           c.gridy = 0;
           //c.insets = new Insets(0,350,0,0);
           add(go, c);
          txtarea1=new JTextArea();
          scroll = new JScrollPane(txtarea1);
           scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

          txtarea1.setPreferredSize(new Dimension(250, 620));
          c.fill = GridBagConstraints.HORIZONTAL;
           c.weightx = 0.5;
           c.gridx = 0;
           c.gridy = 1;
           c.insets = new Insets(0,10,0,0);
           add(scroll, c);
           txtarea2=new JTextArea();
           scroll1 = new JScrollPane(txtarea2);
           scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
              txtarea2.setPreferredSize(new Dimension(250, 620));
              c.fill = GridBagConstraints.HORIZONTAL;
               c.weightx = 0.5;
               c.gridx = 1;
               c.gridy = 1;
               c.insets = new Insets(0,3,0,0);
               add(scroll1, c);

      }
    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == file)
        {
            JFileChooser fileopen = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(".java", ".java");
            fileopen.addChoosableFileFilter(filter);

            int ret = fileopen.showDialog(null, "Open file");

            if (ret == JFileChooser.APPROVE_OPTION) {
              File file = fileopen.getSelectedFile();       
              String text = readFile(file);
              txtarea1.setText(text);
             text=search(file);
              txtarea2.setText(text);
            }
        }
        }
    private String readFile(File file2)
    {
    StringBuffer fileBuffer = null;
        String fileString = null;
        String line = null;
    try
    {
            FileReader in = new FileReader(file2);
            BufferedReader brd = new BufferedReader(in);
            fileBuffer = new StringBuffer();

            while ((line = brd.readLine()) != null) {
               fileBuffer.append(line).append( System.getProperty("line.separator"));

               }

            in.close();
            fileString = fileBuffer.toString();
        } catch (IOException e) {
            return null;
        }
        return fileString;
    }
    String search(File file1){
        //JTree tree = new JTree();
        StringBuffer fileBuffer = null;
        String fileString = null;

          try{
                // Open the file that is the first 
                // command line parameter
                FileInputStream fstream = new FileInputStream(file1);


                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                fileBuffer = new StringBuffer();

                //Read File Line By Line            
                while ((strLine = br.readLine()) != null){
                     if (strLine.contains("import")){

                         fileBuffer.append("The import library  is: " + strLine.substring(strLine.indexOf("import ") + 6, strLine.indexOf(";"))).append( System.getProperty("line.separator"));
                         //System.out.println ("The import library  is: " + strLine.substring(strLine.indexOf("import ") + 6, strLine.indexOf(";")));
                     }


                    if (strLine.contains("class")){
                         fileBuffer.append("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{"))).append( System.getProperty("line.separator"));

                       // System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
                    }

                     if (strLine.contains("int")){
                         fileBuffer.append("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";"))).append( System.getProperty("line.separator"));

                        //System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
                    }
                     if (strLine.contains("//")){
                         fileBuffer.append("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));

                        //System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
                    }
                    // fileBuffer.append(strLine).append( System.getProperty("line.separator"));

                }


                //Close the input stream
                in.close();
                fileString = fileBuffer.toString();
            }

            catch (Exception e){
                //Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }

        return fileString;

    }

}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
rosedoli
  • 39
  • 6
  • Please don't use DataInputStream to read a text file. You don't need it so please remove it as people might copy this code. – Peter Lawrey Apr 24 '13 at 08:22

1 Answers1

0

filepanel is JUST ANOTHER Class defined in same .java file as choosefile class. You can define as many classes you want in a .java file. But you can have only one public class per .java file.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • Ok,now if i want to extract information of both class for example their names,interface if implements ...etc.how can i do it using AST parser? – rosedoli Apr 08 '13 at 14:44