Skip to content

Commit

Permalink
make localVariablesExposedToChildWithinABlock pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Aug 12, 2018
1 parent 03bae3e commit 299eb89
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
Expand Up @@ -58,7 +58,9 @@ default SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolv

/* Symbol resolution */

SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver);
default SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
return getParent().solveSymbol(name, typeSolver);
}

default Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
SymbolReference<? extends ResolvedValueDeclaration> ref = solveSymbol(name, typeSolver);
Expand Down Expand Up @@ -163,7 +165,8 @@ default Optional<ResolvedFieldDeclaration> fieldDeclarationInScope(String name)
/**
* We find the method declaration which is the best match for the given name and list of typeParametersValues.
*/
default SymbolReference<ResolvedConstructorDeclaration> solveConstructor(List<ResolvedType> argumentsTypes, TypeSolver typeSolver) {
default SymbolReference<ResolvedConstructorDeclaration> solveConstructor(List<ResolvedType> argumentsTypes,
TypeSolver typeSolver) {
throw new IllegalArgumentException("Constructor resolution is available only on Class Context");
}

Expand All @@ -172,7 +175,10 @@ default SymbolReference<ResolvedConstructorDeclaration> solveConstructor(List<Re
/**
* We find the method declaration which is the best match for the given name and list of typeParametersValues.
*/
SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver);
default SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes,
boolean staticOnly, TypeSolver typeSolver) {
return getParent().solveMethod(name, argumentsTypes, staticOnly, typeSolver);
}

/**
* Similar to solveMethod but we return a MethodUsage. A MethodUsage corresponds to a MethodDeclaration plus the
Expand Down
Expand Up @@ -83,6 +83,10 @@ public static Context getContext(Node node, TypeSolver typeSolver) {
return new StatementContext<>((Statement) node, typeSolver);
} else if (node instanceof CatchClause) {
return new CatchClauseContext((CatchClause) node, typeSolver);
} else if (node instanceof VariableDeclarator) {
return new VariableDeclaratorContext((VariableDeclarator) node, typeSolver);
} else if (node instanceof VariableDeclarationExpr) {
return new VariableDeclarationExprContext((VariableDeclarationExpr) node, typeSolver);
} else if (node instanceof ObjectCreationExpr &&
((ObjectCreationExpr) node).getAnonymousClassBody().isPresent()) {
return new AnonymousClassDeclarationContext((ObjectCreationExpr) node, typeSolver);
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright 2016 Federico Tomassetti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;

import java.util.Collections;
import java.util.List;

/**
* @author Federico Tomassetti
*/
public class VariableDeclarationExprContext extends AbstractJavaParserContext<VariableDeclarationExpr> {

public VariableDeclarationExprContext(VariableDeclarationExpr wrappedNode, TypeSolver typeSolver) {
super(wrappedNode, typeSolver);
}

@Override
public List<VariableDeclarator> localVariablesExposedToChild(Node child) {
for (int i=0;i<wrappedNode.getVariables().size();i++) {
if (child == wrappedNode.getVariable(i)) {
return wrappedNode.getVariables().subList(0, i);
}
}
return Collections.emptyList();
}

}
@@ -0,0 +1,43 @@
/*
* Copyright 2016 Federico Tomassetti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;

import java.util.Collections;
import java.util.List;

/**
* @author Federico Tomassetti
*/
public class VariableDeclaratorContext extends AbstractJavaParserContext<VariableDeclarator> {

public VariableDeclaratorContext(VariableDeclarator wrappedNode, TypeSolver typeSolver) {
super(wrappedNode, typeSolver);
}

@Override
public List<VariableDeclarator> localVariablesExposedToChild(Node child) {
if (wrappedNode.getInitializer().isPresent() && wrappedNode.getInitializer().get() == child) {
return Collections.singletonList(wrappedNode);
}
return Collections.emptyList();
}

}
Expand Up @@ -22,6 +22,7 @@
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.resolution.MethodUsage;
Expand All @@ -44,6 +45,7 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -533,8 +535,9 @@ private void assertOneVarExposedToChildInContextNamed(Node parent, Node child, S

private void assertNumberOfVarsExposedToChildInContextNamed(Node parent, Node child, String paramName,
int expectedNumber, String message) {
assertEquals(message, expectedNumber, JavaParserFactory.getContext(parent, typeSolver)
.localVariablesExposedToChild(child).stream().filter(p -> p.getNameAsString().equals(paramName)).count());
List<VariableDeclarator> vars = JavaParserFactory.getContext(parent, typeSolver)
.localVariablesExposedToChild(child);
assertEquals(message, expectedNumber, vars.stream().filter(p -> p.getNameAsString().equals(paramName)).count());
}

@Test
Expand Down Expand Up @@ -578,12 +581,14 @@ public void localVariablesExposedToChildWithinABlock() {

VariableDeclarationExpr varDecl = blockStmt.getStatement(1).asExpressionStmt().getExpression()
.asVariableDeclarationExpr();
assertOneVarExposedToChildInContextNamed(varDecl.getVariables().get(0),
varDecl.getVariables().get(0).getInitializer().get(), "a");
VariableDeclarator varA = varDecl.getVariables().get(0);
VariableDeclarator varB = varDecl.getVariables().get(1);
assertOneVarExposedToChildInContextNamed(varA,
varA.getInitializer().get(), "a");
assertOneVarExposedToChildInContextNamed(varDecl,
varDecl.getVariables().get(1), "a");
varB, "a");
assertNoVarsExposedToChildInContextNamed(varDecl,
varDecl.getVariables().get(0), "b");
varA, "b");
}

// The scope of a local variable declared in the ForInit part of a basic for statement (§14.14.1) includes all of the following:
Expand Down

0 comments on commit 299eb89

Please sign in to comment.