Skip to content

Commit

Permalink
Struggle with CallableDeclaration merge
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Feb 7, 2017
1 parent 9041570 commit 6084184
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 127 deletions.
Expand Up @@ -27,56 +27,35 @@
import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName; import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.observer.ObservableProperty; import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ReferenceType; import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.TypeParameter; import com.github.javaparser.ast.type.TypeParameter;

import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;

import static com.github.javaparser.utils.Utils.assertNotNull; import static com.github.javaparser.utils.Utils.assertNotNull;


/** /**
* Represents a declaration which is callable eg. a method or a constructor. * Represents a declaration which is callable eg. a method or a constructor.
*/ */
public abstract class CallableDeclaration<T extends Node> extends BodyDeclaration<T> { public abstract class CallableDeclaration<T extends Node> extends BodyDeclaration<T> {


protected EnumSet<Modifier> modifiers; private EnumSet<Modifier> modifiers;

protected NodeList<TypeParameter> typeParameters;


protected SimpleName name; private NodeList<TypeParameter> typeParameters;


protected NodeList<Parameter> parameters; private SimpleName name;


protected NodeList<ReferenceType> thrownExceptions; private NodeList<Parameter> parameters;


protected BlockStmt body; private NodeList<ReferenceType> thrownExceptions;


public CallableDeclaration(Range range, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) { public CallableDeclaration(Range range, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions) {
super(range, annotations); super(range, annotations);
setModifiers(modifiers); setModifiers(modifiers);
setTypeParameters(typeParameters); setTypeParameters(typeParameters);
setName(name); setName(name);
setParameters(parameters); setParameters(parameters);
setThrownExceptions(thrownExceptions); setThrownExceptions(thrownExceptions);
setBody(body);
}

/**
* Sets the body.
* Attention: a constructors body can not be null and must be tested before calling this method!
*
* @param body the body, can be null
*/
public CallableDeclaration<T> setBody(final BlockStmt body) {
notifyPropertyChange(ObservableProperty.BODY, this.body, body);
if (this.body != null)
this.body.setParentNode(null);
this.body = body;
setAsParentNodeOf(body);
return this;
} }


/** /**
Expand All @@ -89,67 +68,67 @@ public EnumSet<Modifier> getModifiers() {
return modifiers; return modifiers;
} }


public CallableDeclaration<T> setModifiers(EnumSet<Modifier> modifiers) { public T setModifiers(final EnumSet<Modifier> modifiers) {
assertNotNull(modifiers); assertNotNull(modifiers);
notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers); notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers);
this.modifiers = modifiers; this.modifiers = modifiers;
return this; return (T) this;
} }


public SimpleName getName() { public SimpleName getName() {
return name; return name;
} }


public CallableDeclaration<T> setName(final SimpleName name) { public T setName(final SimpleName name) {
assertNotNull(name); assertNotNull(name);
notifyPropertyChange(ObservableProperty.NAME, this.name, name); notifyPropertyChange(ObservableProperty.NAME, this.name, name);
if (this.name != null) if (this.name != null)
this.name.setParentNode(null); this.name.setParentNode(null);
this.name = name; this.name = name;
setAsParentNodeOf(name); setAsParentNodeOf(name);
return this; return (T) this;
} }


public NodeList<Parameter> getParameters() { public NodeList<Parameter> getParameters() {
return parameters; return parameters;
} }


public CallableDeclaration<T> setParameters(final NodeList<Parameter> parameters) { public T setParameters(final NodeList<Parameter> parameters) {
assertNotNull(parameters); assertNotNull(parameters);
notifyPropertyChange(ObservableProperty.PARAMETERS, this.parameters, parameters); notifyPropertyChange(ObservableProperty.PARAMETERS, this.parameters, parameters);
if (this.parameters != null) if (this.parameters != null)
this.parameters.setParentNode(null); this.parameters.setParentNode(null);
this.parameters = parameters; this.parameters = parameters;
setAsParentNodeOf(parameters); setAsParentNodeOf(parameters);
return this; return (T) this;
} }


public NodeList<ReferenceType> getThrownExceptions() { public NodeList<ReferenceType> getThrownExceptions() {
return thrownExceptions; return thrownExceptions;
} }


public CallableDeclaration<T> setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) { public T setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) {
assertNotNull(thrownExceptions); assertNotNull(thrownExceptions);
notifyPropertyChange(ObservableProperty.THROWN_EXCEPTIONS, this.thrownExceptions, thrownExceptions); notifyPropertyChange(ObservableProperty.THROWN_EXCEPTIONS, this.thrownExceptions, thrownExceptions);
if (this.thrownExceptions != null) if (this.thrownExceptions != null)
this.thrownExceptions.setParentNode(null); this.thrownExceptions.setParentNode(null);
this.thrownExceptions = thrownExceptions; this.thrownExceptions = thrownExceptions;
setAsParentNodeOf(thrownExceptions); setAsParentNodeOf(thrownExceptions);
return this; return (T) this;
} }


public NodeList<TypeParameter> getTypeParameters() { public NodeList<TypeParameter> getTypeParameters() {
return typeParameters; return typeParameters;
} }


public CallableDeclaration<T> setTypeParameters(final NodeList<TypeParameter> typeParameters) { public T setTypeParameters(final NodeList<TypeParameter> typeParameters) {
assertNotNull(typeParameters); assertNotNull(typeParameters);
notifyPropertyChange(ObservableProperty.TYPE_PARAMETERS, this.typeParameters, typeParameters); notifyPropertyChange(ObservableProperty.TYPE_PARAMETERS, this.typeParameters, typeParameters);
if (this.typeParameters != null) if (this.typeParameters != null)
this.typeParameters.setParentNode(null); this.typeParameters.setParentNode(null);
this.typeParameters = typeParameters; this.typeParameters = typeParameters;
setAsParentNodeOf(typeParameters); setAsParentNodeOf(typeParameters);
return this; return (T) this;
} }


public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows) { public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows) {
Expand Down Expand Up @@ -184,4 +163,29 @@ public List<NodeList<?>> getNodeLists() {
return Arrays.asList(getParameters(), getThrownExceptions(), getTypeParameters(), getAnnotations()); return Arrays.asList(getParameters(), getThrownExceptions(), getTypeParameters(), getAnnotations());
} }


@Override
public boolean remove(Node node) {
if (node == null)
return false;
for (int i = 0; i < parameters.size(); i++) {
if (parameters.get(i) == node) {
parameters.remove(i);
return true;
}
}
for (int i = 0; i < thrownExceptions.size(); i++) {
if (thrownExceptions.get(i) == node) {
thrownExceptions.remove(i);
return true;
}
}
for (int i = 0; i < typeParameters.size(); i++) {
if (typeParameters.get(i) == node) {
typeParameters.remove(i);
return true;
}
}
return super.remove(node);
}
} }

Expand Up @@ -28,14 +28,15 @@
import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName; import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.*; import com.github.javaparser.ast.nodeTypes.*;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.stmt.BlockStmt; import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ReferenceType; import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.TypeParameter; import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor; import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;

import java.util.List;
import static com.github.javaparser.utils.Utils.assertNotNull; import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;


Expand All @@ -44,11 +45,9 @@
* *
* @author Julio Vilmar Gesser * @author Julio Vilmar Gesser
*/ */
public final class ConstructorDeclaration extends CallableDeclaration<ConstructorDeclaration> public final class ConstructorDeclaration extends CallableDeclaration<ConstructorDeclaration> implements NodeWithBlockStmt<ConstructorDeclaration>, NodeWithModifiers<ConstructorDeclaration>, NodeWithJavadoc<ConstructorDeclaration>, NodeWithDeclaration, NodeWithSimpleName<ConstructorDeclaration>, NodeWithParameters<ConstructorDeclaration>, NodeWithThrownExceptions<ConstructorDeclaration>, NodeWithTypeParameters<ConstructorDeclaration> {
implements NodeWithBlockStmt<ConstructorDeclaration>, NodeWithModifiers<ConstructorDeclaration>,
NodeWithJavadoc<ConstructorDeclaration>, NodeWithDeclaration, private BlockStmt body;
NodeWithSimpleName<ConstructorDeclaration>, NodeWithParameters<ConstructorDeclaration>,
NodeWithThrownExceptions<ConstructorDeclaration>, NodeWithTypeParameters<ConstructorDeclaration> {


public ConstructorDeclaration() { 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());
Expand All @@ -64,7 +63,8 @@ public ConstructorDeclaration(EnumSet<Modifier> modifiers, NodeList<AnnotationEx
} }


public ConstructorDeclaration(Range range, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) { public ConstructorDeclaration(Range range, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) {
super(range, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body); super(range, modifiers, annotations, typeParameters, name, parameters, thrownExceptions);
setBody(body);
} }


@Override @Override
Expand All @@ -91,32 +91,37 @@ public BlockStmt getBody() {
@Override @Override
public ConstructorDeclaration setBody(final BlockStmt body) { public ConstructorDeclaration setBody(final BlockStmt body) {
assertNotNull(body); assertNotNull(body);
return (ConstructorDeclaration) super.setBody(body); notifyPropertyChange(ObservableProperty.BODY, this.body, body);
if (this.body != null)
this.body.setParentNode(null);
this.body = body;
setAsParentNodeOf(body);
return this;
} }


@Override @Override
public ConstructorDeclaration setModifiers(final EnumSet<Modifier> modifiers) { public ConstructorDeclaration setModifiers(final EnumSet<Modifier> modifiers) {
return (ConstructorDeclaration) super.setModifiers(modifiers); return super.setModifiers(modifiers);
} }


@Override @Override
public ConstructorDeclaration setName(final SimpleName name) { public ConstructorDeclaration setName(final SimpleName name) {
return (ConstructorDeclaration) super.setName(name); return super.setName(name);
} }


@Override @Override
public ConstructorDeclaration setParameters(final NodeList<Parameter> parameters) { public ConstructorDeclaration setParameters(final NodeList<Parameter> parameters) {
return (ConstructorDeclaration) super.setParameters(parameters); return super.setParameters(parameters);
} }


@Override @Override
public ConstructorDeclaration setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) { public ConstructorDeclaration setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) {
return (ConstructorDeclaration) super.setThrownExceptions(thrownExceptions); return super.setThrownExceptions(thrownExceptions);
} }


@Override @Override
public ConstructorDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) { public ConstructorDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) {
return (ConstructorDeclaration) super.setTypeParameters(typeParameters); return super.setTypeParameters(typeParameters);
} }


