From 9b9064beae9aef93bd82e9d7a2eb319d33af5180 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 10 Jun 2021 12:42:48 +0200 Subject: [PATCH] Small code style fixes and optimizations --- .../SignatureVerifyingInputStream.java | 4 ++-- .../modification/secretkeyring/SecretKeyRingEditor.java | 5 +---- .../src/main/java/org/pgpainless/key/util/UserId.java | 4 +++- .../main/java/org/pgpainless/util/CollectionUtils.java | 4 ++-- .../test/java/org/bouncycastle/PGPPublicKeyRingTest.java | 8 +------- .../ModificationDetectionTests.java | 6 +++--- .../decryption_verification/RecursionDepthTest.java | 2 -- .../java/org/pgpainless/provider/ProviderFactoryTest.java | 1 + 8 files changed, 13 insertions(+), 21 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java index d5f4c891..ab2f22b5 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java @@ -179,12 +179,12 @@ public class SignatureVerifyingInputStream extends FilterInputStream { } @Override - public int read(byte[] b) throws IOException { + public int read(@Nonnull byte[] b) throws IOException { return read(b, 0, b.length); } @Override - public int read(byte[] b, int off, int len) throws IOException { + public int read(@Nonnull byte[] b, int off, int len) throws IOException { int read = super.read(b, off, len); final boolean endOfStream = read == -1; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java index 3d1b9670..ee8fca9f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java @@ -342,10 +342,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface { } boolean found = false; - Iterator iterator = secretKeyRing.iterator(); - while (iterator.hasNext()) { - PGPSecretKey secretKey = iterator.next(); - + for (PGPSecretKey secretKey : secretKeyRing) { // Skip over unaffected subkeys if (secretKey.getKeyID() != fingerprint.getKeyId()) { secretKeyList.add(secretKey); diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/util/UserId.java b/pgpainless-core/src/main/java/org/pgpainless/key/util/UserId.java index dc232b8c..eae331fc 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/util/UserId.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/util/UserId.java @@ -16,6 +16,8 @@ package org.pgpainless.key.util; +import javax.annotation.Nonnull; + public final class UserId implements CharSequence { public static final class Builder { private String name; @@ -127,7 +129,7 @@ public final class UserId implements CharSequence { } @Override - public String toString() { + public @Nonnull String toString() { return asString(false); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java b/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java index bd618e4e..bf687229 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/CollectionUtils.java @@ -24,8 +24,8 @@ public class CollectionUtils { public static List iteratorToList(Iterator iterator) { List items = new ArrayList<>(); while (iterator.hasNext()) { - Object o = iterator.next(); - items.add((I) o); + I item = iterator.next(); + items.add(item); } return items; } diff --git a/pgpainless-core/src/test/java/org/bouncycastle/PGPPublicKeyRingTest.java b/pgpainless-core/src/test/java/org/bouncycastle/PGPPublicKeyRingTest.java index 216c8bb7..6cf8d237 100644 --- a/pgpainless-core/src/test/java/org/bouncycastle/PGPPublicKeyRingTest.java +++ b/pgpainless-core/src/test/java/org/bouncycastle/PGPPublicKeyRingTest.java @@ -40,10 +40,6 @@ public class PGPPublicKeyRingTest { * It does not. * * see also https://security.stackexchange.com/questions/92635/is-it-possible-to-assign-different-uids-to-subkeys-for-the-purpose-of-having-mul - * - * @throws InvalidAlgorithmParameterException - * @throws NoSuchAlgorithmException - * @throws PGPException */ @Test public void subkeysDoNotHaveUserIDsTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { @@ -54,10 +50,8 @@ public class PGPPublicKeyRingTest { Iterator userIds = subkey.getUserIDs(); if (primaryKey == subkey) { assertEquals("primary@user.id", userIds.next()); - assertFalse(userIds.hasNext()); - } else { - assertFalse(userIds.hasNext()); } + assertFalse(userIds.hasNext()); } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/ModificationDetectionTests.java b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/ModificationDetectionTests.java index aa1938d1..a9b92750 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/ModificationDetectionTests.java +++ b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/ModificationDetectionTests.java @@ -126,9 +126,9 @@ public class ModificationDetectionTests { /** * Messages containing a missing MDC shall fail to decrypt. - * @param implementationFactory - * @throws IOException - * @throws PGPException + * @param implementationFactory implementation factory + * @throws IOException in case of an io-error + * @throws PGPException in case of a pgp error */ @ParameterizedTest @MethodSource("org.pgpainless.util.TestUtil#provideImplementationFactories") diff --git a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/RecursionDepthTest.java b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/RecursionDepthTest.java index 30f8b609..74dfa22f 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/RecursionDepthTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/RecursionDepthTest.java @@ -39,8 +39,6 @@ public class RecursionDepthTest { * Test that decryption is aborted when maximum recursion depth of nested packets is exceeded. * * @see Sequoia-PGP Test Suite - * @throws IOException - * @throws PGPException */ @ParameterizedTest @MethodSource("org.pgpainless.util.TestUtil#provideImplementationFactories") diff --git a/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java b/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java index d815d68d..be642344 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/provider/ProviderFactoryTest.java @@ -38,6 +38,7 @@ public class ProviderFactoryTest { private ProviderFactory customProviderFactory = new ProviderFactory() { + @SuppressWarnings("deprecation") Provider provider = new Provider("PL", 1L, "PGPainlessTestProvider") { };