Skip to content

Commit

Permalink
Give ThisExpr 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 2e7bdfe commit 1bf4c06
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

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

class ThisExprTest {
Expand All @@ -15,19 +16,19 @@ void justThis() {

@Test
void singleScopeThis() {
Expression expr = parseExpression("a.this");
Expression expr = parseExpression("A.this");

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

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

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

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

assertTrue(classExpr.isFieldAccessExpr());
assertEquals("a.B", className.asString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@
import com.github.javaparser.utils.ClassUtils;
import com.github.javaparser.utils.CodeGenerationUtils;
import com.github.javaparser.utils.Utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static com.github.javaparser.JavaToken.Kind.*;
import static com.github.javaparser.Providers.UTF8;
import static com.github.javaparser.Providers.provider;
Expand All @@ -60,9 +58,9 @@
import static com.github.javaparser.ast.Modifier.createModifierList;
import static com.github.javaparser.utils.CodeGenerationUtils.subtractPaths;
import static com.github.javaparser.utils.Utils.assertNotNull;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.Generated;
import com.github.javaparser.TokenRange;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ public Optional<AnnotationDeclaration> toAnnotationDeclaration() {

@Override
public FieldDeclaration addField(Type type, String name, Modifier.Keyword... modifiers) {
throw new IllegalStateException("Cannot add a field to an annotation declaration.");
throw new IllegalStateException("Cannot add a field to an annotation declaration.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;

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

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

/**
* A constructor declaration: <code>class X { X() { } }</code> where X(){} is the constructor declaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.Optional;

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

import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.MethodDeclarationMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.TokenRange;
import com.github.javaparser.metamodel.OptionalProperty;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;

import java.util.function.Consumer;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.Generated;

/**
* A method declaration. "public int abc() {return 1;}" in this example: <code>class X { public int abc() {return 1;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@
public class ThisExpr extends Expression implements Resolvable<ResolvedTypeDeclaration> {

@OptionalProperty
private Expression classExpr;
private Name className;

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

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

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

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

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

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

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

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

@Override
Expand All @@ -139,9 +139,9 @@ public ThisExprMetaModel getMetaModel() {
public boolean replace(Node node, Node replacementNode) {
if (node == null)
return false;
if (classExpr != null) {
if (node == classExpr) {
setClassExpr((Expression) replacementNode);
if (className != null) {
if (node == className) {
setClassName((Name) replacementNode);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum ObservableProperty {
CLASS_BODY(Type.MULTIPLE_REFERENCE),
CLASS_DECLARATION(Type.SINGLE_REFERENCE),
CLASS_EXPR(Type.SINGLE_REFERENCE),
CLASS_NAME(Type.SINGLE_REFERENCE),
COMMENT(Type.SINGLE_REFERENCE),
COMPARE(Type.SINGLE_REFERENCE),
COMPONENT_TYPE(Type.SINGLE_REFERENCE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,9 @@ public Visitable visit(final SimpleName n, final Object arg) {

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

public Integer visit(final ThisExpr 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 ThrowStmt n, final Void arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,9 +876,9 @@ public Visitable visit(final SynchronizedStmt n, final A arg) {

@Override
public Visitable visit(final ThisExpr 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);
n.setClassExpr(classExpr);
n.setClassName(className);
n.setComment(comment);
return n;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public Boolean visit(final SimpleName n, final Visitable arg) {
@Override
public Boolean visit(final ThisExpr n, final Visitable arg) {
final ThisExpr n2 = (ThisExpr) arg;
if (!nodeEquals(n.getClassExpr(), n2.getClassExpr()))
if (!nodeEquals(n.getClassName(), n2.getClassName()))
return false;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public Integer visit(final SynchronizedStmt n, final Void arg) {
}

public Integer visit(final ThisExpr 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 ThrowStmt n, final Void arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public void visit(final SynchronizedStmt n, final A arg) {

@Override
public void visit(final ThisExpr 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static void initializeConstructorParameters() {
singleMemberAnnotationExprMetaModel.getConstructorParameters().add(annotationExprMetaModel.namePropertyMetaModel);
singleMemberAnnotationExprMetaModel.getConstructorParameters().add(singleMemberAnnotationExprMetaModel.memberValuePropertyMetaModel);
superExprMetaModel.getConstructorParameters().add(superExprMetaModel.classExprPropertyMetaModel);
thisExprMetaModel.getConstructorParameters().add(thisExprMetaModel.classExprPropertyMetaModel);
thisExprMetaModel.getConstructorParameters().add(thisExprMetaModel.classNamePropertyMetaModel);
typeExprMetaModel.getConstructorParameters().add(typeExprMetaModel.typePropertyMetaModel);
unaryExprMetaModel.getConstructorParameters().add(unaryExprMetaModel.expressionPropertyMetaModel);
unaryExprMetaModel.getConstructorParameters().add(unaryExprMetaModel.operatorPropertyMetaModel);
Expand Down Expand Up @@ -589,8 +589,8 @@ private static void initializePropertyMetaModels() {
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.getDeclaredPropertyMetaModels().add(superExprMetaModel.classExprPropertyMetaModel);
thisExprMetaModel.classExprPropertyMetaModel = new PropertyMetaModel(thisExprMetaModel, "classExpr", com.github.javaparser.ast.expr.Expression.class, Optional.of(expressionMetaModel), true, false, false, false);
thisExprMetaModel.getDeclaredPropertyMetaModels().add(thisExprMetaModel.classExprPropertyMetaModel);
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);
typeExprMetaModel.typePropertyMetaModel = new PropertyMetaModel(typeExprMetaModel, "type", com.github.javaparser.ast.type.Type.class, Optional.of(typeMetaModel), false, false, false, false);
typeExprMetaModel.getDeclaredPropertyMetaModels().add(typeExprMetaModel.typePropertyMetaModel);
unaryExprMetaModel.expressionPropertyMetaModel = new PropertyMetaModel(unaryExprMetaModel, "expression", com.github.javaparser.ast.expr.Expression.class, Optional.of(expressionMetaModel), false, false, false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public class ThisExprMetaModel extends ExpressionMetaModel {
super(superBaseNodeMetaModel, com.github.javaparser.ast.expr.ThisExpr.class, "ThisExpr", "com.github.javaparser.ast.expr", false, false);
}

public PropertyMetaModel classExprPropertyMetaModel;
public PropertyMetaModel classNamePropertyMetaModel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private static CsmElement typeArguments() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,8 @@ public void visit(final NullLiteralExpr n, final Void arg) {
@Override
public void visit(final ThisExpr n, final Void arg) {
printComment(n.getComment(), arg);
if (n.getClassExpr().isPresent()) {
n.getClassExpr().get().accept(this, arg);
if (n.getClassName().isPresent()) {
n.getClassName().get().accept(this, arg);
printer.print(".");
}
printer.print("this");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,20 @@ private String makeMessageForParseException(ParseException exception) {
}
return sb.toString();
}

/**
* Converts a NameExpr or a FieldAccessExpr scope to a Name.
*/
Name scopeToName(Expression scope) {
if (scope.isNameExpr()) {
SimpleName simpleName = scope.asNameExpr().getName();
return new Name(simpleName.getTokenRange().get(), null, simpleName.getIdentifier());
}
if (scope.isFieldAccessExpr()) {
FieldAccessExpr fieldAccessExpr = scope.asFieldAccessExpr();
return new Name(fieldAccessExpr.getTokenRange().get(), scopeToName(fieldAccessExpr.getScope()), fieldAccessExpr.getName().getIdentifier());

}
throw new IllegalStateException("Unexpected expression type: " + scope.getClass().getSimpleName());
}
}
2 changes: 1 addition & 1 deletion javaparser-core/src/main/javacc/java.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ Expression PrimarySuffixWithoutSuper(Expression scope):
(
"."
(
"this" { ret = new ThisExpr(range(scope, token()), scope); }
"this" { ret = new ThisExpr(range(scope, token()), scopeToName(scope)); }
|
ret = AllocationExpression(scope)
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public SymbolReference<ResolvedConstructorDeclaration> solve(ExplicitConstructor

public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node) {
// If 'this' is prefixed by a class eg. MyClass.this
if (node.getClassExpr().isPresent()) {
if (node.getClassName().isPresent()) {
// Get the class name
String className = node.getClassExpr().get().toString();
String className = node.getClassName().get().asString();
// Attempt to resolve using a typeSolver
SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className);
if (clazz.isSolved()) {
Expand Down

0 comments on commit 1bf4c06

Please sign in to comment.