Skip to content

Commit

Permalink
Merge pull request #2133 from jjbr/inter-blockStmt-resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
MysterAitch committed Nov 14, 2020
2 parents 149eade + 9ff9453 commit 222a9f4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.javaparser.symbolsolver.resolution;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class VariableResolutionTest extends AbstractResolutionTest {

@Test
void variableResolutionNoBlockStmt() {
// Test without nested block statement

CompilationUnit cu = parseSample("VariableResolutionInVariousScopes");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VariableResolutionInVariousScopes");

MethodDeclaration method = Navigator.demandMethod(clazz, "noBlock");
MethodCallExpr callExpr = method.findFirst(MethodCallExpr.class).get();
MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(callExpr);

assertTrue(methodUsage.declaringType().getQualifiedName().equals("java.lang.String"));
}

@Test
void variableResolutionWithBlockStmt() {
// Test without nested block statement

CompilationUnit cu = parseSample("VariableResolutionInVariousScopes");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VariableResolutionInVariousScopes");

MethodDeclaration method = Navigator.demandMethod(clazz, "withBlock");
MethodCallExpr callExpr = method.findFirst(MethodCallExpr.class).get();
MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(callExpr);

assertTrue(methodUsage.declaringType().getQualifiedName().equals("java.lang.String"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testcase;

public class VariableResolutionInVariousScopes {
public void withBlock() {
String s = "";
{
s.length();
}
}

public void noBlock() {
String s = "";
s.length();
}
}

0 comments on commit 222a9f4

Please sign in to comment.