GH-1411: put marker on the annotation attribute that is obsolete, not the entire annotation
Fixes gh-1411
This commit is contained in:
@@ -81,31 +81,34 @@ public class ImplicitWebAnnotationNamesReconciler implements JdtAstReconciler {
|
||||
|
||||
private void processWebAnnotation(Annotation a) {
|
||||
if (isApplicableWebAnnotation(a)) {
|
||||
ReconcileProblemImpl problem = new ReconcileProblemImpl(getProblemType(), PROBLEM_LABEL, a.getStartPosition(), a.getLength());
|
||||
String uri = docUri.toASCIIString();
|
||||
Range range = ReconcileUtils.createOpenRewriteRange(cu, a);
|
||||
ReconcileUtils.setRewriteFixes(registry, problem, List.of(
|
||||
|
||||
ASTNode nodeForProblemRange = getNodeForProblemRange(a);
|
||||
ReconcileProblemImpl problem = new ReconcileProblemImpl(getProblemType(), PROBLEM_LABEL, nodeForProblemRange.getStartPosition(), nodeForProblemRange.getLength());
|
||||
|
||||
String uri = docUri.toASCIIString();
|
||||
Range range = ReconcileUtils.createOpenRewriteRange(cu, a);
|
||||
ReconcileUtils.setRewriteFixes(registry, problem, List.of(
|
||||
new FixDescriptor(org.openrewrite.java.spring.ImplicitWebAnnotationNames.class.getName(), List.of(uri), FIX_LABEL)
|
||||
.withRangeScope(range)
|
||||
.withRecipeScope(RecipeScope.NODE),
|
||||
.withRangeScope(range)
|
||||
.withRecipeScope(RecipeScope.NODE),
|
||||
new FixDescriptor(org.openrewrite.java.spring.ImplicitWebAnnotationNames.class.getName(), List.of(uri),
|
||||
ReconcileUtils.buildLabel(FIX_LABEL_PLURAL, RecipeScope.FILE))
|
||||
.withRecipeScope(RecipeScope.FILE),
|
||||
.withRecipeScope(RecipeScope.FILE),
|
||||
new FixDescriptor(org.openrewrite.java.spring.ImplicitWebAnnotationNames.class.getName(), List.of(uri),
|
||||
ReconcileUtils.buildLabel(FIX_LABEL_PLURAL, RecipeScope.PROJECT))
|
||||
.withRecipeScope(RecipeScope.PROJECT)
|
||||
));
|
||||
problemCollector.accept(problem);
|
||||
}
|
||||
.withRecipeScope(RecipeScope.PROJECT)
|
||||
));
|
||||
problemCollector.accept(problem);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean isApplicableWebAnnotation(Annotation a) {
|
||||
if (a.isSingleMemberAnnotation() || a.isNormalAnnotation()) {
|
||||
String typeName = a.getTypeName().getFullyQualifiedName();
|
||||
String annotationParam = getAnnotationParameter(a);
|
||||
String variableName = getParameterName(a);
|
||||
private static boolean isApplicableWebAnnotation(Annotation annotation) {
|
||||
if (annotation.isSingleMemberAnnotation() || annotation.isNormalAnnotation()) {
|
||||
String typeName = annotation.getTypeName().getFullyQualifiedName();
|
||||
String annotationParam = getAnnotationParameter(annotation);
|
||||
String variableName = getParameterName(annotation);
|
||||
if (PARAM_ANNOTATIONS.contains(typeName) && annotationParam != null && variableName != null) {
|
||||
if(Objects.equals(annotationParam, variableName))
|
||||
return true;
|
||||
@@ -115,24 +118,41 @@ public class ImplicitWebAnnotationNamesReconciler implements JdtAstReconciler {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static String getAnnotationParameter(Annotation a) {
|
||||
private static String getAnnotationParameter(Annotation annotation) {
|
||||
Expression value = null;
|
||||
if (a.isSingleMemberAnnotation()) {
|
||||
value = ((SingleMemberAnnotation) a).getValue();
|
||||
} else if (a.isNormalAnnotation()) {
|
||||
for (MemberValuePair pair : (List<MemberValuePair>) ((NormalAnnotation) a).values()) {
|
||||
|
||||
if (annotation.isSingleMemberAnnotation()) {
|
||||
value = ((SingleMemberAnnotation) annotation).getValue();
|
||||
} else if (annotation.isNormalAnnotation()) {
|
||||
for (MemberValuePair pair : (List<MemberValuePair>) ((NormalAnnotation) annotation).values()) {
|
||||
String identifier = pair.getName().toString();
|
||||
value = identifier.equals("value") || identifier.equals("name") ? pair.getValue() : value;
|
||||
}
|
||||
}
|
||||
|
||||
if (value instanceof StringLiteral) {
|
||||
return ((StringLiteral) value).getLiteralValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static ASTNode getNodeForProblemRange(Annotation annotation) {
|
||||
if (annotation.isSingleMemberAnnotation()) {
|
||||
return ((SingleMemberAnnotation) annotation).getValue();
|
||||
} else if (annotation.isNormalAnnotation()) {
|
||||
for (MemberValuePair pair : (List<MemberValuePair>) ((NormalAnnotation) annotation).values()) {
|
||||
String identifier = pair.getName().toString();
|
||||
if (identifier.equals("value") || identifier.equals("name")) {
|
||||
return pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotation;
|
||||
}
|
||||
|
||||
private static String getParameterName(Annotation a) {
|
||||
ASTNode parent = a.getParent();
|
||||
private static String getParameterName(Annotation annotation) {
|
||||
ASTNode parent = annotation.getParent();
|
||||
if (parent instanceof SingleVariableDeclaration) {
|
||||
SingleVariableDeclaration svd = (SingleVariableDeclaration) parent;
|
||||
return svd.getName().getIdentifier();
|
||||
|
||||
@@ -68,7 +68,7 @@ public class ImplicitWebAnnotationNamesReconcilerTest extends BaseReconcilerTest
|
||||
assertEquals(Boot2JavaProblemType.WEB_ANNOTATION_NAMES, problem.getType());
|
||||
|
||||
String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
|
||||
assertEquals("@PathVariable(\"message\")", markedStr);
|
||||
assertEquals("\"message\"", markedStr);
|
||||
|
||||
assertEquals(3, problem.getQuickfixes().size());
|
||||
assertEquals("Remove implicit web annotation name", problems.get(0).getQuickfixes().get(0).title);
|
||||
@@ -130,7 +130,7 @@ public class ImplicitWebAnnotationNamesReconcilerTest extends BaseReconcilerTest
|
||||
assertEquals(Boot2JavaProblemType.WEB_ANNOTATION_NAMES, problem.getType());
|
||||
|
||||
String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
|
||||
assertEquals("@PathVariable(value=\"message\")", markedStr);
|
||||
assertEquals("value=\"message\"", markedStr);
|
||||
|
||||
assertEquals(3, problem.getQuickfixes().size());
|
||||
|
||||
@@ -164,7 +164,7 @@ public class ImplicitWebAnnotationNamesReconcilerTest extends BaseReconcilerTest
|
||||
assertEquals(Boot2JavaProblemType.WEB_ANNOTATION_NAMES, problem.getType());
|
||||
|
||||
String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
|
||||
assertEquals("@RequestParam(name=\"message\", defaultValue = \"world\")", markedStr);
|
||||
assertEquals("name=\"message\"", markedStr);
|
||||
|
||||
assertEquals(3, problem.getQuickfixes().size());
|
||||
|
||||
@@ -200,10 +200,10 @@ public class ImplicitWebAnnotationNamesReconcilerTest extends BaseReconcilerTest
|
||||
assertEquals(Boot2JavaProblemType.WEB_ANNOTATION_NAMES, problem2.getType());
|
||||
|
||||
String markedStr1 = source.substring(problem1.getOffset(), problem1.getOffset() + problem1.getLength());
|
||||
assertEquals("@PathVariable(value=\"message\", required=false)", markedStr1);
|
||||
assertEquals("value=\"message\"", markedStr1);
|
||||
|
||||
String markedStr2 = source.substring(problem2.getOffset(), problem2.getOffset() + problem2.getLength());
|
||||
assertEquals("@PathVariable(value=\"name\")", markedStr2);
|
||||
assertEquals("value=\"name\"", markedStr2);
|
||||
|
||||
assertEquals(3, problem1.getQuickfixes().size());
|
||||
assertEquals(3, problem2.getQuickfixes().size());
|
||||
|
||||
Reference in New Issue
Block a user