Annotation -> Class

This commit is contained in:
jeffcheasey88 2023-06-11 17:12:52 +02:00
parent 01107c788f
commit cc6142562b
3 changed files with 35 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
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.regex.Matcher;
@ -13,22 +14,41 @@ public class Class extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
private List<Annotation> annotations;
private int modifier;
private String name;
private List<JavaElement> childs;
public Class(){}
public Class(){
this.annotations = new ArrayList<>();
}
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
String[] split = matcher.group(2).split("\\s+");
for(int i = 0; i < split.length-1; i++){
this.modifier+=JavaParser.getModifier(split[i]);
Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, global, local);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
}
String type = value; //class interface enum
this.name = values.next();
if(values.hasNext()){
//extends implements
}
this.name = split[split.length-1];
this.childs = new ArrayList<>();
@ -100,6 +120,8 @@ public class Class extends JavaElement{
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations) annotation.build(buffer, tab);
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{");
buffer.add("");

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Import {
private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);)");
private static Pattern PATTERN = Pattern.compile("^(\\s*import\\s+([^;]*);)");
public static boolean isImport(String content){
return PATTERN.matcher(content).lookingAt();

View file

@ -24,6 +24,11 @@ public class 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 {
@ -90,8 +95,9 @@ public class JavaParser{
line = cleaner.clean(line);
index = line.indexOf("//");
if(index >= 0) line = line.substring(0, index);
content+=line;
content+=line+" ";
}
System.out.println(content);
this.pack = new Package();
index = this.pack.parse(content);