Skip to content

Commit

Permalink
Recognize OpenDaylight's Immutable interface
Browse files Browse the repository at this point in the history
OpenDaylight has a type-safe marker interface which acts as API contract
specification of effective immutability. Recognize this contract as
equivalent to an inherited @immutable annotation. Fixes spotbugs#1697.

Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
  • Loading branch information
rovarga committed Sep 15, 2021
1 parent 4f8f660 commit 2cf5b6f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
`SW_SWING_METHODS_INVOKED_IN_SWING_THREAD` ([#1664](https://github.com/spotbugs/spotbugs/pull/1664))
- Wrong description of the `SE_TRANSIENT_FIELD_OF_NONSERIALIZABLE_CLASS` ([#1664](https://github.com/spotbugs/spotbugs/pull/1664))
- Treat types with `@com.google.errorprone.annotations.Immutable` as immutable ([#1705](https://github.com/spotbugs/spotbugs/pull/1705))
- Treat types which implement `org.opendaylight.yangtools.concepts.Immutable` as immutable ([#1697](https://github.com/spotbugs/spotbugs/issues/1697))

## 4.4.1 - 2021-09-07
### Changed
Expand Down
1 change: 1 addition & 0 deletions spotbugs/build.gradle
Expand Up @@ -98,6 +98,7 @@ dependencies {
testImplementation 'org.apache.ant:ant:1.10.11'
testImplementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.14.0'
testImplementation 'com.google.errorprone:error_prone_annotations:2.9.0'
testImplementation 'org.opendaylight.yangtools:concepts:7.0.7'

guiImplementation sourceSets.main.runtimeClasspath
guiCompileOnly 'com.apple:AppleJavaExtensions:1.4'
Expand Down
Expand Up @@ -171,6 +171,14 @@ private boolean computeByImmutableContract() {
}
}

// We are not considering transitive interfaces for now to side-step class loading
for (String iface : cls.getInterfaceNames()) {
// OpenDaylight's Immutable interface is an explicit contract
if (iface.equals("org.opendaylight.yangtools.concepts.Immutable")) {
return true;
}
}

final ClassAnalysis maybeSuper = getSuperAnalysis();
return maybeSuper != null && maybeSuper.isImmutableByContract();
}
Expand Down
Expand Up @@ -116,4 +116,24 @@ public void TestErrorProneImmutable() {
Assert.assertFalse(MutableClasses.mutableSignature(
"Ledu/umd/cs/findbugs/util/MutableClassesTest$ErrorProneImmutableSubclass;"));
}

public static class OpenDaylightImmutable implements org.opendaylight.yangtools.concepts.Immutable {
public void clear() {
// Does not matter
}
}

public static class OpenDaylightImmutableSubclass extends OpenDaylightImmutable {
public String put(String str) {
return "";
}
}

@Test
public void TestOpenDaylightImmutable() {
Assert.assertFalse(MutableClasses.mutableSignature(
"Ledu/umd/cs/findbugs/util/MutableClassesTest$OpenDaylightImmutable;"));
Assert.assertFalse(MutableClasses.mutableSignature(
"Ledu/umd/cs/findbugs/util/MutableClassesTest$OpenDaylightImmutableSubclass;"));
}
}

0 comments on commit 2cf5b6f

Please sign in to comment.