Dumb Notebook doc service. PreciseBeanType recipe from OR.

This commit is contained in:
aboyko
2022-09-23 10:55:11 -04:00
parent 88e1965369
commit 0b1462a661
4 changed files with 49 additions and 112 deletions

View File

@@ -43,6 +43,10 @@ import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DidChangeNotebookDocumentParams;
import org.eclipse.lsp4j.DidCloseNotebookDocumentParams;
import org.eclipse.lsp4j.DidOpenNotebookDocumentParams;
import org.eclipse.lsp4j.DidSaveNotebookDocumentParams;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.InitializeParams;
@@ -68,6 +72,7 @@ import org.eclipse.lsp4j.WorkspaceServerCapabilities;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.NotebookDocumentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
@@ -130,6 +135,7 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
public final LazyCompletionResolver completionResolver = createCompletionResolver();
private SimpleTextDocumentService tds;
private NotebookDocumentService nts;
private SimpleWorkspaceService workspace;
private STS4LanguageClient client;
private final LanguageServerProperties props;
@@ -596,6 +602,39 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
return tds;
}
@Override
public NotebookDocumentService getNotebookDocumentService() {
if (nts == null) {
nts = createNotebookDocumentService();
}
return nts;
}
private NotebookDocumentService createNotebookDocumentService() {
return new NotebookDocumentService() {
@Override
public void didSave(DidSaveNotebookDocumentParams params) {
throw new UnsupportedOperationException();
}
@Override
public void didOpen(DidOpenNotebookDocumentParams params) {
throw new UnsupportedOperationException();
}
@Override
public void didClose(DidCloseNotebookDocumentParams params) {
throw new UnsupportedOperationException();
}
@Override
public void didChange(DidChangeNotebookDocumentParams params) {
throw new UnsupportedOperationException();
}
};
}
protected SimpleTextDocumentService createTextDocumentService() {
return new SimpleTextDocumentService(this, props, appContext);
}

View File

@@ -12,6 +12,7 @@ package org.springframework.ide.vscode.commons.rewrite.java;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.ClosedByInterruptException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
@@ -246,6 +247,14 @@ public class ORAstUtils {
private static void logExceptionWhileParsing(Throwable t) {
if (!(t instanceof JavaParsingException || t instanceof StringIndexOutOfBoundsException)) {
if (t instanceof RuntimeException) {
RuntimeException re = (RuntimeException) t;
if (re.getCause() instanceof ClosedByInterruptException) {
// Parse or scan interrupted
log.debug("", t);
return;
}
}
// Do not log parse exceptions. Can be too many while user is typing code
log.error("", t);
}

View File

@@ -1,111 +0,0 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, 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
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.rewrite.java;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.java.tree.TypeUtils;
public class PreciseBeanType extends Recipe {
private final static String BEAN = "org.springframework.context.annotation.Bean";
private final static String MSG_KEY = "returnType";
@Override
public String getDisplayName() {
return "Replace Bean method return types with concrete types being returned. This is required for Spring 6 AOT";
}
@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new UsesType<ExecutionContext>(BEAN);
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, executionContext);
if (isBeanMethod(m)) {
Object o = getCursor().pollMessage(MSG_KEY);
if (o != null) {
if (!o.equals(method.getReturnTypeExpression().getType())) {
if (o instanceof JavaType.FullyQualified) {
JavaType.FullyQualified actualType = (JavaType.FullyQualified) o;
if (m.getReturnTypeExpression() instanceof J.Identifier) {
J.Identifier identifierReturnExpr = (J.Identifier) m.getReturnTypeExpression();
maybeAddImport(actualType);
if (identifierReturnExpr.getType() instanceof JavaType.FullyQualified) {
maybeRemoveImport((JavaType.FullyQualified) identifierReturnExpr.getType());
}
m = m.withReturnTypeExpression(identifierReturnExpr
.withType(actualType)
.withSimpleName(actualType.getClassName())
);
} else if (m.getReturnTypeExpression() instanceof J.ParameterizedType) {
J.ParameterizedType parameterizedType = (J.ParameterizedType) m.getReturnTypeExpression();
maybeAddImport(actualType);
if (parameterizedType.getType() instanceof JavaType.FullyQualified) {
maybeRemoveImport((JavaType.FullyQualified) parameterizedType.getType());
}
m = m.withReturnTypeExpression(parameterizedType
.withType(actualType)
.withClazz(TypeTree.build(actualType.getClassName()).withType(actualType))
);
}
} else if (o instanceof JavaType.Array) {
JavaType.Array actualType = (JavaType.Array) o;
if (m.getReturnTypeExpression() instanceof J.ArrayType && actualType.getElemType() instanceof JavaType.FullyQualified) {
JavaType.FullyQualified actualElementType = (JavaType.FullyQualified) actualType.getElemType();
J.ArrayType arrayType = (J.ArrayType) m.getReturnTypeExpression();
maybeAddImport(actualElementType);
if (arrayType.getElementType() instanceof JavaType.FullyQualified) {
maybeRemoveImport((JavaType.FullyQualified) arrayType.getElementType());
}
m = m.withReturnTypeExpression(arrayType
.withElementType(TypeTree.build(actualElementType.getClassName()).withType(actualType))
);
}
}
}
}
}
return m;
}
private boolean isBeanMethod(J.MethodDeclaration m) {
return m.getLeadingAnnotations().stream().anyMatch(a -> TypeUtils.isOfClassType(a.getType(), BEAN));
}
@Override
public J.Return visitReturn(J.Return _return, ExecutionContext executionContext) {
Cursor methodCursor = getCursor();
while (methodCursor != null && !(methodCursor.getValue() instanceof J.Lambda || methodCursor.getValue() instanceof J.MethodDeclaration)) {
methodCursor = methodCursor.getParent();
}
if (methodCursor != null && methodCursor.getValue() instanceof J.MethodDeclaration) {
methodCursor.putMessage(MSG_KEY, _return.getExpression().getType());
}
return super.visitReturn(_return, executionContext);
}
};
}
}

View File

@@ -37,7 +37,7 @@ public class PreciseBeanTypeProblem implements RecipeSpringJavaProblemDescriptor
@Override
public String getRecipeId() {
return "org.springframework.ide.vscode.commons.rewrite.java.PreciseBeanType";
return "org.openrewrite.java.spring.boot3.PreciseBeanType";
}
@Override