Skip to content
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

apply UnusedLocalVariable #34489

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9135e97
fix import
pinguin3245678 Feb 24, 2025
17bfeaa
fix dry AvoidStarImport
pinguin3245678 Feb 24, 2025
26660e0
add unused import to check CS
pinguin3245678 Feb 24, 2025
0e9bd82
break ci
pinguin3245678 Feb 24, 2025
5a4df03
wip
pinguin3245678 Feb 24, 2025
8b7fc53
wip
pinguin3245678 Feb 24, 2025
f3bb11e
doc
pinguin3245678 Feb 24, 2025
9a0245f
apply VariableDeclarationUsageDistance
pinguin3245678 Feb 24, 2025
71b56e7
wip
pinguin3245678 Feb 24, 2025
e42192a
RemoveUnusedPrivateFields
pinguin3245678 Feb 24, 2025
d0be958
RemoveUnusedPrivateMethods
pinguin3245678 Feb 25, 2025
67ee894
RemoveUnusedPrivateMethods
pinguin3245678 Feb 25, 2025
33af2ec
fix /ServiceLocatorFactoryBeanTests.java:130:17: Unused local variabl…
pinguin3245678 Feb 25, 2025
24c4d91
SpelCompilationCoverageTests.java:7148:25: Unused local variable 'out…
pinguin3245678 Feb 25, 2025
19bdae2
add assertion
pinguin3245678 Feb 25, 2025
413d322
unused
pinguin3245678 Feb 25, 2025
d23950e
UnusedLocalVariable
pinguin3245678 Feb 25, 2025
96d31fe
UnusedLocalVariable
pinguin3245678 Feb 25, 2025
2055377
form
pinguin3245678 Feb 25, 2025
d505077
dead code
pinguin3245678 Feb 25, 2025
f6ff1f6
form
pinguin3245678 Feb 25, 2025
b435f48
fix import
pinguin3245678 Feb 25, 2025
867fcc2
align format
pinguin3245678 Feb 25, 2025
bd11e5c
sort imoprt
pinguin3245678 Feb 25, 2025
199648f
fix sort
pinguin3245678 Feb 25, 2025
dbc528b
align .editorconfig
pinguin3245678 Feb 25, 2025
504b6c3
doc
pinguin3245678 Feb 25, 2025
a1e4d0a
check test
pinguin3245678 Feb 25, 2025
bf1da9a
inline
pinguin3245678 Feb 25, 2025
be7cc10
doc
pinguin3245678 Feb 25, 2025
e59c700
EmptyStatement
pinguin3245678 Feb 25, 2025
a9e09bf
apply EmptyStatement
pinguin3245678 Feb 25, 2025
ed592ce
VariableDeclarationUsageDistance
pinguin3245678 Feb 25, 2025
4e441b3
unused UnusedLocalMethod
pinguin3245678 Feb 25, 2025
8b5c797
undo
pinguin3245678 Feb 25, 2025
7842977
add UnusedLocalVariable"
pinguin3245678 Feb 25, 2025
423200b
ignore
pinguin3245678 Feb 25, 2025
71be520
ignore
pinguin3245678 Feb 25, 2025
c08f7c1
ignored as
pinguin3245678 Feb 25, 2025
4ddde8d
sort
pinguin3245678 Feb 25, 2025
bd7eb42
undo
pinguin3245678 Feb 25, 2025
520a35a
undo
pinguin3245678 Feb 25, 2025
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
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
root = true

[*.{adoc,bat,groovy,html,java,js,jsp,kt,kts,md,properties,py,rb,sh,sql,svg,txt,xml,xsd}]
[*]
charset = utf-8
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarification, is the intent to not make this the default?

I have never seen it otherwise, only globally, with the most common encoding being UTF-8.

This is new to me—my previous knowledge was that properties files were the only files not normally encoded in UTF-8, but now even they have switched.

"As of Java 9, properties files switched from being loaded using ISO-8859-1."
Source

