// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.util.regex.*;

public class JavaParse {
   private static String load(String filename) throws Exception {
      StringBuffer sb = new StringBuffer("");
      InputStream is = new FileInputStream(filename);
      int c;
      while((c = is.read()) >= 0) {
         sb.append((char)c);
      }
      return sb.toString();
   }
   private static final String classpat = "class [A-Za-z][A-Za-z0-9]*";
   private static final String methodpat = "[A-Za-z][A-Za-z0-9]*\\(";
   private static final String attr1pat = "[A-Za-z][A-Za-z0-9]*;";
   private static final String attr2pat = "[A-Za-z][A-Za-z0-9]* =";
   private static final String startbracket = "\\{";
   private static final String endbracket = "\\}";
   private static final String allpat = classpat + "|" + methodpat + "|" +
                                        attr1pat + "|" + attr2pat + "|" +
                                        startbracket + "|" + endbracket;
   private static final Pattern pall = Pattern.compile(allpat);
   private static final Pattern pclass = Pattern.compile(classpat);
   private static final Pattern pmethod = Pattern.compile(methodpat);
   private static final Pattern pattr1 = Pattern.compile(attr1pat);
   private static final Pattern pattr2 = Pattern.compile(attr2pat);
   private static void parse(String code) {
      Matcher m = pall.matcher(code);
      int ind = 0;
      while(m.find()) {
         String s = m.group();
         if(pclass.matcher(s).matches()) {
            System.out.println("class=" + s.substring(6));
         } else if(ind==1 && pmethod.matcher(s).matches()) {
            System.out.println("method=" + s.substring(0, s.length() - 1));
         } else if(ind==1 && pattr1.matcher(s).matches()) {
            System.out.println("attribute=" + s.substring(0, s.length() - 1));
         } else if(ind==1 && pattr2.matcher(s).matches()) {
            System.out.println("attribute=" + s.substring(0, s.length() - 2));
         } else if(s.equals("{")) {
            ind++;
         } else if(s.equals("}")) {
            ind--;
         }
      }
   }
   public static void main(String[] args) throws Exception {
      String code = load("C:\\GUI.java");
      parse(code);
   }
}
