Skip to content

Commit 75efb86

Browse files
authored
RAT-566: fix some spotbugs issues in core (#688)
* fixed spotbugs issues * added exception tests * added license headers
1 parent a28d77b commit 75efb86

10 files changed

Lines changed: 110 additions & 7 deletions

File tree

apache-rat-core/src/main/java/org/apache/rat/ConfigurationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class ConfigurationException extends RuntimeException {
2626
private static final long serialVersionUID = 7257245932787579431L;
2727

2828
public static ConfigurationException from(final Exception e) {
29-
if (e instanceof ConfigurationException) {
30-
return (ConfigurationException) e;
29+
if (e instanceof ConfigurationException exists) {
30+
return exists;
3131
}
3232
return new ConfigurationException(e);
3333
}

apache-rat-core/src/main/java/org/apache/rat/Defaults.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public final class Defaults {
4949
/** The default configuration file from the package. */
5050
private static final URI DEFAULT_CONFIG_URI;
5151
/** The path to the default configuration file. */
52-
private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml";
52+
// sonar wants this to be configurable.
53+
private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml"; // NOSONAR
5354
/** The default ARCHIVES processing style. */
5455
public static final ReportConfiguration.Processing ARCHIVE_PROCESSING = ReportConfiguration.Processing.NOTIFICATION;
5556
/** The default STANDARD processing style. */

apache-rat-core/src/main/java/org/apache/rat/ImplementationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class ImplementationException extends RuntimeException {
2626
private static final long serialVersionUID = 7257245932787579431L;
2727

2828
public static ImplementationException makeInstance(final Exception e) {
29-
if (e instanceof ImplementationException) {
30-
return (ImplementationException) e;
29+
if (e instanceof ImplementationException exists) {
30+
return exists;
3131
}
3232
return new ImplementationException(e);
3333
}

apache-rat-core/src/main/java/org/apache/rat/analysis/license/SimplePatternBasedLicense.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.Arrays;
2222

23+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2324
import org.apache.rat.DeprecationReporter;
2425
import org.apache.rat.configuration.builders.AbstractBuilder;
2526
import org.apache.rat.configuration.builders.AnyBuilder;
@@ -31,6 +32,7 @@
3132
* @since Rat 0.8
3233
* @deprecated Use new configuration options
3334
*/
35+
@SuppressFBWarnings("EI_EXPOSE_REP2")
3436
@Deprecated // Since 0.16
3537
@DeprecationReporter.Info(since = "0.16", forRemoval = true, use = "new configuration options")
3638
public class SimplePatternBasedLicense extends BaseLicense {

apache-rat-core/src/main/java/org/apache/rat/analysis/matchers/SimpleTextMatcher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.rat.config.parameters.ComponentType;
2424
import org.apache.rat.config.parameters.ConfigComponent;
2525

26+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27+
2628
/**
2729
* A simple text matching IHeaderMatcher implementation.
2830
*/
@@ -44,11 +46,12 @@ public SimpleTextMatcher(final String simpleText) {
4446
this(null, simpleText);
4547
}
4648

49+
// no sonar and supress FI_USELESS because this is how we ensure that the finalize bug does not bite us
50+
@SuppressFBWarnings("FI_USELESS")
4751
@Override
4852
protected final void finalize() throws Throwable { // NOSONAR
49-
// no sonar because this is how we ensure that the finalize bug does not bite us
53+
// finalizer attack remediation.
5054
super.finalize(); // NOSONAR
51-
// finalizer attack remediation.
5255
}
5356

5457
/**

apache-rat-core/src/main/java/org/apache/rat/configuration/builders/MatcherRefBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.rat.config.parameters.ConfigComponent;
2828
import org.apache.rat.config.parameters.MatcherBuilder;
2929

30+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31+
3032
/**
3133
* A reference matching Matcher builder.
3234
* <p>
@@ -62,6 +64,7 @@ public MatcherRefBuilder setRefId(final String refId) {
6264
* @param matchers the Map of ids to instances.
6365
* @return this builder for chaining.
6466
*/
67+
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Expected external update of matchers.")
6568
public MatcherRefBuilder setMatcherMap(final Map<String, IHeaderMatcher> matchers) {
6669
// this method is called by reflection
6770
this.matchers = matchers;
@@ -109,6 +112,7 @@ public static class IHeaderMatcherProxy implements IHeaderMatcher {
109112
* @param proxyId the id of the matcher to find.
110113
* @param matchers a mapping of matchers that have been found.
111114
*/
115+
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Expected external update of matchers.")
112116
public IHeaderMatcherProxy(final String proxyId, final Map<String, IHeaderMatcher> matchers) {
113117
this.proxyId = proxyId;
114118
this.matchers = matchers;

apache-rat-core/src/main/java/org/apache/rat/header/HeaderMatcher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.rat.header;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import org.apache.rat.DeprecationReporter;
2223

2324
import java.io.IOException;
@@ -35,6 +36,7 @@
3536
* <p><strong>Note:</strong> use only from a single thread.</p>
3637
*
3738
*/
39+
@SuppressFBWarnings("EI_EXPOSE_REP2")
3840
@Deprecated // since 0.17
3941
@DeprecationReporter.Info(since = "0.17", forRemoval = true)
4042
public class HeaderMatcher {

apache-rat-core/src/main/java/org/apache/rat/help/Licenses.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.apache.rat.license.ILicenseFamily;
4444
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
4545

46+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
47+
4648
import static java.lang.String.format;
4749

4850
/**
@@ -64,6 +66,7 @@ public final class Licenses extends AbstractHelp {
6466
* @param config The configuration that contains the license information.
6567
* @param writer the writer to write the report to.
6668
*/
69+
@SuppressFBWarnings("EI_EXPOSE_REP2")
6770
public Licenses(final ReportConfiguration config, final Writer writer) {
6871
this.config = config;
6972
this.licenses = config.getLicenses(LicenseFilter.ALL);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one *
3+
* or more contributor license agreements. See the NOTICE file *
4+
* distributed with this work for additional information *
5+
* regarding copyright ownership. The ASF licenses this file *
6+
* to you under the Apache License, Version 2.0 (the *
7+
* "License"); you may not use this file except in compliance *
8+
* with the License. You may obtain a copy of the License at *
9+
* *
10+
* https://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, *
13+
* software distributed under the License is distributed on an *
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15+
* KIND, either express or implied. See the License for the *
16+
* specific language governing permissions and limitations *
17+
* under the License. *
18+
*/
19+
package org.apache.rat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
class ConfigurationExceptionTest {
26+
27+
@Test
28+
void fromTest() {
29+
RuntimeException runTime = new RuntimeException();
30+
assertThat(ConfigurationException.from(runTime))
31+
.isInstanceOf(ConfigurationException.class)
32+
.hasCause(runTime);
33+
34+
Exception ex = new Exception();
35+
assertThat(ConfigurationException.from(ex))
36+
.isInstanceOf(ConfigurationException.class)
37+
.hasCause(ex);
38+
39+
ex = new ConfigurationException("yee haw");
40+
assertThat(ConfigurationException.from(ex))
41+
.isInstanceOf(ConfigurationException.class)
42+
.isEqualTo(ex);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one *
3+
* or more contributor license agreements. See the NOTICE file *
4+
* distributed with this work for additional information *
5+
* regarding copyright ownership. The ASF licenses this file *
6+
* to you under the Apache License, Version 2.0 (the *
7+
* "License"); you may not use this file except in compliance *
8+
* with the License. You may obtain a copy of the License at *
9+
* *
10+
* https://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, *
13+
* software distributed under the License is distributed on an *
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15+
* KIND, either express or implied. See the License for the *
16+
* specific language governing permissions and limitations *
17+
* under the License. *
18+
*/
19+
package org.apache.rat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
class ImplementationExceptionTest {
26+
27+
@Test
28+
void fromTest() {
29+
RuntimeException runTime = new RuntimeException();
30+
assertThat(ImplementationException.makeInstance(runTime))
31+
.isInstanceOf(ImplementationException.class)
32+
.hasCause(runTime);
33+
34+
Exception ex = new Exception();
35+
assertThat(ImplementationException.makeInstance(ex))
36+
.isInstanceOf(ImplementationException.class)
37+
.hasCause(ex);
38+
39+
ex = new ImplementationException("yee haw");
40+
assertThat(ImplementationException.makeInstance(ex))
41+
.isInstanceOf(ImplementationException.class)
42+
.isEqualTo(ex);
43+
}
44+
}

0 commit comments

Comments
 (0)