Refactored live conditionals so that they are parsed via SpringBootApp
API
This commit is contained in:
@@ -12,7 +12,6 @@ package org.springframework.ide.vscode.boot.java.conditionals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -27,13 +26,10 @@ import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.MarkedString;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.HoverProvider;
|
||||
import org.springframework.ide.vscode.boot.java.utils.HoverContentUtils;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.LiveConditional;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
|
||||
import org.springframework.ide.vscode.commons.util.BadLocationException;
|
||||
import org.springframework.ide.vscode.commons.util.Log;
|
||||
import org.springframework.ide.vscode.commons.util.text.TextDocument;
|
||||
|
||||
@@ -55,28 +51,45 @@ public class ConditionalsLiveHoverProvider implements HoverProvider {
|
||||
@Override
|
||||
public Collection<Range> getLiveHoverHints(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
|
||||
try {
|
||||
if (runningApps.length > 0) {
|
||||
ConditionalParserFromRunningApp parser = new ConditionalParserFromRunningApp();
|
||||
Optional<List<LiveConditional>> val = parser.parse(annotation, runningApps);
|
||||
if (val.isPresent()) {
|
||||
Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
|
||||
return ImmutableList.of(hoverRange);
|
||||
}
|
||||
Optional<List<LiveConditional>> val = getMatchedLiveConditionals(annotation, runningApps);
|
||||
if (val.isPresent()) {
|
||||
Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
|
||||
return ImmutableList.of(hoverRange);
|
||||
}
|
||||
} catch (BadLocationException e) {
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Optional<List<LiveConditional>> getMatchedLiveConditionals(Annotation annotation,
|
||||
SpringBootApp[] runningApps) throws Exception {
|
||||
if (runningApps != null) {
|
||||
List<LiveConditional> all = new ArrayList<>();
|
||||
|
||||
for (SpringBootApp springBootApp : runningApps) {
|
||||
springBootApp.getLiveConditionals().ifPresent((conditionals) -> {
|
||||
conditionals.stream().forEach((conditional) -> {
|
||||
if (matchesAnnotation(annotation, conditional)) {
|
||||
all.add(conditional);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
if (!all.isEmpty()) {
|
||||
return Optional.of(all);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private CompletableFuture<Hover> provideHover(Annotation annotation, TextDocument doc,
|
||||
SpringBootApp[] runningApps) {
|
||||
|
||||
try {
|
||||
List<Either<String, MarkedString>> hoverContent = new ArrayList<>();
|
||||
ConditionalParserFromRunningApp parser = new ConditionalParserFromRunningApp();
|
||||
Optional<List<LiveConditional>> val = parser.parse(annotation, runningApps);
|
||||
Optional<List<LiveConditional>> val = getMatchedLiveConditionals(annotation, runningApps);
|
||||
|
||||
if (val.isPresent()) {
|
||||
addHoverContent(val.get(), hoverContent);
|
||||
@@ -96,138 +109,43 @@ public class ConditionalsLiveHoverProvider implements HoverProvider {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addHoverContent(List<LiveConditional> conditions,
|
||||
List<Either<String, MarkedString>> hoverContent) throws Exception {
|
||||
for (int i = 0; i < conditions.size(); i++) {
|
||||
LiveConditional condition = conditions.get(i);
|
||||
hoverContent.add(Either.forLeft(condition.message));
|
||||
hoverContent.add(Either
|
||||
.forLeft(HoverContentUtils.getProcessInformation(condition.app)));
|
||||
private void addHoverContent(List<LiveConditional> conditionals, List<Either<String, MarkedString>> hoverContent)
|
||||
throws Exception {
|
||||
for (int i = 0; i < conditionals.size(); i++) {
|
||||
LiveConditional conditional = conditionals.get(i);
|
||||
hoverContent.add(Either.forLeft(conditional.getMessage()));
|
||||
hoverContent.add(Either.forLeft(HoverContentUtils.getProcessInformation(conditional.getProcessId(), conditional.getProcessName())));
|
||||
|
||||
if (i < conditions.size() - 1) {
|
||||
if (i < conditionals.size() - 1) {
|
||||
hoverContent.add(Either.forLeft("---"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parsers @ConditionalOn annotations from a spring boot running app's
|
||||
* autoconfig report. An example of a conditional that is parsed from a running
|
||||
* app's autoconfig report would be:
|
||||
*
|
||||
* {"TraceRepositoryAutoConfiguration#traceRepository":[{"condition":"OnBeanCondition","message":"@ConditionalOnMissingBean
|
||||
* (types: org.springframework.boot.actuate.trace.TraceRepository;
|
||||
* SearchStrategy: all) did not find any beans"}]
|
||||
*
|
||||
* @param annotation
|
||||
* @param jsonKey
|
||||
* @return true if the annotation matches the information in the json key from
|
||||
* the running app.
|
||||
*/
|
||||
public static class ConditionalParserFromRunningApp {
|
||||
protected boolean matchesAnnotation(Annotation annotation, LiveConditional liveConditional) {
|
||||
|
||||
public Optional<List<LiveConditional>> parse(Annotation annotation, SpringBootApp[] runningApps) {
|
||||
ASTNode parent = annotation.getParent();
|
||||
String rawJsonKey = liveConditional.getPositiveMatchKey();
|
||||
|
||||
try {
|
||||
List<LiveConditional> allConditionals = new ArrayList<>();
|
||||
for (SpringBootApp app : runningApps) {
|
||||
String autoConfigRecord = app.getAutoConfigReport();
|
||||
if (autoConfigRecord != null) {
|
||||
JSONObject autoConfigJson = new JSONObject(autoConfigRecord);
|
||||
List<LiveConditional> conditionalsFromPositiveMatches = getConditionals(app, annotation,
|
||||
autoConfigJson);
|
||||
if (!conditionalsFromPositiveMatches.isEmpty()) {
|
||||
allConditionals.addAll(conditionalsFromPositiveMatches);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!allConditionals.isEmpty()) {
|
||||
return Optional.of(allConditionals);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param app
|
||||
* @param annotation
|
||||
* @param autoConfigJson
|
||||
* @return non-null list of conditionals parsed from an autoconfig report. List
|
||||
* may be empty.
|
||||
*/
|
||||
private List<LiveConditional> getConditionals(SpringBootApp app, Annotation annotation,
|
||||
JSONObject autoConfigJson) {
|
||||
List<LiveConditional> conditions = new ArrayList<>();
|
||||
|
||||
getPositiveMatches(autoConfigJson).ifPresent((positiveMatches) -> {
|
||||
Iterator<String> pMKeys = positiveMatches.keys();
|
||||
while (pMKeys.hasNext()) {
|
||||
String positiveMatchKey = pMKeys.next();
|
||||
if (matchesAnnotation(annotation, positiveMatchKey)) {
|
||||
JSONArray matchList = (JSONArray) positiveMatches.get(positiveMatchKey);
|
||||
matchList.forEach((match) -> {
|
||||
if (match instanceof JSONObject) {
|
||||
getMatchedCondition(app, (JSONObject) match, annotation)
|
||||
.ifPresent((condition) -> conditions.add(condition));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return conditions;
|
||||
}
|
||||
|
||||
protected Optional<JSONObject> getPositiveMatches(JSONObject autoConfigJson) {
|
||||
|
||||
Iterator<String> keys = autoConfigJson.keys();
|
||||
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
if ("positiveMatches".equals(key)) {
|
||||
Object obj = autoConfigJson.get(key);
|
||||
if (obj instanceof JSONObject) {
|
||||
return Optional.of((JSONObject) obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param annotation
|
||||
* @param jsonKey
|
||||
* @return true if the annotation matches the information in the json key from
|
||||
* the running app.
|
||||
*/
|
||||
protected boolean matchesAnnotation(Annotation annotation, String jsonKey) {
|
||||
|
||||
ASTNode parent = annotation.getParent();
|
||||
if (parent instanceof MethodDeclaration) {
|
||||
MethodDeclaration methodDec = (MethodDeclaration) parent;
|
||||
IMethodBinding binding = methodDec.resolveBinding();
|
||||
String annotationDeclaringClassName = binding.getDeclaringClass().getName();
|
||||
String annotationMethodName = binding.getName();
|
||||
return jsonKey.contains(annotationDeclaringClassName) && jsonKey.contains(annotationMethodName);
|
||||
} else if (parent instanceof TypeDeclaration) {
|
||||
TypeDeclaration typeDec = (TypeDeclaration) parent;
|
||||
String annotationDeclaringClassName = typeDec.resolveBinding().getName();
|
||||
return jsonKey.contains(annotationDeclaringClassName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Optional<LiveConditional> getMatchedCondition(SpringBootApp app, JSONObject conditionJson,
|
||||
Annotation annotation) {
|
||||
if (conditionJson != null) {
|
||||
String condition = (String) conditionJson.get("condition");
|
||||
String message = (String) conditionJson.get("message");
|
||||
String annotationName = annotation.resolveTypeBinding().getName();
|
||||
if (message.contains(annotationName)) {
|
||||
return Optional.of(new LiveConditional(app, condition, message));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
if (parent instanceof MethodDeclaration) {
|
||||
MethodDeclaration methodDec = (MethodDeclaration) parent;
|
||||
IMethodBinding binding = methodDec.resolveBinding();
|
||||
String annotationDeclaringClassName = binding.getDeclaringClass().getName();
|
||||
String annotationMethodName = binding.getName();
|
||||
return rawJsonKey.contains(annotationDeclaringClassName) && rawJsonKey.contains(annotationMethodName);
|
||||
} else if (parent instanceof TypeDeclaration) {
|
||||
TypeDeclaration typeDec = (TypeDeclaration) parent;
|
||||
String annotationDeclaringClassName = typeDec.resolveBinding().getName();
|
||||
return rawJsonKey.contains(annotationDeclaringClassName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,11 @@ import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
|
||||
public class HoverContentUtils {
|
||||
|
||||
public static String getProcessInformation(SpringBootApp app) {
|
||||
return "Process " + app.getProcessID() + ": " + app.getProcessName();
|
||||
return getProcessInformation(app.getProcessID(), app.getProcessName());
|
||||
}
|
||||
|
||||
public static String getProcessInformation(String processId, String processName) {
|
||||
return "Process " + processId + ": " + processName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ConditionalsLiveHoverTest {
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"ConditionalOnBeanConfig#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}]}}")
|
||||
.build();
|
||||
|
||||
@@ -91,7 +91,7 @@ public class ConditionalsLiveHoverTest {
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
|
||||
.build();
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ConditionalsLiveHoverTest {
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"HelloConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}],\"HelloConfig2#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}],\"MultipleConditionals#hi\":[{\"condition\":\"OnClassCondition\",\"message\":\"@ConditionalOnClass found required class; @ConditionalOnMissingClass did not find unwanted class\"},{\"condition\":\"OnWebApplicationCondition\",\"message\":\"@ConditionalOnWebApplication (required) found StandardServletEnvironment\"},{\"condition\":\"OnJavaCondition\",\"message\":\"@ConditionalOnJava (1.8 or newer) found 1.8\"},{\"condition\":\"OnExpressionCondition\",\"message\":\"@ConditionalOnExpression (#{true}) resulted in true\"},{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found beans 'hi', 'missing'\"}]}}")
|
||||
.build();
|
||||
|
||||
@@ -156,19 +156,19 @@ public class ConditionalsLiveHoverTest {
|
||||
// Build a mock running boot app
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1000").processId("70000").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
|
||||
.build();
|
||||
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1001").processId("80000").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
|
||||
.build();
|
||||
|
||||
mockAppProvider.builder().isSpringBootApp(true).port("1002").processId("90000").host("cfapps.io")
|
||||
.processName("test-conditionals-live-hover")
|
||||
.getAutoConfigReport(
|
||||
.getLiveConditionals(
|
||||
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
|
||||
.build();
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ import java.util.Collection;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.requestmappings.RequestMapping;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel;
|
||||
import org.springframework.ide.vscode.commons.boot.app.cli.requestmappings.RequestMapping;
|
||||
import org.springframework.ide.vscode.commons.util.ExceptionUtil;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -57,6 +57,8 @@ public class MockRunningAppProvider {
|
||||
public static class MockAppBuilder {
|
||||
public final SpringBootApp app = mock(SpringBootApp.class);
|
||||
private MockRunningAppProvider runningAppProvider;
|
||||
private String processId;
|
||||
private String processName;
|
||||
|
||||
public MockAppBuilder(MockRunningAppProvider runningAppProvider) {
|
||||
this.runningAppProvider = runningAppProvider;
|
||||
@@ -77,11 +79,13 @@ public class MockRunningAppProvider {
|
||||
}
|
||||
|
||||
public MockAppBuilder processId(String processId) {
|
||||
this.processId = processId;
|
||||
when(app.getProcessID()).thenReturn(processId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockAppBuilder processName(String name) {
|
||||
this.processName = name;
|
||||
when(app.getProcessName()).thenReturn(name);
|
||||
return this;
|
||||
}
|
||||
@@ -112,6 +116,11 @@ public class MockRunningAppProvider {
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockAppBuilder getLiveConditionals(String autoConfigReport) throws Exception{
|
||||
when(app.getLiveConditionals()).thenReturn(SpringBootApp.getLiveConditionals(autoConfigReport, processId, processName));
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockAppBuilder profiles(String... names) {
|
||||
when(app.getActiveProfiles()).thenReturn(ImmutableList.copyOf(names));
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user