Skip to content

Commit

Permalink
GH-1345: use diagnostics tags from lsp protocol to mark unnecessary a…
Browse files Browse the repository at this point in the history
…nd deprecated things accordingly
  • Loading branch information
martinlippert committed Nov 18, 2024
1 parent b4292f6 commit e285e3a
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Pivotal, Inc.
* Copyright (c) 2016, 2024 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
Expand All @@ -11,6 +11,9 @@

package org.springframework.ide.vscode.commons.languageserver.reconcile;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;
import org.springframework.ide.vscode.commons.util.text.IDocument;

/**
Expand Down Expand Up @@ -51,6 +54,11 @@ public String getLabel() {
public ProblemCategory getCategory() {
return null;
}

@Override
public List<DiagnosticTag> getTags() {
return null;
}
}

private final String[] BADWORDS = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Pivotal, Inc.
* Copyright (c) 2016, 2024 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
Expand All @@ -11,6 +11,10 @@

package org.springframework.ide.vscode.commons.languageserver.reconcile;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;

/**
* Besides the methods below, the only hard requirement for a 'problem type' is
* that it is a unique object that is not 'equals' to any other object.
Expand All @@ -29,4 +33,5 @@ public interface ProblemType {
String getLabel();
String getDescription();
ProblemCategory getCategory();
List<DiagnosticTag> getTags();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2024 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
Expand All @@ -10,6 +10,10 @@
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.reconcile;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;

public class ProblemTypes {

/**
Expand All @@ -24,7 +28,7 @@ public class ProblemTypes {
* @return A newly create problem type.
*/
@Deprecated
public static ProblemType create(String typeName, ProblemSeverity defaultSeverity, ProblemCategory category) {
public static ProblemType create(String typeName, ProblemSeverity defaultSeverity, ProblemCategory category, List<DiagnosticTag> tags) {
return new ProblemType() {
@Override
public String toString() {
Expand All @@ -50,6 +54,10 @@ public String getLabel() {
public ProblemCategory getCategory() {
return category;
}
@Override
public List<DiagnosticTag> getTags() {
return tags;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,14 @@ public void accept(ReconcileProblem problem) {
Diagnostic d = new Diagnostic();
d.setCode(problem.getCode());
d.setMessage(problem.getMessage());

Range rng = docRef.get().toRange(problem.getOffset(), problem.getLength());
d.setRange(rng);

d.setSeverity(severity);
d.setSource(getServer().EXTENSION_ID);
d.setTags(problem.getType().getTags());

List<QuickfixData<?>> fixes = problem.getQuickfixes();

// Copy original diagnsotic without the data field to avoid stackoverflow is hashCode() method call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.lsp4j.DiagnosticTag;
import org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixType;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemCategory;
Expand Down Expand Up @@ -53,8 +54,8 @@ public class YamlSchemaProblems {

public static final ProblemType SYNTAX_PROBLEM = problemType("YamlSyntaxProblem", CATEGORY);
public static final ProblemType SCHEMA_PROBLEM = problemType("YamlSchemaProblem", CATEGORY);
public static final ProblemType DEPRECATED_PROPERTY = problemType("DeprecatedProperty", ProblemSeverity.WARNING, CATEGORY);
public static final ProblemType DEPRECATED_VALUE = problemType("DeprecatedValue", ProblemSeverity.WARNING, CATEGORY);
public static final ProblemType DEPRECATED_PROPERTY = problemType("DeprecatedProperty", ProblemSeverity.WARNING, CATEGORY, List.of(DiagnosticTag.Deprecated));
public static final ProblemType DEPRECATED_VALUE = problemType("DeprecatedValue", ProblemSeverity.WARNING, CATEGORY, List.of(DiagnosticTag.Deprecated));
public static final ProblemType MISSING_PROPERTY = problemType("MissingProperty", ProblemSeverity.ERROR, CATEGORY);
public static final ProblemType EXTRA_PROPERTY = problemType("ExtraProperty", ProblemSeverity.ERROR, CATEGORY);
public static final ProblemType EMPTY_OPTIONAL_STRING = problemType("EmptyOptionalString", ProblemSeverity.WARNING, CATEGORY);
Expand All @@ -63,8 +64,12 @@ public class YamlSchemaProblems {
MISSING_PROPERTY, EXTRA_PROPERTY
);

public static ProblemType problemType(final String typeName, ProblemSeverity defaultSeverity, ProblemCategory category, List<DiagnosticTag> tags) {
return ProblemTypes.create(typeName, defaultSeverity, category, tags);
}

public static ProblemType problemType(final String typeName, ProblemSeverity defaultSeverity, ProblemCategory category) {
return ProblemTypes.create(typeName, defaultSeverity, category);
return problemType(typeName, defaultSeverity, category, null);
}

public static ProblemType problemType(final String typeName, ProblemCategory category) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022, 2023 VMware, Inc.
* Copyright (c) 2022, 2024 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
Expand All @@ -12,49 +12,47 @@

import static org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity.*;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;
import org.springframework.ide.vscode.boot.common.SpringProblemCategories;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemCategory;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType;

public enum Boot2JavaProblemType implements ProblemType {

JAVA_AUTOWIRED_CONSTRUCTOR(WARNING, "Unnecessary `@Autowired` over the only constructor", "Unnecessary `@Autowired`"),

JAVA_PUBLIC_BEAN_METHOD(HINT, "Public modifier on `@Bean` method. They no longer have to be public visibility to be usable by Spring.", "public `@Bean` method"),

JAVA_TEST_SPRING_EXTENSION(WARNING, "`@SpringBootTest` and all test slice annotations already applies `@SpringExtension` as of Spring Boot 2.1.0.", "Unnecessary `@SpringExtension`"),

JAVA_AUTOWIRED_CONSTRUCTOR(WARNING, "Unnecessary `@Autowired` over the only constructor", "Unnecessary `@Autowired`", List.of(DiagnosticTag.Unnecessary)),
JAVA_PUBLIC_BEAN_METHOD(HINT, "Public modifier on `@Bean` method. They no longer have to be public visibility to be usable by Spring.", "public `@Bean` method", List.of(DiagnosticTag.Unnecessary)),
JAVA_TEST_SPRING_EXTENSION(WARNING, "`@SpringBootTest` and all test slice annotations already applies `@SpringExtension` as of Spring Boot 2.1.0.", "Unnecessary `@SpringExtension`", List.of(DiagnosticTag.Unnecessary)),
JAVA_CONSTRUCTOR_PARAMETER_INJECTION(IGNORE, "Use constructor parameter injection", "Use constructor parameter injection"),

JAVA_PRECISE_REQUEST_MAPPING(HINT, "Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc.", "Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc."),

JAVA_REPOSITORY(WARNING, "Unnecessary `@Repository`", "Unnecessary `@Repository`"),

JAVA_REPOSITORY(WARNING, "Unnecessary `@Repository`", "Unnecessary `@Repository`", List.of(DiagnosticTag.Unnecessary)),
JAVA_LAMBDA_DSL(INFO, "Consider switching to Lambda DSL syntax", "Switch to Lambda DSL syntax"),

MISSING_CONFIGURATION_ANNOTATION(WARNING, "Class likely missing '@Configuration' annotation, i.e. has Bean methods but no '@Configuration' annotation", "Missing '@Configuration'"),

HTTP_SECURITY_AUTHORIZE_HTTP_REQUESTS(WARNING, "'HttpSecurity.authroizeRequests(...)' API and related classes are to be deprecated use new `authorizeHttpRequests(...) and related classes", "Usage of old 'HttpSecurity.authroizeRequests(...)' API"),

WEB_SECURITY_CONFIGURER_ADAPTER(WARNING, "'WebSecurityConfigurerAdapter' is removed in Spring-Security 6.x. Refactor classes extending the 'WebSecurityConfigurerAdapter' into 'Configuration' beans and methods into 'Bean' definitions ", "Replace usage of 'WebSecurityConfigurerAdapter' as this class to be removed in Security 6.x"),

DOMAIN_ID_FOR_REPOSITORY(ERROR, "Invalid Domain ID type for Spring Data Repository", "Invalid Domain ID Type for Spring Data Repository"),

WEB_ANNOTATION_NAMES(HINT, "Web annotation names are unnecessary when it is the same as method parameter name", "Implicit web annotations names");
WEB_ANNOTATION_NAMES(HINT, "Web annotation names are unnecessary when it is the same as method parameter name", "Implicit web annotations names", List.of(DiagnosticTag.Unnecessary));

private final ProblemSeverity defaultSeverity;
private String description;
private final String description;
private String label;
private List<DiagnosticTag> tags;

private Boot2JavaProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

private Boot2JavaProblemType(ProblemSeverity defaultSeverity, String description, String label) {
private Boot2JavaProblemType(ProblemSeverity defaultSeverity, String description, String label, List<DiagnosticTag> tags) {
this.description = description;
this.defaultSeverity = defaultSeverity;
this.label = label;
this.tags = tags;
}

private Boot2JavaProblemType(ProblemSeverity defaultSeverity, String description, String label) {
this(defaultSeverity, description, label, null);
}

private Boot2JavaProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

@Override
Expand All @@ -63,7 +61,7 @@ public ProblemSeverity getDefaultSeverity() {
}

public String getLabel() {
if (label==null) {
if (label == null) {
label = createDefaultLabel();
}
return label;
Expand All @@ -89,4 +87,9 @@ public ProblemCategory getCategory() {
return SpringProblemCategories.BOOT_2;
}

@Override
public List<DiagnosticTag> getTags() {
return tags;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Pivotal, Inc.
* Copyright (c) 2020, 2024 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
Expand All @@ -12,6 +12,9 @@

import static org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity.ERROR;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;
import org.springframework.ide.vscode.boot.common.SpringProblemCategories;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemCategory;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity;
Expand All @@ -24,23 +27,27 @@
public enum Boot3JavaProblemType implements ProblemType {

JAVA_TYPE_NOT_SUPPORTED(ERROR, "Type no supported as of Spring Boot 3", "Type not supported as of Spring Boot 3"),

FACTORIES_KEY_NOT_SUPPORTED(ERROR, "Spring factories key not supported", "Spring factories key not supported"),

MODULITH_TYPE_REF_VIOLATION(ERROR, "Modulith restricted type reference", "Modulith restricted type reference");

private final ProblemSeverity defaultSeverity;
private String description;
private final String description;
private String label;
private final List<DiagnosticTag> tags;

private Boot3JavaProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

private Boot3JavaProblemType(ProblemSeverity defaultSeverity, String description, String label) {
private Boot3JavaProblemType(ProblemSeverity defaultSeverity, String description, String label, List<DiagnosticTag> tags) {
this.description = description;
this.defaultSeverity = defaultSeverity;
this.label = label;
this.tags = tags;
}

private Boot3JavaProblemType(ProblemSeverity defaultSeverity, String description, String label) {
this(defaultSeverity, description, label, null);
}

private Boot3JavaProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

@Override
Expand All @@ -49,7 +56,7 @@ public ProblemSeverity getDefaultSeverity() {
}

public String getLabel() {
if (label==null) {
if (label == null) {
label = createDefaultLabel();
}
return label;
Expand All @@ -74,5 +81,10 @@ public String getCode() {
public ProblemCategory getCategory() {
return SpringProblemCategories.BOOT_3;
}

@Override
public List<DiagnosticTag> getTags() {
return tags;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* Copyright (c) 2022, 2024 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
Expand All @@ -12,6 +12,9 @@

import static org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity.ERROR;

import java.util.List;

import org.eclipse.lsp4j.DiagnosticTag;
import org.springframework.ide.vscode.boot.common.SpringProblemCategories;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemCategory;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemSeverity;
Expand All @@ -23,17 +26,23 @@ public enum SpelProblemType implements ProblemType {
PROPERTY_PLACE_HOLDER_SYNTAX(ERROR, "Property place holder raised a ParseException", "Property Place Holder Syntax");

private final ProblemSeverity defaultSeverity;
private String description;
private final String description;
private String label;
private final List<DiagnosticTag> tags;

private SpelProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

private SpelProblemType(ProblemSeverity defaultSeverity, String description, String label) {
private SpelProblemType(ProblemSeverity defaultSeverity, String description, String label, List<DiagnosticTag> tags) {
this.description = description;
this.defaultSeverity = defaultSeverity;
this.label = label;
this.tags = tags;
}

private SpelProblemType(ProblemSeverity defaultSeverity, String description, String label) {
this(defaultSeverity, description, label, null);
}

private SpelProblemType(ProblemSeverity defaultSeverity, String description) {
this(defaultSeverity, description, null);
}

@Override
Expand All @@ -42,7 +51,7 @@ public ProblemSeverity getDefaultSeverity() {
}

public String getLabel() {
if (label==null) {
if (label == null) {
label = createDefaultLabel();
}
return label;
Expand All @@ -67,4 +76,9 @@ public String getCode() {
public ProblemCategory getCategory() {
return SpringProblemCategories.SPEL;
}

@Override
public List<DiagnosticTag> getTags() {
return tags;
}
}
Loading

0 comments on commit e285e3a

Please sign in to comment.