Skip to content

[S1161] use PMD to avoid missing @Override on overriding and implementing methods #35063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .pmd/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<ruleset name="Definition of Done"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Programmatic approach to maintain code quality on large scale.</description>
<rule ref="category/java/bestpractices.xml/UnusedAssignment" />
<rule ref="category/java/bestpractices.xml/UnusedFormalParameter" />
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
<rule ref="category/java/bestpractices.xml/UseTryWithResources" />
<rule ref="category/java/codestyle.xml/EmptyControlStatement" />
<rule ref="category/java/codestyle.xml/UnnecessaryAnnotationValueElement" />
<rule ref="category/java/codestyle.xml/UnnecessaryBoxing" />
<rule ref="category/java/codestyle.xml/UnnecessaryCast" />
<rule ref="category/java/codestyle.xml/UnnecessaryConstructor" />
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" />
<rule ref="category/java/codestyle.xml/UnnecessaryImport" />
<rule ref="category/java/codestyle.xml/UnnecessaryLocalBeforeReturn" />
<rule ref="category/java/codestyle.xml/UnnecessaryModifier" />
<rule ref="category/java/codestyle.xml/UnnecessaryReturn" />
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon" />
<rule ref="category/java/codestyle.xml/UnusedImports" />
<rule ref="category/java/codestyle.xml/UselessParentheses" />
<rule ref="category/java/codestyle.xml/UselessQualifiedThis" />
<rule ref="category/java/design.xml/AvoidCatchingGenericException" />
<rule ref="category/java/design.xml/AvoidThrowingRawExceptionTypes" />
<!-- <rule ref="category/java/bestpractices.xml"/>-->
<rule ref="category/java/bestpractices.xml/MissingOverride" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
<!-- <rule ref="category/java/codestyle.xml"/>-->
<!-- <rule ref="category/java/errorprone.xml"/>-->
<rule ref="category/java/codestyle.xml/EmptyControlStatement" />
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" />
</ruleset>
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id 'io.github.goooler.shadow' version '8.1.8' apply false
id 'me.champeau.jmh' version '0.7.2' apply false
id "io.spring.nullability" version "0.0.1" apply false
id 'pmd'
}

