Skip to content

Commit

Permalink
Replace TypePatternExpr with PatternExpr in javaparsermodel.contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
johannescoetzee committed Apr 24, 2024
1 parent d3fb640 commit b59fc92
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.quality.Nullable;
import com.github.javaparser.resolution.declarations.*;
Expand Down Expand Up @@ -216,19 +217,19 @@ default List<Parameter> parametersExposedToChild(Node child) {
* The pattern expressions that are declared in this immediate context and made visible to a given child.
* This list could include values which are shadowed.
*/
default List<TypePatternExpr> patternExprsExposedToChild(Node child) {
default List<PatternExpr> patternExprsExposedToChild(Node child) {
return Collections.emptyList();
}

/**
*/
default List<TypePatternExpr> patternExprsExposedFromChildren() {
default List<PatternExpr> patternExprsExposedFromChildren() {
return Collections.emptyList();
}

/**
*/
default List<TypePatternExpr> negatedPatternExprsExposedFromChildren() {
default List<PatternExpr> negatedPatternExprsExposedFromChildren() {
return Collections.emptyList();
}

Expand Down Expand Up @@ -316,7 +317,7 @@ default Optional<Parameter> parameterDeclarationInScope(String name) {
* }
* }</pre>
*/
default Optional<TypePatternExpr> patternExprInScope(String name) {
default Optional<PatternExpr> patternExprInScope(String name) {
if (!getParent().isPresent()) {
return Optional.empty();
}
Expand All @@ -325,10 +326,10 @@ default Optional<TypePatternExpr> patternExprInScope(String name) {
// FIXME: If there are multiple patterns, throw an error?
// First check if the pattern is directly declared within this context.
Node wrappedNode = getWrappedNode();
Optional<TypePatternExpr> localResolutionResults = parentContext
Optional<PatternExpr> localResolutionResults = parentContext
.patternExprsExposedToChild(wrappedNode)
.stream()
.filter(vd -> vd.getNameAsString().equals(name))
.filter(vd -> vd instanceof TypePatternExpr && ((TypePatternExpr) vd).getNameAsString().equals(name))
.findFirst();
if (localResolutionResults.isPresent()) {
return localResolutionResults;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
///*
// * Copyright (C) 2015-2016 Federico Tomassetti
// * Copyright (C) 2017-2024 The JavaParser Team.
// *
// * This file is part of JavaParser.
// *
// * JavaParser can be used either under the terms of
// * a) the GNU Lesser General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// * b) the terms of the Apache License
// *
// * You should have received a copy of both licenses in LICENCE.LGPL and
// * LICENCE.APACHE. Please refer to those files for details.
// *
// * JavaParser is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU Lesser General Public License for more details.
// */

package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

Expand Down Expand Up @@ -144,16 +144,17 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbolInParentCo
// First check if there are any pattern expressions available to this node.
Context parentContext = optionalParentContext.get();
if(parentContext instanceof BinaryExprContext || parentContext instanceof IfStatementContext) {
List<TypePatternExpr> typePatternExprs = parentContext.patternExprsExposedToChild(this.getWrappedNode());
List<PatternExpr> typePatternExprs = parentContext.patternExprsExposedToChild(this.getWrappedNode());

Optional<TypePatternExpr> localResolutionResults = typePatternExprs
Optional<PatternExpr> localResolutionResults = typePatternExprs
.stream()
.filter(vd -> vd.getNameAsString().equals(name))
.filter(vd -> vd instanceof TypePatternExpr && ((TypePatternExpr) vd).getNameAsString().equals(name))
.findFirst();

if (localResolutionResults.isPresent()) {
if (localResolutionResults.isPresent() && (localResolutionResults.get() instanceof TypePatternExpr)) {
if (typePatternExprs.size() == 1) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(localResolutionResults.get(), typeSolver);
TypePatternExpr typePatternExpr = (TypePatternExpr) localResolutionResults.get();
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver);
return SymbolReference.solved(decl);
}
if(typePatternExprs.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.TypeSolver;
Expand All @@ -39,13 +40,13 @@ public BinaryExprContext(BinaryExpr wrappedNode, TypeSolver typeSolver) {
}

@Override
public List<TypePatternExpr> patternExprsExposedFromChildren() {
public List<PatternExpr> patternExprsExposedFromChildren() {

BinaryExpr binaryExpr = wrappedNode;
Expression leftBranch = binaryExpr.getLeft();
Expression rightBranch = binaryExpr.getRight();

List<TypePatternExpr> results = new ArrayList<>();
List<PatternExpr> results = new ArrayList<>();

if (binaryExpr.getOperator().equals(BinaryExpr.Operator.EQUALS)) {
if (rightBranch.isBooleanLiteralExpr()) {
Expand Down Expand Up @@ -95,13 +96,13 @@ public List<TypePatternExpr> patternExprsExposedFromChildren() {
}

@Override
public List<TypePatternExpr> negatedPatternExprsExposedFromChildren() {
public List<PatternExpr> negatedPatternExprsExposedFromChildren() {

BinaryExpr binaryExpr = wrappedNode;
Expression leftBranch = binaryExpr.getLeft();
Expression rightBranch = binaryExpr.getRight();

List<TypePatternExpr> results = new ArrayList<>();
List<PatternExpr> results = new ArrayList<>();

// FIXME: Redo the `.getValue() == true` to take more complex code into account when determining if definitively true (e.g. `
if (binaryExpr.getOperator().equals(BinaryExpr.Operator.EQUALS)) {
Expand Down Expand Up @@ -161,7 +162,7 @@ public List<TypePatternExpr> negatedPatternExprsExposedFromChildren() {
return results;
}

private List<TypePatternExpr> patternExprsExposedToDirectParentFromBranch(Expression branch) {
private List<PatternExpr> patternExprsExposedToDirectParentFromBranch(Expression branch) {
if (branch.isEnclosedExpr() || branch.isBinaryExpr() || branch.isUnaryExpr() || branch.isInstanceOfExpr()) {
Context branchContext = JavaParserFactory.getContext(branch, typeSolver);
return branchContext.patternExprsExposedFromChildren();
Expand All @@ -170,7 +171,7 @@ private List<TypePatternExpr> patternExprsExposedToDirectParentFromBranch(Expres
return new ArrayList<>();
}

private List<TypePatternExpr> negatedPatternExprsExposedToDirectParentFromBranch(Expression branch) {
private List<PatternExpr> negatedPatternExprsExposedToDirectParentFromBranch(Expression branch) {
if (branch.isEnclosedExpr() || branch.isBinaryExpr() || branch.isUnaryExpr() || branch.isInstanceOfExpr()) {
Context branchContext = JavaParserFactory.getContext(branch, typeSolver);
return branchContext.negatedPatternExprsExposedFromChildren();
Expand All @@ -179,12 +180,12 @@ private List<TypePatternExpr> negatedPatternExprsExposedToDirectParentFromBranch
return new ArrayList<>();
}

public List<TypePatternExpr> patternExprsExposedToChild(Node child) {
public List<PatternExpr> patternExprsExposedToChild(Node child) {
BinaryExpr binaryExpr = wrappedNode;
Expression leftBranch = binaryExpr.getLeft();
Expression rightBranch = binaryExpr.getRight();

List<TypePatternExpr> results = new ArrayList<>();
List<PatternExpr> results = new ArrayList<>();
if (child == leftBranch) {
results.addAll(patternExprsExposedToDirectParentFromBranch(leftBranch));
} else if (child == rightBranch) {
Expand All @@ -202,15 +203,15 @@ public List<TypePatternExpr> patternExprsExposedToChild(Node child) {
}


public Optional<TypePatternExpr> patternExprInScope(String name) {
public Optional<PatternExpr> patternExprInScope(String name) {
BinaryExpr binaryExpr = wrappedNode;
Expression leftBranch = binaryExpr.getLeft();
Expression rightBranch = binaryExpr.getRight();

List<TypePatternExpr> typePatternExprs = patternExprsExposedToDirectParentFromBranch(leftBranch);
Optional<TypePatternExpr> localResolutionResults = typePatternExprs
List<PatternExpr> patternExprs = patternExprsExposedToDirectParentFromBranch(leftBranch);
Optional<PatternExpr> localResolutionResults = patternExprs
.stream()
.filter(vd -> vd.getNameAsString().equals(name))
.filter(vd -> vd instanceof TypePatternExpr && ((TypePatternExpr) vd).getNameAsString().equals(name))
.findFirst();

if (localResolutionResults.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
Expand All @@ -37,8 +37,8 @@ public EnclosedExprContext(EnclosedExpr wrappedNode, TypeSolver typeSolver) {
}

@Override
public List<TypePatternExpr> patternExprsExposedFromChildren() {
List<TypePatternExpr> results = new ArrayList<>();
public List<PatternExpr> patternExprsExposedFromChildren() {
List<PatternExpr> results = new ArrayList<>();

/*
* Test for an assignment expression
Expand All @@ -58,8 +58,8 @@ public List<TypePatternExpr> patternExprsExposedFromChildren() {
}

@Override
public List<TypePatternExpr> negatedPatternExprsExposedFromChildren() {
List<TypePatternExpr> results = new ArrayList<>();
public List<PatternExpr> negatedPatternExprsExposedFromChildren() {
List<PatternExpr> results = new ArrayList<>();

/*
* Test for an assignment expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.TypeSolver;
Expand All @@ -40,11 +40,11 @@ public IfStatementContext(IfStmt wrappedNode, TypeSolver typeSolver) {


@Override
public List<TypePatternExpr> patternExprsExposedToChild(Node child) {
public List<PatternExpr> patternExprsExposedToChild(Node child) {
Expression condition = wrappedNode.getCondition();
Context conditionContext = JavaParserFactory.getContext(condition, typeSolver);

List<TypePatternExpr> results = new ArrayList<>();
List<PatternExpr> results = new ArrayList<>();

boolean givenNodeIsWithinThenStatement = wrappedNode.getThenStmt().containsWithinRange(child);
if(givenNodeIsWithinThenStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

import com.github.javaparser.ast.expr.InstanceOfExpr;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.TypeSolver;
Expand All @@ -44,10 +45,12 @@ public InstanceOfExprContext(InstanceOfExpr wrappedNode, TypeSolver typeSolver)

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name) {
Optional<TypePatternExpr> optionalPatternExpr = wrappedNode.getPattern();
if(optionalPatternExpr.isPresent()) {
if(optionalPatternExpr.get().getNameAsString().equals(name)) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(optionalPatternExpr.get(), typeSolver);
// TODO: Add PatternExprContext and solve in that
Optional<PatternExpr> optionalPatternExpr = wrappedNode.getPattern();
if(optionalPatternExpr.isPresent() && (optionalPatternExpr.get() instanceof TypePatternExpr)) {
TypePatternExpr typePatternExpr = (TypePatternExpr) optionalPatternExpr.get();
if(typePatternExpr.getNameAsString().equals(name)) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver);
return SymbolReference.solved(decl);
}
}
Expand All @@ -60,9 +63,10 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String na

Context parentContext = optionalParentContext.get();
if(parentContext instanceof BinaryExprContext) {
Optional<TypePatternExpr> optionalPatternExpr1 = parentContext.patternExprInScope(name);
if(optionalPatternExpr1.isPresent()) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(optionalPatternExpr1.get(), typeSolver);
Optional<PatternExpr> optionalPatternExpr1 = parentContext.patternExprInScope(name);
if(optionalPatternExpr1.isPresent() && (optionalPatternExpr1.get() instanceof TypePatternExpr)) {
TypePatternExpr typePatternExpr = (TypePatternExpr) optionalPatternExpr1.get();
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver);
return SymbolReference.solved(decl);
}
} // TODO: Also consider unary expr context
Expand All @@ -73,8 +77,8 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String na
}

@Override
public List<TypePatternExpr> patternExprsExposedFromChildren() {
List<TypePatternExpr> results = new ArrayList<>();
public List<PatternExpr> patternExprsExposedFromChildren() {
List<PatternExpr> results = new ArrayList<>();

// If this instanceof expression has a pattern, add it to the list.
wrappedNode.getPattern().ifPresent(results::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.PatternExpr;
import com.github.javaparser.ast.expr.TypePatternExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithStatements;
import com.github.javaparser.ast.stmt.Statement;
Expand Down Expand Up @@ -217,11 +218,14 @@ private SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String n
* If we're in a statement that contains a pattern expression.
* Example: {@code double x = a instanceof String s;}
*/
List<TypePatternExpr> typePatternExprs = patternExprsExposedFromChildren();
for (int i = 0; i < typePatternExprs.size(); i++) {
TypePatternExpr typePatternExpr = typePatternExprs.get(i);
if(typePatternExpr.getNameAsString().equals(name)) {
return SymbolReference.solved(JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver));
List<PatternExpr> patternExprs = patternExprsExposedFromChildren();
for (int i = 0; i < patternExprs.size(); i++) {
PatternExpr patternExpr = patternExprs.get(i);
if (patternExpr instanceof TypePatternExpr) {
TypePatternExpr typePatternExpr = (TypePatternExpr) patternExpr;
if(typePatternExpr.getNameAsString().equals(name)) {
return SymbolReference.solved(JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver));
}
}
}

Expand Down Expand Up @@ -320,14 +324,14 @@ public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<


@Override
public List<TypePatternExpr> patternExprsExposedFromChildren() {
public List<PatternExpr> patternExprsExposedFromChildren() {
// Statements never make pattern expressions available.
return Collections.emptyList();

}

@Override
public List<TypePatternExpr> negatedPatternExprsExposedFromChildren() {
public List<PatternExpr> negatedPatternExprsExposedFromChildren() {
// Statements never make pattern expressions available.
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String na
continue;
}

TypePatternExpr pattern = (TypePatternExpr) e;
if (pattern.getNameAsString().equals(name)) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(pattern, typeSolver);
TypePatternExpr typePatternExpr = (TypePatternExpr) e;
if (typePatternExpr.getNameAsString().equals(name)) {
JavaParserPatternDeclaration decl = JavaParserSymbolDeclaration.patternVar(typePatternExpr, typeSolver);
return SymbolReference.solved(decl);
}
}
Expand Down
Loading

0 comments on commit b59fc92

Please sign in to comment.