Skip to content

Commit

Permalink
Merge pull request #3529 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix issue #3526 Variable or FieldDeclaration is not resolved correctl…
  • Loading branch information
jlerbsc committed Mar 23, 2022
2 parents 78dbb18 + 03cb6a8 commit 9b9536f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

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

import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
Expand All @@ -39,11 +44,6 @@
import com.github.javaparser.symbolsolver.model.resolution.Value;
import com.github.javaparser.symbolsolver.resolution.SymbolDeclarator;

import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;

/**
* @author Federico Tomassetti
*/
Expand Down Expand Up @@ -174,8 +174,8 @@ public Optional<Value> solveSymbolAsValue(String name) {
}
}

// If nothing is found we should ask the parent context.
return solveSymbolAsValueInParentContext(name);
// If nothing is found we should ask the grand parent context.
return parentContext.getParent().map(context -> context.solveSymbolAsValue(name)).orElse(Optional.empty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2019 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.resolution.javaparser.contexts;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.contexts.BlockStmtContext;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

/**
*
*/
class BlockStmtContextResolutionTest extends AbstractResolutionTest {

@BeforeEach
void setup() {
}

// issue #3526
@Test
void field_must_be_resolved_from_previous_declaration(){
String src = "public class Example {\n"
+ " int a = 3;\n"
+ " public void bla() {\n"
+ " a = 7; // 'a' must be resolved as int not String"
+ " String a = \"\";\n"
+ " a = \"test\";\n"
+ " }\n"
+ "}";
ParserConfiguration configuration = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(new CombinedTypeSolver(new ReflectionTypeSolver())));
StaticJavaParser.setConfiguration(configuration);
CompilationUnit cu = StaticJavaParser.parse(src);
BlockStmt stmt = cu.findFirst(BlockStmt.class).get();
BlockStmtContext ctx = new BlockStmtContext(stmt, new ReflectionTypeSolver());
SymbolReference<? extends ResolvedValueDeclaration> ref = ctx.solveSymbol("a");

assertEquals(true, ref.isSolved());
assertEquals("int", ref.getCorrespondingDeclaration().getType().asPrimitive().describe());
}

}

0 comments on commit 9b9536f

Please sign in to comment.