[#4] add checkstyle #5

Merged
orikik merged 1 commit from orikik/frostfs-sdk-java:feature/checkstyle into master 2024-08-01 11:29:47 +00:00
14 changed files with 139 additions and 25 deletions
Showing only changes of commit ab8a574d0d - Show all commits

74
checkstyle.xml Normal file
View file

@ -0,0 +1,74 @@
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="java, properties, xml"/>
<module name="SuppressWarningsFilter" />
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="LineLength">
<property name="max" value="120"/>
</module>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="SuppressWarningsHolder" />
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<module name="AbbreviationAsWordInName">
<property name="tokens" value="METHOD_DEF,CLASS_DEF"/>
<property name="ignoreStatic" value="false"/>
</module>
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="IllegalImport"> <!-- defaults to sun.* packages -->
</module>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<module name="ConstantName">
<property name="applyToPrivate" value="false"/>
</module>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
</module>
<module name="MethodParamPad"/>
<module name="EmptyForInitializerPad"/>
<module name="MissingOverride"/>
<module name="ParameterNumber">
<property name="ignoreOverriddenMethods" value="true"/>
</module>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<module name="MagicNumber">
<property name="ignoreHashCodeMethod" value="true"/>
<property name="ignoreAnnotation" value="true"/>
<property name="ignoreFieldDeclaration" value="true"/>
<property name="ignoreNumbers" value="-1, 0, 1, 2, 4"/>
</module>
<module name="RequireThis"/>
<module name="DeclarationOrder"/>
</module>
</module>

View file

@ -1,8 +1,8 @@
package info.frostfs.sdk.jdo;
import info.frostfs.sdk.FrostFSClient;
import info.frostfs.sdk.dto.OwnerId;
import info.frostfs.sdk.dto.Version;
import info.frostfs.sdk.FrostFSClient;
import io.grpc.Channel;
import org.apache.commons.lang3.StringUtils;

View file

@ -40,7 +40,7 @@ public class NetmapClientImpl extends ContextAccessor implements NetmapClient {
private static long getLongValue(byte[] bytes) {
long val = 0;
for (var i = bytes.length - 1; i >= 0; i--) {
val = (val << 8) + bytes[i];
val = (val << Byte.SIZE) + bytes[i];
}
return val;

View file

@ -49,7 +49,11 @@ public class ObjectToolsImpl extends ContextAccessor implements ToolsClient {
var split = objectFrostFs.getHeader().getSplit();
if (nonNull(split)) {
var splitGrpc = Types.Header.Split.newBuilder()
.setSplitId(nonNull(split.getSplitId()) ? ByteString.copyFrom(split.getSplitId().toBinary()) : null);
.setSplitId(
nonNull(split.getSplitId())
? ByteString.copyFrom(split.getSplitId().toBinary())
: null
);
ListUtils.emptyIfNull(split.getChildren()).stream()
.map(ObjectIdMapper::toGrpcMessage)

View file

@ -23,12 +23,13 @@ import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray;
public class RequestSigner {
public static final String ERROR_UNSUPPORTED_TYPE_TEMPLATE = "Unsupported message type: %s";
public static final int RFC6979_SIGNATURE_SIZE = 64;
public static final int HASH_SIGNATURE_SIZE = 65;
private RequestSigner() {
}
public static byte[] signData(ECDsa key, byte[] data) {
var hash = new byte[65];
var hash = new byte[HASH_SIGNATURE_SIZE];
hash[0] = 0x04;
try {
Signature signature = Signature.getInstance(CryptoConst.SIGNATURE_ALGORITHM);

View file

@ -82,7 +82,8 @@ public class Verifier {
public static boolean verify(Message response) {
var body = MessageHelper.getField(response, BODY_FIELD_NAME);
var metaHeader = (Types.ResponseMetaHeader) MessageHelper.getField(response, META_HEADER_FIELD_NAME);
var verifyHeader = (Types.ResponseVerificationHeader) MessageHelper.getField(response, VERIFY_HEADER_FIELD_NAME);
var verifyHeader = (Types.ResponseVerificationHeader)
MessageHelper.getField(response, VERIFY_HEADER_FIELD_NAME);
return verifyMatryoshkaLevel(body, metaHeader, verifyHeader);
}

View file

@ -10,8 +10,12 @@ import static java.util.Objects.isNull;
public class Base58 {
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
public static final int BASE58_SYMBOL_COUNT = 58;
public static final int BASE256_SYMBOL_COUNT = 256;
private static final int BYTE_DIVISION = 0xFF;
private static final char ENCODED_ZERO = ALPHABET[0];
private static final int[] INDEXES = new int[128];
private static final char BASE58_ASCII_MAX_VALUE = 128;
private static final int[] INDEXES = new int[BASE58_ASCII_MAX_VALUE];
static {
Arrays.fill(INDEXES, -1);
@ -69,7 +73,7 @@ public class Base58 {
char[] encoded = new char[input.length * 2]; // upper bound
int outputStart = encoded.length;
for (int inputStart = zeros; inputStart < input.length; ) {
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, 256, 58)];
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, BASE256_SYMBOL_COUNT, BASE58_SYMBOL_COUNT)];
if (input[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
@ -93,7 +97,7 @@ public class Base58 {
byte[] input58 = new byte[input.length()];
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
int digit = c < 128 ? INDEXES[c] : -1;
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
if (digit < 0) {
throw new IllegalArgumentException(String.format("Invalid character in Base58: 0x%04x", (int) c));
}
@ -108,7 +112,7 @@ public class Base58 {
byte[] decoded = new byte[input.length()];
int outputStart = decoded.length;
for (int inputStart = zeros; inputStart < input58.length; ) {
decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
decoded[--outputStart] = divmod(input58, inputStart, BASE58_SYMBOL_COUNT, BASE256_SYMBOL_COUNT);
if (input58[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
@ -125,7 +129,7 @@ public class Base58 {
// this is just long division which accounts for the base of the input digits
int remainder = 0;
for (int i = firstDigit; i < number.length; i++) {
int digit = (int) number[i] & 0xFF;
int digit = (int) number[i] & BYTE_DIVISION;
int temp = remainder * base + digit;
number[i] = (byte) (temp / divisor);
remainder = temp % divisor;

View file

@ -11,15 +11,17 @@ import java.security.NoSuchAlgorithmException;
import static java.util.Objects.isNull;
public class Helper {
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
private Helper() {
}
public static byte[] getRIPEMD160(byte[] value) {
public static byte[] getRipemd160(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException("Input value is missing");
}
var hash = new byte[20];
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
var digest = new RIPEMD160Digest();
digest.update(value, 0, value.length);
digest.doFinal(hash, 0);

View file

@ -24,13 +24,14 @@ import java.security.spec.ECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import static info.frostfs.sdk.Helper.getRIPEMD160;
import static info.frostfs.sdk.Helper.getRipemd160;
import static info.frostfs.sdk.Helper.getSha256;
import static java.util.Objects.isNull;
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
public class KeyExtension {
public static final byte NEO_ADDRESS_VERSION = 0x35;
private static final int PS_IN_HASH160 = 0x0C;
private static final int DECODE_ADDRESS_LENGTH = 21;
private static final int COMPRESSED_PUBLIC_KEY_LENGTH = 33;
private static final int UNCOMPRESSED_PUBLIC_KEY_LENGTH = 65;
@ -134,7 +135,7 @@ public class KeyExtension {
checkInputValue(publicKey);
var script = createSignatureRedeemScript(publicKey);
return getRIPEMD160(getSha256(script));
return getRipemd160(getSha256(script));
}
public static String publicKeyToAddress(byte[] publicKey) {
@ -161,7 +162,7 @@ public class KeyExtension {
byte[] buffer = new byte[4];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) (value >> i * 8);
buffer[i] = (byte) (value >> i * Byte.SIZE);
}
return buffer;
@ -176,10 +177,10 @@ public class KeyExtension {
);
}
var script = new byte[]{0x0c, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
var script = new byte[]{PS_IN_HASH160, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
script = ArrayHelper.concat(script, publicKey);
script = ArrayHelper.concat(script, new byte[]{0x41}); //SYSCALL
script = ArrayHelper.concat(script, new byte[]{UNCOMPRESSED_PUBLIC_KEY_LENGTH}); //SYSCALL
script = ArrayHelper.concat(script, getBytes(CHECK_SIG_DESCRIPTOR)); //Neo_Crypto_CheckSig
return script;
}

View file

@ -5,10 +5,10 @@ import java.util.UUID;
import static java.util.Objects.isNull;
public class UUIDExtension {
public class UuidExtension {
private static final int UUID_BYTE_ARRAY_LENGTH = 16;
private UUIDExtension() {
private UuidExtension() {
}
public static UUID asUuid(byte[] bytes) {
@ -27,7 +27,7 @@ public class UUIDExtension {
throw new IllegalArgumentException("Uuid is not present");
}
ByteBuffer bb = ByteBuffer.allocate(16);
ByteBuffer bb = ByteBuffer.allocate(UUID_BYTE_ARRAY_LENGTH);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();

View file

@ -3,7 +3,11 @@ package info.frostfs.sdk.constants;
public class AppConst {
public static final int DEFAULT_MAJOR_VERSION = 2;
public static final int DEFAULT_MINOR_VERSION = 13;
public static final int OBJECT_CHUNK_SIZE = 3 * (1 << 20);
public static final int BYTE_SHIFT = 10;
public static final int BYTE = 1;
public static final int KIB = BYTE << BYTE_SHIFT;
public static final int MIB = KIB << BYTE_SHIFT;
public static final int OBJECT_CHUNK_SIZE = 3 * MIB;
public static final int SHA256_HASH_LENGTH = 32;
private AppConst() {

View file

@ -2,8 +2,8 @@ package info.frostfs.sdk.dto;
import java.util.UUID;
import static info.frostfs.sdk.UUIDExtension.asBytes;
import static info.frostfs.sdk.UUIDExtension.asUuid;
import static info.frostfs.sdk.UuidExtension.asBytes;
import static info.frostfs.sdk.UuidExtension.asUuid;
import static java.util.Objects.isNull;
public class SplitId {

View file

@ -7,8 +7,8 @@ import info.frostfs.sdk.enums.BasicAcl;
import info.frostfs.sdk.mappers.VersionMapper;
import info.frostfs.sdk.mappers.netmap.PlacementPolicyMapper;
import static info.frostfs.sdk.UUIDExtension.asBytes;
import static info.frostfs.sdk.UUIDExtension.asUuid;
import static info.frostfs.sdk.UuidExtension.asBytes;
import static info.frostfs.sdk.UuidExtension.asUuid;
import static java.util.Objects.isNull;
public class ContainerMapper {

23
pom.xml
View file

@ -19,5 +19,28 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<checkstyle.config.location>checkstyle.xml</checkstyle.config.location>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
<version>3.4.0</version>
</plugin>
</plugins>
</build>
</project>