All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.staticanalysis.UnnecessaryPrimitiveAnnotations Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2024 the original author or authors.
 * 

* Licensed under the Moderne Source Available License (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://docs.moderne.io/licensing/moderne-source-available-license *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openrewrite.staticanalysis; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.AnnotationMatcher; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; import java.time.Duration; import java.util.Collections; import java.util.List; import java.util.Set; public class UnnecessaryPrimitiveAnnotations extends Recipe { private static final AnnotationMatcher CHECK_FOR_NULL_ANNOTATION_MATCHER = new AnnotationMatcher("@javax.annotation.CheckForNull"); private static final AnnotationMatcher NULLABLE_ANNOTATION_MATCHER = new AnnotationMatcher("@javax.annotation.Nullable"); @Override public String getDisplayName() { return "Remove `@Nullable` and `@CheckForNull` annotations from primitives"; } @Override public String getDescription() { return "Primitives can't be null anyway, so these annotations are not useful in this context."; } @Override public Set getTags() { return Collections.singleton("RSPEC-S4682"); } @Override public Duration getEstimatedEffortPerOccurrence() { return Duration.ofMinutes(1); } @Override public TreeVisitor getVisitor() { return Preconditions.check( Preconditions.or(new UsesType<>("javax.annotation.CheckForNull", false), new UsesType<>("javax.annotation.Nullable", false)), new JavaIsoVisitor() { @Override public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx); if (md.getReturnTypeExpression() != null && !(md.getReturnTypeExpression() instanceof J.ArrayType) && md.getReturnTypeExpression().getType() instanceof JavaType.Primitive) { md = maybeAutoFormat(md, md.withLeadingAnnotations(filterAnnotations(md.getLeadingAnnotations())), ctx); } return md; } @Override public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { J.VariableDeclarations varDecls = super.visitVariableDeclarations(multiVariable, ctx); if (varDecls.getType() instanceof JavaType.Primitive && varDecls.getVariables().stream().noneMatch(nv -> nv.getType() instanceof JavaType.Array)) { varDecls = varDecls.withLeadingAnnotations(filterAnnotations(varDecls.getLeadingAnnotations())); } return varDecls; } private List filterAnnotations(List annotations) { return ListUtils.map(annotations, anno -> { if (NULLABLE_ANNOTATION_MATCHER.matches(anno) || CHECK_FOR_NULL_ANNOTATION_MATCHER.matches(anno)) { maybeRemoveImport("javax.annotation.CheckForNull"); maybeRemoveImport("javax.annotation.Nullable"); return null; } return anno; }); } } ); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy