resolve constants within the AST to get real value instead of constant name

This commit is contained in:
Martin Lippert
2019-04-30 09:44:25 +02:00
parent 8fb7960887
commit 0b8a952e55
4 changed files with 39 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2018 Pivotal, Inc.
* Copyright (c) 2017, 2019 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -20,7 +20,9 @@ import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.NormalAnnotation;
@@ -180,6 +182,14 @@ public class ASTUtils {
if (exp instanceof StringLiteral) {
return getLiteralValue((StringLiteral) exp);
} else if (exp instanceof QualifiedName) {
IBinding binding = ((QualifiedName) exp).resolveBinding();
if (binding != null && binding.getKind() == IBinding.VARIABLE) {
IVariableBinding varBinding = (IVariableBinding) binding;
Object constValue = varBinding.getConstantValue();
if (constValue != null) {
return constValue.toString();
}
}
return getExpressionValueAsString(((QualifiedName) exp).getName());
} else if (exp instanceof SimpleName) {
return ((SimpleName) exp).getIdentifier();

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2018 Pivotal, Inc.
* Copyright (c) 2017, 2019 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -70,6 +70,14 @@ public class RequestMappingSymbolProviderTest {
assertTrue(containsSymbol(symbols, "@/greeting", docUri, 6, 1, 6, 29));
}
@Test
public void testSimpleRequestMappingSymbolFromConstant() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/SimpleMappingClassWithConstant.java").toUri().toString();
List<? extends SymbolInformation> symbols = indexer.getSymbols(docUri);
assertEquals(1, symbols.size());
assertTrue(containsSymbol(symbols, "@/path/from/constant", docUri, 6, 1, 6, 48));
}
@Test
public void testParentRequestMappingSymbol() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/ParentMappingClass.java").toUri().toString();

View File

@@ -0,0 +1,7 @@
package org.test;
public class Constants {
public static final String REQUEST_MAPPING_PATH = "path/from/constant";
}

View File

@@ -0,0 +1,12 @@
package org.test;
import org.springframework.web.bind.annotation.RequestMapping;
public class SimpleMappingClassWithConstant {
@RequestMapping(Constants.REQUEST_MAPPING_PATH)
public String hello() {
return "Hello";
}
}