Skip to content

Commit 13ea513

Browse files
Replace checkstyle with PMD
Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
1 parent d5aa6c5 commit 13ea513

File tree

86 files changed

+973
-1026
lines changed

Some content is hidden

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

86 files changed

+973
-1026
lines changed

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/contractinstall/ContractInstallTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void setUp() throws Exception {
2222
}
2323

2424
@Test
25-
public void TestInstall() {
25+
public void testInstall() {
2626

2727
InvokeHelper helper = InvokeHelper.newHelper("baregradlecc", "sachannel");
2828
String text = helper.invoke("org1", "whoami");

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/ledgertests/LedgerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void setUp() throws Exception {
2323
}
2424

2525
@Test
26-
public void TestLedgers() {
26+
public void testLedgers() {
2727
InvokeHelper helper = InvokeHelper.newHelper("ledgercc", "sachannel");
2828

2929
String text = helper.invoke("org1", "accessLedgers");

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SACCIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void setUp() throws Exception {
2222
}
2323

2424
@Test
25-
public void TestLedger() {
25+
public void testLedger() {
2626

2727
InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");
2828
String text = helper.invoke("org1", "putBulkStates");

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SBECCIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void setUp() throws Exception {
2222
}
2323

2424
@Test
25-
public void RunSBE_pub_setget() {
25+
public void runSBE_pub_setget() {
2626
final String mode = "pub";
2727

2828
final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");
@@ -85,7 +85,7 @@ public void RunSBE_pub_setget() {
8585
}
8686

8787
@Test
88-
public void RunSBE_priv() {
88+
public void runSBE_priv() {
8989
final String mode = "priv";
9090

9191
final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");

fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/util/FabricState.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@
99
import java.nio.file.Paths;
1010
import java.util.HashMap;
1111
import java.util.Map;
12-
import java.util.concurrent.Semaphore;
1312
import org.hyperleder.fabric.shim.integration.util.Bash.BashBuilder;
1413

1514
public final class FabricState {
1615

1716
private static FabricState state;
1817

19-
private static final Map<String, Boolean> channelStarted = new HashMap<>();
20-
21-
// sempaphore to protect access
22-
private static final Semaphore flag = new Semaphore(1);
23-
24-
public static FabricState getState() {
18+
public static synchronized FabricState getState() {
2519
if (state == null) {
2620
state = new FabricState();
2721
}

fabric-chaincode-shim/build.gradle

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ plugins {
88
id 'maven-publish'
99
id 'jacoco'
1010
id 'signing'
11-
id 'checkstyle'
11+
id 'pmd'
1212
}
1313

14-
checkstyle {
15-
toolVersion '10.18.1'
16-
configFile file("../ci/checkstyle/checkstyle.xml")
17-
configProperties = [root_dir: file("..") ]
18-
}
19-
checkstyleMain {
20-
source ='src/main/java'
21-
}
22-
checkstyleMain.exclude("**/ChaincodeServerProperties.**")
23-
checkstyleTest {
24-
source ='src/test/java'
14+
pmd {
15+
toolVersion = '7.7.0'
16+
ruleSetFiles = files('../pmd-ruleset.xml')
17+
ruleSets = [] // explicitly set to empty to avoid using the default configuration
18+
ignoreFailures = false
2519
}
2620

2721
configurations {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected Logger(final String name) {
1818
super(name, null);
1919

2020
// ensure that the parent logger is set
21-
this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
21+
super.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
2222
}
2323

2424
/**
@@ -45,9 +45,9 @@ public void debug(final String msg) {
4545
*/
4646
public static Logger getLogger(final Class<?> class1) {
4747
// important to add the logger to the log manager
48-
final Logger l = Logger.getLogger(class1.getName());
49-
LogManager.getLogManager().addLogger(l);
50-
return l;
48+
final Logger result = Logger.getLogger(class1.getName());
49+
LogManager.getLogManager().addLogger(result);
50+
return result;
5151
}
5252

5353
/** @param message */

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import java.io.PrintWriter;
99
import java.io.StringWriter;
10-
import java.util.ArrayList;
1110
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Locale;
1213
import java.util.logging.Level;
1314
import java.util.logging.LogManager;
1415

@@ -50,7 +51,7 @@ public static String formatError(final Throwable throwable) {
5051
final Throwable cause = throwable.getCause();
5152
if (cause != null) {
5253
buffer.append(".. caused by ..").append(System.lineSeparator());
53-
buffer.append(Logging.formatError(cause));
54+
buffer.append(formatError(cause));
5455
}
5556

5657
return buffer.toString();
@@ -67,11 +68,11 @@ public static void setLogLevel(final String newLevel) {
6768
final LogManager logManager = LogManager.getLogManager();
6869
// slightly cumbersome approach - but the loggers don't have a 'get children'
6970
// so find those that have the correct stem.
70-
final ArrayList<String> allLoggers = Collections.list(logManager.getLoggerNames());
71+
final List<String> allLoggers = Collections.list(logManager.getLoggerNames());
7172
allLoggers.add("org.hyperledger");
7273
allLoggers.stream()
7374
.filter(name -> name.startsWith("org.hyperledger"))
74-
.map(name -> logManager.getLogger(name))
75+
.map(logManager::getLogger)
7576
.forEach(logger -> {
7677
if (logger != null) {
7778
logger.setLevel(l);
@@ -81,7 +82,7 @@ public static void setLogLevel(final String newLevel) {
8182

8283
private static Level mapLevel(final String level) {
8384
if (level != null) {
84-
switch (level.toUpperCase().trim()) {
85+
switch (level.toUpperCase(Locale.getDefault()).trim()) {
8586
case "ERROR":
8687
case "CRITICAL":
8788
return Level.SEVERE;

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ClientIdentity.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
* and attributes. Such information is useful in enforcing access control by the chaincode.
3131
*/
3232
public final class ClientIdentity {
33-
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());
33+
private static final Logger LOGGER = Logger.getLogger(ContractRouter.class.getName());
3434

3535
private final String mspId;
3636
private final X509Certificate cert;
37-
private Map<String, String> attrs;
37+
private final Map<String, String> attrs;
3838
private final String id;
3939
// special OID used by Fabric to save attributes in x.509 certificates
4040
private static final String FABRIC_CERT_ATTR_OID = "1.2.3.4.5.6.7.8.1";
@@ -47,7 +47,7 @@ public final class ClientIdentity {
4747
* @throws JSONException
4848
* @throws IOException
4949
*/
50-
public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSONException, IOException {
50+
public ClientIdentity(final ChaincodeStub stub) throws CertificateException, IOException {
5151
final byte[] signingId = stub.getCreator();
5252

5353
// Create a Serialized Identity protobuf
@@ -60,11 +60,12 @@ public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSO
6060
CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(idBytes));
6161
this.cert = cert;
6262

63-
this.attrs = new HashMap<String, String>();
6463
// Get the extension where the identity attributes are stored
6564
final byte[] extensionValue = cert.getExtensionValue(FABRIC_CERT_ATTR_OID);
6665
if (extensionValue != null) {
6766
this.attrs = parseAttributes(extensionValue);
67+
} else {
68+
this.attrs = new HashMap<>();
6869
}
6970

7071
// Populate identity
@@ -100,7 +101,7 @@ public String getMSPID() {
100101
*/
101102
private Map<String, String> parseAttributes(final byte[] extensionValue) throws IOException {
102103

103-
final Map<String, String> attrMap = new HashMap<String, String>();
104+
final Map<String, String> attrMap = new HashMap<>();
104105

105106
// Create ASN1InputStream from extensionValue
106107
try (ByteArrayInputStream inStream = new ByteArrayInputStream(extensionValue);
@@ -126,7 +127,7 @@ private Map<String, String> parseAttributes(final byte[] extensionValue) throws
126127
} catch (final JSONException error) {
127128
// creating a JSON object failed
128129
// decoded extensionValue is not a string containing JSON
129-
logger.error(() -> logger.formatError(error));
130+
LOGGER.error(() -> LOGGER.formatError(error));
130131
// return empty map
131132
}
132133
return attrMap;
@@ -142,11 +143,7 @@ private Map<String, String> parseAttributes(final byte[] extensionValue) throws
142143
* @return {String | null} Value of the attribute or null if the invoking identity does not possess the attribute.
143144
*/
144145
public String getAttributeValue(final String attrName) {
145-
if (this.attrs.containsKey(attrName)) {
146-
return this.attrs.get(attrName);
147-
} else {
148-
return null;
149-
}
146+
return this.attrs.getOrDefault(attrName, null);
150147
}
151148

152149
/**
@@ -160,11 +157,7 @@ public String getAttributeValue(final String attrName) {
160157
* expected value. Otherwise, returns false.
161158
*/
162159
public boolean assertAttributeValue(final String attrName, final String attrValue) {
163-
if (!this.attrs.containsKey(attrName)) {
164-
return false;
165-
} else {
166-
return attrValue.equals(this.attrs.get(attrName));
167-
}
160+
return this.attrs.containsKey(attrName) && attrValue.equals(this.attrs.get(attrName));
168161
}
169162

170163
/**

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@
1010

1111
/** Factory to create {@link Context} from {@link ChaincodeStub} by wrapping stub with dynamic proxy. */
1212
public final class ContextFactory {
13-
private static ContextFactory cf;
13+
private static final ContextFactory INSTANCE = new ContextFactory();
1414

1515
/** @return ContextFactory */
16-
public static synchronized ContextFactory getInstance() {
17-
if (cf == null) {
18-
cf = new ContextFactory();
19-
}
20-
return cf;
16+
public static ContextFactory getInstance() {
17+
return INSTANCE;
2118
}
2219

2320
/**
2421
* @param stub
2522
* @return Context
2623
*/
2724
public Context createContext(final ChaincodeStub stub) {
28-
final Context newContext = new Context(stub);
29-
return newContext;
25+
return new Context(stub);
3026
}
3127
}

0 commit comments

Comments
 (0)