Skip to content

Commit 0532016

Browse files
committed
Issue #440: resolves NoNullForCollectionReturn
1 parent d74fd00 commit 0532016

6 files changed

Lines changed: 26 additions & 41 deletions

File tree

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/configtypes/BuiltInConfigurationType.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package net.sf.eclipsecs.core.config.configtypes;
2222

2323
import java.net.URL;
24+
import java.util.Optional;
2425

2526
import org.eclipse.core.runtime.FileLocator;
2627
import org.eclipse.core.runtime.Path;
@@ -63,10 +64,10 @@ protected URL resolveLocation(ICheckConfiguration checkConfiguration) {
6364
}
6465

6566
@Override
66-
protected byte[] getAdditionPropertiesBundleBytes(URL checkConfigURL) {
67-
// just returns null since additional property file is not needed nor
67+
protected Optional<byte[]> getAdditionPropertiesBundleBytes(URL checkConfigURL) {
68+
// just returns empty since additional property file is not needed nor
6869
// supported
69-
return null;
70+
return Optional.empty();
7071
}
7172

7273
@Override

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/configtypes/ConfigurationType.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URL;
2828
import java.net.URLConnection;
2929
import java.util.Objects;
30+
import java.util.Optional;
3031
import java.util.PropertyResourceBundle;
3132
import java.util.ResourceBundle;
3233

@@ -142,9 +143,8 @@ public CheckstyleConfigurationFile getCheckstyleConfiguration(
142143
data.setCheckConfigFileBytes(configurationFileData);
143144

144145
// get the properties bundle
145-
byte[] additionalPropertiesBytes = getAdditionPropertiesBundleBytes(
146-
data.getResolvedConfigFileURL());
147-
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
146+
getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL())
147+
.ifPresent(data::setAdditionalPropertyBundleBytes);
148148

149149
// get the property resolver
150150
PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
@@ -157,7 +157,7 @@ public CheckstyleConfigurationFile getCheckstyleConfiguration(
157157
return data;
158158
}
159159

160-
protected byte[] getAdditionPropertiesBundleBytes(URL checkConfigURL) {
160+
protected Optional<byte[]> getAdditionPropertiesBundleBytes(URL checkConfigURL) {
161161

162162
String location = checkConfigURL.toString();
163163

@@ -173,17 +173,16 @@ protected byte[] getAdditionPropertiesBundleBytes(URL checkConfigURL) {
173173
propsLocation = propsLocation + ".properties"; //$NON-NLS-1$
174174

175175
try {
176-
177176
URL propertyFileURL = new URL(propsLocation);
178177
URLConnection connection = propertyFileURL.openConnection();
179178

180-
return getBytesFromURLConnection(connection);
179+
return Optional.of(getBytesFromURLConnection(connection));
181180
} catch (IOException ex) {
182181
// we won't load the bundle then
183182
// disabled logging bug #1647602
184183
// CheckstyleLog.log(ioe);
185184
}
186-
return null;
185+
return Optional.empty();
187186
}
188187

189188
/**
@@ -225,14 +224,9 @@ protected PropertyResolver getPropertyResolver(ICheckConfiguration config,
225224
}
226225

227226
protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOException {
228-
229-
byte[] configurationFileData = null;
230-
231227
try (InputStream in = connection.getInputStream()) {
232-
configurationFileData = ByteStreams.toByteArray(in);
228+
return ByteStreams.toByteArray(in);
233229
}
234-
235-
return configurationFileData;
236230
}
237231

238232
@Override

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/configtypes/RemoteConfigurationType.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.security.NoSuchAlgorithmException;
3636
import java.util.Base64;
3737
import java.util.HashSet;
38+
import java.util.Optional;
3839
import java.util.Set;
3940

4041
import org.eclipse.core.runtime.IPath;
@@ -128,23 +129,24 @@ public CheckstyleConfigurationFile getCheckstyleConfiguration(
128129
data.setCheckConfigFileBytes(configurationFileData);
129130

130131
// get the properties bundle
131-
byte[] additionalPropertiesBytes = null;
132+
Optional<byte[]> additionalPropertiesBytes = Optional.empty();
132133
if (originalFileSuccess) {
133134
additionalPropertiesBytes = getAdditionPropertiesBundleBytes(
134135
data.getResolvedConfigFileURL());
135136
} else if (useCacheFile) {
136137
additionalPropertiesBytes = getBytesFromCacheBundleFile(checkConfiguration);
137138
}
138139

139-
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
140+
additionalPropertiesBytes.ifPresent(data::setAdditionalPropertyBundleBytes);
140141

141142
// get the property resolver
142143
final PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
143144
data.setPropertyResolver(resolver);
144145

145146
// write to cache file
146147
if (originalFileSuccess && useCacheFile) {
147-
writeToCacheFile(checkConfiguration, configurationFileData, additionalPropertiesBytes);
148+
writeToCacheFile(checkConfiguration, configurationFileData,
149+
additionalPropertiesBytes.orElse(null));
148150
}
149151

150152
}
@@ -229,13 +231,12 @@ private byte[] getBytesFromCacheFile(ICheckConfiguration checkConfig) throws IOE
229231
* @throws IOException
230232
* error getting the stream (file does not exist)
231233
*/
232-
private byte[] getBytesFromCacheBundleFile(ICheckConfiguration checkConfig) {
233-
234+
private Optional<byte[]> getBytesFromCacheBundleFile(ICheckConfiguration checkConfig) {
234235
String cacheFileLocation = checkConfig.getAdditionalData().get(KEY_CACHE_PROPS_FILE_LOCATION);
235236

236237
// bug 1748626
237238
if (cacheFileLocation == null) {
238-
return null;
239+
return Optional.empty();
239240
}
240241

241242
try {
@@ -246,13 +247,13 @@ private byte[] getBytesFromCacheBundleFile(ICheckConfiguration checkConfig) {
246247
URL configURL = cacheFile.toURI().toURL();
247248
URLConnection connection = configURL.openConnection();
248249

249-
return getBytesFromURLConnection(connection);
250+
return Optional.of(getBytesFromURLConnection(connection));
250251
} catch (IOException ex) {
251252
// we won't load the bundle then
252253
// disabled logging bug #1647602
253254
// CheckstyleLog.log(ioe);
254255
}
255-
return null;
256+
return Optional.empty();
256257
}
257258

258259
private void writeToCacheFile(ICheckConfiguration checkConfig, byte[] configFileBytes,
@@ -290,9 +291,6 @@ private void writeToCacheFile(ICheckConfiguration checkConfig, byte[] configFile
290291

291292
@Override
292293
protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOException {
293-
294-
byte[] configurationFileData = null;
295-
296294
// set timeouts - bug 2941010
297295
connection.setConnectTimeout(10000);
298296
connection.setReadTimeout(10000);
@@ -322,10 +320,8 @@ protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOEx
322320
}
323321

324322
try (InputStream in = connection.getInputStream()) {
325-
configurationFileData = ByteStreams.toByteArray(in);
323+
return ByteStreams.toByteArray(in);
326324
}
327-
328-
return configurationFileData;
329325
}
330326

331327
/**

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/ProjectConfigurationWorkingCopy.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,9 @@ private void writeFilter(IFilter filter, Element docRoot) {
525525
filterEl.addAttribute(XMLTags.ENABLED_TAG, Boolean.toString(filter.isEnabled()));
526526

527527
List<String> data = filter.getFilterData();
528-
if (data != null && !data.isEmpty()) {
529-
for (String item : data) {
530-
531-
Element dataEl = filterEl.addElement(XMLTags.FILTER_DATA_TAG);
532-
dataEl.addAttribute(XMLTags.VALUE_TAG, item);
533-
}
528+
for (String item : data) {
529+
Element dataEl = filterEl.addElement(XMLTags.FILTER_DATA_TAG);
530+
dataEl.addAttribute(XMLTags.VALUE_TAG, item);
534531
}
535532
}
536533
}

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/filters/AbstractFilter.java

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

2121
package net.sf.eclipsecs.core.projectconfig.filters;
2222

23+
import java.util.Collections;
2324
import java.util.List;
2425
import java.util.Objects;
2526

@@ -103,7 +104,7 @@ public boolean isReadonly() {
103104
@Override
104105
public List<String> getFilterData() {
105106
// NOOP
106-
return null;
107+
return Collections.emptyList();
107108
}
108109

109110
@Override

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/filters/PackageFilter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ public boolean accept(Object object) {
7878

7979
@Override
8080
public void setFilterData(List<String> filterData) {
81-
if (filterData == null) {
82-
mData = new ArrayList<>();
83-
}
84-
8581
mData = filterData;
8682

8783
if (mData.contains(RECURSE_OFF_MARKER)) {

0 commit comments

Comments
 (0)