From 53cc13b51b4b9d0c0c21df93cfb3a162bd1db220 Mon Sep 17 00:00:00 2001 From: Tal Moran Date: Tue, 24 Jan 2017 16:28:57 +0200 Subject: [PATCH] Cleaning up unchecked casts --- .../java/meerkat/crypto/dkg/feldman/Protocol.java | 4 ++-- .../java/meerkat/crypto/dkg/feldman/User.java | 2 +- .../java/meerkat/comm/MessageInputStream.java | 13 +++++++------ .../test/java/meerkat/comm/MessageStreamTest.java | 2 +- .../mixer/proofs/concrete/DlogConjunction.java | 2 +- .../meerkat/mixer/proofs/concrete/Mix2nizk.java | 2 +- .../mixer/proofs/generic/SigmaProtocolAnd2.java | 15 +++++++++++---- .../mixer/proofs/generic/SigmaProtocolOr2.java | 15 ++++++++++----- .../proofs/concrete/DlogOrStatementSigmaTest.java | 3 ++- .../PollingStationScannerWebApp.java | 12 ++++++++++-- .../java/meerkat/voting/VotingBoothToyRun.java | 6 +++--- .../voting/storage/StorageManagerMockup.java | 2 +- 12 files changed, 50 insertions(+), 28 deletions(-) diff --git a/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/Protocol.java b/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/Protocol.java index f612b07..781a585 100644 --- a/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/Protocol.java +++ b/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/Protocol.java @@ -104,7 +104,7 @@ public class Protocol extends VerifiableSecretSharing { * setter * @param parties */ - protected void setParties(Party[] parties){ + protected void setParties(Party[] parties){ this.parties = parties; } @@ -112,7 +112,7 @@ public class Protocol extends VerifiableSecretSharing { * getter * @return */ - protected Party[] getParties(){ + protected Party[] getParties(){ return parties; } diff --git a/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/User.java b/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/User.java index f7c4624..235dab6 100644 --- a/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/User.java +++ b/distributed-key-generation/src/main/java/meerkat/crypto/dkg/feldman/User.java @@ -77,7 +77,7 @@ public class User implements Runnable { * All parties participating in key generation. * parties[id-1] has my info. */ - protected final Party[] parties; + protected final Party[] parties; /** * set of all non-disqualified parties diff --git a/meerkat-common/src/main/java/meerkat/comm/MessageInputStream.java b/meerkat-common/src/main/java/meerkat/comm/MessageInputStream.java index b1e5255..bc7c118 100644 --- a/meerkat-common/src/main/java/meerkat/comm/MessageInputStream.java +++ b/meerkat-common/src/main/java/meerkat/comm/MessageInputStream.java @@ -15,11 +15,14 @@ import java.util.List; */ public class MessageInputStream implements Iterable{ + Class type; + private T.Builder builder; private InputStream in; MessageInputStream(InputStream in, Class type) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + this.type = type; this.in = in; this.builder = (T.Builder) type.getMethod("newBuilder").invoke(type); } @@ -41,7 +44,7 @@ public class MessageInputStream implements Iterable{ @Override public T next() { try { - return readMessage(); + return readMessage(type); } catch (IOException e) { return null; } @@ -65,12 +68,10 @@ public class MessageInputStream implements Iterable{ } - public T readMessage() throws IOException{ - + public T readMessage(Class msgType) throws IOException{ builder.clear(); builder.mergeDelimitedFrom(in); - return (T) builder.build(); - + return msgType.cast(builder.build()); } public boolean isAvailable() throws IOException { @@ -82,7 +83,7 @@ public class MessageInputStream implements Iterable{ List list = new LinkedList<>(); while (isAvailable()){ - list.add(readMessage()); + list.add(readMessage(type)); } return list; diff --git a/meerkat-common/src/test/java/meerkat/comm/MessageStreamTest.java b/meerkat-common/src/test/java/meerkat/comm/MessageStreamTest.java index 58dc7c1..27eeca3 100644 --- a/meerkat-common/src/test/java/meerkat/comm/MessageStreamTest.java +++ b/meerkat-common/src/test/java/meerkat/comm/MessageStreamTest.java @@ -81,7 +81,7 @@ public class MessageStreamTest { new ByteArrayInputStream(stream.toByteArray()), BulletinBoardMessage.class); - assertThat("Retrieved message was not identical to send message", comparator.compare(message, in.readMessage()), is(equalTo(0))); + assertThat("Retrieved message was not identical to send message", comparator.compare(message, in.readMessage(BulletinBoardMessage.class)), is(equalTo(0))); } catch (IOException e) { diff --git a/mixer/src/main/java/meerkat/mixer/proofs/concrete/DlogConjunction.java b/mixer/src/main/java/meerkat/mixer/proofs/concrete/DlogConjunction.java index d2487b5..b9a9263 100644 --- a/mixer/src/main/java/meerkat/mixer/proofs/concrete/DlogConjunction.java +++ b/mixer/src/main/java/meerkat/mixer/proofs/concrete/DlogConjunction.java @@ -22,7 +22,7 @@ public class DlogConjunction { } } - public static class Simulator extends SigmaProtocolAnd2.Simulator { + public static class Simulator extends SigmaProtocolAnd2.Simulator { public Simulator(ECElGamalEncryption encryptor, Random rand, Statements.AndStatement statement) { super(ProtobufConcatenators.concatAnd1, ProtobufConcatenators.concatAnd2, new SchnorrDlogEquivalence.Simulator(encryptor, rand, statement.clauses[0]), new SchnorrDlogEquivalence.Simulator(encryptor, rand, statement.clauses[1])); diff --git a/mixer/src/main/java/meerkat/mixer/proofs/concrete/Mix2nizk.java b/mixer/src/main/java/meerkat/mixer/proofs/concrete/Mix2nizk.java index 93b8d24..482b694 100644 --- a/mixer/src/main/java/meerkat/mixer/proofs/concrete/Mix2nizk.java +++ b/mixer/src/main/java/meerkat/mixer/proofs/concrete/Mix2nizk.java @@ -34,7 +34,7 @@ public class Mix2nizk implements Prover, Verifier { this.encryptor = encryptor; this.group = this.encryptor.getGroup(); this.mixParams = new Statements(encryptor); - this.mix2NIZK = new SigmaFiatShamir(ProtobufConcatenators.concatNIZK); + this.mix2NIZK = new SigmaFiatShamir<>(ProtobufConcatenators.concatNIZK); } /** diff --git a/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolAnd2.java b/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolAnd2.java index 3e14edd..c411ef6 100644 --- a/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolAnd2.java +++ b/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolAnd2.java @@ -16,7 +16,9 @@ public class SigmaProtocolAnd2 { final Concatenator.Pair firstMessageConcatenator; final Concatenator.Pair finalMessageConcatenator; - public Prover(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator, + @SafeVarargs + public Prover(Concatenator.Pair firstMessageConcatenator, + Concatenator.Pair finalMessageConcatenator, SigmaProtocol.Prover... provers) { this.firstMessageConcatenator = firstMessageConcatenator; this.finalMessageConcatenator = finalMessageConcatenator; @@ -42,8 +44,10 @@ public class SigmaProtocolAnd2 { final Concatenator.Pair firstMessageConcatenator; final Concatenator.Pair finalMessageConcatenator; - public Verifier(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator, - SigmaProtocol.Verifier... verifiers) { + @SafeVarargs + public Verifier(Concatenator.Pair firstMessageConcatenator, + Concatenator.Pair finalMessageConcatenator, + SigmaProtocol.Verifier... verifiers) { this.firstMessageConcatenator = firstMessageConcatenator; this.finalMessageConcatenator = finalMessageConcatenator; this.verifiers = verifiers; @@ -63,7 +67,10 @@ public class SigmaProtocolAnd2 { final Concatenator.Pair firstMessageConcatenator; final Concatenator.Pair finalMessageConcatenator; - public Simulator(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator, SigmaProtocol.Simulator... simulators) { + @SafeVarargs + public Simulator(Concatenator.Pair firstMessageConcatenator, + Concatenator.Pair finalMessageConcatenator, + SigmaProtocol.Simulator... simulators) { this.firstMessageConcatenator = firstMessageConcatenator; this.finalMessageConcatenator = finalMessageConcatenator; this.simulators = simulators; diff --git a/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolOr2.java b/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolOr2.java index 097d2a8..66dfbf4 100644 --- a/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolOr2.java +++ b/mixer/src/main/java/meerkat/mixer/proofs/generic/SigmaProtocolOr2.java @@ -44,8 +44,11 @@ public class SigmaProtocolOr2 { * @param simulator * @param proverIdx Which index should the prover be inserted at (0 means first, 1 means second) */ - public Prover(Concatenator.Pair firstMessageConcatenator, Concatenator.Triplet finalMessageConcatenator, ChallengeGenerator challengeGenerator, - SigmaProtocol.Prover prover, SigmaProtocol.Simulator simulator, int proverIdx) { + public Prover(Concatenator.Pair firstMessageConcatenator, + Concatenator.Triplet finalMessageConcatenator, + ChallengeGenerator challengeGenerator, + SigmaProtocol.Prover prover, + SigmaProtocol.Simulator simulator, int proverIdx) { this.firstMessageConcatenator = firstMessageConcatenator; this.finalMessageConcatenator = finalMessageConcatenator; this.challengeGenerator = challengeGenerator; @@ -87,10 +90,11 @@ public class SigmaProtocolOr2 { final SigmaProtocol.Verifier[] verifiers; - - public Verifier(Concatenator.Pair firstMessageConcatenator, Concatenator.Triplet finalMessageConcatenator, + @SafeVarargs + public Verifier(Concatenator.Pair firstMessageConcatenator, + Concatenator.Triplet finalMessageConcatenator, ChallengeGenerator challengeGenerator, - SigmaProtocol.Verifier... verifiers) { + SigmaProtocol.Verifier... verifiers) { this.firstMessageConcatenator = firstMessageConcatenator; this.finalMessageConcatenator = finalMessageConcatenator; this.challengeGenerator = challengeGenerator; @@ -119,6 +123,7 @@ public class SigmaProtocolOr2 { BigInteger simChallenge0; + @SafeVarargs public Simulator(Concatenator.Pair firstMessageConcatenator, Concatenator.Triplet finalMessageConcatenator, ChallengeGenerator challengeGenerator, SigmaProtocol.Simulator... simulators) { diff --git a/mixer/src/test/java/meerkat/mixer/proofs/concrete/DlogOrStatementSigmaTest.java b/mixer/src/test/java/meerkat/mixer/proofs/concrete/DlogOrStatementSigmaTest.java index 91eca64..a4cb333 100644 --- a/mixer/src/test/java/meerkat/mixer/proofs/concrete/DlogOrStatementSigmaTest.java +++ b/mixer/src/test/java/meerkat/mixer/proofs/concrete/DlogOrStatementSigmaTest.java @@ -74,7 +74,8 @@ public class DlogOrStatementSigmaTest extends SigmaProtocolTest getNewSimulator() { Mix2.ChallengeGenerator gen = new Mix2.ChallengeGenerator(dlogtest.encryptor, rand); - return new SigmaProtocolOr2.Simulator<>(ProtobufConcatenators.concatMix1, ProtobufConcatenators.concatMix2, + return new SigmaProtocolOr2.Simulator(ProtobufConcatenators.concatMix1, ProtobufConcatenators.concatMix2, gen, new DlogConjunction.Simulator(dlogtest.encryptor, rand, statements[0]), new DlogConjunction.Simulator(dlogtest.encryptor, rand, statements[1])); } diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java index 327c774..db3195d 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java @@ -37,8 +37,16 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc * This method is called by the Jetty engine when instantiating the servlet */ @PostConstruct - public void init() throws Exception{ - callbacks = (Iterable>) servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME); + @SuppressWarnings("unchecked") + public void init() throws Exception { + Object context = servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME); + + try { + callbacks = (Iterable>) context; + } catch (ClassCastException e) { + throw e; + } + } @POST diff --git a/voting-booth/src/main/java/meerkat/voting/VotingBoothToyRun.java b/voting-booth/src/main/java/meerkat/voting/VotingBoothToyRun.java index 65b6685..5815920 100644 --- a/voting-booth/src/main/java/meerkat/voting/VotingBoothToyRun.java +++ b/voting-booth/src/main/java/meerkat/voting/VotingBoothToyRun.java @@ -90,7 +90,7 @@ public class VotingBoothToyRun { private static List generateChannelChoiceQuestions() { - ArrayList channelChoiceQuestions = new ArrayList(); + ArrayList channelChoiceQuestions = new ArrayList<>(); String[] ans1 = {"Red", "Blue", "Green"}; BallotQuestion ccquestion1 = generateBallotQuestion("What is your favorite color?", "Pick one answer", ans1); @@ -105,7 +105,7 @@ public class VotingBoothToyRun { private static List generateBallotQuestions() { - ArrayList allBallotQuestions = new ArrayList(); + ArrayList allBallotQuestions = new ArrayList<>(); String[] answers1 = {"answer 1", "answer 2", "answer 3", "answer 4"}; allBallotQuestions.add(generateBallotQuestion("question 1. Asking something...", "Pick one answer", answers1)); @@ -222,7 +222,7 @@ public class VotingBoothToyRun { private static void generateSystemMessages() throws IOException{ - Map systemMessageMap = new HashMap(); + Map systemMessageMap = new HashMap<>(); systemMessageMap.put(StorageManager.WAIT_FOR_COMMIT_MESSAGE, UIElement.newBuilder() .setType(UIElementDataType.TEXT) diff --git a/voting-booth/src/main/java/meerkat/voting/storage/StorageManagerMockup.java b/voting-booth/src/main/java/meerkat/voting/storage/StorageManagerMockup.java index f556e3b..6f01357 100644 --- a/voting-booth/src/main/java/meerkat/voting/storage/StorageManagerMockup.java +++ b/voting-booth/src/main/java/meerkat/voting/storage/StorageManagerMockup.java @@ -84,7 +84,7 @@ public class StorageManagerMockup implements StorageManager { logger.error("Could not read from the systemMessages file: '" + systemMessagesFilename + "'."); throw e; } - return systemMessages.getSystemMessage(); + return systemMessages.getSystemMessageMap(); } }