Cleaning up unchecked casts

android-scanner
Tal Moran 2017-01-24 16:28:57 +02:00
parent ce0e0e0d62
commit 53cc13b51b
12 changed files with 50 additions and 28 deletions

View File

@ -104,7 +104,7 @@ public class Protocol<T> extends VerifiableSecretSharing<T> {
* setter
* @param parties
*/
protected void setParties(Party[] parties){
protected void setParties(Party<T>[] parties){
this.parties = parties;
}
@ -112,7 +112,7 @@ public class Protocol<T> extends VerifiableSecretSharing<T> {
* getter
* @return
*/
protected Party[] getParties(){
protected Party<T>[] getParties(){
return parties;
}

View File

@ -77,7 +77,7 @@ public class User<T> implements Runnable {
* All parties participating in key generation.
* parties[id-1] has my info.
*/
protected final Party[] parties;
protected final Party<T>[] parties;
/**
* set of all non-disqualified parties

View File

@ -15,11 +15,14 @@ import java.util.List;
*/
public class MessageInputStream<T extends Message> implements Iterable<T>{
Class<T> type;
private T.Builder builder;
private InputStream in;
MessageInputStream(InputStream in, Class<T> 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<T extends Message> implements Iterable<T>{
@Override
public T next() {
try {
return readMessage();
return readMessage(type);
} catch (IOException e) {
return null;
}
@ -65,12 +68,10 @@ public class MessageInputStream<T extends Message> implements Iterable<T>{
}
public T readMessage() throws IOException{
public T readMessage(Class<T> 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<T extends Message> implements Iterable<T>{
List<T> list = new LinkedList<>();
while (isAvailable()){
list.add(readMessage());
list.add(readMessage(type));
}
return list;

View File

@ -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) {

View File

@ -22,7 +22,7 @@ public class DlogConjunction {
}
}
public static class Simulator extends SigmaProtocolAnd2.Simulator {
public static class Simulator extends SigmaProtocolAnd2.Simulator<Mixing.Mix2Proof.AndProof.FirstMessage, Mixing.Mix2Proof.DlogProof.FirstMessage, Mixing.Mix2Proof.AndProof.FinalMessage, Mixing.Mix2Proof.DlogProof.FinalMessage> {
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]));

View File

@ -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);
}
/**

View File

@ -16,7 +16,9 @@ public class SigmaProtocolAnd2 {
final Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator;
final Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator;
public Prover(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator,
@SafeVarargs
public Prover(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
SigmaProtocol.Prover<FirstMessageIn, FinalMessageIn>... provers) {
this.firstMessageConcatenator = firstMessageConcatenator;
this.finalMessageConcatenator = finalMessageConcatenator;
@ -42,8 +44,10 @@ public class SigmaProtocolAnd2 {
final Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator;
final Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator;
public Verifier(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator,
SigmaProtocol.Verifier... verifiers) {
@SafeVarargs
public Verifier(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
SigmaProtocol.Verifier<FirstMessageIn, FinalMessageIn>... verifiers) {
this.firstMessageConcatenator = firstMessageConcatenator;
this.finalMessageConcatenator = finalMessageConcatenator;
this.verifiers = verifiers;
@ -63,7 +67,10 @@ public class SigmaProtocolAnd2 {
final Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator;
final Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator;
public Simulator(Concatenator.Pair firstMessageConcatenator, Concatenator.Pair finalMessageConcatenator, SigmaProtocol.Simulator... simulators) {
@SafeVarargs
public Simulator(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Pair<FinalMessageOut, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
SigmaProtocol.Simulator<FirstMessageIn, FinalMessageIn>... simulators) {
this.firstMessageConcatenator = firstMessageConcatenator;
this.finalMessageConcatenator = finalMessageConcatenator;
this.simulators = simulators;

View File

@ -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<FirstMessageIn, FinalMessageIn> prover, SigmaProtocol.Simulator<FirstMessageIn, FinalMessageIn> simulator, int proverIdx) {
public Prover(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Triplet<FinalMessageOut, BigInteger, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
ChallengeGenerator challengeGenerator,
SigmaProtocol.Prover<FirstMessageIn, FinalMessageIn> prover,
SigmaProtocol.Simulator<FirstMessageIn, FinalMessageIn> simulator, int proverIdx) {
this.firstMessageConcatenator = firstMessageConcatenator;
this.finalMessageConcatenator = finalMessageConcatenator;
this.challengeGenerator = challengeGenerator;
@ -87,10 +90,11 @@ public class SigmaProtocolOr2 {
final SigmaProtocol.Verifier<FirstMessageIn, FinalMessageIn>[] verifiers;
public Verifier(Concatenator.Pair firstMessageConcatenator, Concatenator.Triplet finalMessageConcatenator,
@SafeVarargs
public Verifier(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Triplet<FinalMessageOut, BigInteger, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
ChallengeGenerator challengeGenerator,
SigmaProtocol.Verifier... verifiers) {
SigmaProtocol.Verifier<FirstMessageIn, FinalMessageIn>... verifiers) {
this.firstMessageConcatenator = firstMessageConcatenator;
this.finalMessageConcatenator = finalMessageConcatenator;
this.challengeGenerator = challengeGenerator;
@ -119,6 +123,7 @@ public class SigmaProtocolOr2 {
BigInteger simChallenge0;
@SafeVarargs
public Simulator(Concatenator.Pair<FirstMessageOut, FirstMessageIn, FirstMessageIn> firstMessageConcatenator,
Concatenator.Triplet<FinalMessageOut, BigInteger, FinalMessageIn, FinalMessageIn> finalMessageConcatenator,
ChallengeGenerator challengeGenerator, SigmaProtocol.Simulator<FirstMessageIn, FinalMessageIn>... simulators) {

View File

@ -74,7 +74,8 @@ public class DlogOrStatementSigmaTest extends SigmaProtocolTest<Mixing.Mix2Proof
protected SigmaProtocol.Simulator<Mixing.Mix2Proof.FirstMessage, Mixing.Mix2Proof.FinalMessage> getNewSimulator() {
Mix2.ChallengeGenerator gen = new Mix2.ChallengeGenerator(dlogtest.encryptor, rand);
return new SigmaProtocolOr2.Simulator<>(ProtobufConcatenators.concatMix1, ProtobufConcatenators.concatMix2,
return new SigmaProtocolOr2.Simulator<Mixing.Mix2Proof.FirstMessage, Mixing.Mix2Proof.AndProof.FirstMessage,
Mixing.Mix2Proof.FinalMessage, Mixing.Mix2Proof.AndProof.FinalMessage>(ProtobufConcatenators.concatMix1, ProtobufConcatenators.concatMix2,
gen, new DlogConjunction.Simulator(dlogtest.encryptor, rand, statements[0]),
new DlogConjunction.Simulator(dlogtest.encryptor, rand, statements[1]));
}

View File

@ -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<FutureCallback<PollingStation.ScannedData>>) servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME);
@SuppressWarnings("unchecked")
public void init() throws Exception {
Object context = servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME);
try {
callbacks = (Iterable<FutureCallback<PollingStation.ScannedData>>) context;
} catch (ClassCastException e) {
throw e;
}
}
@POST

View File

@ -90,7 +90,7 @@ public class VotingBoothToyRun {
private static List<BallotQuestion> generateChannelChoiceQuestions() {
ArrayList<BallotQuestion> channelChoiceQuestions = new ArrayList();
ArrayList<BallotQuestion> 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<BallotQuestion> generateBallotQuestions() {
ArrayList<BallotQuestion> allBallotQuestions = new ArrayList();
ArrayList<BallotQuestion> 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<String, UIElement> systemMessageMap = new HashMap();
Map<String, UIElement> systemMessageMap = new HashMap<>();
systemMessageMap.put(StorageManager.WAIT_FOR_COMMIT_MESSAGE, UIElement.newBuilder()
.setType(UIElementDataType.TEXT)

View File

@ -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();
}
}