/** /**
Expand Down Expand Up @@ -153,5 +158,16 @@ public String getDeclarationAsString(boolean includingModifiers, boolean includi
return sb.toString(); return sb.toString();
} }


@Override
public List<NodeList<?>> getNodeLists() {
return Arrays.asList(getParameters(), getThrownExceptions(), getTypeParameters(), getAnnotations());
}

@Override
public boolean remove(Node node) {
if (node == null)
return false;
return super.remove(node);
}
} }


Expand Up @@ -36,10 +36,10 @@
import com.github.javaparser.ast.type.TypeParameter; import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor; import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List;
import java.util.Optional; import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNotNull; import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;


Expand All @@ -49,16 +49,14 @@
* *
* @author Julio Vilmar Gesser * @author Julio Vilmar Gesser
*/ */
public final class MethodDeclaration extends CallableDeclaration<MethodDeclaration> public final class MethodDeclaration extends CallableDeclaration<MethodDeclaration> implements NodeWithType<MethodDeclaration, Type>, NodeWithOptionalBlockStmt<MethodDeclaration>, NodeWithModifiers<MethodDeclaration>, NodeWithJavadoc<MethodDeclaration>, NodeWithDeclaration, NodeWithSimpleName<MethodDeclaration>, NodeWithParameters<MethodDeclaration>, NodeWithThrownExceptions<MethodDeclaration>, NodeWithTypeParameters<MethodDeclaration> {
implements NodeWithType<MethodDeclaration, Type>, NodeWithOptionalBlockStmt<MethodDeclaration>,
NodeWithModifiers<MethodDeclaration>, NodeWithJavadoc<MethodDeclaration>, NodeWithDeclaration,
NodeWithSimpleName<MethodDeclaration>, NodeWithParameters<MethodDeclaration>,
NodeWithThrownExceptions<MethodDeclaration>, NodeWithTypeParameters<MethodDeclaration> {


private boolean isDefault; private boolean isDefault;


private Type type; private Type type;


private BlockStmt body;

public MethodDeclaration() { public MethodDeclaration() {
this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), new NodeList<>(), new ClassOrInterfaceType(), new SimpleName(), false, new NodeList<>(), new NodeList<>(), new BlockStmt()); this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), new NodeList<>(), new ClassOrInterfaceType(), new SimpleName(), false, new NodeList<>(), new NodeList<>(), new BlockStmt());
} }
Expand All @@ -77,9 +75,10 @@ public MethodDeclaration(final EnumSet<Modifier> modifiers, final NodeList<Annot
} }


