179 lines
No EOL
5.1 KiB
Java
179 lines
No EOL
5.1 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.Iterator;
|
|
import java.util.List;
|
|
import java.util.function.BiFunction;
|
|
import java.util.function.Function;
|
|
|
|
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\\routes\\PuzzleResponse.java");
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
|
JavaParser parser = new JavaParser();
|
|
parser.parse(reader);
|
|
System.out.println("build-----------------");
|
|
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt"))));
|
|
|
|
reader = new BufferedReader(new FileReader(new File("/home/buildClazzFromParser.txt")));
|
|
String line;
|
|
while((line = reader.readLine()) != null) System.out.println(line);
|
|
reader.close();
|
|
}
|
|
|
|
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();
|
|
parser.parse(reader);
|
|
System.out.println("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();
|
|
parser.parse(reader);
|
|
|
|
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;
|
|
|
|
//later, maybe put string in the element
|
|
private CleanerPool cleaner;
|
|
|
|
public JavaParser(){}
|
|
|
|
public void parse(BufferedReader reader) throws Exception{
|
|
String content = "";
|
|
int index;
|
|
|
|
cleaner = new CleanerPool(
|
|
new Cleaner("CONSTANT_STRING",'"','"'),
|
|
new Cleaner("CONSTANT_CHAR",'\'','\''));
|
|
|
|
String line;
|
|
while((line = reader.readLine()) != null){
|
|
line = cleaner.clean(line);
|
|
index = line.indexOf("//");
|
|
if(index >= 0) line = line.substring(0, index);
|
|
content+=line+" ";
|
|
}
|
|
System.out.println(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, null);
|
|
content = content.substring(index);
|
|
}
|
|
|
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep){
|
|
return (E)this.clazz.find(search, deep, new ArrayList<>());
|
|
}
|
|
|
|
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search){
|
|
return (E) this.clazz.find(search, new ArrayList<>());
|
|
}
|
|
|
|
public Package getPackage(){
|
|
return this.pack;
|
|
}
|
|
|
|
public List<Import> getImports(){
|
|
return this.imports;
|
|
}
|
|
|
|
public Class getClazz(){
|
|
return this.clazz;
|
|
}
|
|
|
|
public void build(BufferedWriter writer) throws Exception{
|
|
writer.write("package "+this.pack.getName()+";\n");
|
|
writer.write("\n");
|
|
for(Import element : this.imports) writer.write("import "+element.getName()+";\n");
|
|
writer.write("\n");
|
|
|
|
ArrayBuffer<String> buffer = new ArrayBuffer<String>(String.class);
|
|
buffer.add("");
|
|
this.clazz.build(buffer, 0);
|
|
|
|
Iterator<String> lines = buffer.iterator();
|
|
while(lines.hasNext()){
|
|
String line = lines.next();
|
|
line = this.cleaner.unzip(line, (s,p) -> s);
|
|
writer.write(line+"\n");
|
|
}
|
|
|
|
writer.flush();
|
|
writer.close();
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
} |