Skip to content

Commit

Permalink
Allow private methods to inherit default annotations from package or …
Browse files Browse the repository at this point in the history
…class scope (#1222)

* refs #374: reproduce reported problem

* allow private methods to inherit default annotations

Remove the restriction that a method must not be private in order to
inherit default annotations from package or class scope.

Resolves #374.

* bump year in license header

* add a comment in changelog as per the PR template

* ./gradlew spotlessApply

* address pr comments

- fix copyright attribution
- move entry in changelog

Co-authored-by: Ritee <chenh0510@qq.com>
  • Loading branch information
cwbriones and RitChan committed Aug 17, 2020
1 parent 6084471 commit 72f9185
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
### Fixed
* [A meaningless exception data from `SAXBugCollectionHandler`](https://lgtm.com/projects/g/spotbugs/spotbugs/rev/a77ab08634687b7791e902636996ab6184462693)
* Use URI for files instead of converting string to URI each time. Fixes tests on Windows.
* Allow private methods to inherit default annotations from package or class scope. ([#374](https://github.com/spotbugs/spotbugs/issues/374))

### Added
* Implement [issue 390](https://github.com/spotbugs/spotbugs/issues/390) as a detector, `DontAssertInstanceofInTests`, which reports bugs of type `JUA_DONT_ASSERT_INSTANCEOF_IN_TESTS`.
Expand Down
@@ -0,0 +1,46 @@
/*
* Contributions to SpotBugs
* Copyright (C) 2020, ritchan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.AbstractIntegrationTest;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder;
import org.junit.Test;

import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly;
import static org.junit.Assert.assertThat;


/**
* @see <a href="https://github.com/spotbugs/spotbugs/issues/374">GitHub issue</a>
*/
public class Issue374Test extends AbstractIntegrationTest {
@Test
public void test() {
performAnalysis(
"ghIssues/issue374/package-info.class",
"ghIssues/issue374/ClassLevel.class",
"ghIssues/issue374/MethodLevel.class",
"ghIssues/issue374/PackageLevel.class");
BugInstanceMatcher matcher = new BugInstanceMatcherBuilder()
.bugType("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
.build();
assertThat(getBugCollection(), containsExactly(3, matcher));
}
}
Expand Up @@ -986,11 +986,6 @@ private static TypeQualifierAnnotation computeEffectiveTypeQualifierAnnotation(T
// default annotations
}

/** private methods don't inherit from class or package scope */
if (xmethod.isPrivate()) {
stopAtMethodScope = true;
}

boolean stopAtClassScope = false;

if (!xmethod.isPublic() && !xmethod.isProtected() && (xmethod.isStatic() || "<init>".equals(xmethod.getName()))) {
Expand Down
19 changes: 19 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/issue374/ClassLevel.java
@@ -0,0 +1,19 @@
package ghIssues.issue374;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class ClassLevel {
public String method() {
return methodNullable(null);
}

private String methodNullable(@Nullable final String test) {
return methodNonNull(test);
}

private String methodNonNull(final String test) {
return test;
}
}
19 changes: 19 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/issue374/MethodLevel.java
@@ -0,0 +1,19 @@
package ghIssues.issue374;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

public class MethodLevel {
public String method() {
return methodNullable(null);
}

private String methodNullable(@Nullable final String test) {
return methodNonNull(test);
}

@ParametersAreNonnullByDefault
private String methodNonNull(final String test) {
return test;
}
}
17 changes: 17 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/issue374/PackageLevel.java
@@ -0,0 +1,17 @@
package ghIssues.issue374;

import javax.annotation.Nullable;

public class PackageLevel {
public String method() {
return methodNullable(null);
}

private String methodNullable(@Nullable final String test) {
return methodNonNull(test);
}

private String methodNonNull(final String test) {
return test;
}
}
@@ -0,0 +1,5 @@
/*
* An example to reproduce problem at https://github.com/spotbugs/spotbugs/issues/374
*/
@javax.annotation.ParametersAreNonnullByDefault
package ghIssues.issue374;

0 comments on commit 72f9185

Please sign in to comment.