From 3f4d824b3380b4790a004cfd31b687d3b4e894ac Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 7 Nov 2023 11:58:15 +0100 Subject: [PATCH] Move annotation in an abstract class + add 2 AST Methiods [findAnnotation, hasAnnotation] --- src/dev/peerat/parser/java/Annotation.java | 60 +++++++++++++++++++ .../peerat/parser/java/AnnotationClass.java | 29 ++------- src/dev/peerat/parser/java/Class.java | 29 ++------- src/dev/peerat/parser/java/ClassBase.java | 7 ++- src/dev/peerat/parser/java/Enumeration.java | 29 ++------- src/dev/peerat/parser/java/Function.java | 34 ++++------- src/dev/peerat/parser/java/Interface.java | 29 ++------- src/dev/peerat/parser/java/Variable.java | 33 +++------- 8 files changed, 109 insertions(+), 141 deletions(-) diff --git a/src/dev/peerat/parser/java/Annotation.java b/src/dev/peerat/parser/java/Annotation.java index 73c33f4..e69c428 100644 --- a/src/dev/peerat/parser/java/Annotation.java +++ b/src/dev/peerat/parser/java/Annotation.java @@ -17,6 +17,66 @@ public class Annotation extends JavaElement{ } + public static abstract class Annotable extends JavaElement{ + + private List annotations; + + public Annotable(List annotations){ + this.annotations = annotations; + } + + public List getAnnotations(){ + return this.annotations; + } + + public Annotation findAnnotation(Function finder){ + if(annotations != null){ + for(Annotation annotation : this.annotations){ + if(finder.apply(annotation)) return annotation; + } + } + return null; + } + + public boolean hasAnnotation(Function 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 find(Function finder){ + if(annotations != null){ + for(Annotation annotation : this.annotations){ + if(finder.apply(annotation)) return (E) annotation; + } + } + return null; + } + + @Override + public void findAll(Function finder, List 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 values; diff --git a/src/dev/peerat/parser/java/AnnotationClass.java b/src/dev/peerat/parser/java/AnnotationClass.java index 04055f6..72481db 100644 --- a/src/dev/peerat/parser/java/AnnotationClass.java +++ b/src/dev/peerat/parser/java/AnnotationClass.java @@ -7,8 +7,6 @@ import dev.peerat.parser.Token; public class AnnotationClass extends ClassBase{ - private List annotations; - //where mod ???? private Token name; private Token extend; @@ -16,7 +14,7 @@ public class AnnotationClass extends ClassBase{ private List elements; public AnnotationClass(List 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 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 find(java.util.function.Function 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 void findAll(java.util.function.Function finder, List 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); diff --git a/src/dev/peerat/parser/java/Class.java b/src/dev/peerat/parser/java/Class.java index 2bdf90b..5503c08 100644 --- a/src/dev/peerat/parser/java/Class.java +++ b/src/dev/peerat/parser/java/Class.java @@ -7,8 +7,6 @@ import dev.peerat.parser.Token; public class Class extends ClassBase{ - private List annotations; - //where mod ???? private Token name; private Token extend; @@ -17,7 +15,7 @@ public class Class extends ClassBase{ private List elements; public Class(List 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 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 find(java.util.function.Function 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 void findAll(java.util.function.Function finder, List 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); diff --git a/src/dev/peerat/parser/java/ClassBase.java b/src/dev/peerat/parser/java/ClassBase.java index c1ca659..73a3774 100644 --- a/src/dev/peerat/parser/java/ClassBase.java +++ b/src/dev/peerat/parser/java/ClassBase.java @@ -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 annotationBuffer; + public ClassBase(List annotations){ + super(annotations); + } + @Override public void addAnnotationBuffer(Annotation annotation){ if(annotationBuffer == null) annotationBuffer = new ArrayList<>(); diff --git a/src/dev/peerat/parser/java/Enumeration.java b/src/dev/peerat/parser/java/Enumeration.java index 1c93c24..b757bcb 100644 --- a/src/dev/peerat/parser/java/Enumeration.java +++ b/src/dev/peerat/parser/java/Enumeration.java @@ -7,8 +7,6 @@ import dev.peerat.parser.Token; public class Enumeration extends ClassBase{ - private List annotations; - //where mod ???? private Token name; private Token extend; @@ -16,7 +14,7 @@ public class Enumeration extends ClassBase{ private List elements; public Enumeration(List 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 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 find(java.util.function.Function 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 void findAll(java.util.function.Function finder, List 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); diff --git a/src/dev/peerat/parser/java/Function.java b/src/dev/peerat/parser/java/Function.java index 43949c0..93f8b54 100644 --- a/src/dev/peerat/parser/java/Function.java +++ b/src/dev/peerat/parser/java/Function.java @@ -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 annotations; - private int mod; private Token generic; private Token type; @@ -29,7 +30,7 @@ public class Function extends JavaElement implements VariableContainer, Operatio private List elements; public Function(List 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 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 find(java.util.function.Function 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 void findAll(java.util.function.Function finder, List 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); diff --git a/src/dev/peerat/parser/java/Interface.java b/src/dev/peerat/parser/java/Interface.java index 25aebe9..ef1bc97 100644 --- a/src/dev/peerat/parser/java/Interface.java +++ b/src/dev/peerat/parser/java/Interface.java @@ -7,8 +7,6 @@ import dev.peerat.parser.Token; public class Interface extends ClassBase{ - private List annotations; - //where mod ???? private Token name; private Token extend; @@ -16,7 +14,7 @@ public class Interface extends ClassBase{ private List elements; public Interface(List 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 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 find(java.util.function.Function 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 void findAll(java.util.function.Function finder, List 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); diff --git a/src/dev/peerat/parser/java/Variable.java b/src/dev/peerat/parser/java/Variable.java index 8162f0d..cbb5537 100644 --- a/src/dev/peerat/parser/java/Variable.java +++ b/src/dev/peerat/parser/java/Variable.java @@ -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 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 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 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 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 find(Function 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 void findAll(Function finder, List 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);