152 lines
4 KiB
Java
152 lines
4 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
|
|
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
|
|
|
public class JavaParser{
|
|
|
|
public static void main(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 mainee(String[] args) throws Exception {
|
|
File dir = new File("");
|
|
show(dir);
|
|
}
|
|
|
|
public static void mainf(String[] args) throws Exception{
|
|
String variable = "var myName = \"Test\";";
|
|
|
|
String clazz = "package test; public class Test{ "+variable+" }";
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt")));
|
|
writer.write(clazz);
|
|
writer.flush();
|
|
writer.close();
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(new File("/home/tmp.txt")));
|
|
|
|
JavaParser parser = new JavaParser(reader);
|
|
parser.parse();
|
|
System.out.println("SHOW-----------------");
|
|
parser.show();
|
|
}
|
|
|
|
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<Import> imports;
|
|
private Class clazz;
|
|
|
|
private BufferedReader reader;
|
|
|
|
public JavaParser(BufferedReader reader){
|
|
this.reader = reader;
|
|
}
|
|
|
|
public void parse() throws Exception{
|
|
String content = "";
|
|
int index;
|
|
|
|
CleanerPool cleaner = new CleanerPool(
|
|
new Cleaner("CONSTANT_STRING",'"','"'),
|
|
new Cleaner("CONSTANT_CHAR",'\'','\''));
|
|
|
|
String line;
|
|
while((line = reader.readLine()) != null){
|
|
index = line.indexOf("//");
|
|
if(index >= 0) line = line.substring(0, index);
|
|
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, cleaner);
|
|
content = content.substring(index);
|
|
}
|
|
|
|
public Package getPackage(){
|
|
return this.pack;
|
|
}
|
|
|
|
public List<Import> 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;
|
|
}
|
|
|
|
}
|