package be.jeffcheasey88.peeratcode.parser.java; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; public class JavaParser{ public static void maine(String[] args) throws Exception { File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); JavaParser parser = new JavaParser(reader); parser.parse(); System.out.println("SHOW-----------------"); parser.show(); } public static void main(String[] args) throws Exception { File dir = new File("C:\\Users\\jeffc\\eclipse-workspace\\"); show(dir); } private static void show(File dir) throws Exception{ if(dir.isFile()){ if(!dir.getName().endsWith(".java")) return; System.out.println("| "+dir.getAbsolutePath()); BufferedReader reader = new BufferedReader(new FileReader(dir)); try { JavaParser parser = new JavaParser(reader); parser.parse(); Class clazz = parser.getClazz(); System.out.println(clazz.getName()); for(Variable variable : clazz.getVariables()){ variable.show(1); } }catch(Exception e) {} }else{ for(File file : dir.listFiles()) show(file); } } private Package pack; private List imports; private Class clazz; private BufferedReader reader; public JavaParser(BufferedReader reader){ this.reader = reader; } public void parse() throws Exception{ String content = ""; int index; String line; while((line = reader.readLine()) != null) content+=line; // content = CleanerPool.getterToDelete.clean(content); this.pack = new Package(); index = this.pack.parse(content); content = content.substring(index); this.imports = new ArrayList<>(); while(Import.isImport(content)){ Import imp = new Import(); index = imp.parse(content); this.imports.add(imp); content = content.substring(index); } this.clazz = new Class(); index = this.clazz.parse(content, new CleanerPool(new Cleaner("CONSTANT_STRING",'"','"'))); content = content.substring(index); } public Package getPackage(){ return this.pack; } public List getImports(){ return this.imports; } public Class getClazz(){ return this.clazz; } public void show(){ System.out.println("package "+this.pack.getName()+";"); System.out.println(); for(Import i : this.imports) System.out.println("import "+i.getName()+";"); System.out.println(); this.clazz.show(); } public static int getModifier(String modifier){ switch(modifier){ case "public": return Modifier.PUBLIC; case "private": return Modifier.PRIVATE; case "protected": return Modifier.PROTECTED; case "static": return Modifier.STATIC; case "final": return Modifier.FINAL; case "synchronized": return Modifier.SYNCHRONIZED; case "volatile": return Modifier.VOLATILE; case "transient": return Modifier.TRANSIENT; case "native": return Modifier.NATIVE; case "abstract": return Modifier.ABSTRACT; case "strictfp": return Modifier.STRICT; default: break; } return 0; } }