Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit605b4d7

Browse files
committed
Fix: issue#3916 Method 'valueOf' cannot be resolved in context MyEnum.One.valueOf("")
1 parent721d60a commit605b4d7

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

‎javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserEnumDeclaration.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class JavaParserEnumDeclaration extends AbstractTypeDeclaration
5858
SymbolResolutionCapability {
5959

6060
privatestaticfinalStringVALUES ="values";
61+
privatestaticfinalStringVALUE_OF ="valueOf";
6162

6263
privateTypeSolvertypeSolver;
6364
privateEnumDeclarationwrappedNode;
@@ -193,6 +194,13 @@ public Optional<MethodUsage> solveMethodAsUsage(
193194
if (VALUES.equals(name) &&argumentTypes.isEmpty()) {
194195
returnOptional.of(newMethodUsage(newJavaParserEnumDeclaration.ValuesMethod(this,typeSolver)));
195196
}
197+
if (VALUE_OF.equals(name) &&argumentTypes.size() ==1) {
198+
ResolvedTypeargument =argumentTypes.get(0);
199+
if (argument.isReferenceType()
200+
&&"java.lang.String".equals(argument.asReferenceType().getQualifiedName())) {
201+
returnOptional.of(newMethodUsage(newJavaParserEnumDeclaration.ValueOfMethod(this,typeSolver)));
202+
}
203+
}
196204
returngetContext().solveMethodAsUsage(name,argumentTypes);
197205
}
198206

@@ -202,7 +210,7 @@ public SymbolReference<ResolvedMethodDeclaration> solveMethod(
202210
if (VALUES.equals(name) &&argumentsTypes.isEmpty()) {
203211
returnSymbolReference.solved(newJavaParserEnumDeclaration.ValuesMethod(this,typeSolver));
204212
}
205-
if ("valueOf".equals(name) &&argumentsTypes.size() ==1) {
213+
if (VALUE_OF.equals(name) &&argumentsTypes.size() ==1) {
206214
ResolvedTypeargument =argumentsTypes.get(0);
207215
if (argument.isReferenceType()
208216
&&"java.lang.String".equals(argument.asReferenceType().getQualifiedName())) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2013-2024 The JavaParser Team.
3+
*
4+
* This file is part of JavaParser.
5+
*
6+
* JavaParser can be used either under the terms of
7+
* a) the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* b) the terms of the Apache License
11+
*
12+
* You should have received a copy of both licenses in LICENCE.LGPL and
13+
* LICENCE.APACHE. Please refer to those files for details.
14+
*
15+
* JavaParser is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Lesser General Public License for more details.
19+
*/
20+
21+
packagecom.github.javaparser.symbolsolver;
22+
23+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
24+
25+
importcom.github.javaparser.JavaParserAdapter;
26+
importcom.github.javaparser.ast.CompilationUnit;
27+
importcom.github.javaparser.ast.expr.MethodCallExpr;
28+
importcom.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
29+
importorg.junit.jupiter.api.Test;
30+
31+
publicclassIssue3916TestextendsAbstractResolutionTest {
32+
33+
@Test
34+
voidissue3916() {
35+
36+
Stringcode ="enum MyEnum {\n"
37+
+" One;\n"
38+
+" }\n"
39+
+"\n"
40+
+" class Foo {\n"
41+
+" String str;\n"
42+
+"\n"
43+
+" public void setStr(String str) {\n"
44+
+" this.str = str;\n"
45+
+" }\n"
46+
+"\n"
47+
+" void test(String str) {\n"
48+
+" switch (MyEnum.One.valueOf(\"\")) {\n"
49+
+" case One:\n"
50+
+" setStr(str);\n"
51+
+" break;\n"
52+
+" }\n"
53+
+" }\n"
54+
+" }";
55+
56+
CompilationUnitcu =JavaParserAdapter.of(createParserWithResolver(defaultTypeSolver()))
57+
.parse(code);
58+
59+
cu.findAll(MethodCallExpr.class).forEach(mce -> {
60+
if (mce.getNameAsString().equals("setStr")) {
61+
System.out.println(mce.toString());
62+
assertEquals("Foo.setStr(java.lang.String)",mce.resolve().getQualifiedSignature());
63+
}
64+
});
65+
}
66+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp