Skip to content

Commit a2ba9b5

Browse files
committed
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
1 parent bda7835 commit a2ba9b5

6 files changed

Lines changed: 47 additions & 19 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
<commons.pmd.version>3.14.0</commons.pmd.version>
175175
<commons.pmd-impl.version>6.36.0</commons.pmd-impl.version>
176176
<commons.jacoco.version>0.8.7</commons.jacoco.version>
177-
<commons.spotbugs.version>4.2.3</commons.spotbugs.version>
177+
<commons.spotbugs.version>4.3.0</commons.spotbugs.version>
178178
<commons.japicmp.version>0.15.3</commons.japicmp.version>
179179
<commons.javadoc.version>3.3.0</commons.javadoc.version>
180180
<biz.aQute.bndlib.version>5.3.0</biz.aQute.bndlib.version>

src/changes/changes.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<action issue="CSV-283" type="fix" dev="ggregory" due-to="belugabehr">Remove Whitespace Check Determines Delimiter Twice #167.</action>
6767
<action issue="CSV-283" type="fix" dev="ggregory" due-to="belugabehr">Document and Automate CSV Benchmark Harness #166.</action>
6868
<action issue="CSV-279" type="fix" dev="ggregory" due-to="belugabehr">Optimize Lexer Delimiter Check for One Character Delimiter #163.</action>
69+
<action type="fix" dev="ggregory" due-to="Gary Gregory">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.</action>
70+
<action type="fix" dev="ggregory" due-to="Gary Gregory">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.</action>
71+
<action type="fix" dev="ggregory" due-to="Gary Gregory">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.</action>
72+
<action type="fix" dev="ggregory" due-to="Gary Gregory">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.</action>
73+
<action type="fix" dev="ggregory" due-to="Gary Gregory">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.</action>
6974
<!-- ADD -->
7075
<action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, Gary Gregory">Make CSVRecord#toList() public.</action>
7176
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVRecord#stream().</action>
@@ -90,7 +95,7 @@
9095
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump checkstyle from 8.29 to 8.44.</action>
9196
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump junit-jupiter from 5.7.0 to 5.8.0-M1 #133, #149.</action>
9297
<action type="update" dev="ggregory" due-to="Dependabot">Bump commons.jacoco.version from 0.8.5 to 0.8.7 (Java 16).</action>
93-
<action type="update" dev="ggregory" due-to="Dependabot">Bump commons.spotbugs.version from 4.0.4 to 4.2.3 (Java 16).</action>
98+
<action type="update" dev="ggregory" due-to="Dependabot">Bump commons.spotbugs.version from 4.0.4 to 4.3.0 (Java 16).</action>
9499
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump maven-javadoc-plugin from 3.2.0 to 3.3.0.</action>
95100
<action type="update" dev="ggregory" due-to="Dependabot">Bump jmh-generator-annprocess from 1.5.2 to 1.32 #151.</action>
96101
<action type="update" dev="ggregory" due-to="Dependabot">Bump PMD core from 6.29.0 to 6.36.0.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,15 @@ public Builder builder() {
13961396
return Builder.create(this);
13971397
}
13981398

1399+
/**
1400+
* Creates a copy of this instance.
1401+
*
1402+
* @return a copy of this instance.
1403+
*/
1404+
CSVFormat copy() {
1405+
return builder().build();
1406+
}
1407+
13991408
@Override
14001409
public boolean equals(final Object obj) {
14011410
if (this == obj) {

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,7 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
355355

356356
private final CSVFormat format;
357357

358-
/** A mapping of column names to column indices */
359-
private final Map<String, Integer> headerMap;
360-
361-
/** The column order to avoid re-computing it. */
362-
private final List<String> headerNames;
358+
private final Headers headers;
363359

364360
private final Lexer lexer;
365361

@@ -430,12 +426,10 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
430426
Objects.requireNonNull(reader, "reader");
431427
Objects.requireNonNull(format, "format");
432428

433-
this.format = format;
429+
this.format = format.copy();
434430
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
435431
this.csvRecordIterator = new CSVRecordIterator();
436-
final Headers headers = createHeaders();
437-
this.headerMap = headers.headerMap;
438-
this.headerNames = headers.headerNames;
432+
this.headers = createHeaders();
439433
this.characterOffset = characterOffset;
440434
this.recordNumber = recordNumber - 1;
441435
}
@@ -566,11 +560,11 @@ public String getFirstEndOfLine() {
566560
* @return a copy of the header map.
567561
*/
568562
public Map<String, Integer> getHeaderMap() {
569-
if (this.headerMap == null) {
563+
if (this.headers.headerMap == null) {
570564
return null;
571565
}
572566
final Map<String, Integer> map = createEmptyHeaderMap();
573-
map.putAll(this.headerMap);
567+
map.putAll(this.headers.headerMap);
574568
return map;
575569
}
576570

@@ -580,7 +574,7 @@ public Map<String, Integer> getHeaderMap() {
580574
* @return the header map.
581575
*/
582576
Map<String, Integer> getHeaderMapRaw() {
583-
return this.headerMap;
577+
return this.headers.headerMap;
584578
}
585579

586580
/**
@@ -596,7 +590,7 @@ Map<String, Integer> getHeaderMapRaw() {
596590
* @since 1.7
597591
*/
598592
public List<String> getHeaderNames() {
599-
return headerNames;
593+
return Collections.unmodifiableList(headers.headerNames);
600594
}
601595

602596
/**

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public CSVPrinter(final Appendable appendable, final CSVFormat format) throws IO
9797
Objects.requireNonNull(format, "format");
9898

9999
this.appendable = appendable;
100-
this.format = format;
100+
this.format = format.copy();
101101
// TODO: Is it a good idea to do this here instead of on the first call to a print method?
102102
// It seems a pain to have to track whether the header has already been printed or not.
103103
if (format.getHeaderComments() != null) {

src/site/resources/spotbugs/spotbugs-exclude-filter.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,32 @@
2020
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
2121

2222

23+
<!-- Deliberate switch statement fall-through. -->
2324
<Match>
2425
<Class name="org.apache.commons.csv.CSVPrinter" />
25-
<Method name="printComment" >
26-
<!-- Deliberate switch statement fall-through -->
27-
</Method>
26+
<Method name="printComment" />
2827
<Bug code="SF" />
2928
</Match>
3029

30+
<!-- A format is immutable. -->
31+
<Match>
32+
<Class name="org.apache.commons.csv.CSVFormat$Predefined" />
33+
<Method name="getFormat" />
34+
<Bug pattern="EI_EXPOSE_REP"/>
35+
</Match>
36+
37+
<!-- Should not have called this method iterator() since it returns the current iterator and not a new one. -->
38+
<Match>
39+
<Class name="org.apache.commons.csv.CSVParser" />
40+
<Method name="iterator" />
41+
<Bug pattern="EI_EXPOSE_REP"/>
42+
</Match>
43+
44+
<!-- Could make package private in the next major version. -->
45+
<Match>
46+
<Class name="org.apache.commons.csv.CSVRecord" />
47+
<Method name="getParser" />
48+
<Bug pattern="EI_EXPOSE_REP"/>
49+
</Match>
50+
3151
</FindBugsFilter>

0 commit comments

Comments
 (0)