Skip to content

Commit 4954f52

Browse files
authored
chore: remove System.out and printStackTrace to align with Sonar rules S106 and S1148 (#4988)
1 parent 8767cea commit 4954f52

File tree

17 files changed

+196
-40
lines changed

17 files changed

+196
-40
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
public class Json {
810

911
private static final class ObjectMapperHolder {
1012
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJson();
1113
}
1214

15+
private static final Logger LOGGER = LoggerFactory.getLogger(Json.class);
16+
1317
public static ObjectMapper mapper() {
1418
return ObjectMapperHolder.MAPPER;
1519
}
@@ -22,16 +26,17 @@ public static String pretty(Object o) {
2226
try {
2327
return pretty().writeValueAsString(o);
2428
} catch (Exception e) {
25-
e.printStackTrace();
29+
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to JSON", e);
2630
return null;
2731
}
2832
}
2933

3034
public static void prettyPrint(Object o) {
3135
try {
32-
System.out.println(pretty().writeValueAsString(o).replace("\r", ""));
36+
String prettyString = pretty().writeValueAsString(o).replace("\r", "");
37+
PrettyPrintHelper.emit(LOGGER, prettyString);
3338
} catch (Exception e) {
34-
e.printStackTrace();
39+
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing JSON", e);
3540
}
3641
}
3742
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private static final class ConverterMapperHolder {
2121
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJsonConverter();
2222
}
2323

24-
static Logger LOGGER = LoggerFactory.getLogger(Json31.class);
24+
private static final Logger LOGGER = LoggerFactory.getLogger(Json31.class);
2525

2626
public static ObjectMapper mapper() {
2727
return ObjectMapperHolder.MAPPER;
@@ -39,16 +39,17 @@ public static String pretty(Object o) {
3939
try {
4040
return pretty().writeValueAsString(o);
4141
} catch (Exception e) {
42-
e.printStackTrace();
42+
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to JSON (3.1)", e);
4343
return null;
4444
}
4545
}
4646

4747
public static void prettyPrint(Object o) {
4848
try {
49-
System.out.println(pretty().writeValueAsString(o).replace("\r", ""));
49+
String prettyString = pretty().writeValueAsString(o).replace("\r", "");
50+
PrettyPrintHelper.emit(LOGGER, prettyString);
5051
} catch (Exception e) {
51-
e.printStackTrace();
52+
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing JSON (3.1)", e);
5253
}
5354
}
5455

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.swagger.v3.core.util;
2+
3+
import org.slf4j.Logger;
4+
5+
import java.io.PrintWriter;
6+
import java.io.StringWriter;
7+
import java.util.function.Consumer;
8+
9+
class PrettyPrintHelper {
10+
11+
private static final ThreadLocal<Consumer<String>> OVERRIDE = new ThreadLocal<>();
12+
13+
private PrettyPrintHelper() {
14+
// utility class
15+
}
16+
17+
static void setOverride(Consumer<String> consumer) {
18+
OVERRIDE.set(consumer);
19+
}
20+
21+
static void clearOverride() {
22+
OVERRIDE.remove();
23+
}
24+
25+
static void emit(Logger logger, String message) {
26+
Consumer<String> consumer = OVERRIDE.get();
27+
if (consumer != null) {
28+
consumer.accept(message);
29+
} else {
30+
logger.debug(message);
31+
}
32+
}
33+
34+
static void emitError(Logger logger, String message, Throwable throwable) {
35+
Consumer<String> consumer = OVERRIDE.get();
36+
if (consumer != null) {
37+
StringBuilder builder = new StringBuilder(message);
38+
if (throwable != null) {
39+
builder.append(System.lineSeparator());
40+
StringWriter writer = new StringWriter();
41+
throwable.printStackTrace(new PrintWriter(writer));
42+
builder.append(writer);
43+
}
44+
consumer.accept(builder.toString());
45+
}
46+
logger.error(message, throwable);
47+
}
48+
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
public class Yaml {
810

911
private static final class ObjectMapperHolder {
1012
private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml();
1113
}
1214

15+
private static final Logger LOGGER = LoggerFactory.getLogger(Yaml.class);
16+
1317
public static ObjectMapper mapper() {
1418
return ObjectMapperHolder.MAPPER;
1519
}
@@ -22,16 +26,17 @@ public static String pretty(Object o) {
2226
try {
2327
return pretty().writeValueAsString(o);
2428
} catch (Exception e) {
25-
e.printStackTrace();
29+
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to YAML", e);
2630
return null;
2731
}
2832
}
2933

3034
public static void prettyPrint(Object o) {
3135
try {
32-
System.out.println(pretty().writeValueAsString(o));
36+
String prettyString = pretty().writeValueAsString(o);
37+
PrettyPrintHelper.emit(LOGGER, prettyString);
3338
} catch (Exception e) {
34-
e.printStackTrace();
39+
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing YAML", e);
3540
}
3641
}
3742
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private static final class ObjectMapperHolder {
1717
}
1818

1919

20-
static Logger LOGGER = LoggerFactory.getLogger(Yaml31.class);
20+
private static final Logger LOGGER = LoggerFactory.getLogger(Yaml31.class);
2121

2222
public static ObjectMapper mapper() {
2323
return ObjectMapperHolder.MAPPER;
@@ -31,16 +31,17 @@ public static String pretty(Object o) {
3131
try {
3232
return pretty().writeValueAsString(o);
3333
} catch (Exception e) {
34-
e.printStackTrace();
34+
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to YAML (3.1)", e);
3535
return null;
3636
}
3737
}
3838

3939
public static void prettyPrint(Object o) {
4040
try {
41-
System.out.println(pretty().writeValueAsString(o));
41+
String prettyString = pretty().writeValueAsString(o);
42+
PrettyPrintHelper.emit(LOGGER, prettyString);
4243
} catch (Exception e) {
43-
e.printStackTrace();
44+
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing YAML (3.1)", e);
4445
}
4546
}
4647

@@ -60,4 +61,5 @@ public static Map<String, Object> jsonSchemaAsMap(Schema schema) {
6061
LOGGER.error("Exception converting jsonSchema to Map", e);
6162
return null;
6263
}
63-
}}
64+
}
65+
}

modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import io.swagger.v3.oas.models.parameters.Parameter;
2828
import io.swagger.v3.oas.models.tags.Tag;
2929
import org.apache.commons.lang3.StringUtils;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3032
import org.testng.annotations.Test;
3133

3234
import java.io.IOException;
@@ -44,6 +46,7 @@
4446
import static org.testng.Assert.fail;
4547

4648
public class SpecFilterTest {
49+
private static final Logger LOGGER = LoggerFactory.getLogger(SpecFilterTest.class);
4750

4851
private static final String RESOURCE_RECURSIVE_MODELS = "specFiles/recursivemodels.json";
4952
private static final String RESOURCE_PATH = "specFiles/petstore-3.0-v2.json";
@@ -189,7 +192,7 @@ public void run() {
189192
try {
190193
filteredMap.put("filtered " + id, new SpecFilter().filter(openAPI, new NoOpOperationsFilter(), null, null, null));
191194
} catch (Exception e) {
192-
e.printStackTrace();
195+
LOGGER.error("Failed to filter OpenAPI concurrently", e);
193196
}
194197
}
195198
}.start();
@@ -220,7 +223,7 @@ public void run() {
220223
}
221224
}
222225
} catch (Exception e) {
223-
e.printStackTrace();
226+
LOGGER.error("Interrupted while waiting for filtering threads to complete", e);
224227
}
225228
for (OpenAPI filtered : filteredMap.values()) {
226229
assertEquals(Json.pretty(openAPI), Json.pretty(filtered));

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/SwaggerTestBase.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.SerializationFeature;
88
import io.swagger.v3.core.jackson.ModelResolver;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
911

1012
public abstract class SwaggerTestBase {
1113
static ObjectMapper mapper;
14+
private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerTestBase.class);
1215

1316
public static ObjectMapper mapper() {
1417
if (mapper == null) {
@@ -26,9 +29,9 @@ protected ModelResolver modelResolver() {
2629

2730
protected void prettyPrint(Object o) {
2831
try {
29-
System.out.println(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
32+
LOGGER.debug(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
3033
} catch (Exception e) {
31-
e.printStackTrace();
34+
LOGGER.error("Failed to pretty print object", e);
3235
}
3336
}
3437
}

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import io.swagger.v3.oas.models.responses.ApiResponse;
3030
import io.swagger.v3.oas.models.responses.ApiResponses;
3131
import io.swagger.v3.oas.models.security.SecurityScheme;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3234
import org.testng.annotations.Test;
3335
import org.yaml.snakeyaml.LoaderOptions;
3436

@@ -37,6 +39,7 @@
3739
import static org.testng.Assert.assertTrue;
3840

3941
public class OpenAPI3_1SerializationTest {
42+
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPI3_1SerializationTest.class);
4043

4144
@Test
4245
public void testSerializePetstore() throws Exception {
@@ -1390,7 +1393,7 @@ public void testBooleanSchemaSerialization() {
13901393
.openapi("3.1.0")
13911394
.components(new Components().addSchemas("test", new Schema().booleanSchemaValue(true)));
13921395

1393-
System.out.println("--------- root ----------");
1396+
LOGGER.debug("--------- root ----------");
13941397
Json31.prettyPrint(openAPI);
13951398
assertEquals(Json31.pretty(openAPI), withJacksonSystemLineSeparator("{\n" +
13961399
" \"openapi\" : \"3.1.0\",\n" +
@@ -1400,19 +1403,19 @@ public void testBooleanSchemaSerialization() {
14001403
" }\n" +
14011404
" }\n" +
14021405
"}"));
1403-
System.out.println("--------- schema ----------");
1406+
LOGGER.debug("--------- schema ----------");
14041407
Json31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
14051408
assertEquals(Json31.pretty(openAPI.getComponents().getSchemas().get("test")), "true");
1406-
System.out.println("--------- root YAML----------");
1409+
LOGGER.debug("--------- root YAML----------");
14071410
Yaml31.prettyPrint(openAPI);
14081411
assertEquals(Yaml31.pretty(openAPI), "openapi: 3.1.0\n" +
14091412
"components:\n" +
14101413
" schemas:\n" +
14111414
" test: true\n");
1412-
System.out.println("--------- schema YAML ----------");
1415+
LOGGER.debug("--------- schema YAML ----------");
14131416
Yaml31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
14141417
assertEquals(Yaml31.pretty(openAPI.getComponents().getSchemas().get("test")), "true\n");
1415-
System.out.println("--------- root 3.0 ----------");
1418+
LOGGER.debug("--------- root 3.0 ----------");
14161419
Json.prettyPrint(openAPI);
14171420
assertEquals(Json.pretty(openAPI), withJacksonSystemLineSeparator("{\n" +
14181421
" \"openapi\" : \"3.1.0\",\n" +
@@ -1422,16 +1425,16 @@ public void testBooleanSchemaSerialization() {
14221425
" }\n" +
14231426
" }\n" +
14241427
"}"));
1425-
System.out.println("--------- schema 3.0 ----------");
1428+
LOGGER.debug("--------- schema 3.0 ----------");
14261429
Json.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
14271430
assertEquals(Json.pretty(openAPI.getComponents().getSchemas().get("test")), "{ }");
1428-
System.out.println("--------- root YAML 3.0 ----------");
1431+
LOGGER.debug("--------- root YAML 3.0 ----------");
14291432
Yaml.prettyPrint(openAPI);
14301433
assertEquals(Yaml.pretty(openAPI), "openapi: 3.1.0\n" +
14311434
"components:\n" +
14321435
" schemas:\n" +
14331436
" test: {}\n");
1434-
System.out.println("--------- schema YAML 3.0 ----------");
1437+
LOGGER.debug("--------- schema YAML 3.0 ----------");
14351438
Yaml.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
14361439
assertEquals(Yaml.pretty(openAPI.getComponents().getSchemas().get("test")), "{}\n");
14371440
}

modules/swagger-core/src/test/java/io/swagger/v3/core/util/OutputReplacer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ void setOutputStream(PrintStream outputStream) {
4949
public String run(Function function) {
5050
final PrintStream out = getOutputStream();
5151
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
52-
52+
final PrintStream capture = new PrintStream(outputStream);
5353
try {
54-
setOutputStream(new PrintStream(outputStream));
54+
setOutputStream(capture);
55+
PrettyPrintHelper.setOverride(message -> {
56+
capture.println(message);
57+
capture.flush();
58+
});
5559
function.run();
5660
} finally {
61+
PrettyPrintHelper.clearOverride();
5762
setOutputStream(out);
63+
capture.close();
5864
}
5965

6066
try {

0 commit comments

Comments
 (0)