end_of_line = lf

[*.{groovy,java,kt,kts,xml,xsd}]
indent_style = tab
indent_size = 4
continuation_indent_size = 8
end_of_line = lf
12 changes: 6 additions & 6 deletions buildSrc/config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">

<!-- Root Checks -->
<module name="io.spring.javaformat.checkstyle.check.SpringHeaderCheck">
<property name="fileExtensions" value="java"/>
Expand All @@ -10,17 +10,17 @@
<property name="packageInfoHeaderType" value="none"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck"/>

<!-- TreeWalker Checks -->
<module name="TreeWalker">

<!-- Imports -->
<module name="AvoidStarImport"/>
<module name="UnusedImports"/>
<module name="RedundantImport"/>
<!-- Modifiers -->
<module name="com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck"/>

<!-- Best Practice -->
<module name="EmptyStatement"/>
<module name="UnusedLocalVariable"/>
<module name="VariableDeclarationUsageDistance"/>
</module>

</module>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.test.agent.EnabledIfRuntimeHintsAgent;
import org.springframework.aot.test.agent.RuntimeHintsInvocations;
import org.springframework.aot.test.agent.RuntimeHintsRecorder;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -34,23 +34,22 @@ void sampleTest() {
RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(String.class);

RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
assertThat(RuntimeHintsRecorder.record(() -> {
SampleReflection sample = new SampleReflection();
sample.sample(); // does Method[] methods = String.class.getMethods();
});
assertThat(invocations).match(hints);
sample.sample(); // String.class.getMethods();
})).match(hints);
}

@Test
void multipleCallsTest() {
RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(String.class);
hints.reflection().registerType(Integer.class);
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {

assertThat(RuntimeHintsRecorder.record(() -> {
SampleReflection sample = new SampleReflection();
sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods();
});
assertThat(invocations).match(hints);
sample.multipleCalls(); // String.class.getMethods(); Integer.class.getMethods();
})).match(hints);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@

package org.springframework.aot.test;

import java.lang.reflect.Method;

/**
* @author Brian Clozel
* @since 6.0
*/
public class SampleReflection {

@SuppressWarnings("unused")
public void sample() {
String value = "Sample";
Method[] methods = String.class.getMethods();
String.class.getMethods();
}

@SuppressWarnings("unused")
public void multipleCalls() {
String value = "Sample";
Method[] methods = String.class.getMethods();
methods = Integer.class.getMethods();
// String.class.getMethods();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I'm wondering if the field is required for the test or if just the method call triggers the hint.

I was not able to run it locally—it seems to be special because of the annotation.
Normally, I test locally, but I was not able to. Please excuse this.

Integer.class.getMethods();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,11 @@ void testStringArgGetter() throws Exception {
.addPropertyValue("serviceLocatorInterface", TestServiceLocator2.class)
.getBeanDefinition());

// test string-arg getter with null id
TestServiceLocator2 factory = (TestServiceLocator2) bf.getBean("factory");

@SuppressWarnings("unused")
TestService testBean = factory.getTestService(null);
// now test with explicit id
testBean = factory.getTestService("testService");
// now verify failure on bad id
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
factory.getTestService("bogusTestService"));
final var factory = (TestServiceLocator2) bf.getBean("factory"); // test string-arg getter
factory.getTestService(null); // with null id
factory.getTestService("testService"); // test with explicit id
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> factory.getTestService("bogusTestService")); // verify failure on bad id
}

@Disabled @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory
Expand All @@ -145,14 +140,6 @@ public void testCombinedLocatorInterface() {
.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class)
.getBeanDefinition());

// StaticApplicationContext ctx = new StaticApplicationContext();
// ctx.registerPrototype("testService", TestService.class, new MutablePropertyValues());
// ctx.registerAlias("testService", "1");
// MutablePropertyValues mpv = new MutablePropertyValues();
// mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
// ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
// ctx.refresh();

