peer-at-code-parser-java/src/dev/peerat/parser/java/Function.java
2023-10-26 08:22:03 +02:00

150 lines
3.6 KiB
Java

package dev.peerat.parser.java;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import dev.peerat.parser.Token;
import dev.peerat.parser.TokenType;
import dev.peerat.parser.java.Operation.OperationContainer;
import dev.peerat.parser.java.Variable.VariableContainer;
public class Function extends JavaElement implements VariableContainer, OperationContainer{
public static interface FunctionContainer{
void addFunction(Function function);
}
private List<Annotation> annotations;
private int mod;
private Token generic;
private Token type;
private Token name;
private List<Variable> parameters;
private List<Token> throwables;
private List<JavaElement> elements;
public Function(List<Annotation> annotations, int mod, Token type, Token name){
this.annotations = annotations;
this.mod = mod;
this.type = type;
this.name = name;
this.elements = new ArrayList<>();
}
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name){
this(annotations, mod, type, name);
this.generic = generic;
}
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name, List<Variable> parameters){
this(annotations, mod, generic, type, name);
this.parameters = parameters;
}
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name, List<Variable> parameters, List<Token> throwables){
this(annotations, mod, generic, type, name, parameters);
this.throwables = throwables;
}
@Override
public void addVariable(Variable variable) {
this.elements.add(variable);
}
@Override
public void addOperation(Operation operation) {
this.elements.add(operation);
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
public int getModifier(){
return this.mod;
}
public Token getGeneric(){
return this.generic;
}
public Token getReturnType(){
return this.type;
}
public Token getName(){
return this.name;
}
public List<Variable> getParameters(){
return this.parameters;
}
public List<Token> getThrowables(){
return this.throwables;
}
public List<JavaElement> getElements(){
return this.elements;
}
@Override
public void build(Builder builder) throws Exception{
if(annotations != null){
for(Annotation annotation : this.annotations){
annotation.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);
builder.append(type);
builder.append(name);
builder.append("(");
if(parameters != null){
for(Variable param : this.parameters){
param.build(builder);
}
}
builder.append(")");
if(throwables != null){
builder.append("throws");
for(Token token : throwables){
builder.append(token);
}
}
builder.append("{");
if(elements != null){
for(JavaElement content : this.elements){
content.build(builder);
}
}
builder.append("}");
}
@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;
}
}
if(parameters != null){
for(Variable param : this.parameters){
if(finder.apply(param)) return (E) param;
}
}
if(elements != null){
for(JavaElement content : this.elements){
if(finder.apply(content)) return (E) content;
}
}
return null;
}
}