Skip to content

Commit 1a45e3d

Browse files
committed
[FABCJ-95] Checkstyle
- Implemented Checkstyle and fixed all warnings - Azure pipeline will fail if checkstyle finds errors - Checkstyle reported published to the build artifacts Signed-off-by: Matthew B White <[email protected]>
1 parent 8db10ca commit 1a45e3d

File tree

155 files changed

+5385
-4340
lines changed

Some content is hidden

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

155 files changed

+5385
-4340
lines changed

ci/azure-pipelines.yml

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ stages:
6666
publishJUnitResults: true
6767
testResultsFiles: "$(System.DefaultWorkingDirectory)/**/TEST-*.xml"
6868
tasks: "build"
69+
- task: PublishBuildArtifacts@1
70+
condition: or(succeeded(), failed())
71+
inputs:
72+
pathToPublish: fabric-chaincode-shim/build/reports/checkstyle/
73+
artifactName: checkstylereport
74+
displayName: 'Checkstyle'
75+
continueOnError: true
6976
- task: PublishTestResults@2
7077
inputs:
7178
testResultsFormat: 'JUnit'

ci/checkstyle/checkstyle.xml

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Check Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
3+
4+
<!--
5+
This configuration file was written by the eclipse-cs plugin configuration editor
6+
-->
7+
<!--
8+
Checkstyle-Configuration: Fabric
9+
Description: none
10+
-->
11+
<module name="Checker">
12+
<property name="severity" value="error"/>
13+
<property name="basedir" value="${basedir}"/>
14+
<property name="fileExtensions" value="java, properties, xml"/>
15+
<module name="TreeWalker">
16+
<module name="SuppressWithNearbyCommentFilter">
17+
<property name="commentFormat" value="checkstyle:ignore-line:(\w+(\|\w+)*)"/>
18+
<property name="checkFormat" value="$1"/>
19+
</module>
20+
<module name="SuppressWithNearbyCommentFilter">
21+
<property name="commentFormat" value="checkstyle:ignore-next-line:(\w+(\|\w+)*)"/>
22+
<property name="checkFormat" value="$1"/>
23+
<property name="influenceFormat" value="1"/>
24+
</module>
25+
<module name="InvalidJavadocPosition"/>
26+
<module name="JavadocMethod"/>
27+
<module name="JavadocType"/>
28+
<module name="JavadocVariable">
29+
<property name="scope" value="protected"/>
30+
</module>
31+
<module name="JavadocStyle"/>
32+
<module name="MissingJavadocMethod"/>
33+
<module name="ConstantName"/>
34+
<module name="LocalFinalVariableName"/>
35+
<module name="LocalVariableName"/>
36+
<module name="MemberName"/>
37+
<module name="MethodName"/>
38+
<module name="PackageName"/>
39+
<module name="ParameterName"/>
40+
<module name="StaticVariableName"/>
41+
<module name="TypeName"/>
42+
<module name="AvoidStarImport"/>
43+
<module name="IllegalImport"/>
44+
<module name="RedundantImport"/>
45+
<module name="UnusedImports">
46+
<property name="processJavadoc" value="false"/>
47+
</module>
48+
<module name="MethodLength"/>
49+
<module name="ParameterNumber"/>
50+
<module name="GenericWhitespace"/>
51+
<module name="MethodParamPad"/>
52+
<module name="NoWhitespaceAfter"/>
53+
<module name="NoWhitespaceBefore"/>
54+
<module name="OperatorWrap"/>
55+
<module name="ParenPad"/>
56+
<module name="TypecastParenPad"/>
57+
<module name="WhitespaceAfter"/>
58+
<module name="WhitespaceAround"/>
59+
<module name="ModifierOrder"/>
60+
<module name="RedundantModifier"/>
61+
<module name="AvoidNestedBlocks"/>
62+
<module name="EmptyBlock"/>
63+
<module name="LeftCurly"/>
64+
<module name="NeedBraces"/>
65+
<module name="RightCurly"/>
66+
<module name="EmptyStatement"/>
67+
<module name="EqualsHashCode"/>
68+
<module name="IllegalInstantiation"/>
69+
<module name="InnerAssignment"/>
70+
<module name="MagicNumber">
71+
<property name="ignoreHashCodeMethod" value="true"/>
72+
</module>
73+
<module name="MissingSwitchDefault"/>
74+
<module name="MultipleVariableDeclarations"/>
75+
<module name="SimplifyBooleanExpression"/>
76+
<module name="SimplifyBooleanReturn"/>
77+
<module name="DesignForExtension"/>
78+
<module name="FinalClass"/>
79+
<module name="HideUtilityClassConstructor"/>
80+
<module name="InterfaceIsType"/>
81+
<module name="VisibilityModifier">
82+
<property name="protectedAllowed" value="true"/>
83+
</module>
84+
<module name="CyclomaticComplexity">
85+
<property name="switchBlockAsSingleDecisionPoint" value="true"/>
86+
<property name="max" value="13"/>
87+
</module>
88+
<module name="ArrayTypeStyle"/>
89+
<module name="FinalParameters"/>
90+
<module name="TodoComment"/>
91+
<module name="UpperEll"/>
92+
</module>
93+
<module name="BeforeExecutionExclusionFileFilter">
94+
<property name="fileNamePattern" value="module\-info\.java$"/>
95+
</module>
96+
<module name="SuppressionSingleFilter">
97+
<property name="files" value="src/test/"/>
98+
<property name="checks" value=".*Javadoc.*"/>
99+
</module>
100+
<module name="SuppressionSingleFilter">
101+
<property name="files" value="src/test/java/"/>
102+
<property name="checks" value="MagicNumber.*"/>
103+
</module>
104+
<module name="SuppressionSingleFilter">
105+
<property name="files" value="src/main/java/.*(.*/)?/impl/.*"/>
106+
<property name="checks" value="JavadocPackage.*"/>
107+
</module>
108+
<module name="JavadocPackage"/>
109+
<module name="NewlineAtEndOfFile"/>
110+
<module name="Translation"/>
111+
<module name="FileLength"/>
112+
<module name="LineLength">
113+
<property name="fileExtensions" value="java"/>
114+
<property name="ignorePattern" value="^ +\* +"/>
115+
<property name="max" value="160"/>
116+
</module>
117+
<module name="FileTabCharacter"/>
118+
<module name="RegexpSingleline">
119+
<property name="format" value="\s+$"/>
120+
<property name="message" value="Line has trailing spaces."/>
121+
</module>
122+
<module name="RegexpHeader">
123+
<property name="headerFile" value="${config_loc}/java-copyright-header.txt"/>
124+
<property name="fileExtensions" value="java"/>
125+
</module>
126+
</module>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
^/\*$
2+
^ \* Copyright \d\d\d\d .*? All Rights Reserved\.$
3+
^ \*$
4+
^ \* SPDX-License-Identifier: Apache-2\.0$
5+
^ \*/$

