Skip to content

Commit

Permalink
Give SuperExpr a name as scope
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed May 1, 2019
1 parent 1bf4c06 commit fe2b93b
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 83 deletions.
Expand Up @@ -4,8 +4,8 @@
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;


import static com.github.javaparser.StaticJavaParser.parseExpression; import static com.github.javaparser.StaticJavaParser.parseExpression;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;


class SuperExprTest { class SuperExprTest {
@Test @Test
Expand All @@ -15,19 +15,19 @@ void justSuper() {


@Test @Test
void singleScopeSuper() { void singleScopeSuper() {
Expression expr = parseExpression("a.super"); Expression expr = parseExpression("A.super");


Expression classExpr = expr.asSuperExpr().getClassExpr().get(); Name className = expr.asSuperExpr().getClassName().get();


assertTrue(classExpr.isNameExpr()); assertEquals("A", className.asString());
} }


@Test @Test
void multiScopeSuper() { void multiScopeSuper() {
Expression expr = parseExpression("a.b.super"); Expression expr = parseExpression("a.B.super");


Expression classExpr = expr.asSuperExpr().getClassExpr().get(); Name className = expr.asSuperExpr().getClassName().get();


assertTrue(classExpr.isFieldAccessExpr()); assertEquals("a.B", className.asString());
} }
} }
Expand Up @@ -50,24 +50,24 @@
public class SuperExpr extends Expression { public class SuperExpr extends Expression {


@OptionalProperty @OptionalProperty
private Expression classExpr; private Name className;


public SuperExpr() { public SuperExpr() {
this(null, null); this(null, null);
} }


@AllFieldsConstructor @AllFieldsConstructor
public SuperExpr(final Expression classExpr) { public SuperExpr(final Name className) {
this(null, classExpr); this(null, className);
} }


/** /**
* This constructor is used by the parser and is considered private. * This constructor is used by the parser and is considered private.
*/ */
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public SuperExpr(TokenRange tokenRange, Expression classExpr) { public SuperExpr(TokenRange tokenRange, Name className) {
super(tokenRange); super(tokenRange);
setClassExpr(classExpr); setClassName(className);
customInitialization(); customInitialization();
} }


Expand All @@ -84,26 +84,20 @@ public <A> void accept(final VoidVisitor<A> v, final A arg) {
} }


@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public Optional<Expression> getClassExpr() { public Optional<Name> getClassName() {
return Optional.ofNullable(classExpr); return Optional.ofNullable(className);
} }


/**
* Sets the classExpr
*
* @param classExpr the classExpr, can be null
* @return this, the SuperExpr
*/
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public SuperExpr setClassExpr(final Expression classExpr) { public SuperExpr setClassName(final Name className) {
if (classExpr == this.classExpr) { if (className == this.className) {
return (SuperExpr) this; return (SuperExpr) this;
} }
notifyPropertyChange(ObservableProperty.CLASS_EXPR, this.classExpr, classExpr); notifyPropertyChange(ObservableProperty.CLASS_NAME, this.className, className);
if (this.classExpr != null) if (this.className != null)
this.classExpr.setParentNode(null); this.className.setParentNode(null);
this.classExpr = classExpr; this.className = className;
setAsParentNodeOf(classExpr); setAsParentNodeOf(className);
return this; return this;
} }


Expand All @@ -112,20 +106,15 @@ public SuperExpr setClassExpr(final Expression classExpr) {
public boolean remove(Node node) { public boolean remove(Node node) {
if (node == null) if (node == null)
return false; return false;
if (classExpr != null) { if (className != null) {
if (node == classExpr) { if (node == className) {
removeClassExpr(); removeClassName();
return true; return true;
} }
} }
return super.remove(node); return super.remove(node);
} }


@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
public SuperExpr removeClassExpr() {
return setClassExpr((Expression) null);
}

@Override @Override
@Generated("com.github.javaparser.generator.core.node.CloneGenerator") @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
public SuperExpr clone() { public SuperExpr clone() {
Expand All @@ -138,20 +127,6 @@ public SuperExprMetaModel getMetaModel() {
return JavaParserMetaModel.superExprMetaModel; return JavaParserMetaModel.superExprMetaModel;
} }


@Override
@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
public boolean replace(Node node, Node replacementNode) {
if (node == null)
return false;
if (classExpr != null) {
if (node == classExpr) {
setClassExpr((Expression) replacementNode);
return true;
}
}
return super.replace(node, replacementNode);
}

@Override @Override
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public boolean isSuperExpr() { public boolean isSuperExpr() {
Expand All @@ -174,4 +149,23 @@ public void ifSuperExpr(Consumer<SuperExpr> action) {
public Optional<SuperExpr> toSuperExpr() { public Optional<SuperExpr> toSuperExpr() {
return Optional.of(this); return Optional.of(this);
} }

@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
public SuperExpr removeClassName() {
return setClassName((Name) null);
}

@Override
@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
public boolean replace(Node node, Node replacementNode) {
if (node == null)
return false;
if (className != null) {
if (node == className) {
setClassName((Name) replacementNode);
return true;
}
}
return super.replace(node, replacementNode);
}
} }
Expand Up @@ -44,7 +44,6 @@ public enum ObservableProperty {
CHECK(Type.SINGLE_REFERENCE), CHECK(Type.SINGLE_REFERENCE),
CLASS_BODY(Type.MULTIPLE_REFERENCE), CLASS_BODY(Type.MULTIPLE_REFERENCE),
CLASS_DECLARATION(Type.SINGLE_REFERENCE), CLASS_DECLARATION(Type.SINGLE_REFERENCE),
CLASS_EXPR(Type.SINGLE_REFERENCE),
CLASS_NAME(Type.SINGLE_REFERENCE), CLASS_NAME(Type.SINGLE_REFERENCE),
COMMENT(Type.SINGLE_REFERENCE), COMMENT(Type.SINGLE_REFERENCE),
COMPARE(Type.SINGLE_REFERENCE), COMPARE(Type.SINGLE_REFERENCE),
Expand Down
Expand Up @@ -608,9 +608,9 @@ public Visitable visit(final ThisExpr n, final Object arg) {


@Override @Override
public Visitable visit(final SuperExpr n, final Object arg) { public Visitable visit(final SuperExpr n, final Object arg) {
Expression classExpr = cloneNode(n.getClassExpr(), arg); Name className = cloneNode(n.getClassName(), arg);
Comment comment = cloneNode(n.getComment(), arg); Comment comment = cloneNode(n.getComment(), arg);
SuperExpr r = new SuperExpr(n.getTokenRange().orElse(null), classExpr); SuperExpr r = new SuperExpr(n.getTokenRange().orElse(null), className);
r.setComment(comment); r.setComment(comment);
copyData(n, r); copyData(n, r);
return r; return r;
Expand Down
Expand Up @@ -802,7 +802,7 @@ public Boolean visit(final ThisExpr n, final Visitable arg) {
@Override @Override
public Boolean visit(final SuperExpr n, final Visitable arg) { public Boolean visit(final SuperExpr n, final Visitable arg) {
final SuperExpr n2 = (SuperExpr) arg; final SuperExpr n2 = (SuperExpr) arg;
if (!nodeEquals(n.getClassExpr(), n2.getClassExpr())) if (!nodeEquals(n.getClassName(), n2.getClassName()))
return false; return false;
if (!nodeEquals(n.getComment(), n2.getComment())) if (!nodeEquals(n.getComment(), n2.getComment()))
return false; return false;
Expand Down
Expand Up @@ -1471,8 +1471,8 @@ public List<R> visit(final StringLiteralExpr n, final A arg) {
public List<R> visit(final SuperExpr n, final A arg) { public List<R> visit(final SuperExpr n, final A arg) {
List<R> result = new ArrayList<>(); List<R> result = new ArrayList<>();
List<R> tmp; List<R> tmp;
if (n.getClassExpr().isPresent()) { if (n.getClassName().isPresent()) {
tmp = n.getClassExpr().get().accept(this, arg); tmp = n.getClassName().get().accept(this, arg);
if (tmp != null) if (tmp != null)
result.addAll(tmp); result.addAll(tmp);
} }
Expand Down
Expand Up @@ -1392,8 +1392,8 @@ public R visit(final StringLiteralExpr n, final A arg) {
@Override @Override
public R visit(final SuperExpr n, final A arg) { public R visit(final SuperExpr n, final A arg) {
R result; R result;
if (n.getClassExpr().isPresent()) { if (n.getClassName().isPresent()) {
result = n.getClassExpr().get().accept(this, arg); result = n.getClassName().get().accept(this, arg);
if (result != null) if (result != null)
return result; return result;
} }
Expand Down
Expand Up @@ -315,7 +315,7 @@ public Integer visit(final StringLiteralExpr n, final Void arg) {
} }


public Integer visit(final SuperExpr n, final Void arg) { public Integer visit(final SuperExpr n, final Void arg) {
return (n.getClassExpr().isPresent() ? n.getClassExpr().get().accept(this, arg) : 0) * 31 + (n.getComment().isPresent() ? n.getComment().get().accept(this, arg) : 0); return (n.getClassName().isPresent() ? n.getClassName().get().accept(this, arg) : 0) * 31 + (n.getComment().isPresent() ? n.getComment().get().accept(this, arg) : 0);
} }


public Integer visit(final SwitchEntry n, final Void arg) { public Integer visit(final SwitchEntry n, final Void arg) {
Expand Down
Expand Up @@ -830,9 +830,9 @@ public Visitable visit(final StringLiteralExpr n, final A arg) {


@Override @Override
public Visitable visit(final SuperExpr n, final A arg) { public Visitable visit(final SuperExpr n, final A arg) {
Expression classExpr = n.getClassExpr().map(s -> (Expression) s.accept(this, arg)).orElse(null); Name className = n.getClassName().map(s -> (Name) s.accept(this, arg)).orElse(null);
Comment comment = n.getComment().map(s -> (Comment) s.accept(this, arg)).orElse(null); Comment comment = n.getComment().map(s -> (Comment) s.accept(this, arg)).orElse(null);
n.setClassExpr(classExpr); n.setClassName(className);
n.setComment(comment); n.setComment(comment);
return n; return n;
} }
Expand Down
Expand Up @@ -651,7 +651,7 @@ public Boolean visit(final ThisExpr n, final Visitable arg) {
@Override @Override
public Boolean visit(final SuperExpr n, final Visitable arg) { public Boolean visit(final SuperExpr n, final Visitable arg) {
final SuperExpr n2 = (SuperExpr) arg; final SuperExpr n2 = (SuperExpr) arg;
if (!nodeEquals(n.getClassExpr(), n2.getClassExpr())) if (!nodeEquals(n.getClassName(), n2.getClassName()))
return false; return false;
return true; return true;
} }
Expand Down
Expand Up @@ -307,7 +307,7 @@ public Integer visit(final StringLiteralExpr n, final Void arg) {
} }


public Integer visit(final SuperExpr n, final Void arg) { public Integer visit(final SuperExpr n, final Void arg) {
return (n.getClassExpr().isPresent() ? n.getClassExpr().get().accept(this, arg) : 0); return (n.getClassName().isPresent() ? n.getClassName().get().accept(this, arg) : 0);
} }


public Integer visit(final SwitchEntry n, final Void arg) { public Integer visit(final SwitchEntry n, final Void arg) {
Expand Down
Expand Up @@ -488,7 +488,7 @@ public void visit(final StringLiteralExpr n, final A arg) {


@Override @Override
public void visit(final SuperExpr n, final A arg) { public void visit(final SuperExpr n, final A arg) {
n.getClassExpr().ifPresent(l -> l.accept(this, arg)); n.getClassName().ifPresent(l -> l.accept(this, arg));
n.getComment().ifPresent(l -> l.accept(this, arg)); n.getComment().ifPresent(l -> l.accept(this, arg));
} }


Expand Down
Expand Up @@ -162,7 +162,7 @@ private static void initializeConstructorParameters() {
simpleNameMetaModel.getConstructorParameters().add(simpleNameMetaModel.identifierPropertyMetaModel); simpleNameMetaModel.getConstructorParameters().add(simpleNameMetaModel.identifierPropertyMetaModel);
singleMemberAnnotationExprMetaModel.getConstructorParameters().add(annotationExprMetaModel.namePropertyMetaModel); singleMemberAnnotationExprMetaModel.getConstructorParameters().add(annotationExprMetaModel.namePropertyMetaModel);
singleMemberAnnotationExprMetaModel.getConstructorParameters().add(singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel); singleMemberAnnotationExprMetaModel.getConstructorParameters().add(singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel);
superExprMetaModel.getConstructorParameters().add(superExprMetaModel.classExprPropertyMetaModel); superExprMetaModel.getConstructorParameters().add(superExprMetaModel.classNamePropertyMetaModel);
thisExprMetaModel.getConstructorParameters().add(thisExprMetaModel.classNamePropertyMetaModel); thisExprMetaModel.getConstructorParameters().add(thisExprMetaModel.classNamePropertyMetaModel);
typeExprMetaModel.getConstructorParameters().add(typeExprMetaModel.typePropertyMetaModel); typeExprMetaModel.getConstructorParameters().add(typeExprMetaModel.typePropertyMetaModel);
unaryExprMetaModel.getConstructorParameters().add(unaryExprMetaModel.expressionPropertyMetaModel); unaryExprMetaModel.getConstructorParameters().add(unaryExprMetaModel.expressionPropertyMetaModel);
Expand Down Expand Up @@ -587,8 +587,8 @@ private static void initializePropertyMetaModels() {
simpleNameMetaModel.getDeclaredPropertyMetaModels().add(simpleNameMetaModel.identifierPropertyMetaModel); simpleNameMetaModel.getDeclaredPropertyMetaModels().add(simpleNameMetaModel.identifierPropertyMetaModel);
singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel = new PropertyMetaModel(singleMemberAnnotationExprMetaModel, "memberValue", com.github.javaparser.ast.expr.Expression.class, Optional.of(expressionMetaModel), false, false, false, false); singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel = new PropertyMetaModel(singleMemberAnnotationExprMetaModel, "memberValue", com.github.javaparser.ast.expr.Expression.class, Optional.of(expressionMetaModel), false, false, false, false);
singleMemberAnnotationExprMetaModel.getDeclaredPropertyMetaModels().add(singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel); singleMemberAnnotationExprMetaModel.getDeclaredPropertyMetaModels().add(singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel);
superExprMetaModel.classExprPropertyMetaModel = new PropertyMetaModel(superExprMetaModel, "classExpr", com.github.javaparser.ast.expr.Expression.class, Optional.of(expressionMetaModel), true, false, false, false); superExprMetaModel.classNamePropertyMetaModel = new PropertyMetaModel(superExprMetaModel, "className", com.github.javaparser.ast.expr.Name.class, Optional.of(nameMetaModel), true, false, false, false);
superExprMetaModel.getDeclaredPropertyMetaModels().add(superExprMetaModel.classExprPropertyMetaModel); superExprMetaModel.getDeclaredPropertyMetaModels().add(superExprMetaModel.classNamePropertyMetaModel);
thisExprMetaModel.classNamePropertyMetaModel = new PropertyMetaModel(thisExprMetaModel, "className", com.github.javaparser.ast.expr.Name.class, Optional.of(nameMetaModel), true, false, false, false); thisExprMetaModel.classNamePropertyMetaModel = new PropertyMetaModel(thisExprMetaModel, "className", com.github.javaparser.ast.expr.Name.class, Optional.of(nameMetaModel), true, false, false, false);
thisExprMetaModel.getDeclaredPropertyMetaModels().add(thisExprMetaModel.classNamePropertyMetaModel); thisExprMetaModel.getDeclaredPropertyMetaModels().add(thisExprMetaModel.classNamePropertyMetaModel);
typeExprMetaModel.typePropertyMetaModel = new PropertyMetaModel(typeExprMetaModel, "type", com.github.javaparser.ast.type.Type.class, Optional.of(typeMetaModel), false, false, false, false); typeExprMetaModel.typePropertyMetaModel = new PropertyMetaModel(typeExprMetaModel, "type", com.github.javaparser.ast.type.Type.class, Optional.of(typeMetaModel), false, false, false, false);
Expand Down
Expand Up @@ -8,5 +8,5 @@ public class SuperExprMetaModel extends ExpressionMetaModel {
super(superBaseNodeMetaModel, com.github.javaparser.ast.expr.SuperExpr.class, "SuperExpr", "com.github.javaparser.ast.expr", false, false); super(superBaseNodeMetaModel, com.github.javaparser.ast.expr.SuperExpr.class, "SuperExpr", "com.github.javaparser.ast.expr", false, false);
} }


public PropertyMetaModel classExprPropertyMetaModel; public PropertyMetaModel classNamePropertyMetaModel;
} }
Expand Up @@ -473,7 +473,7 @@ private static CsmElement typeArguments() {


concreteSyntaxModelByClass.put(SuperExpr.class, sequence( concreteSyntaxModelByClass.put(SuperExpr.class, sequence(
comment(), comment(),
conditional(ObservableProperty.CLASS_EXPR, IS_PRESENT, sequence(child(ObservableProperty.CLASS_EXPR), token(GeneratedJavaParserConstants.DOT))), conditional(CLASS_NAME, IS_PRESENT, sequence(child(CLASS_NAME), token(GeneratedJavaParserConstants.DOT))),
token(GeneratedJavaParserConstants.SUPER) token(GeneratedJavaParserConstants.SUPER)
)); ));


Expand Down
Expand Up @@ -719,8 +719,8 @@ public void visit(final ThisExpr n, final Void arg) {
@Override @Override
public void visit(final SuperExpr n, final Void arg) { public void visit(final SuperExpr n, final Void arg) {
printComment(n.getComment(), arg); printComment(n.getComment(), arg);
if (n.getClassExpr().isPresent()) { if (n.getClassName().isPresent()) {
n.getClassExpr().get().accept(this, arg); n.getClassName().get().accept(this, arg);
printer.print("."); printer.print(".");
} }
printer.print("super"); printer.print("super");
Expand Down
2 changes: 1 addition & 1 deletion javaparser-core/src/main/javacc/java.jj
Expand Up @@ -1864,7 +1864,7 @@ Expression PrimarySuffix(Expression scope):
LOOKAHEAD(2) LOOKAHEAD(2)
ret = PrimarySuffixWithoutSuper(scope) ret = PrimarySuffixWithoutSuper(scope)
| |
"." "super" { ret = new SuperExpr(range(scope, token()), scope); } "." "super" { ret = new SuperExpr(range(scope, token()), scopeToName(scope)); }
) )
{ return ret; } { return ret; }
} }
Expand Down
Expand Up @@ -218,7 +218,7 @@ public static NameRole classifyRole(Node name) {
if (whenParentIs(ThisExpr.class, name, (p, c) -> p.getClassName().isPresent() && p.getClassName().get() == c)) { if (whenParentIs(ThisExpr.class, name, (p, c) -> p.getClassName().isPresent() && p.getClassName().get() == c)) {
return NameRole.REFERENCE; return NameRole.REFERENCE;
} }
if (whenParentIs(SuperExpr.class, name, (p, c) -> p.getClassExpr().isPresent() && p.getClassExpr().get() == c)) { if (whenParentIs(SuperExpr.class, name, (p, c) -> p.getClassName().isPresent() && p.getClassName().get() == c)) {
return NameRole.REFERENCE; return NameRole.REFERENCE;
} }
if (whenParentIs(VariableDeclarator.class, name, (p, c) -> p.getName() == c)) { if (whenParentIs(VariableDeclarator.class, name, (p, c) -> p.getName() == c)) {
Expand Down Expand Up @@ -698,14 +698,8 @@ private static boolean isSyntacticallyATypeName(Node name) {


// 9. To the left of .super in a qualified superclass field access expression (§15.11.2) // 9. To the left of .super in a qualified superclass field access expression (§15.11.2)


if (whenParentIs(NameExpr.class, name, (nameExpr, c) ->
nameExpr.getName() == c && whenParentIs(SuperExpr.class, nameExpr, (ne, c2) ->
ne.getClassExpr().isPresent() && ne.getClassExpr().get() == c2)
)) {
return true;
}
if (whenParentIs(SuperExpr.class, name, (ne, c2) -> if (whenParentIs(SuperExpr.class, name, (ne, c2) ->
ne.getClassExpr().isPresent() && ne.getClassExpr().get() == c2)) { ne.getClassName().isPresent() && ne.getClassName().get() == c2)) {
return true; return true;
} }


Expand Down Expand Up @@ -987,9 +981,10 @@ private static <P extends Node, C extends Node> boolean whenParentIs(Class<P> pa
return whenParentIs(parentClass, child, (p, c) -> true); return whenParentIs(parentClass, child, (p, c) -> true);
} }


private static <P extends Node, C extends Node> boolean whenParentIs(Class<P> parentClass, private static <P extends Node, C extends Node> boolean whenParentIs(
C child, Class<P> parentClass,
PredicateOnParentAndChild<P, C> predicate) { C child,
PredicateOnParentAndChild<P, C> predicate) {
if (child.getParentNode().isPresent()) { if (child.getParentNode().isPresent()) {
Node parent = child.getParentNode().get(); Node parent = child.getParentNode().get();
return parentClass.isInstance(parent) && predicate.isSatisfied(parentClass.cast(parent), child); return parentClass.isInstance(parent) && predicate.isSatisfied(parentClass.cast(parent), child);
Expand Down

0 comments on commit fe2b93b

Please sign in to comment.