Skip to content

Commit

Permalink
Unnecessary @repository problem and quickfix
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Dec 2, 2022
1 parent 88b5914 commit fb3f20c
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public enum Boot2JavaProblemType implements ProblemType {

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_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`");

private final ProblemSeverity defaultSeverity;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.BeanPostProcessingIgnoreInAotProblem;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.Boot3NotSupportedTypeProblem;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.NoAutowiredOnConstructorProblem;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.NoRepoAnnotationProblem;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.NoRequestMappingAnnotationCodeAction;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.NotRegisteredBeansProblem;
import org.springframework.ide.vscode.boot.java.rewrite.reconcile.PreciseBeanTypeProblem;
Expand All @@ -37,7 +38,8 @@ public List<RecipeCodeActionDescriptor> getCodeActionDescriptors() {
new NotRegisteredBeansProblem(),
new Boot3NotSupportedTypeProblem(),
new NoRequestMappingAnnotationCodeAction(),
new AutowiredFieldIntoConstructorParameterCodeAction()
new AutowiredFieldIntoConstructorParameterCodeAction(),
new NoRepoAnnotationProblem()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private ReconcileProblemImpl createProblem(IDocument doc, RecipeCodeActionDescri
QuickfixType quickfixType = quickfixRegistry.getQuickfixType(RewriteRefactorings.REWRITE_RECIPE_QUICKFIX);
if (quickfixType != null) {
for (FixDescriptor f : m.getFixes()) {
if (recipeRepo.getRecipe(f.getRecipeId()) != null) {
if (recipeRepo.getRecipe(f.getRecipeId()).isPresent()) {
problem.addQuickfix(new QuickfixData<>(
quickfixType,
f,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*******************************************************************************
* 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.boot.java.rewrite.reconcile;

import static org.springframework.ide.vscode.commons.java.SpringProjectUtil.springBootVersionGreaterOrEqual;

import java.util.List;

import org.openrewrite.ExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.ClassDeclaration;
import org.openrewrite.marker.Range;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.rewrite.config.RecipeCodeActionDescriptor;
import org.springframework.ide.vscode.commons.rewrite.config.RecipeScope;
import org.springframework.ide.vscode.commons.rewrite.java.FixAssistMarker;
import org.springframework.ide.vscode.commons.rewrite.java.FixDescriptor;

public class NoRepoAnnotationProblem implements RecipeCodeActionDescriptor {

private static final String ID = "org.openrewrite.java.spring.NoRepoAnnotationOnRepoInterface";
private static final String LABEL = "Remove Unnecessary @Repository";
private static final String INTERFACE_REPOSITORY = "org.springframework.data.repository.Repository";
private static final String ANNOTATION_REPOSITORY = Annotations.REPOSITORY;

@Override
public JavaVisitor<ExecutionContext> getMarkerVisitor(ApplicationContext applicationContext) {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public ClassDeclaration visitClassDeclaration(ClassDeclaration classDecl,
ExecutionContext executionContext) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, executionContext);
if (c.getKind() == ClassDeclaration.Kind.Type.Interface) {
final J.Annotation repoAnnotation = c.getLeadingAnnotations().stream().filter(annotation -> {
if (annotation.getArguments() == null || annotation.getArguments().isEmpty()
|| annotation.getArguments().get(0) instanceof J.Empty) {
JavaType.FullyQualified type = TypeUtils.asFullyQualified(annotation.getType());
return type != null && ANNOTATION_REPOSITORY.equals(type.getFullyQualifiedName());
}
return false;
}).findFirst().orElse(null);
if (repoAnnotation != null && TypeUtils.isAssignableTo(INTERFACE_REPOSITORY, c.getType())) {
c = c.withLeadingAnnotations(ListUtils.map(c.getLeadingAnnotations(), a -> {
if (a == repoAnnotation) {
String uri = getCursor().firstEnclosing(SourceFile.class).getSourcePath().toUri()
.toString();
FixAssistMarker fixAssistMarker = new FixAssistMarker(Tree.randomId(), getId()).withFixes(
new FixDescriptor(ID, List.of(uri), LABEL)
.withRangeScope(classDecl.getMarkers().findFirst(Range.class).get())
.withRecipeScope(RecipeScope.NODE),
new FixDescriptor(ID, List.of(uri),
RecipeCodeActionDescriptor.buildLabel(LABEL, RecipeScope.FILE))
.withRecipeScope(RecipeScope.FILE),
new FixDescriptor(ID, List.of(uri),
RecipeCodeActionDescriptor.buildLabel(LABEL, RecipeScope.PROJECT))
.withRecipeScope(RecipeScope.PROJECT)

);
return a.withMarkers(a.getMarkers().add(fixAssistMarker));
}
return a;
}));
}
}
return c;
}

};
}

@Override
public boolean isApplicable(IJavaProject project) {
return springBootVersionGreaterOrEqual(2, 0, 0).test(project);
}

@Override
public Boot2JavaProblemType getProblemType() {
return Boot2JavaProblemType.JAVA_REPOSITORY;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"label": "Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc.",
"description": "Use precise mapping annotation, i.e. '@GetMapping', '@PostMapping', etc.",
"defaultSeverity": "HINT"
},
{
"code": "JAVA_REPOSITORY",
"label": "Unnecessary `@Repository`",
"description": "Unnecessary `@Repository`",
"defaultSeverity": "WARNING"
}
]
},
Expand Down
12 changes: 12 additions & 0 deletions vscode-extensions/vscode-spring-boot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,18 @@
"HINT",
"ERROR"
]
},
"spring-boot.ls.problem.boot2.JAVA_REPOSITORY": {
"type": "string",
"default": "WARNING",
"description": "Unnecessary `@Repository`",
"enum": [
"IGNORE",
"INFO",
"WARNING",
"HINT",
"ERROR"
]
}
}
},
Expand Down

0 comments on commit fb3f20c

Please sign in to comment.