Skip to content

Commit e8c120d

Browse files
committed
Add default for caseInsensitive...
This fix is necessary for campatibility for implementations using the misspelled getCaseInsensitiveId method. Note that if you reversed the default, new implementation will fail since they don't implement the misspelled version. The solution is to have default implementations for both versions. This could, however, cause a stack overflow if no implementation is provided. To avoid this, we use introspection to see if the default is used. Note that this changes the behavior of a missing interface from a compile time error to a run-time error.
1 parent 13df3e0 commit e8c120d

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/main/java/org/spdx/storage/IModelStore.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.spdx.storage;
1919

20+
import java.lang.reflect.Method;
2021
import java.util.Iterator;
2122
import java.util.List;
2223
import java.util.Optional;
@@ -221,15 +222,6 @@ boolean isPropertyValueAssignableTo(String objectUri, PropertyDescriptor propert
221222
*/
222223
IdType getIdType(String objectUri);
223224

224-
/**
225-
* In SPDX 2.2 license refs are allowed to be matched case-insensitive. This function will return
226-
* the case-sensitive ID (e.g. if you have LicenseRef-ABC, calling this function with licenseref-abc will return LicenseRef-ABC
227-
* @param nameSpace the nameSpace used for the ID - the URI is formed by the nameSpace + "#" + caseInsensitiveId
228-
* @param caseInsensitiveId ID - case will be ignored
229-
* @return the case-sensitive ID if it exists
230-
*/
231-
Optional<String> getCaseSensitiveId(String nameSpace, String caseInsensitiveId);
232-
233225
/**
234226
* Alias for getCaseSensitiveId
235227
* @param nameSpace the nameSpace used for the ID - the URI is formed by the nameSpace + "#" + caseInsensisitiveId
@@ -239,7 +231,38 @@ boolean isPropertyValueAssignableTo(String objectUri, PropertyDescriptor propert
239231
*/
240232
@Deprecated
241233
default Optional<String> getCaseSensisitiveId(String nameSpace, String caseInsensisitiveId) {
242-
return getCaseSensitiveId(nameSpace, caseInsensisitiveId);
234+
Method[] methods = this.getClass().getMethods();
235+
for (Method method:methods) {
236+
if (method.getName().equals("getCaseSensitiveId")) {
237+
if (method.isDefault()) {
238+
throw new RuntimeException("No implementation for getCaseSensitiveId in a ModelStore implementation " + this.getClass());
239+
} else {
240+
return getCaseSensitiveId(nameSpace, caseInsensisitiveId);
241+
}
242+
}
243+
}
244+
throw new RuntimeException("Could not find implementation for getCaseSensitiveId in a ModelStore implementation " + this.getClass());
245+
}
246+
247+
/**
248+
* In SPDX 2.2 license refs are allowed to be matched case-insensitive. This function will return
249+
* the case-sensitive ID (e.g. if you have LicenseRef-ABC, calling this function with licenseref-abc will return LicenseRef-ABC
250+
* @param nameSpace the nameSpace used for the ID - the URI is formed by the nameSpace + "#" + caseInsensitiveId
251+
* @param caseInsensitiveId ID - case will be ignored
252+
* @return the case-sensitive ID if it exists
253+
*/
254+
default Optional<String> getCaseSensitiveId(String nameSpace, String caseInsensitiveId) {
255+
Method[] methods = this.getClass().getMethods();
256+
for (Method method:methods) {
257+
if (method.getName().equals("getCaseSensisitiveId")) {
258+
if (method.isDefault()) {
259+
throw new RuntimeException("No implementation for getCaseSensitiveId in a ModelStore implementation " + this.getClass());
260+
} else {
261+
return getCaseSensisitiveId(nameSpace, caseInsensitiveId);
262+
}
263+
}
264+
}
265+
throw new RuntimeException("Could not find implementation for getCaseSensitiveId in a ModelStore implementation " + this.getClass());
243266
}
244267

245268
/**

0 commit comments

Comments
 (0)