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.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -13,22 +14,41 @@ public class Class extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
private List<Annotation> annotations;
private int modifier; private int modifier;
private String name; private String name;
private List<JavaElement> childs; private List<JavaElement> childs;
public Class(){} public Class(){
this.annotations = new ArrayList<>();
}
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
String[] split = matcher.group(2).split("\\s+"); Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
for(int i = 0; i < split.length-1; i++){ String value = null;
this.modifier+=JavaParser.getModifier(split[i]); 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<>(); this.childs = new ArrayList<>();
@ -100,6 +120,8 @@ public class Class extends JavaElement{
@Override @Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{ public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations) annotation.build(buffer, tab);
super.build(buffer, tab); super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{"); buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{");
buffer.add(""); buffer.add("");

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Import { 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){ public static boolean isImport(String content){
return PATTERN.matcher(content).lookingAt(); return PATTERN.matcher(content).lookingAt();

View file

@ -24,6 +24,11 @@ public class JavaParser{
parser.parse(reader); parser.parse(reader);
System.out.println("build-----------------"); System.out.println("build-----------------");
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt")))); 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 { public static void mainee(String[] args) throws Exception {
@ -90,8 +95,9 @@ public class JavaParser{
line = cleaner.clean(line); line = cleaner.clean(line);
index = line.indexOf("//"); index = line.indexOf("//");
if(index >= 0) line = line.substring(0, index); if(index >= 0) line = line.substring(0, index);
content+=line; content+=line+" ";
} }
System.out.println(content);
this.pack = new Package(); this.pack = new Package();
index = this.pack.parse(content); index = this.pack.parse(content);