fabric-chaincode-docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ RUN gradle \
3939
fabric-chaincode-shim:install \
4040
fabric-chaincode-shim:publishToMavenLocal \
4141
-x javadoc \
42-
-x test
42+
-x test \
43+
-x checkstyleMain -x checkstyleTest
4344

4445
# Installing all protos jar dependencies to maven local
4546
WORKDIR /root/chaincode-java/shim-src/fabric-chaincode-protos/build/publications/protosJar/

fabric-chaincode-shim/build.gradle

+17
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,27 @@ plugins {
1717
id 'jacoco'
1818
id 'maven'
1919
id 'signing'
20+
id 'checkstyle'
2021
}
2122

2223
apply plugin: 'org.owasp.dependencycheck'
2324

25+
26+
checkstyle {
27+
toolVersion '8.27'
28+
configFile file("../ci/checkstyle/checkstyle.xml")
29+
configProperties = [config_loc: file("../ci/checkstyle") ]
30+
}
31+
checkstyleMain {
32+
source ='src/main/java'
33+
}
34+
checkstyleTest {
35+
source ='src/test/java'
36+
}
37+
38+
39+
40+
2441
check.dependsOn dependencyCheckAnalyze
2542

2643
tasks.withType(org.gradle.api.tasks.testing.Test) {

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java

+42-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
Copyright IBM Corp. All Rights Reserved.
3-
4-
SPDX-License-Identifier: Apache-2.0
5-
*/
2+
* Copyright 2019 IBM All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
66
package org.hyperledger.fabric;
77

88
import java.io.PrintWriter;
@@ -12,53 +12,79 @@
1212
import java.util.logging.LogManager;
1313

1414
/**
15-
* Logger class to use throughout the Contract Implementation
15+
* Logger class to use throughout the Contract Implementation.
1616
*
1717
*/
1818
public class Logger extends java.util.logging.Logger {
1919

20-
protected Logger(String name) {
20+
protected Logger(final String name) {
2121
super(name, null);
2222

2323
// ensure that the parent logger is set
2424
this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
2525
}
2626

27-
public static Logger getLogger(String name) {
27+
/**
28+
* @param name
29+
* @return Logger
30+
*/
31+
public static Logger getLogger(final String name) {
2832
return new Logger(name);
2933
}
3034

31-
public void debug(Supplier<String> msgSupplier) {
35+
/**
36+
* @param msgSupplier
37+
*/
38+
public void debug(final Supplier<String> msgSupplier) {
3239
log(Level.FINEST, msgSupplier);
3340
}
3441

35-
public void debug(String msg) {
42+
/**
43+
* @param msg
44+
*/
45+
public void debug(final String msg) {
3646
log(Level.FINEST, msg);
3747
}
3848

39-
public static Logger getLogger(Class<?> class1) {
49+
/**
50+
* @param class1
51+
* @return Logger
52+
*/
53+
public static Logger getLogger(final Class<?> class1) {
4054
// important to add the logger to the log manager
41-
Logger l = Logger.getLogger(class1.getName());
55+
final Logger l = Logger.getLogger(class1.getName());
4256
LogManager.getLogManager().addLogger(l);
4357
return l;
4458
}
4559

46-
public void error(String message) {
60+
/**
61+
* @param message
62+
*/
63+
public void error(final String message) {
4764
log(Level.SEVERE, message);
4865
}
4966

50-
public void error(Supplier<String> msgSupplier) {
67+
/**
68+
* @param msgSupplier
69+
*/
70+
public void error(final Supplier<String> msgSupplier) {
5171
log(Level.SEVERE, msgSupplier);
5272
}
5373

54-
public String formatError(Throwable throwable) {
55-
if (throwable == null)
74+
/**
75+
* @param throwable
76+
* @return Throwable
77+
*/
78+
public String formatError(final Throwable throwable) {
79+
if (throwable == null) {
5680
return null;
81+
}
82+
5783
final StringWriter buffer = new StringWriter();
5884
buffer.append(throwable.getMessage());
5985
throwable.printStackTrace(new PrintWriter(buffer));
6086

61-
Throwable cause = throwable.getCause();
87+
final Throwable cause = throwable.getCause();
6288
if (cause != null) {
6389
buffer.append(".. caused by ..");
6490
buffer.append(this.formatError(cause));

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java

+34-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
Copyright IBM Corp. All Rights Reserved.
3-
4-
SPDX-License-Identifier: Apache-2.0
5-
*/
2+
* Copyright 2019 IBM All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
66
package org.hyperledger.fabric;
77

88
import java.io.PrintWriter;
@@ -14,26 +14,36 @@
1414

1515
/**
1616
* Assistance class to use when logging.
17-
*
17+
*
1818
* For chaincode/contract implementations please use java.util.logging or your
1919
* own framework. All the Hyperledger Fabric code here is logged in loggers with
2020
* names starting org.hyperledger
2121
*
2222
* Control of this is via the environment variables
2323
* 'CORE_CHAINCODE_LOGGING_LEVEL' this takes a string that matches the following
2424
* Java.util.logging levels (case insensitive)
25-
*
25+
*
2626
* CRITICAL, ERROR == Level.SEVERE, WARNING == Level.WARNING, INFO == Level.INFO
2727
* NOTICE == Level.CONFIG, DEBUG == Level.FINEST
28-
*
28+
*
2929
*/
30-
public class Logging {
30+
public final class Logging {
3131

32+
/**
33+
* Name of the Performance logger.
34+
*/
3235
public static final String PERFLOGGER = "org.hyperledger.Performance";
3336

37+
/** Private Constructor.
38+
*
39+
*/
40+
private Logging() {
41+
42+
}
43+
3444
/**
35-
* Formats a Throwable to a string with details of all the causes as well
36-
*
45+
* Formats a Throwable to a string with details of all the causes.
46+
*
3747
* @param throwable Exception
3848
* @return String formatted with all the details
3949
*/
@@ -43,7 +53,7 @@ public static String formatError(final Throwable throwable) {
4353
}
4454
final StringWriter buffer = new StringWriter();
4555
buffer.append(throwable.getMessage()).append(System.lineSeparator());
46-
56+
4757
throwable.printStackTrace(new PrintWriter(buffer));
4858

4959
final Throwable cause = throwable.getCause();
@@ -57,23 +67,23 @@ public static String formatError(final Throwable throwable) {
5767
}
5868

5969
/**
60-
* Sets the log level to the the
61-
* @param newLevel the new logging level
70+
* Sets the log level to the the.
71+
*
72+
* @param newLevel the new logging level
6273
*/
63-
public static void setLogLevel(String newLevel) {
74+
public static void setLogLevel(final String newLevel) {
6475

65-
Level l = mapLevel(newLevel);
66-
LogManager logManager = LogManager.getLogManager();
76+
final Level l = mapLevel(newLevel);
77+
final LogManager logManager = LogManager.getLogManager();
6778
// slightly cumbersome approach - but the loggers don't have a 'get children'
6879
// so find those that have the correct stem.
6980
final ArrayList<String> allLoggers = Collections.list(logManager.getLoggerNames());
7081
allLoggers.add("org.hyperledger");
71-
allLoggers.stream().filter(name -> name.startsWith("org.hyperledger")).map(name -> logManager.getLogger(name))
72-
.forEach(logger -> {
73-
if (logger != null) {
74-
logger.setLevel(l);
75-
}
76-
});
82+
allLoggers.stream().filter(name -> name.startsWith("org.hyperledger")).map(name -> logManager.getLogger(name)).forEach(logger -> {
83+
if (logger != null) {
84+
logger.setLevel(l);
85+
}
86+
});
7787
}
7888

7989
private static Level mapLevel(final String level) {
@@ -90,6 +100,8 @@ private static Level mapLevel(final String level) {
90100
return Level.CONFIG;
91101
case "DEBUG":
92102
return Level.FINEST;
103+
default:
104+
return Level.INFO;
93105
}
94106
}
95107
return Level.INFO;

0 commit comments

Comments
 (0)