Move annotation in an abstract class + add 2 AST Methiods [findAnnotation, hasAnnotation]

This commit is contained in:
jeffcheasey88 2023-11-07 11:58:15 +01:00
parent 9137136ef7
commit 3f4d824b33
8 changed files with 109 additions and 141 deletions

View file

@ -17,6 +17,66 @@ public class Annotation extends JavaElement{
}
public static abstract class Annotable extends JavaElement{
private List<Annotation> annotations;
public Annotable(List<Annotation> annotations){
this.annotations = annotations;
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public Annotation findAnnotation(Function<Annotation, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return annotation;
}
}
return null;
}
public boolean hasAnnotation(Function<Annotation, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return true;
}
}
return false;
}
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
return null;
}
@Override
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
}
}
private Token name;
private Map<Token, Value> values;

View file

@ -7,8 +7,6 @@ import dev.peerat.parser.Token;
public class AnnotationClass extends ClassBase{
private List<Annotation> annotations;
//where mod ????
private Token name;
private Token extend;
@ -16,7 +14,7 @@ public class AnnotationClass extends ClassBase{
private List<JavaElement> elements;
public AnnotationClass(List<Annotation> annotations, Token name){
this.annotations = annotations;
super(annotations);
this.name = name;
this.elements = new ArrayList<>();
}
@ -36,10 +34,6 @@ public class AnnotationClass extends ClassBase{
this.elements.add(clazz);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public Token getName(){
return this.name;
}
@ -54,11 +48,7 @@ public class AnnotationClass extends ClassBase{
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
builder.append("@interface");
builder.append(name);
@ -77,11 +67,9 @@ public class AnnotationClass extends ClassBase{
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) return (E) element;
@ -92,12 +80,7 @@ public class AnnotationClass extends ClassBase{
@Override
public <E extends JavaElement> void findAll(java.util.function.Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) list.add((E) element);

View file

@ -7,8 +7,6 @@ import dev.peerat.parser.Token;
public class Class extends ClassBase{
private List<Annotation> annotations;
//where mod ????
private Token name;
private Token extend;
@ -17,7 +15,7 @@ public class Class extends ClassBase{
private List<JavaElement> elements;
public Class(List<Annotation> annotations, Token name){
this.annotations = annotations;
super(annotations);
this.name = name;
this.elements = new ArrayList<>();
}
@ -43,10 +41,6 @@ public class Class extends ClassBase{
this.elements.add(clazz);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public Token getName(){
return this.name;
}
@ -65,11 +59,7 @@ public class Class extends ClassBase{
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
builder.append("class");
builder.append(name);
@ -92,11 +82,9 @@ public class Class extends ClassBase{
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) return (E) element;
@ -107,12 +95,7 @@ public class Class extends ClassBase{
@Override
public <E extends JavaElement> void findAll(java.util.function.Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) list.add((E) element);

View file

@ -3,6 +3,7 @@ package dev.peerat.parser.java;
import java.util.ArrayList;
import java.util.List;
import dev.peerat.parser.java.Annotation.Annotable;
import dev.peerat.parser.java.Annotation.AnnotableBuffer;
import dev.peerat.parser.java.Function.FunctionContainer;
import dev.peerat.parser.java.Variable.VariableContainer;
@ -13,10 +14,14 @@ interface ClassContainer{
}
public abstract class ClassBase extends JavaElement implements AnnotableBuffer, ClassContainer, FunctionContainer, VariableContainer{
public abstract class ClassBase extends Annotable implements AnnotableBuffer, ClassContainer, FunctionContainer, VariableContainer{
private List<Annotation> annotationBuffer;
public ClassBase(List<Annotation> annotations){
super(annotations);
}
@Override
public void addAnnotationBuffer(Annotation annotation){
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();

View file

@ -7,8 +7,6 @@ import dev.peerat.parser.Token;
public class Enumeration extends ClassBase{
private List<Annotation> annotations;
//where mod ????
private Token name;
private Token extend;
@ -16,7 +14,7 @@ public class Enumeration extends ClassBase{
private List<JavaElement> elements;
public Enumeration(List<Annotation> annotations, Token name){
this.annotations = annotations;
super(annotations);
this.name = name;
this.elements = new ArrayList<>();
}
@ -41,10 +39,6 @@ public class Enumeration extends ClassBase{
this.elements.add(clazz);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public Token getName(){
return this.name;
}
@ -59,11 +53,7 @@ public class Enumeration extends ClassBase{
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
builder.append("enum");
builder.append(name);
@ -82,11 +72,9 @@ public class Enumeration extends ClassBase{
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) return (E) element;
@ -97,12 +85,7 @@ public class Enumeration extends ClassBase{
@Override
public <E extends JavaElement> void findAll(java.util.function.Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) list.add((E) element);

View file

@ -4,12 +4,15 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import javax.xml.crypto.dsig.CanonicalizationMethod;
import dev.peerat.parser.Token;
import dev.peerat.parser.TokenType;
import dev.peerat.parser.java.Annotation.Annotable;
import dev.peerat.parser.java.Operation.OperationContainer;
import dev.peerat.parser.java.Variable.VariableContainer;
public class Function extends JavaElement implements VariableContainer, OperationContainer{
public class Function extends Annotable implements VariableContainer, OperationContainer{
public static interface FunctionContainer{
@ -17,8 +20,6 @@ public class Function extends JavaElement implements VariableContainer, Operatio
}
private List<Annotation> annotations;
private int mod;
private Token generic;
private Token type;
@ -29,7 +30,7 @@ public class Function extends JavaElement implements VariableContainer, Operatio
private List<JavaElement> elements;
public Function(List<Annotation> annotations, int mod, Token type, Token name){
this.annotations = annotations;
super(annotations);
this.mod = mod;
this.type = type;
this.name = name;
@ -61,10 +62,6 @@ public class Function extends JavaElement implements VariableContainer, Operatio
this.elements.add(operation);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public int getModifier(){
return this.mod;
}
@ -95,11 +92,7 @@ public class Function extends JavaElement implements VariableContainer, Operatio
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
String mod = Modifier.toString(this.mod);
builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP));
if(generic != null) builder.append(generic);
@ -129,11 +122,9 @@ public class Function extends JavaElement implements VariableContainer, Operatio
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
if(parameters != null){
for(Variable param : this.parameters){
if(finder.apply(param)) return (E) param;
@ -149,12 +140,7 @@ public class Function extends JavaElement implements VariableContainer, Operatio
@Override
public <E extends JavaElement> void findAll(java.util.function.Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(parameters != null){
for(Variable param : this.parameters){
if(finder.apply(param)) list.add((E) param);

View file

@ -7,8 +7,6 @@ import dev.peerat.parser.Token;
public class Interface extends ClassBase{
private List<Annotation> annotations;
//where mod ????
private Token name;
private Token extend;
@ -16,7 +14,7 @@ public class Interface extends ClassBase{
private List<JavaElement> elements;
public Interface(List<Annotation> annotations, Token name){
this.annotations = annotations;
super(annotations);
this.name = name;
this.elements = new ArrayList<>();
}
@ -41,10 +39,6 @@ public class Interface extends ClassBase{
this.elements.add(clazz);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public Token getName(){
return this.name;
}
@ -59,11 +53,7 @@ public class Interface extends ClassBase{
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
builder.append("interface");
builder.append(name);
@ -82,11 +72,9 @@ public class Interface extends ClassBase{
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) return (E) element;
@ -97,12 +85,7 @@ public class Interface extends ClassBase{
@Override
public <E extends JavaElement> void findAll(java.util.function.Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E) annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(elements != null){
for(JavaElement element : this.elements){
if(finder.apply(element)) list.add((E) element);

View file

@ -7,8 +7,9 @@ import java.util.function.Function;
import dev.peerat.parser.Token;
import dev.peerat.parser.TokenType;
import dev.peerat.parser.java.Annotation.Annotable;
public class Variable extends JavaElement{
public class Variable extends Annotable{
public static interface VariableContainer{
@ -16,8 +17,6 @@ public class Variable extends JavaElement{
}
private List<Annotation> annotations;
private int mod;
private Token type;
private Token name;
@ -25,10 +24,10 @@ public class Variable extends JavaElement{
private Value value;
public Variable(List<Annotation> annotations, int mod, Token type, Token name){
super(annotations);
this.mod = mod;
this.type = type;
this.name = name;
this.annotations = new ArrayList<>();
}
public Variable(List<Annotation> annotations, int mod, Token type, Token name, boolean elips){
@ -46,10 +45,6 @@ public class Variable extends JavaElement{
return "Variable[mod="+mod+", type="+type+", name="+name+", elips="+elips+", value="+value+"]";
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public int getModifier(){
return this.mod;
}
@ -72,11 +67,8 @@ public class Variable extends JavaElement{
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.build(builder);
}
}
super.build(builder);
String mod = Modifier.toString(this.mod);
builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP));
builder.append(type);
@ -90,22 +82,15 @@ public class Variable extends JavaElement{
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation;
}
}
E annotation = super.find(finder);
if(annotation != null) return annotation;
return value != null ? finder.apply(value) ? (E)value : null : null;
}
@Override
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
if(annotations != null){
for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) list.add((E)annotation);
annotation.findAll(finder, list);
}
}
super.findAll(finder, list);
if(value != null){
if(finder.apply(value)) list.add((E)value);
value.findAll(finder, list);