From a2ba9b52886162d7f046ff7fe8019c1ba5334a9a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 14 Jul 2021 11:29:27 -0400 Subject: [PATCH] Bump spotbugs-maven-plugin from 4.2.3 to 4.3.0. Fix SpotBugs Errors - Error: Medium: org.apache.commons.csv.CSVParser.getHeaderNames() may expose internal representation by returning CSVParser.headerNames [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 599] EI_EXPOSE_REP - Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.format [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 433] EI_EXPOSE_REP2 - Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.headerMap [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 437] EI_EXPOSE_REP2 - Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.headerNames [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 438] EI_EXPOSE_REP2 - Error: Medium: new org.apache.commons.csv.CSVPrinter(Appendable, CSVFormat) may expose internal representation by storing an externally mutable object into CSVPrinter.format [org.apache.commons.csv.CSVPrinter] At CSVPrinter.java:[line 100] EI_EXPOSE_REP2 Suppresss SpotBugs Errors: - Error: Medium: org.apache.commons.csv.CSVFormat$Predefined.getFormat() may expose internal representation by returning CSVFormat$Predefined.format [org.apache.commons.csv.CSVFormat$Predefined] At CSVFormat.java:[line 758] EI_EXPOSE_REP - Error: Medium: org.apache.commons.csv.CSVParser.iterator() may expose internal representation by returning CSVParser.csvRecordIterator [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 690] EI_EXPOSE_REP - Error: Medium: org.apache.commons.csv.CSVRecord.getParser() may expose internal representation by returning CSVRecord.parser [org.apache.commons.csv.CSVRecord] At CSVRecord.java:[line 171] EI_EXPOSE_REP --- pom.xml | 2 +- src/changes/changes.xml | 7 ++++- .../org/apache/commons/csv/CSVFormat.java | 9 +++++++ .../org/apache/commons/csv/CSVParser.java | 20 +++++--------- .../org/apache/commons/csv/CSVPrinter.java | 2 +- .../spotbugs/spotbugs-exclude-filter.xml | 26 ++++++++++++++++--- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index d4fa1024..44a02386 100644 --- a/pom.xml +++ b/pom.xml @@ -174,7 +174,7 @@ 3.14.0 6.36.0 0.8.7 - 4.2.3 + 4.3.0 0.15.3 3.3.0 5.3.0 diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e8533981..b634edda 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -66,6 +66,11 @@ Remove Whitespace Check Determines Delimiter Twice #167. Document and Automate CSV Benchmark Harness #166. Optimize Lexer Delimiter Check for One Character Delimiter #163. + SpotBugs Error: Medium: org.apache.commons.csv.CSVParser.getHeaderNames() may expose internal representation by returning CSVParser.headerNames [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 599] EI_EXPOSE_REP. + SpotBugs Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.format [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 433] EI_EXPOSE_REP2. + SpotBugs Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.headerMap [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 437] EI_EXPOSE_REP2. + SpotBugs Error: Medium: new org.apache.commons.csv.CSVParser(Reader, CSVFormat, long, long) may expose internal representation by storing an externally mutable object into CSVParser.headerNames [org.apache.commons.csv.CSVParser] At CSVParser.java:[line 438] EI_EXPOSE_REP2. + SpotBugs Error: Medium: new org.apache.commons.csv.CSVPrinter(Appendable, CSVFormat) may expose internal representation by storing an externally mutable object into CSVPrinter.format [org.apache.commons.csv.CSVPrinter] At CSVPrinter.java:[line 100] EI_EXPOSE_REP2. Make CSVRecord#toList() public. Add CSVRecord#stream(). @@ -90,7 +95,7 @@ Bump checkstyle from 8.29 to 8.44. Bump junit-jupiter from 5.7.0 to 5.8.0-M1 #133, #149. Bump commons.jacoco.version from 0.8.5 to 0.8.7 (Java 16). - Bump commons.spotbugs.version from 4.0.4 to 4.2.3 (Java 16). + Bump commons.spotbugs.version from 4.0.4 to 4.3.0 (Java 16). Bump maven-javadoc-plugin from 3.2.0 to 3.3.0. Bump jmh-generator-annprocess from 1.5.2 to 1.32 #151. Bump PMD core from 6.29.0 to 6.36.0. diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index add2c2ce..2b732d6b 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1396,6 +1396,15 @@ public final class CSVFormat implements Serializable { return Builder.create(this); } + /** + * Creates a copy of this instance. + * + * @return a copy of this instance. + */ + CSVFormat copy() { + return builder().build(); + } + @Override public boolean equals(final Object obj) { if (this == obj) { diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index 94bb3612..60ecc737 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -355,11 +355,7 @@ public final class CSVParser implements Iterable, Closeable { private final CSVFormat format; - /** A mapping of column names to column indices */ - private final Map headerMap; - - /** The column order to avoid re-computing it. */ - private final List headerNames; + private final Headers headers; private final Lexer lexer; @@ -430,12 +426,10 @@ public final class CSVParser implements Iterable, Closeable { Objects.requireNonNull(reader, "reader"); Objects.requireNonNull(format, "format"); - this.format = format; + this.format = format.copy(); this.lexer = new Lexer(format, new ExtendedBufferedReader(reader)); this.csvRecordIterator = new CSVRecordIterator(); - final Headers headers = createHeaders(); - this.headerMap = headers.headerMap; - this.headerNames = headers.headerNames; + this.headers = createHeaders(); this.characterOffset = characterOffset; this.recordNumber = recordNumber - 1; } @@ -566,11 +560,11 @@ public final class CSVParser implements Iterable, Closeable { * @return a copy of the header map. */ public Map getHeaderMap() { - if (this.headerMap == null) { + if (this.headers.headerMap == null) { return null; } final Map map = createEmptyHeaderMap(); - map.putAll(this.headerMap); + map.putAll(this.headers.headerMap); return map; } @@ -580,7 +574,7 @@ public final class CSVParser implements Iterable, Closeable { * @return the header map. */ Map getHeaderMapRaw() { - return this.headerMap; + return this.headers.headerMap; } /** @@ -596,7 +590,7 @@ public final class CSVParser implements Iterable, Closeable { * @since 1.7 */ public List getHeaderNames() { - return headerNames; + return Collections.unmodifiableList(headers.headerNames); } /** diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index c0529ee9..171bcff2 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -97,7 +97,7 @@ public final class CSVPrinter implements Flushable, Closeable { Objects.requireNonNull(format, "format"); this.appendable = appendable; - this.format = format; + this.format = format.copy(); // TODO: Is it a good idea to do this here instead of on the first call to a print method? // It seems a pain to have to track whether the header has already been printed or not. if (format.getHeaderComments() != null) { diff --git a/src/site/resources/spotbugs/spotbugs-exclude-filter.xml b/src/site/resources/spotbugs/spotbugs-exclude-filter.xml index 4af22bc7..a13b5fda 100644 --- a/src/site/resources/spotbugs/spotbugs-exclude-filter.xml +++ b/src/site/resources/spotbugs/spotbugs-exclude-filter.xml @@ -20,12 +20,32 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd"> + - - - + + + + + + + + + + + + + + + + + + + + + +