Skip to content

Commit caf93b6

Browse files
committed
Refactor: fix PMD code smells
Fix ControlStatementBraces, GuardLogStatement, DoubleBraceInitialization and UseUtilityClass patterns. Signed-off-by: AleGTorres <[email protected]>
1 parent 8f08b38 commit caf93b6

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
@ImportRuntimeHints(PetClinicRuntimeHints.class)
3030
public class PetClinicApplication {
3131

32+
private PetClinicApplication() {
33+
// prevent instantiation
34+
}
35+
3236
public static void main(String[] args) {
3337
SpringApplication.run(PetClinicApplication.class, args);
3438
}

src/main/java/org/springframework/samples/petclinic/owner/PetController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ public String initCreationForm(Owner owner, ModelMap model) {
107107
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result,
108108
RedirectAttributes redirectAttributes) {
109109

110-
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null)
110+
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
111111
result.rejectValue("name", "duplicate", "already exists");
112+
}
112113

113114
LocalDate currentDate = LocalDate.now();
114115
if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) {

src/test/java/org/springframework/samples/petclinic/MysqlTestApplication.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232
@Configuration
3333
public class MysqlTestApplication {
3434

35+
private MysqlTestApplication() {
36+
// prevent instantiation
37+
}
38+
3539
@ServiceConnection
3640
@Profile("mysql")
3741
@Bean
38-
static MySQLContainer container() {
39-
return new MySQLContainer(DockerImageName.parse("mysql:9.5"));
42+
static MySQLContainer<?> container() {
43+
return new MySQLContainer<>(DockerImageName.parse("mysql:9.5"));
4044
}
4145

4246
public static void main(String[] args) {

src/test/java/org/springframework/samples/petclinic/PostgresIntegrationTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ public void onApplicationEvent(ApplicationPreparedEvent event) {
110110

111111
public void printProperties() {
112112
for (EnumerablePropertySource<?> source : findPropertiesPropertySources()) {
113-
log.info("PropertySource: " + source.getName());
113+
if (log.isInfoEnabled()) {
114+
log.info("PropertySource: " + source.getName());
115+
}
116+
114117
String[] names = source.getPropertyNames();
115118
Arrays.sort(names);
116119
for (String name : names) {
@@ -125,11 +128,14 @@ public void printProperties() {
125128
assertNotNull(sourceProperty.toString(), "source property toString() returned null.");
126129

127130
String value = sourceProperty.toString();
128-
if (resolved.equals(value)) {
129-
log.info(name + "=" + resolved);
130-
}
131-
else {
132-
log.info(name + "=" + value + " OVERRIDDEN to " + resolved);
131+
132+
if (log.isInfoEnabled()) {
133+
if (resolved.equals(value)) {
134+
log.info(name + "=" + resolved);
135+
}
136+
else {
137+
log.info(name + "=" + value + " OVERRIDDEN to " + resolved);
138+
}
133139
}
134140
}
135141
}

src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ void shouldThrowParseException() {
8181
*/
8282
private List<PetType> makePetTypes() {
8383
List<PetType> petTypes = new ArrayList<>();
84-
petTypes.add(new PetType() {
85-
{
86-
setName("Dog");
87-
}
88-
});
89-
petTypes.add(new PetType() {
90-
{
91-
setName("Bird");
92-
}
93-
});
84+
85+
PetType dog = new PetType();
86+
dog.setName("Dog");
87+
petTypes.add(dog);
88+
89+
PetType bird = new PetType();
90+
bird.setName("Bird");
91+
petTypes.add(bird);
92+
9493
return petTypes;
9594
}
9695

src/test/java/org/springframework/samples/petclinic/system/I18nPropertiesSyncTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public void checkNonInternationalizedStrings() throws IOException {
5656
String line = lines.get(i).trim();
5757

5858
if (line.startsWith("//") || line.startsWith("@") || line.contains("log.")
59-
|| line.contains("System.out"))
59+
|| line.contains("System.out")) {
6060
continue;
61+
}
6162

6263
if (file.toString().endsWith(".html")) {
6364
boolean hasLiteralText = HTML_TEXT_LITERAL.matcher(line).find();
@@ -115,8 +116,9 @@ public void checkI18nPropertyFilesAreInSync() throws IOException {
115116
String fileName = entry.getKey();
116117
// We use fallback logic to include english strings, hence messages_en is not
117118
// populated.
118-
if (fileName.equals(baseFile) || fileName.equals("messages_en.properties"))
119+
if (fileName.equals(baseFile) || fileName.equals("messages_en.properties")) {
119120
continue;
121+
}
120122

121123
Properties props = entry.getValue();
122124
Set<String> missingKeys = new TreeSet<>(baseKeys);

0 commit comments

Comments
 (0)