Skip to content

Commit

Permalink
Issue checkstyle#10751: Add ability to suppress forbidden API by anno…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
shashwatj07 committed Aug 29, 2021
1 parent 87a8533 commit 1b89123
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<!-- until https://github.com/checkstyle/checkstyle/issues/5234 -->
<suppress id="MatchXPathBranchContains" files="[\\/]DetailAstImplTest.java"/>

<!-- Needed for readibility. -->
<suppress checks="CyclomaticComplexity" files="[\\/]AllTestsTest.java"/>

<!-- until https://github.com/checkstyle/checkstyle/issues/9142 -->
<suppress id="ImportControlTest"
files="[\\/]src[\\/]it[\\/]java[\\/]org[\\/]checkstyle[\\/]base[\\/]AbstractItModuleTestSupport.java"/>
Expand Down
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,9 @@
<signaturesFiles>
<signaturesFile>${basedir}/config/signatures-test.txt</signaturesFile>
</signaturesFiles>
<suppressAnnotations>
<suppressAnnotation>com.puppycrawl.tools.checkstyle.SuppressForbiddenApi</suppressAnnotation>
</suppressAnnotations>
<excludes>
<exclude>**/Input*</exclude>
<!-- usage of system output by design -->
Expand Down Expand Up @@ -1582,7 +1585,7 @@

<exclude>**/SuppressWithNearbyCommentFilterTest.class</exclude>

<exclude>**/PackageDeclarationCheckTest.class</exclude>
<!-- <exclude>**/PackageDeclarationCheckTest.class</exclude> -->

<exclude>**/AbstractModuleTestSupport.class</exclude>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2021 the original author or authors.
//
// 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 com.puppycrawl.tools.checkstyle;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Custom annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface SuppressForbiddenApi {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.SuppressForbiddenApi;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

public class PackageDeclarationCheckTest extends AbstractModuleTestSupport {
Expand Down Expand Up @@ -151,6 +152,7 @@ public void testNoPackage() throws Exception {
expected);
}

@SuppressForbiddenApi
@Test
public void testEmptyFile() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ private static void verifyHasProductionFile(Map<String, List<String>> allTests,
&& !"XpathMapper.java".equals(fileName)
// JavadocMetadataScraper and related classes are temporarily hosted in test
&& !file.getPath().contains("meta")
// InlineConfigParser is hosted in test
&& !file.getPath().contains("bdd")) {
// Inline Config Parser is hosted in test
&& !file.getPath().contains("bdd")
// Annotation to suppress invocation of forbidden apis
&& !"SuppressForbiddenApi.java".equals(fileName)) {
final String path;

try {
Expand Down

0 comments on commit 1b89123

Please sign in to comment.