Skip to content

Commit da4a5c6

Browse files
authored
Merge pull request #100 from spdx/lyft
Fix a few possible issues identified by Sonatype Lyft
2 parents 7a9edb5 + 525347f commit da4a5c6

19 files changed

+164
-135
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<dependency>
9494
<groupId>org.apache.logging.log4j</groupId>
9595
<artifactId>log4j-slf4j-impl</artifactId>
96-
<version>2.17.0</version>
96+
<version>2.17.1</version>
9797
</dependency>
9898
<dependency>
9999
<groupId>org.apache.commons</groupId>

src/main/java/org/spdx/library/DefaultModelStore.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ private DefaultModelStore() {
4444
}
4545

4646
public static IModelStore getDefaultModelStore() {
47-
return defaultModelStore;
47+
lock.readLock().lock();
48+
try {
49+
return defaultModelStore;
50+
} finally {
51+
lock.readLock().unlock();
52+
}
4853
}
4954

5055
public static String getDefaultDocumentUri() {

src/main/java/org/spdx/library/model/ExternalDocumentRef.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public String getSpdxDocumentNamespace() throws InvalidSPDXAnalysisException {
187187
return "";
188188
}
189189
if (docNamespace.get() instanceof IndividualUriValue) {
190-
String docUri = ((IndividualUriValue)(docNamespace.get())).getIndividualURI();
190+
String docUri = ((IndividualUriValue)docNamespace.get()).getIndividualURI();
191191
if (Objects.isNull(docUri)) {
192192
logger.warn("Missing individual URI in doc namespace");
193193
return "";
@@ -306,7 +306,6 @@ protected List<String> _verify(List<String> verifiedIds, String specVersion) {
306306
*/
307307
@Override
308308
public int compareTo(ExternalDocumentRef o) {
309-
int retval = 0;
310309
String myDocumentNamespace;
311310
try {
312311
myDocumentNamespace = getSpdxDocumentNamespace();
@@ -321,7 +320,7 @@ public int compareTo(ExternalDocumentRef o) {
321320
logger.warn("Error getting compare document namepsace",e);
322321
compareDocumentNamespace = "";
323322
}
324-
retval = myDocumentNamespace.compareTo(compareDocumentNamespace);
323+
int retval = myDocumentNamespace.compareTo(compareDocumentNamespace);
325324
if (retval != 0) {
326325
return retval;
327326
}

src/main/java/org/spdx/library/model/ExternalSpdxElement.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ExternalSpdxElement(IModelStore modelStore, String documentUri, String id
6363
throws InvalidSPDXAnalysisException {
6464
super(modelStore, documentUri, id, copyManager, create);
6565
if (!SpdxConstants.EXTERNAL_ELEMENT_REF_PATTERN.matcher(id).matches()) {
66-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID"));
66+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID");
6767
}
6868
getExternalSpdxElementURI(); //this will check to make sure the external document reference is available
6969
}
@@ -75,7 +75,7 @@ public ExternalSpdxElement(IModelStore modelStore, String documentUri, String id
7575
public String getExternalDocumentId() throws InvalidSPDXAnalysisException {
7676
Matcher matcher = SpdxConstants.EXTERNAL_ELEMENT_REF_PATTERN.matcher(this.getId());
7777
if (!matcher.matches()) {
78-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID"));
78+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID");
7979
}
8080
return matcher.group(1);
8181
}
@@ -87,7 +87,7 @@ public String getExternalDocumentId() throws InvalidSPDXAnalysisException {
8787
public String getExternalElementId() throws InvalidSPDXAnalysisException {
8888
Matcher matcher = SpdxConstants.EXTERNAL_ELEMENT_REF_PATTERN.matcher(this.getId());
8989
if (!matcher.matches()) {
90-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID"));
90+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:SPDXID");
9191
}
9292
return matcher.group(2);
9393
}
@@ -198,7 +198,7 @@ public static String externalDocumentIdToNamespace(String externalDocumentId,
198198
Optional<Object> retval = getObjectPropertyValue(stModelStore, stDocumentUri,
199199
externalDocumentId, SpdxConstants.PROP_EXTERNAL_SPDX_DOCUMENT, copyManager);
200200
if (!retval.isPresent()) {
201-
throw(new InvalidSPDXAnalysisException("No external document reference exists for document ID "+externalDocumentId));
201+
throw new InvalidSPDXAnalysisException("No external document reference exists for document ID "+externalDocumentId);
202202
}
203203
if (!(retval.get() instanceof IndividualUriValue)) {
204204
logger.error("Invalid type returned for external document. Expected IndividualValue, actual "+retval.get().getClass().toString());

src/main/java/org/spdx/library/model/ModelObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public abstract class ModelObject {
111111
// the following fields are for debugging when equivalent returns false
112112
enum NotEquivalent {
113113
DIFFERENT_CLASS, MISSING_PROPERTY, PROPERTY_NOT_EQUIVALENT, COMPARE_PROPERTY_MISSING};
114-
class NotEquivalentReason {
114+
static class NotEquivalentReason {
115115
NotEquivalent reason;
116116
String property = null;
117117

@@ -782,7 +782,7 @@ public boolean equivalent(ModelObject compare, boolean ignoreRelatedElements) th
782782
Optional<Object> propertyValue = this.getObjectPropertyValue(propertyName);
783783
if (propertyValue.isPresent()) {
784784
if (propertyValue.get() instanceof ModelCollection) {
785-
if (((ModelCollection<?>)(propertyValue.get())).size() > 0) {
785+
if (((ModelCollection<?>)propertyValue.get()).size() > 0) {
786786
lastNotEquivalentReason = new NotEquivalentReason(
787787
NotEquivalent.COMPARE_PROPERTY_MISSING, propertyName);
788788
return false;

src/main/java/org/spdx/library/model/ModelStorageClassConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static Object modelObjectToStoredObject(Object value, String stDocumentUr
121121
return copyManager.copy(stModelStore, stDocumentUri,
122122
mValue.getModelStore(), mValue.getDocumentUri(), mValue.getId(), mValue.getType());
123123
} else {
124-
throw(new SpdxObjectNotInStoreException("Can not set a property value to a Model Object stored in a different model store"));
124+
throw new SpdxObjectNotInStoreException("Can not set a property value to a Model Object stored in a different model store");
125125
}
126126
} else {
127127
return mValue.toTypedValue();

src/main/java/org/spdx/library/model/enumerations/ChecksumAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public enum ChecksumAlgorithm implements IndividualUriValue {
4747
ADLER32("checksumAlgorithm_adler32"),
4848
;
4949

50-
private String longName;
50+
private final String longName;
5151

5252
private ChecksumAlgorithm(String longName) {
5353
this.longName = longName;

src/main/java/org/spdx/library/model/license/ExternalExtractedLicenseInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ExternalExtractedLicenseInfo(IModelStore modelStore, String documentUri,
6666
throws InvalidSPDXAnalysisException {
6767
super(modelStore, documentUri, id, copyManager, create);
6868
if (!SpdxConstants.EXTERNAL_EXTRACTED_LICENSE_PATTERN.matcher(id).matches()) {
69-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX"));
69+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX");
7070
}
7171
getExternalExtractedLicenseURI(); //this will check to make sure the external document reference is available
7272
}
@@ -78,7 +78,7 @@ public ExternalExtractedLicenseInfo(IModelStore modelStore, String documentUri,
7878
public String getExternalDocumentId() throws InvalidSPDXAnalysisException {
7979
Matcher matcher = SpdxConstants.EXTERNAL_EXTRACTED_LICENSE_PATTERN.matcher(this.getId());
8080
if (!matcher.matches()) {
81-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX"));
81+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX");
8282
}
8383
return matcher.group(1);
8484
}
@@ -90,7 +90,7 @@ public String getExternalDocumentId() throws InvalidSPDXAnalysisException {
9090
public String getExternalLicenseRef() throws InvalidSPDXAnalysisException {
9191
Matcher matcher = SpdxConstants.EXTERNAL_EXTRACTED_LICENSE_PATTERN.matcher(this.getId());
9292
if (!matcher.matches()) {
93-
throw(new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX"));
93+
throw new InvalidSPDXAnalysisException("Invalid id format for an external document reference. Must be of the form ExternalSPDXRef:LicenseRef-XXX");
9494
}
9595
return matcher.group(2);
9696
}

src/main/java/org/spdx/library/model/license/LicenseExpressionParser.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ enum Operator {
7676
static AnyLicenseInfo parseLicenseExpression(String expression, IModelStore store,
7777
String documentUri, ModelCopyManager copyManager) throws InvalidSPDXAnalysisException {
7878
if (expression == null || expression.trim().isEmpty()) {
79-
throw(new LicenseParserException("Empty license expression"));
79+
throw new LicenseParserException("Empty license expression");
8080
}
8181
Objects.requireNonNull(store, "Model store can not be null");
8282
Objects.requireNonNull(documentUri, "Document URI can not be null");
@@ -90,9 +90,9 @@ static AnyLicenseInfo parseLicenseExpression(String expression, IModelStore stor
9090
return parseLicenseExpression(tokens, store, documentUri, copyManager);
9191
} catch (LicenseParserException ex) {
9292
// Add the expression to the error message to provide additional information to the user
93-
throw(new LicenseParserException(ex.getMessage()+" License expression: '"+expression+"'", ex));
93+
throw new LicenseParserException(ex.getMessage()+" License expression: '"+expression+"'", ex);
9494
} catch (EmptyStackException ex) {
95-
throw(new LicenseParserException("Invalid license expression: '"+expression+"' - check that every operator (e.g. AND and OR) has operators and that parenthesis are matched"));
95+
throw new LicenseParserException("Invalid license expression: '"+expression+"' - check that every operator (e.g. AND and OR) has operators and that parenthesis are matched");
9696
}
9797
}
9898
}
@@ -145,7 +145,7 @@ private static void processPreToken(String preToken,
145145
private static AnyLicenseInfo parseLicenseExpression(String[] tokens, IModelStore store,
146146
String documentUri, ModelCopyManager copyManager) throws InvalidSPDXAnalysisException {
147147
if (tokens == null || tokens.length == 0) {
148-
throw(new LicenseParserException("Expected license expression"));
148+
throw new LicenseParserException("Expected license expression");
149149
}
150150
Stack<AnyLicenseInfo> operandStack = new Stack<AnyLicenseInfo>();
151151
Stack<Operator> operatorStack = new Stack<Operator>();
@@ -157,7 +157,7 @@ private static AnyLicenseInfo parseLicenseExpression(String[] tokens, IModelStor
157157
if (LEFT_PAREN.equals(token)) {
158158
int rightParenIndex = findMatchingParen(tokens, tokenIndex);
159159
if (rightParenIndex < 0) {
160-
throw(new LicenseParserException("Missing right parenthesis"));
160+
throw new LicenseParserException("Missing right parenthesis");
161161
}
162162
String[] nestedTokens = Arrays.copyOfRange(tokens, tokenIndex, rightParenIndex);
163163
operandStack.push(parseLicenseExpression(nestedTokens, store, documentUri, copyManager));
@@ -173,7 +173,7 @@ private static AnyLicenseInfo parseLicenseExpression(String[] tokens, IModelStor
173173
evaluateExpression(tosOperator, operandStack, store, documentUri, copyManager);
174174
}
175175
if (tokenIndex >= tokens.length) {
176-
throw(new LicenseParserException("Missing exception clause"));
176+
throw new LicenseParserException("Missing exception clause");
177177
}
178178
token = tokens[tokenIndex++];
179179
LicenseException licenseException = null;
@@ -189,10 +189,10 @@ private static AnyLicenseInfo parseLicenseExpression(String[] tokens, IModelStor
189189
}
190190
AnyLicenseInfo operand = operandStack.pop();
191191
if (operand == null) {
192-
throw(new LicenseParserException("Missing license for with clause"));
192+
throw new LicenseParserException("Missing license for with clause");
193193
}
194194
if (!((operand instanceof SimpleLicensingInfo) || (operand instanceof OrLaterOperator))) {
195-
throw(new LicenseParserException("License with exception is not of type SimpleLicensingInfo or OrLaterOperator"));
195+
throw new LicenseParserException("License with exception is not of type SimpleLicensingInfo or OrLaterOperator");
196196
}
197197
WithExceptionOperator weo = new WithExceptionOperator(store, documentUri, store.getNextId(IdType.Anonymous, documentUri), copyManager, true);
198198
weo.setLicense(operand);
@@ -216,7 +216,7 @@ private static AnyLicenseInfo parseLicenseExpression(String[] tokens, IModelStor
216216
}
217217
AnyLicenseInfo retval = operandStack.pop();
218218
if (!operandStack.isEmpty()) {
219-
throw(new LicenseParserException("Invalid license expression. Expecting more operands."));
219+
throw new LicenseParserException("Invalid license expression. Expecting more operands.");
220220
}
221221
return retval;
222222
}
@@ -311,7 +311,7 @@ private static void evaluateExpression(Operator operator,
311311
// unary operator
312312
AnyLicenseInfo license = operandStack.pop();
313313
if (!(license instanceof SimpleLicensingInfo)) {
314-
throw(new LicenseParserException("Missing license for the '+' or later operator"));
314+
throw new LicenseParserException("Missing license for the '+' or later operator");
315315
}
316316
OrLaterOperator olo = new OrLaterOperator(store, documentUri, store.getNextId(IdType.Anonymous, documentUri), copyManager, true);
317317
olo.setLicense((SimpleLicensingInfo)license);
@@ -321,7 +321,7 @@ private static void evaluateExpression(Operator operator,
321321
AnyLicenseInfo operand2 = operandStack.pop();
322322
AnyLicenseInfo operand1 = operandStack.pop();
323323
if (operand1 == null || operand2 == null) {
324-
throw(new LicenseParserException("Missing operands for the "+operator.toString()+" operator"));
324+
throw new LicenseParserException("Missing operands for the "+operator.toString()+" operator");
325325
}
326326
operandStack.push(evaluateBinary(operator, operand1, operand2, store, documentUri, copyManager));
327327
}
@@ -364,7 +364,7 @@ private static AnyLicenseInfo evaluateBinary(Operator tosOperator,
364364
return retval;
365365
}
366366
} else {
367-
throw(new LicenseParserException("Unknown operator "+tosOperator.toString()));
367+
throw new LicenseParserException("Unknown operator "+tosOperator.toString());
368368
}
369369
}
370370
}

src/main/java/org/spdx/library/referencetype/ListedReferenceTypes.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void loadReferenceTypeNames() {
9999
listedReferenceTypesModificationLock.readLock().lock();
100100
try {
101101
String referenceTypeNamesStr = this.listedReferenceTypeProperties.getProperty(PROPERTY_LISTED_REFERENCE_TYPES);
102-
String[] referenceTypeNamesAr = referenceTypeNamesStr.split(",");
102+
String[] referenceTypeNamesAr = referenceTypeNamesStr.split(",", -1);
103103
for (String name:referenceTypeNamesAr) {
104104
this.listedReferenceNames.add(name.trim());
105105
}
@@ -112,17 +112,15 @@ private void loadReferenceTypeNames() {
112112
* @return the listed reference types as maintained by the SPDX workgroup
113113
*/
114114
public static ListedReferenceTypes getListedReferenceTypes() {
115-
if (listedReferenceTypes == null) {
116-
listedReferenceTypesModificationLock.writeLock().lock();
117-
try {
118-
if (listedReferenceTypes == null) {
119-
listedReferenceTypes = new ListedReferenceTypes();
120-
}
121-
} finally {
122-
listedReferenceTypesModificationLock.writeLock().unlock();
115+
listedReferenceTypesModificationLock.writeLock().lock();
116+
try {
117+
if (listedReferenceTypes == null) {
118+
listedReferenceTypes = new ListedReferenceTypes();
123119
}
120+
return listedReferenceTypes;
121+
} finally {
122+
listedReferenceTypesModificationLock.writeLock().unlock();
124123
}
125-
return listedReferenceTypes;
126124
}
127125

128126
/**
@@ -167,10 +165,10 @@ public URI getListedReferenceUri(String listedReferenceName) throws InvalidSPDXA
167165
retval = new URI(SpdxConstants.SPDX_LISTED_REFERENCE_TYPES_PREFIX + listedReferenceName);
168166
} catch (URISyntaxException e) {
169167
logger.error("Error forming listed license URI",e);
170-
throw(new InvalidSPDXAnalysisException(listedReferenceName + " is not a valid SPDX listed reference type syntax."));
168+
throw new InvalidSPDXAnalysisException(listedReferenceName + " is not a valid SPDX listed reference type syntax.");
171169
}
172170
if (!isListedReferenceType(retval)) {
173-
throw(new InvalidSPDXAnalysisException(listedReferenceName + " is not a valid SPDX listed reference type."));
171+
throw new InvalidSPDXAnalysisException(listedReferenceName + " is not a valid SPDX listed reference type.");
174172
}
175173
return retval;
176174
}
@@ -196,7 +194,7 @@ public ReferenceType getListedReferenceTypeByName(String listedReferenceName) th
196194
*/
197195
public String getListedReferenceName(URI uri) throws InvalidSPDXAnalysisException {
198196
if (!this.isListedReferenceType(uri)) {
199-
throw(new InvalidSPDXAnalysisException(uri.toString() + " is not a valid URI for an SPDX listed reference type."));
197+
throw new InvalidSPDXAnalysisException(uri.toString() + " is not a valid URI for an SPDX listed reference type.");
200198
}
201199
return uri.toString().substring(SpdxConstants.SPDX_LISTED_REFERENCE_TYPES_PREFIX.length());
202200
}

0 commit comments

Comments
 (0)