public MethodDeclaration(Range range, final EnumSet<Modifier> modifiers, final NodeList<AnnotationExpr> annotations, final NodeList<TypeParameter> typeParameters, final Type type, final SimpleName name, final boolean isDefault, final NodeList<Parameter> parameters, final NodeList<ReferenceType> thrownExceptions, final BlockStmt body) { public MethodDeclaration(Range range, final EnumSet<Modifier> modifiers, final NodeList<AnnotationExpr> annotations, final NodeList<TypeParameter> typeParameters, final Type type, final SimpleName name, final boolean isDefault, final NodeList<Parameter> parameters, final NodeList<ReferenceType> thrownExceptions, final BlockStmt body) {
super(range, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body); super(range, modifiers, annotations, typeParameters, name, parameters, thrownExceptions);
setType(type); setType(type);
setDefault(isDefault); setDefault(isDefault);
setBody(body);
} }


@Override @Override
Expand All @@ -105,7 +104,12 @@ public Optional<BlockStmt> getBody() {
*/ */
@Override @Override
public MethodDeclaration setBody(final BlockStmt body) { public MethodDeclaration setBody(final BlockStmt body) {
return (MethodDeclaration) super.setBody(body); notifyPropertyChange(ObservableProperty.BODY, this.body, body);
if (this.body != null)
this.body.setParentNode(null);
this.body = body;
setAsParentNodeOf(body);
return this;
} }


@Override @Override
Expand All @@ -126,27 +130,27 @@ public MethodDeclaration setType(final Type type) {


@Override @Override
public MethodDeclaration setModifiers(final EnumSet<Modifier> modifiers) { public MethodDeclaration setModifiers(final EnumSet<Modifier> modifiers) {
return (MethodDeclaration) super.setModifiers(modifiers); return super.setModifiers(modifiers);
} }


@Override @Override
public MethodDeclaration setName(final SimpleName name) { public MethodDeclaration setName(final SimpleName name) {
return (MethodDeclaration) super.setName(name); return super.setName(name);
} }


@Override @Override
public MethodDeclaration setParameters(final NodeList<Parameter> parameters) { public MethodDeclaration setParameters(final NodeList<Parameter> parameters) {
return (MethodDeclaration) super.setParameters(parameters); return super.setParameters(parameters);
} }


@Override @Override
public MethodDeclaration setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) { public MethodDeclaration setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) {
return (MethodDeclaration) super.setThrownExceptions(thrownExceptions); return super.setThrownExceptions(thrownExceptions);
} }


@Override @Override
public MethodDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) { public MethodDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) {
return (MethodDeclaration) super.setTypeParameters(typeParameters); return super.setTypeParameters(typeParameters);
} }


public boolean isDefault() { public boolean isDefault() {
Expand Down Expand Up @@ -217,5 +221,26 @@ public String getDeclarationAsString(boolean includingModifiers, boolean includi
return sb.toString(); return sb.toString();
} }


@Override
public List<NodeList<?>> getNodeLists() {
return Arrays.asList(getParameters(), getThrownExceptions(), getTypeParameters(), getAnnotations());
}

@Override
public boolean remove(Node node) {
if (node == null)
return false;
if (body != null) {
if (node == body) {
removeBody();
return true;
}
}
return super.remove(node);
}

public MethodDeclaration removeBody() {
return setBody((BlockStmt) null);
}
} }


0 comments on commit 6084184

Please sign in to comment.