Skip to content

Commit bb49f7d

Browse files
authored
Merge pull request #817 from Stypox/checkstyle
Add checkstyle
2 parents 9284569 + 9c07e8a commit bb49f7d

File tree

175 files changed

+3224
-2277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+3224
-2277
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ jobs:
4040
echo running with mock downloader
4141
./gradlew check --stacktrace -Ddownloader=MOCK
4242
fi
43+
44+
- name: Upload test reports when failure occurs
45+
uses: actions/upload-artifact@v3
46+
if: failure()
47+
with:
48+
name: NewPipeExtractor-test-reports
49+
path: extractor/build/reports/tests/test/**

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ allprojects {
3030
nanojsonVersion = "1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751"
3131
spotbugsVersion = "4.6.0"
3232
junitVersion = "5.8.2"
33+
checkstyleVersion = "9.3" // do not use latest version (10.0) as it requires compile JDK 11
3334
}
3435
}
3536

checkstyle/checkstyle.xml

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="Checker">
6+
<!--
7+
If you set the basedir property below, then all reported file
8+
names will be relative to the specified directory. See
9+
https://checkstyle.org/5.x/config.html#Checker
10+
11+
<property name="basedir" value="${basedir}"/>
12+
-->
13+
<property name="severity" value="error"/>
14+
15+
<property name="fileExtensions" value="java, properties, xml"/>
16+
17+
<!-- Excludes all 'module-info.java' files -->
18+
<!-- See https://checkstyle.org/config_filefilters.html -->
19+
<module name="BeforeExecutionExclusionFileFilter">
20+
<property name="fileNamePattern" value="module\-info\.java$"/>
21+
</module>
22+
23+
<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
24+
<module name="SuppressionFilter">
25+
<property name="file" value="${config_loc}/suppressions.xml" />
26+
<property name="optional" value="true"/>
27+
</module>
28+
29+
<!-- Checks that a package-info.java file exists for each package. -->
30+
<!-- See https://checkstyle.org/config_javadoc.html#JavadocPackage -->
31+
<!--<module name="JavadocPackage"/>-->
32+
33+
<!-- Checks whether files end with a new line. -->
34+
<!-- See https://checkstyle.org/config_misc.html#NewlineAtEndOfFile -->
35+
<module name="NewlineAtEndOfFile"/>
36+
37+
<!-- Checks that property files contain the same keys. -->
38+
<!-- See https://checkstyle.org/config_misc.html#Translation -->
39+
<module name="Translation"/>
40+
41+
<!-- Checks for Size Violations. -->
42+
<!-- See https://checkstyle.org/config_sizes.html -->
43+
<module name="FileLength"/>
44+
<module name="LineLength">
45+
<property name="max" value="100"/>
46+
<property name="fileExtensions" value="java"/>
47+
</module>
48+
49+
<!-- Checks for whitespace -->
50+
<!-- See https://checkstyle.org/config_whitespace.html -->
51+
<module name="FileTabCharacter"/>
52+
53+
<!-- Miscellaneous other checks. -->
54+
<!-- See https://checkstyle.org/config_misc.html -->
55+
<module name="RegexpSingleline">
56+
<property name="format" value="\s+$"/>
57+
<property name="minimum" value="0"/>
58+
<property name="maximum" value="0"/>
59+
<property name="message" value="Line has trailing spaces."/>
60+
</module>
61+
62+
<!-- Checks for Headers -->
63+
<!-- See https://checkstyle.org/config_header.html -->
64+
<!-- <module name="Header"> -->
65+
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
66+
<!-- <property name="fileExtensions" value="java"/> -->
67+
<!-- </module> -->
68+
69+
<module name="SuppressWarningsFilter" />
70+
71+
<module name="TreeWalker">
72+
<!-- Checks for Javadoc comments. -->
73+
<!-- See https://checkstyle.org/config_javadoc.html -->
74+
<module name="InvalidJavadocPosition"/>
75+
<module name="JavadocMethod">
76+
<property name="allowMissingParamTags" value="true"/>
77+
<property name="allowMissingReturnTag" value="true"/>
78+
</module>
79+
<module name="JavadocType"/>
80+
<!--<module name="JavadocVariable"/>-->
81+
<module name="JavadocStyle">
82+
<property name="checkFirstSentence" value="false"/>
83+
</module>
84+
<!--<module name="MissingJavadocMethod"/>-->
85+
86+
<!-- Checks for Naming Conventions. -->
87+
<!-- See https://checkstyle.org/config_naming.html -->
88+
<module name="ConstantName"/>
89+
<module name="LocalFinalVariableName"/>
90+
<module name="LocalVariableName"/>
91+
<module name="MemberName">
92+
<property name="format" value="^(TAG|DEBUG|[a-z][a-zA-Z0-9]*)$"/>
93+
</module>
94+
<module name="MethodName"/>
95+
<module name="PackageName"/>
96+
<module name="ParameterName"/>
97+
<module name="StaticVariableName"/>
98+
<module name="TypeName"/>
99+
100+
<!-- Checks for imports -->
101+
<!-- See https://checkstyle.org/config_import.html -->
102+
<module name="AvoidStarImport"/>
103+
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
104+
<module name="RedundantImport"/>
105+
<module name="UnusedImports"/>
106+
107+
<!-- Checks for Size Violations. -->
108+
<!-- See https://checkstyle.org/config_sizes.html -->
109+
<module name="MethodLength">
110+
<property name="severity" value="warning"/>
111+
</module>
112+
<module name="ParameterNumber">
113+
<property name="severity" value="warning"/>
114+
</module>
115+
116+
<!-- Checks for whitespace -->
117+
<!-- See https://checkstyle.org/config_whitespace.html -->
118+
<module name="EmptyForIteratorPad"/>
119+
<module name="GenericWhitespace"/>
120+
<module name="MethodParamPad"/>
121+
<module name="NoWhitespaceAfter"/>
122+
<module name="NoWhitespaceBefore"/>
123+
<module name="OperatorWrap"/>
124+
<module name="ParenPad"/>
125+
<module name="TypecastParenPad"/>
126+
<module name="WhitespaceAfter"/>
127+
<module name="WhitespaceAround"/>
128+
129+
<!-- Modifier Checks -->
130+
<!-- See https://checkstyle.org/config_modifiers.html -->
131+
<module name="ModifierOrder"/>
132+
<module name="RedundantModifier"/>
133+
134+
<!-- Checks for blocks. You know, those {}'s -->
135+
<!-- See https://checkstyle.org/config_blocks.html -->
136+
<module name="AvoidNestedBlocks"/>
137+
<module name="EmptyBlock"/>
138+
<module name="LeftCurly"/>
139+
<module name="NeedBraces"/>
140+
<module name="RightCurly"/>
141+
142+
<!-- Checks for common coding problems -->
143+
<!-- See https://checkstyle.org/config_coding.html -->
144+
<module name="EmptyStatement"/>
145+
<module name="EqualsHashCode">
146+
<property name="severity" value="warning"/>
147+
</module>
148+
<module name="HiddenField">
149+
<property name="ignoreConstructorParameter" value="true"/>
150+
<property name="ignoreSetter" value="true"/>
151+
</module>
152+
<module name="IllegalInstantiation"/>
153+
<module name="InnerAssignment"/>
154+
<!--<module name="MagicNumber"/>-->
155+
<!--<module name="MissingSwitchDefault">
156+
<property name="severity" value="warning"/>
157+
</module>-->
158+
<module name="MultipleVariableDeclarations"/>
159+
<module name="SimplifyBooleanExpression"/>
160+
<module name="SimplifyBooleanReturn"/>
161+
<module name="FinalLocalVariable">
162+
<property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
163+
<property name="validateEnhancedForLoopVariable" value="true"/>
164+
</module>
165+
166+
<!-- Checks for class design -->
167+
<!-- See https://checkstyle.org/config_design.html -->
168+
<!--<module name="DesignForExtension"/>-->
169+
<module name="FinalClass"/>
170+
<module name="HideUtilityClassConstructor"/>
171+
<module name="InterfaceIsType"/>
172+
<!--<module name="VisibilityModifier">
173+
<property name="ignoreAnnotationCanonicalNames" value="State,ColumnInfo"/>
174+
<property name="severity" value="warning"/>
175+
</module>-->
176+
177+
<!-- Miscellaneous other checks. -->
178+
<!-- See https://checkstyle.org/config_misc.html -->
179+
<module name="ArrayTypeStyle"/>
180+
<module name="FinalParameters"/>
181+
<!--<module name="TodoComment">
182+
<property name="format" value="(TODO:|FIXME:)"/>
183+
<property name="severity" value="warning"/>
184+
</module>-->
185+
<module name="UpperEll"/>
186+
187+
<module name="SuppressWarningsHolder" />
188+
</module>
189+
</module>

checkstyle/suppressions.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE suppressions PUBLIC
3+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
4+
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
5+
<suppressions>
6+
<!-- Use @SuppressWarnings("...") if it is possible, only use this file if it is not -->
7+
8+
<suppress checks="LineLength"
9+
files="BandcampExtractorHelper.java"
10+
lines="54"/>
11+
12+
<suppress checks="LineLength"
13+
files="ItagItem.java"
14+
lines="19"/>
15+
</suppressions>

extractor/build.gradle

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
plugins {
2+
id 'checkstyle'
3+
}
4+
15
test {
2-
// Pass on downloader type to tests for different CI jobs. See DownloaderFactory.java and ci.yml
6+
// Pass on downloader type to tests for different CI jobs. See DownloaderFactory.java and ci.yml
37
if (System.properties.containsKey('downloader')) {
48
systemProperty('downloader', System.getProperty('downloader'))
59
}
610
useJUnitPlatform()
11+
dependsOn checkstyleMain // run checkstyle when testing
12+
}
13+
14+
checkstyle {
15+
getConfigDirectory().set(rootProject.file("checkstyle"))
16+
ignoreFailures false
17+
showViolations true
18+
toolVersion checkstyleVersion
19+
}
20+
21+
checkstyleTest {
22+
enabled false // do not checkstyle test files
723
}
824

925
dependencies {
@@ -15,6 +31,8 @@ dependencies {
1531
implementation "com.github.spotbugs:spotbugs-annotations:$spotbugsVersion"
1632
implementation 'org.nibor.autolink:autolink:0.10.0'
1733

34+
checkstyle "com.puppycrawl.tools:checkstyle:$checkstyleVersion"
35+
1836
testImplementation platform("org.junit:junit-bom:$junitVersion")
1937
testImplementation 'org.junit.jupiter:junit-jupiter-api'
2038
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
import javax.annotation.Nonnull;
1212
import javax.annotation.Nullable;
13+
1314
import java.io.IOException;
1415
import java.util.Objects;
1516

1617
public abstract class Extractor {
1718
/**
1819
* {@link StreamingService} currently related to this extractor.<br>
19-
* Useful for getting other things from a service (like the url handlers for cleaning/accepting/get id from urls).
20+
* Useful for getting other things from a service (like the url handlers for
21+
* cleaning/accepting/get id from urls).
2022
*/
2123
private final StreamingService service;
2224
private final LinkHandler linkHandler;
@@ -27,16 +29,18 @@ public abstract class Extractor {
2729
private ContentCountry forcedContentCountry = null;
2830

2931
private boolean pageFetched = false;
32+
// called like this to prevent checkstyle errors about "hiding a field"
3033
private final Downloader downloader;
3134

32-
public Extractor(final StreamingService service, final LinkHandler linkHandler) {
35+
protected Extractor(final StreamingService service, final LinkHandler linkHandler) {
3336
this.service = Objects.requireNonNull(service, "service is null");
3437
this.linkHandler = Objects.requireNonNull(linkHandler, "LinkHandler is null");
3538
this.downloader = Objects.requireNonNull(NewPipe.getDownloader(), "downloader is null");
3639
}
3740

3841
/**
39-
* @return The {@link LinkHandler} of the current extractor object (e.g. a ChannelExtractor should return a channel url handler).
42+
* @return The {@link LinkHandler} of the current extractor object (e.g. a ChannelExtractor
43+
* should return a channel url handler).
4044
*/
4145
@Nonnull
4246
public LinkHandler getLinkHandler() {
@@ -50,13 +54,17 @@ public LinkHandler getLinkHandler() {
5054
* @throws ExtractionException if the pages content is not understood
5155
*/
5256
public void fetchPage() throws IOException, ExtractionException {
53-
if (pageFetched) return;
57+
if (pageFetched) {
58+
return;
59+
}
5460
onFetchPage(downloader);
5561
pageFetched = true;
5662
}
5763

5864
protected void assertPageFetched() {
59-
if (!pageFetched) throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()");
65+
if (!pageFetched) {
66+
throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()");
67+
}
6068
}
6169

6270
protected boolean isPageFetched() {
@@ -66,11 +74,13 @@ protected boolean isPageFetched() {
6674
/**
6775
* Fetch the current page.
6876
*
69-
* @param downloader the download to use
77+
* @param downloader the downloader to use
7078
* @throws IOException if the page can not be loaded
7179
* @throws ExtractionException if the pages content is not understood
7280
*/
73-
public abstract void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException;
81+
@SuppressWarnings("HiddenField")
82+
public abstract void onFetchPage(@Nonnull Downloader downloader)
83+
throws IOException, ExtractionException;
7484

7585
@Nonnull
7686
public String getId() throws ParsingException {
@@ -118,11 +128,11 @@ public Downloader getDownloader() {
118128
// Localization
119129
//////////////////////////////////////////////////////////////////////////*/
120130

121-
public void forceLocalization(Localization localization) {
131+
public void forceLocalization(final Localization localization) {
122132
this.forcedLocalization = localization;
123133
}
124134

125-
public void forceContentCountry(ContentCountry contentCountry) {
135+
public void forceContentCountry(final ContentCountry contentCountry) {
126136
this.forcedContentCountry = contentCountry;
127137
}
128138

@@ -133,7 +143,8 @@ public Localization getExtractorLocalization() {
133143

134144
@Nonnull
135145
public ContentCountry getExtractorContentCountry() {
136-
return forcedContentCountry == null ? getService().getContentCountry() : forcedContentCountry;
146+
return forcedContentCountry == null ? getService().getContentCountry()
147+
: forcedContentCountry;
137148
}
138149

139150
@Nonnull

0 commit comments

Comments
 (0)