Skip to content

Commit

Permalink
Allow a receiver parameter on constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Nov 5, 2017
1 parent 4192f68 commit 660b724
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 82 deletions.
Expand Up @@ -43,6 +43,8 @@
import static com.github.javaparser.utils.Utils.assertNotNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import java.util.Optional;
import java.util.function.Consumer;

/**
Expand All @@ -60,22 +62,25 @@ public abstract class CallableDeclaration<T extends CallableDeclaration<?>> exte

private NodeList<ReferenceType> thrownExceptions;

private ReceiverParameter receiverParameter;

@AllFieldsConstructor
public CallableDeclaration(EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions) {
this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions);
CallableDeclaration(EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, ReceiverParameter receiverParameter) {
this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, receiverParameter);
}

/**
* This constructor is used by the parser and is considered private.
*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public CallableDeclaration(TokenRange tokenRange, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions) {
CallableDeclaration(TokenRange tokenRange, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, ReceiverParameter receiverParameter) {
super(tokenRange, annotations);
setModifiers(modifiers);
setTypeParameters(typeParameters);
setName(name);
setParameters(parameters);
setThrownExceptions(thrownExceptions);
setReceiverParameter(receiverParameter);
customInitialization();
}

Expand Down Expand Up @@ -377,4 +382,22 @@ public CallableDeclaration asCallableDeclaration() {
public void ifCallableDeclaration(Consumer<CallableDeclaration> action) {
action.accept(this);
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public Optional<ReceiverParameter> getReceiverParameter() {
return Optional.ofNullable(receiverParameter);
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public CallableDeclaration<T> setReceiverParameter(final ReceiverParameter receiverParameter) {
if (receiverParameter == this.receiverParameter) {
return (CallableDeclaration<T>) this;
}
notifyPropertyChange(ObservableProperty.RECEIVER_PARAMETER, this.receiverParameter, receiverParameter);
if (this.receiverParameter != null)
this.receiverParameter.setParentNode(null);
this.receiverParameter = receiverParameter;
setAsParentNodeOf(receiverParameter);
return this;
}
}
Expand Up @@ -34,9 +34,7 @@
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.CloneVisitor;
Expand All @@ -45,7 +43,6 @@
import javax.annotation.Generated;
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import java.util.function.Consumer;

Expand All @@ -62,28 +59,32 @@ public final class ConstructorDeclaration extends CallableDeclaration<Constructo
private BlockStmt body;

public ConstructorDeclaration() {
this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), new NodeList<>(), new SimpleName(), new NodeList<>(), new NodeList<>(), new BlockStmt());
this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), new NodeList<>(), new SimpleName(), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
}

public ConstructorDeclaration(String name) {
this(null, EnumSet.of(Modifier.PUBLIC), new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt());
this(null, EnumSet.of(Modifier.PUBLIC), new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
}

public ConstructorDeclaration(EnumSet<Modifier> modifiers, String name) {
this(null, modifiers, new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt());
this(null, modifiers, new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
}

@AllFieldsConstructor
public ConstructorDeclaration(EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) {
this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body);
this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body, null);
}

@AllFieldsConstructor
public ConstructorDeclaration(EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body, ReceiverParameter receiverParameter) {
this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body, receiverParameter);
}

/**
* This constructor is used by the parser and is considered private.
*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public ConstructorDeclaration(TokenRange tokenRange, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) {
super(tokenRange, modifiers, annotations, typeParameters, name, parameters, thrownExceptions);
public ConstructorDeclaration(TokenRange tokenRange, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body, ReceiverParameter receiverParameter) {
super(tokenRange, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, receiverParameter);
setBody(body);
customInitialization();
}
Expand Down
Expand Up @@ -36,9 +36,7 @@
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import static com.github.javaparser.ast.Modifier.*;
import static com.github.javaparser.utils.Utils.assertNotNull;
Expand Down Expand Up @@ -67,8 +65,6 @@ public final class MethodDeclaration extends CallableDeclaration<MethodDeclarati

private BlockStmt body;

private ReceiverParameter receiverParameter;

public MethodDeclaration() {
this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), new NodeList<>(), new ClassOrInterfaceType(), new SimpleName(), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
}
Expand Down Expand Up @@ -102,7 +98,7 @@ public MethodDeclaration(final EnumSet<Modifier> modifiers, final NodeList<Annot
*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public MethodDeclaration(TokenRange tokenRange, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, Type type, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body, ReceiverParameter receiverParameter) {
super(tokenRange, modifiers, annotations, typeParameters, name, parameters, thrownExceptions);
super(tokenRange, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, receiverParameter);
setType(type);
setBody(body);
setReceiverParameter(receiverParameter);
Expand Down Expand Up @@ -282,12 +278,6 @@ public boolean remove(Node node) {
return true;
}
}
if (receiverParameter != null) {
if (node == receiverParameter) {
removeReceiverParameter();
return true;
}
}
return super.remove(node);
}

Expand Down Expand Up @@ -319,12 +309,6 @@ public boolean replace(Node node, Node replacementNode) {
return true;
}
}
if (receiverParameter != null) {
if (node == receiverParameter) {
setReceiverParameter((ReceiverParameter) replacementNode);
return true;
}
}
if (node == type) {
setType((Type) replacementNode);
return true;
Expand Down Expand Up @@ -353,27 +337,4 @@ public void ifMethodDeclaration(Consumer<MethodDeclaration> action) {
public ResolvedMethodDeclaration resolve() {
return getSymbolResolver().resolveDeclaration(this, ResolvedMethodDeclaration.class);
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public Optional<ReceiverParameter> getReceiverParameter() {
return Optional.ofNullable(receiverParameter);
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public MethodDeclaration setReceiverParameter(final ReceiverParameter receiverParameter) {
if (receiverParameter == this.receiverParameter) {
return (MethodDeclaration) this;
}
notifyPropertyChange(ObservableProperty.RECEIVER_PARAMETER, this.receiverParameter, receiverParameter);
if (this.receiverParameter != null)
this.receiverParameter.setParentNode(null);
this.receiverParameter = receiverParameter;
setAsParentNodeOf(receiverParameter);
return this;
}

@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
public MethodDeclaration removeReceiverParameter() {
return setReceiverParameter((ReceiverParameter) null);
}
}
Expand Up @@ -189,7 +189,7 @@ public Visitable visit(final ConstructorDeclaration n, final Object arg) {
NodeList<TypeParameter> typeParameters = cloneList(n.getTypeParameters(), arg);
NodeList<AnnotationExpr> annotations = cloneList(n.getAnnotations(), arg);
Comment comment = cloneNode(n.getComment(), arg);
ConstructorDeclaration r = new ConstructorDeclaration(n.getTokenRange().orElse(null), n.getModifiers(), annotations, typeParameters, name, parameters, thrownExceptions, body);
ConstructorDeclaration r = new ConstructorDeclaration(n.getTokenRange().orElse(null), n.getModifiers(), annotations, typeParameters, name, parameters, thrownExceptions, body, null);
r.setComment(comment);
return r;
}
Expand Down
Expand Up @@ -19,6 +19,8 @@ protected CallableDeclarationMetaModel(Optional<BaseNodeMetaModel> superNodeMeta

public PropertyMetaModel parametersPropertyMetaModel;

public PropertyMetaModel receiverParameterPropertyMetaModel;

public PropertyMetaModel thrownExceptionsPropertyMetaModel;

public PropertyMetaModel typeParametersPropertyMetaModel;
Expand Down
Expand Up @@ -23,6 +23,7 @@ private static void initializeConstructorParameters() {
callableDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.namePropertyMetaModel);
callableDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.parametersPropertyMetaModel);
callableDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.thrownExceptionsPropertyMetaModel);
callableDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.receiverParameterPropertyMetaModel);
typeMetaModel.getConstructorParameters().add(typeMetaModel.annotationsPropertyMetaModel);
annotationExprMetaModel.getConstructorParameters().add(annotationExprMetaModel.namePropertyMetaModel);
typeDeclarationMetaModel.getConstructorParameters().add(typeDeclarationMetaModel.modifiersPropertyMetaModel);
Expand Down Expand Up @@ -68,6 +69,7 @@ private static void initializeConstructorParameters() {
constructorDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.parametersPropertyMetaModel);
constructorDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.thrownExceptionsPropertyMetaModel);
constructorDeclarationMetaModel.getConstructorParameters().add(constructorDeclarationMetaModel.bodyPropertyMetaModel);
constructorDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.receiverParameterPropertyMetaModel);
enumConstantDeclarationMetaModel.getConstructorParameters().add(bodyDeclarationMetaModel.annotationsPropertyMetaModel);
enumConstantDeclarationMetaModel.getConstructorParameters().add(enumConstantDeclarationMetaModel.namePropertyMetaModel);
enumConstantDeclarationMetaModel.getConstructorParameters().add(enumConstantDeclarationMetaModel.argumentsPropertyMetaModel);
Expand All @@ -91,7 +93,7 @@ private static void initializeConstructorParameters() {
methodDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.parametersPropertyMetaModel);
methodDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.thrownExceptionsPropertyMetaModel);
methodDeclarationMetaModel.getConstructorParameters().add(methodDeclarationMetaModel.bodyPropertyMetaModel);
methodDeclarationMetaModel.getConstructorParameters().add(methodDeclarationMetaModel.receiverParameterPropertyMetaModel);
methodDeclarationMetaModel.getConstructorParameters().add(callableDeclarationMetaModel.receiverParameterPropertyMetaModel);
parameterMetaModel.getConstructorParameters().add(parameterMetaModel.modifiersPropertyMetaModel);
parameterMetaModel.getConstructorParameters().add(parameterMetaModel.annotationsPropertyMetaModel);
parameterMetaModel.getConstructorParameters().add(parameterMetaModel.typePropertyMetaModel);
Expand Down Expand Up @@ -365,6 +367,8 @@ private static void initializePropertyMetaModels() {
callableDeclarationMetaModel.getDeclaredPropertyMetaModels().add(callableDeclarationMetaModel.namePropertyMetaModel);
callableDeclarationMetaModel.parametersPropertyMetaModel = new PropertyMetaModel(callableDeclarationMetaModel, "parameters", com.github.javaparser.ast.body.Parameter.class, Optional.of(parameterMetaModel), false, false, true, false, false);
callableDeclarationMetaModel.getDeclaredPropertyMetaModels().add(callableDeclarationMetaModel.parametersPropertyMetaModel);
callableDeclarationMetaModel.receiverParameterPropertyMetaModel = new PropertyMetaModel(callableDeclarationMetaModel, "receiverParameter", com.github.javaparser.ast.body.ReceiverParameter.class, Optional.of(receiverParameterMetaModel), true, false, false, false, false);
callableDeclarationMetaModel.getDeclaredPropertyMetaModels().add(callableDeclarationMetaModel.receiverParameterPropertyMetaModel);
callableDeclarationMetaModel.thrownExceptionsPropertyMetaModel = new PropertyMetaModel(callableDeclarationMetaModel, "thrownExceptions", com.github.javaparser.ast.type.ReferenceType.class, Optional.of(referenceTypeMetaModel), false, false, true, false, false);
callableDeclarationMetaModel.getDeclaredPropertyMetaModels().add(callableDeclarationMetaModel.thrownExceptionsPropertyMetaModel);
callableDeclarationMetaModel.typeParametersPropertyMetaModel = new PropertyMetaModel(callableDeclarationMetaModel, "typeParameters", com.github.javaparser.ast.type.TypeParameter.class, Optional.of(typeParameterMetaModel), false, false, true, false, false);
Expand Down Expand Up @@ -445,8 +449,6 @@ private static void initializePropertyMetaModels() {
initializerDeclarationMetaModel.getDeclaredPropertyMetaModels().add(initializerDeclarationMetaModel.isStaticPropertyMetaModel);
methodDeclarationMetaModel.bodyPropertyMetaModel = new PropertyMetaModel(methodDeclarationMetaModel, "body", com.github.javaparser.ast.stmt.BlockStmt.class, Optional.of(blockStmtMetaModel), true, false, false, false, false);
methodDeclarationMetaModel.getDeclaredPropertyMetaModels().add(methodDeclarationMetaModel.bodyPropertyMetaModel);
methodDeclarationMetaModel.receiverParameterPropertyMetaModel = new PropertyMetaModel(methodDeclarationMetaModel, "receiverParameter", com.github.javaparser.ast.body.ReceiverParameter.class, Optional.of(receiverParameterMetaModel), true, false, false, false, false);
methodDeclarationMetaModel.getDeclaredPropertyMetaModels().add(methodDeclarationMetaModel.receiverParameterPropertyMetaModel);
methodDeclarationMetaModel.typePropertyMetaModel = new PropertyMetaModel(methodDeclarationMetaModel, "type", com.github.javaparser.ast.type.Type.class, Optional.of(typeMetaModel), false, false, false, false, false);
methodDeclarationMetaModel.getDeclaredPropertyMetaModels().add(methodDeclarationMetaModel.typePropertyMetaModel);
parameterMetaModel.annotationsPropertyMetaModel = new PropertyMetaModel(parameterMetaModel, "annotations", com.github.javaparser.ast.expr.AnnotationExpr.class, Optional.of(annotationExprMetaModel), false, false, true, false, false);
Expand Down
Expand Up @@ -10,7 +10,5 @@ public class MethodDeclarationMetaModel extends CallableDeclarationMetaModel {

public PropertyMetaModel bodyPropertyMetaModel;

public PropertyMetaModel receiverParameterPropertyMetaModel;

public PropertyMetaModel typePropertyMetaModel;
}
5 changes: 1 addition & 4 deletions javaparser-core/src/main/javacc/java.jj
Expand Up @@ -1304,10 +1304,7 @@ ConstructorDeclaration ConstructorDeclaration(ModifierHolder modifier):
if (exConsInv != null) {
stmts = prepend(stmts, exConsInv);
}
if (parameters.b != null) {
addProblem("The receiver cannot be used in a static context.");
}
return new ConstructorDeclaration(range(begin, token()), modifier.modifiers, modifier.annotations, typeParameters.list, name, parameters.a, throws_, new BlockStmt(range(blockBegin, token()), stmts));
return new ConstructorDeclaration(range(begin, token()), modifier.modifiers, modifier.annotations, typeParameters.list, name, parameters.a, throws_, new BlockStmt(range(blockBegin, token()), stmts), parameters.b);
}
}

Expand Down

0 comments on commit 660b724

Please sign in to comment.