|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.github.javaparser.ast.body; |
|
|
|
import com.github.javaparser.TokenRange; |
|
import com.github.javaparser.ast.*; |
|
import com.github.javaparser.ast.expr.AnnotationExpr; |
|
import com.github.javaparser.ast.expr.AssignExpr; |
|
import com.github.javaparser.ast.expr.AssignExpr.Operator; |
|
import com.github.javaparser.ast.expr.NameExpr; |
|
import com.github.javaparser.ast.nodeTypes.NodeWithJavadoc; |
|
import com.github.javaparser.ast.nodeTypes.NodeWithVariables; |
|
import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers; |
|
import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier; |
|
import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithStaticModifier; |
|
import com.github.javaparser.ast.observer.ObservableProperty; |
|
import com.github.javaparser.ast.stmt.BlockStmt; |
|
import com.github.javaparser.ast.stmt.ReturnStmt; |
|
import com.github.javaparser.ast.type.Type; |
|
import com.github.javaparser.ast.type.VoidType; |
|
import com.github.javaparser.ast.visitor.CloneVisitor; |
|
import com.github.javaparser.ast.visitor.GenericVisitor; |
|
import com.github.javaparser.ast.visitor.VoidVisitor; |
|
import com.github.javaparser.metamodel.FieldDeclarationMetaModel; |
|
import com.github.javaparser.metamodel.JavaParserMetaModel; |
|
import com.github.javaparser.metamodel.NonEmptyProperty; |
|
import com.github.javaparser.resolution.Resolvable; |
|
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration; |
|
import java.util.Optional; |
|
import java.util.function.Consumer; |
|
import static com.github.javaparser.ast.NodeList.nodeList; |
|
import static com.github.javaparser.utils.Utils.assertNotNull; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class FieldDeclaration extends BodyDeclaration<FieldDeclaration> implements NodeWithJavadoc<FieldDeclaration>, NodeWithVariables<FieldDeclaration>, NodeWithAccessModifiers<FieldDeclaration>, NodeWithStaticModifier<FieldDeclaration>, NodeWithFinalModifier<FieldDeclaration>, Resolvable<ResolvedFieldDeclaration> { |
|
|
|
private NodeList<Modifier> modifiers; |
|
|
|
@NonEmptyProperty |
|
private NodeList<VariableDeclarator> variables; |
|
|
|
public FieldDeclaration() { |
|
this(null, new NodeList<>(), new NodeList<>(), new NodeList<>()); |
|
} |
|
|
|
public FieldDeclaration(NodeList<Modifier> modifiers, VariableDeclarator variable) { |
|
this(null, modifiers, new NodeList<>(), nodeList(variable)); |
|
} |
|
|
|
public FieldDeclaration(NodeList<Modifier> modifiers, NodeList<VariableDeclarator> variables) { |
|
this(null, modifiers, new NodeList<>(), variables); |
|
} |
|
|
|
@AllFieldsConstructor |
|
public FieldDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<VariableDeclarator> variables) { |
|
this(null, modifiers, annotations, variables); |
|
} |
|
|
|
|
|
|
|
*/ |
|
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
|
public FieldDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<VariableDeclarator> variables) { |
|
super(tokenRange, annotations); |
|
setModifiers(modifiers); |
|
setVariables(variables); |
|
customInitialization(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public FieldDeclaration(NodeList<Modifier> modifiers, Type type, String name) { |
|
this(assertNotNull(modifiers), new VariableDeclarator(type, assertNotNull(name))); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
|
public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
|
return v.visit(this, arg); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
|
public <A> void accept(final VoidVisitor<A> v, final A arg) { |
|
v.visit(this, arg); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
|
public NodeList<Modifier> getModifiers() { |
|
return modifiers; |
|
} |
|
|
|
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
|
public NodeList<VariableDeclarator> getVariables() { |
|
return variables; |
|
} |
|
|
|
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
|
public FieldDeclaration setModifiers(final NodeList<Modifier> modifiers) { |
|
assertNotNull(modifiers); |
|
if (modifiers == this.modifiers) { |
|
return this; |
|
} |
|
notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers); |
|
if (this.modifiers != null) |
|
this.modifiers.setParentNode(null); |
|
this.modifiers = modifiers; |
|
setAsParentNodeOf(modifiers); |
|
return this; |
|
} |
|
|
|
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
|
public FieldDeclaration setVariables(final NodeList<VariableDeclarator> variables) { |
|
assertNotNull(variables); |
|
if (variables == this.variables) { |
|
return this; |
|
} |
|
notifyPropertyChange(ObservableProperty.VARIABLES, this.variables, variables); |
|
if (this.variables != null) |
|
this.variables.setParentNode(null); |
|
this.variables = variables; |
|
setAsParentNodeOf(variables); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public MethodDeclaration createGetter() { |
|
if (getVariables().size() != 1) |
|
throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); |
|
Optional<ClassOrInterfaceDeclaration> parentClass = findAncestor(ClassOrInterfaceDeclaration.class); |
|
Optional<EnumDeclaration> parentEnum = findAncestor(EnumDeclaration.class); |
|
if (!(parentClass.isPresent() || parentEnum.isPresent()) || (parentClass.isPresent() && parentClass.get().isInterface())) |
|
throw new IllegalStateException("You can use this only when the field is attached to a class or an enum"); |
|
VariableDeclarator variable = getVariable(0); |
|
String fieldName = variable.getNameAsString(); |
|
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); |
|
final MethodDeclaration getter; |
|
getter = parentClass.map(clazz -> clazz.addMethod("get" + fieldNameUpper, Modifier.Keyword.PUBLIC)).orElseGet(() -> parentEnum.get().addMethod("get" + fieldNameUpper, Modifier.Keyword.PUBLIC)); |
|
getter.setType(variable.getType()); |
|
BlockStmt blockStmt = new BlockStmt(); |
|
getter.setBody(blockStmt); |
|
blockStmt.addStatement(new ReturnStmt(fieldName)); |
|
return getter; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public MethodDeclaration createSetter() { |
|
if (getVariables().size() != 1) |
|
throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); |
|
Optional<ClassOrInterfaceDeclaration> parentClass = findAncestor(ClassOrInterfaceDeclaration.class); |
|
Optional<EnumDeclaration> parentEnum = findAncestor(EnumDeclaration.class); |
|
if (!(parentClass.isPresent() || parentEnum.isPresent()) || (parentClass.isPresent() && parentClass.get().isInterface())) |
|
throw new IllegalStateException("You can use this only when the field is attached to a class or an enum"); |
|
VariableDeclarator variable = getVariable(0); |
|
String fieldName = variable.getNameAsString(); |
|
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); |
|
final MethodDeclaration setter; |
|
setter = parentClass.map(clazz -> clazz.addMethod("set" + fieldNameUpper, Modifier.Keyword.PUBLIC)).orElseGet(() -> parentEnum.get().addMethod("set" + fieldNameUpper, Modifier.Keyword.PUBLIC)); |
|
setter.setType(new VoidType()); |
|
setter.getParameters().add(new Parameter(variable.getType(), fieldName)); |
|
BlockStmt blockStmt2 = new BlockStmt(); |
|
setter.setBody(blockStmt2); |
|
blockStmt2.addStatement(new AssignExpr(new NameExpr("this." + fieldName), new NameExpr(fieldName), Operator.ASSIGN)); |
|
return setter; |
|
} |
|
|
|
public boolean isTransient() { |
|
return hasModifier(Modifier.Keyword.TRANSIENT); |
|
} |
|
|
|
public boolean isVolatile() { |
|
return hasModifier(Modifier.Keyword.VOLATILE); |
|
} |
|
|
|
public FieldDeclaration setTransient(boolean set) { |
|
return setModifier(Modifier.Keyword.TRANSIENT, set); |
|
} |
|
|
|
public FieldDeclaration setVolatile(boolean set) { |
|
return setModifier(Modifier.Keyword.VOLATILE, set); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
|
public boolean remove(Node node) { |
|
if (node == null) |
|
return false; |
|
for (int i = 0; i < modifiers.size(); i++) { |
|
if (modifiers.get(i) == node) { |
|
modifiers.remove(i); |
|
return true; |
|
} |
|
} |
|
for (int i = 0; i < variables.size(); i++) { |
|
if (variables.get(i) == node) { |
|
variables.remove(i); |
|
return true; |
|
} |
|
} |
|
return super.remove(node); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
|
public FieldDeclaration clone() { |
|
return (FieldDeclaration) accept(new CloneVisitor(), null); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
|
public FieldDeclarationMetaModel getMetaModel() { |
|
return JavaParserMetaModel.fieldDeclarationMetaModel; |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
|
public boolean replace(Node node, Node replacementNode) { |
|
if (node == null) |
|
return false; |
|
for (int i = 0; i < modifiers.size(); i++) { |
|
if (modifiers.get(i) == node) { |
|
modifiers.set(i, (Modifier) replacementNode); |
|
return true; |
|
} |
|
} |
|
for (int i = 0; i < variables.size(); i++) { |
|
if (variables.get(i) == node) { |
|
variables.set(i, (VariableDeclarator) replacementNode); |
|
return true; |
|
} |
|
} |
|
return super.replace(node, replacementNode); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
|
public boolean isFieldDeclaration() { |
|
return true; |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
|
public FieldDeclaration asFieldDeclaration() { |
|
return this; |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
|
public void ifFieldDeclaration(Consumer<FieldDeclaration> action) { |
|
action.accept(this); |
|
} |
|
|
|
@Override |
|
public ResolvedFieldDeclaration resolve() { |
|
return getSymbolResolver().resolveDeclaration(this, ResolvedFieldDeclaration.class); |
|
} |
|
|
|
@Override |
|
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
|
public Optional<FieldDeclaration> toFieldDeclaration() { |
|
return Optional.of(this); |
|
} |
|
} |