TestServiceLocator3 factory = (TestServiceLocator3) bf.getBean("factory");
TestService testBean1 = factory.getTestService();
TestService testBean2 = factory.getTestService("testService");
Expand All @@ -177,16 +164,6 @@ public void testServiceMappings() {
.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class)
.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2")
.getBeanDefinition());

// StaticApplicationContext ctx = new StaticApplicationContext();
// ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues());
// ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues());
// MutablePropertyValues mpv = new MutablePropertyValues();
// mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
// mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2");
// ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
// ctx.refresh();

TestServiceLocator3 factory = (TestServiceLocator3) bf.getBean("factory");
TestService testBean1 = factory.getTestService();
TestService testBean2 = factory.getTestService("testService1");
Expand Down Expand Up @@ -303,8 +280,6 @@ public interface TestService2Locator {

public interface ServiceLocatorInterfaceWithExtraNonCompliantMethod {

TestService2 getTestService();

TestService2 getTestService(String serviceName, String defaultNotAllowedParameter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7145,8 +7145,7 @@ public TestClass9(int i) {
static class HttpServlet3RequestFactory {

static Servlet3SecurityContextHolderAwareRequestWrapper getOne() {
HttpServlet3RequestFactory outer = new HttpServlet3RequestFactory();
return outer.new Servlet3SecurityContextHolderAwareRequestWrapper();
return new HttpServlet3RequestFactory().new Servlet3SecurityContextHolderAwareRequestWrapper();
}

// private class Servlet3SecurityContextHolderAwareRequestWrapper extends SecurityContextHolderAwareRequestWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
Expand All @@ -37,6 +36,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
import static org.springframework.test.web.client.ExpectedCount.never;
import static org.springframework.test.web.client.ExpectedCount.once;
Expand Down Expand Up @@ -69,11 +70,10 @@ public void performGet() {

String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

// We are only validating the request. The response is mocked out.
// hotel.getId() == 42
Expand All @@ -87,11 +87,10 @@ public void performGetManyTimes() {

String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

// We are only validating the request. The response is mocked out.
// hotel.getId() == 42
Expand All @@ -109,9 +108,9 @@ public void expectNever() {

String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
Expand All @@ -124,9 +123,9 @@ public void expectNeverViolated() {

String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
Expand All @@ -139,11 +138,10 @@ public void performGetWithResponseBodyFromFile() {

Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());

this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

// hotel.getId() == 42
// hotel.getName().equals("Holiday Inn")
Expand All @@ -154,25 +152,19 @@ public void performGetWithResponseBodyFromFile() {
@Test
public void verify() {

this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("1", MediaType.TEXT_PLAIN));

this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("2", MediaType.TEXT_PLAIN));
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
.andRespond(withSuccess("1", TEXT_PLAIN));

this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("4", MediaType.TEXT_PLAIN));
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
.andRespond(withSuccess("2", TEXT_PLAIN));

this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
.andRespond(withSuccess("4", TEXT_PLAIN));

@SuppressWarnings("unused")
String result1 = this.restTemplate.getForObject("/number", String.class);
// result1 == "1"
this.mockServer.expect(requestTo("/number")).andExpect(method(GET)).andRespond(withSuccess("8", TEXT_PLAIN));

@SuppressWarnings("unused")
String result2 = this.restTemplate.getForObject("/number", String.class);
// result == "2"
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("1");
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("2");

try {
this.mockServer.verify();
Expand All @@ -195,7 +187,7 @@ public void repeatedAccessToResponseViaResource() {
.bufferContent() // enable repeated reads of response body
.build();

mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

restTemplate.getForObject("/composers/{id}", Person.class, 42);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void getCorsConfigWithBeanNameHandler() throws Exception {

this.mapping.setApplicationContext(context);
this.mapping.registerMapping(key, beanName, this.method1);
HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
}

@Test
Expand Down
Loading