Skip to content

Commit

Permalink
[FAB-4235] Fix compile and Javadoc warnings
Browse files Browse the repository at this point in the history
Fix to compile warnings due to code issues.
* Minor logic errors.
* Generic declarations and casts.
* Resources not closed (now use try-with-resources)

Resolve issues in existing Javadoc.

Fixes for actual bugs flagged by Checkstyle
* Two (static) utility classes with public constructors.
* Peer class had equals() implementation but no hashCode().

Change-Id: I5484a93b3b36cb27761b297aaa33f8321f0f4d02
Signed-off-by: Mark S. Lewis <Mark_Lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday committed Jun 2, 2017
1 parent 15d2faf commit 37b344c
Show file tree
Hide file tree
Showing 31 changed files with 253 additions and 289 deletions.
10 changes: 5 additions & 5 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/proto">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
Expand All @@ -39,5 +34,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ChaincodeEndorsementPolicy {
public ChaincodeEndorsementPolicy() {
}

private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, Map mp) throws ChaincodeEndorsementPolicyParseException {
private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, Map<?, ?> mp) throws ChaincodeEndorsementPolicyParseException {

if (mp == null) {
throw new ChaincodeEndorsementPolicyParseException("No policy section was found in the document.");
Expand All @@ -61,7 +61,7 @@ private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal>

}

for (Map.Entry<Object, Object> ks : ((Map<Object, Object>) mp).entrySet()) {
for (Map.Entry<?, ?> ks : mp.entrySet()) {
Object ko = ks.getKey();
Object vo = ks.getValue();
final String key = (String) ko;
Expand Down Expand Up @@ -92,20 +92,23 @@ private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal>
int matchNo = Integer.parseInt(matchStingNo);

if (!(vo instanceof List)) {

throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have list but found %s.", key, "" + vo));
throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have list but found %s.", key, String.valueOf(vo)));
}
if (((List) vo).size() < matchNo) {

throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have at least %d items to match but only found %d.", key, matchNo, ((List) vo).size()));
@SuppressWarnings("unchecked")
final List<Map<?, ?>> voList = (List<Map<?, ?>>)vo;

if (voList.size() < matchNo) {

throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have at least %d items to match but only found %d.", key, matchNo, voList.size()));
}

SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder()
.setN(matchNo);

for (Object nlo : (List) vo) {
for (Map<?, ?> nlo : voList) {

SignaturePolicy sp = parsePolicy(identities, (Map) nlo);
SignaturePolicy sp = parsePolicy(identities, nlo);
spBuilder.addPolicies(sp);
}

Expand All @@ -123,12 +126,12 @@ private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal>

}

private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<Object, Object> identities) throws ChaincodeEndorsementPolicyParseException {
private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> identities) throws ChaincodeEndorsementPolicyParseException {
//Only Role types are excepted at this time.

IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>();

for (Map.Entry<Object, Object> kp : identities.entrySet()) {
for (Map.Entry<?, ?> kp : identities.entrySet()) {
Object key = kp.getKey();
Object val = kp.getValue();

Expand All @@ -144,13 +147,14 @@ private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<Object,
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map got %s ", key, val == null ? "null" : val.getClass().getName()));
}

Object role = ((Map<String, Object>) val).get("role");
Object role = ((Map<?, ?>) val).get("role");

if (!(role instanceof Map)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map for role got %s ", key, role == null ? "null" : role.getClass().getName()));
}
final Map<?, ?> roleMap = (Map<?, ?>)role;

Object name = ((Map) role).get("name");
Object name = (roleMap).get("name");

if (!(name instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected String in role got %s ", key, name == null ? "null" : name.getClass().getName()));
Expand All @@ -160,7 +164,7 @@ private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<Object,
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member or admin in role got %s ", key, name));
}

Object mspId = ((Map) role).get("mspId");
Object mspId = roleMap.get("mspId");

if (!(mspId instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId expected String in role got %s ", key, mspId == null ? "null" : mspId.getClass().getName()));
Expand Down Expand Up @@ -212,15 +216,15 @@ public void fromFile(File policyFile) throws IOException {

public void fromYamlFile(File yamlPolicyFile) throws IOException, ChaincodeEndorsementPolicyParseException {
final Yaml yaml = new Yaml();
final Map load = (Map) yaml.load(new FileInputStream(yamlPolicyFile));
final Map<?, ?> load = (Map<?, ?>)yaml.load(new FileInputStream(yamlPolicyFile));

Map<String, Map> mp = (Map<String, Map>) load.get("policy");
Map<?, ?> mp = (Map<?, ?>)load.get("policy");

if (null == mp) {
throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
}

IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<Object, Object>) load.get("identities"));
IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<?, ?>)load.get("identities"));

SignaturePolicy sp = parsePolicy(identities, mp);

Expand Down Expand Up @@ -258,6 +262,7 @@ public byte[] getChaincodeEndorsementPolicyAsBytes() {
return policyBytes;
}

@SuppressWarnings("serial")
private static class IndexedHashMap<K, V> extends LinkedHashMap<K, V> {
final HashMap<K, Integer> kmap = new HashMap<>();

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/hyperledger/fabric/sdk/ChaincodeID.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@
/**
* ChaincodeID identifies chaincode.
*/
public class ChaincodeID {
public final class ChaincodeID {

private final org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID fabricChaincodeID;

private ChaincodeID() {

fabricChaincodeID = null;

}

public org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID getFabricChaincodeID() {
return fabricChaincodeID;
}
Expand All @@ -53,7 +47,7 @@ public String getVersion() {
* Build a new ChaincodeID
*/

public static class Builder {
public static final class Builder {
private final org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID.Builder protoBuilder = org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeID.newBuilder();

private Builder() {
Expand Down
Loading

0 comments on commit 37b344c

Please sign in to comment.