ext {
Expand All @@ -18,6 +19,7 @@ description = "Spring Framework"

configure(allprojects) { project ->
apply plugin: "org.springframework.build.localdev"
apply plugin: "pmd"
group = "org.springframework"
repositories {
mavenCentral()
Expand All @@ -34,6 +36,9 @@ configure(allprojects) { project ->
cacheDynamicVersionsFor 0, "seconds"
}
}
pmd {
ruleSetFiles = files(".pmd/ruleset.xml")
}
}

configure(allprojects - project(":framework-platform")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected JavaToolchainService getToolchains() {
throw new UnsupportedOperationException();
}

@Override
public void apply(Project project) {
project.getPlugins().apply(JavaPlugin.class);
ExtensionContainer extensions = project.getExtensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}

@Override
public int getOrder() {
return this.order;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void createStructSample() throws ParseException {
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));

SqlTypeValue value = new AbstractSqlTypeValue() {
@Override
protected Object createTypeValue(Connection connection, int sqlType, String typeName) throws SQLException {
Object[] item = new Object[] { testItem.getId(), testItem.getDescription(),
new java.sql.Date(testItem.getExpirationDate().getTime()) };
Expand All @@ -49,6 +50,7 @@ void createOracleArray() {
Long[] ids = new Long[] {1L, 2L};

SqlTypeValue value = new AbstractSqlTypeValue() {
@Override
protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
return conn.unwrap(OracleConnection.class).createOracleArray(typeName, ids);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// tag::snippet[]
public class ExampleListener implements MessageListener {

@Override
public void onMessage(Message message) {
if (message instanceof TextMessage textMessage) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public MessagePrinterTask(String message) {
this.message = message;
}

@Override
public void run() {
System.out.println(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@Component
public class MyPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
// ...
return bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import org.aopalliance.intercept.MethodInvocation;
import org.jspecify.annotations.Nullable;

import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.util.Assert;

import java.util.Objects;

/**
* Convenient implementation of the
* {@link org.springframework.aop.IntroductionInterceptor} interface.
Expand Down Expand Up @@ -110,7 +111,7 @@ private void init(Object delegate) {

// Massage return value if possible: if the delegate returned itself,
// we really want to return the proxy.
if (retVal == this.delegate && mi instanceof ProxyMethodInvocation pmi) {
if (Objects.requireNonNull(retVal).equals(this.delegate) && mi instanceof ProxyMethodInvocation pmi) {
Object proxy = pmi.getProxy();
if (mi.getMethod().getReturnType().isInstance(proxy)) {
retVal = proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ interface ProxyInterface {

static class WithInterface implements ProxyInterface {

@Override
public void handle(List<String> list) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static class CacheInterceptorBeanPostProcessor implements BeanPostProcessor {

CacheInterceptorBeanPostProcessor(BeanFactory beanFactory) {this.beanFactory = beanFactory;}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("jCacheInterceptor")) {
JCacheInterceptor cacheInterceptor = new TestCacheInterceptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static class CacheInterceptorBeanPostProcessor implements BeanPostProcessor {

CacheInterceptorBeanPostProcessor(BeanFactory beanFactory) {this.beanFactory = beanFactory;}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("cacheInterceptor")) {
CacheInterceptor cacheInterceptor = new TestCacheInterceptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static class CustomSmartClassLoader extends SecureClassLoader implements SmartCl
super(parent);
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.contains("MyConfig")) {
String path = name.replace('.', '/').concat(".class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public final int getPosition() {
* @see #getSimpleMessage()
* @see java.lang.Throwable#getMessage()
*/
@Override
public String getMessage() {
return toDetailedString();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public LocalSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeF
* @see #addPackage
* @see #scanPackages
*/
@Override
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages) {
for (String annotatedPackage : annotatedPackages) {
addPackage(annotatedPackage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ public void sendRedirect(String url) throws IOException {
}

// @Override - on Servlet 6.1
@Override
public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException {
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
Assert.notNull(url, "Redirect URL must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class JdkResponseCookieParser implements ResponseCookie.Parser {
/**
* Parse the given headers.
*/
@Override
public List<ResponseCookie> parse(String header) {
Matcher matcher = SAME_SITE_PATTERN.matcher(header);
String sameSite = (matcher.matches() ? matcher.group(1) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public Comparable<?> parseVersion(String version) {
return this.versionParser.parseVersion(version);
}

@Override
public void validateVersion(@Nullable Comparable<?> requestVersion, HttpServletRequest request)
throws MissingApiVersionException, InvalidApiVersionException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Build
* Configure the inserter to set a header.
* @param header the name of the header to hold the version
*/
@Override
public ApiVersionInserter.Builder useHeader(@Nullable String header) {
this.header = header;
return this;
Expand All @@ -59,6 +60,7 @@ public ApiVersionInserter.Builder useHeader(@Nullable String header) {
* Configure the inserter to set a query parameter.
* @param queryParam the name of the query parameter to hold the version
*/
@Override
public ApiVersionInserter.Builder useQueryParam(@Nullable String queryParam) {
this.queryParam = queryParam;
return this;
Expand All @@ -68,6 +70,7 @@ public ApiVersionInserter.Builder useQueryParam(@Nullable String queryParam) {
* Configure the inserter to insert a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
@Override
public ApiVersionInserter.Builder usePathSegment(@Nullable Integer pathSegmentIndex) {
this.pathSegmentIndex = pathSegmentIndex;
return this;
Expand All @@ -78,6 +81,7 @@ public ApiVersionInserter.Builder usePathSegment(@Nullable Integer pathSegmentIn
* <p>By default, the version is formatted with {@link Object#toString()}.
* @param versionFormatter the formatter to use
*/
@Override
public ApiVersionInserter.Builder withVersionFormatter(ApiVersionFormatter versionFormatter) {
this.versionFormatter = versionFormatter;
return this;
Expand All @@ -86,6 +90,7 @@ public ApiVersionInserter.Builder withVersionFormatter(ApiVersionFormatter versi
/**
* Build the inserter.
*/
@Override
public ApiVersionInserter build() {
return new DefaultApiVersionInserter(
this.header, this.queryParam, this.pathSegmentIndex, this.versionFormatter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public void write(int b) throws IOException {
}
}

@Override
public void write(byte[] buf, int offset, int len) throws IOException {
int level = this.response.obtainLockOrRaiseException();
try {
Expand Down
18 changes: 12 additions & 6 deletions spring-web/src/test/java/org/springframework/protobuf/Msg.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void basic(Class<?> configClass) throws InterruptedException {
void beansAreCreatedUsingBeanClassLoader() {
ClassLoader beanClassLoader = new OverridingClassLoader(getClass().getClassLoader()) {

@Override
protected boolean isEligibleForOverriding(String className) {
return className.contains("EchoA");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ public void sendRedirect(String url) throws IOException {
}

// @Override - on Servlet 6.1
@Override
public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException {
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
Assert.notNull(url, "Redirect URL must not be null");
Expand Down
Loading