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

101 lines
2.3 KiB
Java

package dev.peerat.parser.java;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import dev.peerat.parser.Token;
import dev.peerat.parser.TokenType;
public class Variable extends JavaElement{
public static interface VariableContainer{
void addVariable(Variable variable);
}
private List<Annotation> annotations;
private int mod;
private Token type;
private Token name;
private boolean elips;
private Value value;
public Variable(List<Annotation> annotations, int mod, Token type, Token name){
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){
this(annotations, mod, type, name);
this.elips = elips;
}
public Variable(List<Annotation> annotations, int mod, Token type, Token name, boolean elips, Value value){
this(annotations, mod, type, name, elips);
this.value = value;
}
@Override
public String toString(){
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;
}
public Token getType(){
return this.type;
}
public Token getName(){
return this.name;
}
public boolean isElipsis(){
return this.elips;
}
public Value getValue(){
return this.value;
}
@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));
builder.append(type);
if(elips) builder.append("...");
builder.append(name);
if(value != null){
builder.append("=");
value.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 value != null ? finder.apply(value) ? (E)value : null : null;
}
}