mirror of
https://github.com/nsacyber/HIRS.git
synced 2024-12-19 21:17:59 +00:00
issue_873: fixed all checkstyle issues
This commit is contained in:
parent
0f2a5864ba
commit
52f5f1bb70
@ -2,22 +2,24 @@ package hirs.structs.converters;
|
|||||||
|
|
||||||
import hirs.structs.elements.Struct;
|
import hirs.structs.elements.Struct;
|
||||||
import hirs.structs.elements.StructElementLength;
|
import hirs.structs.elements.StructElementLength;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import org.apache.commons.lang3.reflect.ConstructorUtils;
|
import org.apache.commons.lang3.reflect.ConstructorUtils;
|
||||||
import org.apache.commons.lang3.reflect.FieldUtils;
|
import org.apache.commons.lang3.reflect.FieldUtils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StructBuilder implementation.
|
* StructBuilder implementation.
|
||||||
*
|
*
|
||||||
* @param <T> the type of Struct to build
|
* @param <T> the type of Struct to build
|
||||||
*/
|
*/
|
||||||
public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
|
public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
|
||||||
private T struct;
|
|
||||||
private final Class<T> clazz;
|
private final Class<T> clazz;
|
||||||
|
private T struct;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates the builder.
|
* Instantiates the builder.
|
||||||
|
*
|
||||||
* @param clazz The type of struct to build
|
* @param clazz The type of struct to build
|
||||||
*/
|
*/
|
||||||
public SimpleStructBuilder(final Class<T> clazz) {
|
public SimpleStructBuilder(final Class<T> clazz) {
|
||||||
@ -59,17 +61,13 @@ public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
|
|||||||
public SimpleStructBuilder<T> set(final String field, final Number value) {
|
public SimpleStructBuilder<T> set(final String field, final Number value) {
|
||||||
try {
|
try {
|
||||||
String type = clazz.getDeclaredField(field).getType().getSimpleName();
|
String type = clazz.getDeclaredField(field).getType().getSimpleName();
|
||||||
switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
|
return switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
|
||||||
case "short":
|
case "short" -> setField(field, value.shortValue());
|
||||||
return setField(field, value.shortValue());
|
case "int" -> setField(field, value.intValue());
|
||||||
case "int":
|
case "byte" -> setField(field, value.byteValue());
|
||||||
return setField(field, value.intValue());
|
default -> throw new StructBuilderException(
|
||||||
case "byte":
|
|
||||||
return setField(field, value.byteValue());
|
|
||||||
default:
|
|
||||||
throw new StructBuilderException(
|
|
||||||
String.format("Unhandled numeric field type: %s", type));
|
String.format("Unhandled numeric field type: %s", type));
|
||||||
}
|
};
|
||||||
} catch (NoSuchFieldException | SecurityException e) {
|
} catch (NoSuchFieldException | SecurityException e) {
|
||||||
throw new StructBuilderException(
|
throw new StructBuilderException(
|
||||||
String.format("Unexpected error setting field: %s",
|
String.format("Unexpected error setting field: %s",
|
||||||
|
@ -23,5 +23,5 @@ public interface StructConverter {
|
|||||||
* @param <T> the {@link Struct} type
|
* @param <T> the {@link Struct} type
|
||||||
* @return de-serialized struct
|
* @return de-serialized struct
|
||||||
*/
|
*/
|
||||||
<T extends Struct> T convert(final byte[] data, final Class<T> type);
|
<T extends Struct> T convert(byte[] data, Class<T> type);
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import java.lang.annotation.Target;
|
|||||||
public @interface StructElementLength {
|
public @interface StructElementLength {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the field that this length represents.
|
* @return the field that this length represents.
|
||||||
*/
|
*/
|
||||||
String fieldName();
|
String fieldName();
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import java.lang.annotation.Target;
|
|||||||
public @interface StructElements {
|
public @interface StructElements {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* elements in order to be processed by a converter.
|
* @return elements in order to be processed by a converter.
|
||||||
*/
|
*/
|
||||||
String[] elements();
|
String[] elements();
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,6 @@ public class IdentityRequestEnvelope implements Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the endorsementCredential
|
* @return the endorsementCredential
|
||||||
*/
|
*/
|
||||||
public byte[] getEndorsementCredential() {
|
public byte[] getEndorsementCredential() {
|
||||||
@ -80,7 +79,6 @@ public class IdentityRequestEnvelope implements Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the length of the device info report
|
* @return the length of the device info report
|
||||||
*/
|
*/
|
||||||
public int getDeviceInfoReportLength() {
|
public int getDeviceInfoReportLength() {
|
||||||
@ -88,7 +86,6 @@ public class IdentityRequestEnvelope implements Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the device info report
|
* @return the device info report
|
||||||
*/
|
*/
|
||||||
public byte[] getDeviceInfoReport() {
|
public byte[] getDeviceInfoReport() {
|
||||||
|
@ -35,11 +35,6 @@ public enum EncryptionScheme {
|
|||||||
this.encryptionScheme = encryptionScheme;
|
this.encryptionScheme = encryptionScheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.encryptionScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps an {@link EncryptionScheme} based upon an integer. If the scheme is unmapped, the
|
* Maps an {@link EncryptionScheme} based upon an integer. If the scheme is unmapped, the
|
||||||
* default, {@link #PKCS1} is returned.
|
* default, {@link #PKCS1} is returned.
|
||||||
@ -48,11 +43,14 @@ public enum EncryptionScheme {
|
|||||||
* @return the encryption scheme, or if unknown, the default.
|
* @return the encryption scheme, or if unknown, the default.
|
||||||
*/
|
*/
|
||||||
public static EncryptionScheme fromInt(final int scheme) {
|
public static EncryptionScheme fromInt(final int scheme) {
|
||||||
switch (scheme) {
|
if (scheme == OAEP_VALUE) {
|
||||||
case OAEP_VALUE:
|
|
||||||
return OAEP;
|
return OAEP;
|
||||||
default:
|
}
|
||||||
return PKCS1;
|
return PKCS1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.encryptionScheme;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package hirs.structs.converters;
|
package hirs.structs.converters;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests suite for {@link SimpleStructConverter}.
|
* Tests suite for {@link SimpleStructConverter}.
|
||||||
@ -13,6 +14,7 @@ public class SimpleStructBuilderTest {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link SimpleStructBuilder#build()}.
|
* Tests {@link SimpleStructBuilder#build()}.
|
||||||
|
*
|
||||||
* @throws NoSuchFieldException sometimes
|
* @throws NoSuchFieldException sometimes
|
||||||
* @throws IllegalAccessException sometimes
|
* @throws IllegalAccessException sometimes
|
||||||
* @throws IllegalArgumentException sometimes
|
* @throws IllegalArgumentException sometimes
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package hirs.structs.converters;
|
package hirs.structs.converters;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests suite for {@link SimpleStructConverter}.
|
* Tests suite for {@link SimpleStructConverter}.
|
||||||
@ -14,7 +15,7 @@ public class SimpleStructConverterTest {
|
|||||||
|
|
||||||
private final TestStruct testStruct = new TestStruct();
|
private final TestStruct testStruct = new TestStruct();
|
||||||
|
|
||||||
private StructConverter converter = new SimpleStructConverter();
|
private final StructConverter converter = new SimpleStructConverter();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link SimpleStructConverter#convert(hirs.structs.elements.Struct)}.
|
* Tests {@link SimpleStructConverter#convert(hirs.structs.elements.Struct)}.
|
||||||
@ -47,8 +48,9 @@ public class SimpleStructConverterTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testNoElementsStructConvertToArray() {
|
public final void testNoElementsStructConvertToArray() {
|
||||||
assertThrows(StructConversionException.class, () ->
|
assertThrows(StructConversionException.class, () -> {
|
||||||
{converter.convert(new TestNoElementsAnnotationStruct());}, ".*@StructElements.*");
|
converter.convert(new TestNoElementsAnnotationStruct());
|
||||||
|
}, ".*@StructElements.*");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,8 +59,9 @@ public class SimpleStructConverterTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testNoElementsStructConvertToStruct() {
|
public final void testNoElementsStructConvertToStruct() {
|
||||||
assertThrows(StructConversionException.class, () ->
|
assertThrows(StructConversionException.class, () -> {
|
||||||
{converter.convert(new byte[1], TestNoElementsAnnotationStruct.class);}, ".*@StructElements.*");
|
converter.convert(new byte[1], TestNoElementsAnnotationStruct.class);
|
||||||
|
}, ".*@StructElements.*");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,8 +71,9 @@ public class SimpleStructConverterTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testInvalidDataTypeStructConvertToArray() {
|
public final void testInvalidDataTypeStructConvertToArray() {
|
||||||
assertThrows(StructConversionException.class, () ->
|
assertThrows(StructConversionException.class, () -> {
|
||||||
{converter.convert(new TestInvalidDataTypeStruct());}, "Unsupported field type.*");
|
converter.convert(new TestInvalidDataTypeStruct());
|
||||||
|
}, "Unsupported field type.*");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -79,8 +83,9 @@ public class SimpleStructConverterTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testInvalidDataTypeStructConvertToStruct() {
|
public final void testInvalidDataTypeStructConvertToStruct() {
|
||||||
assertThrows(StructConversionException.class, () ->
|
assertThrows(StructConversionException.class, () -> {
|
||||||
{converter.convert(new byte[1], TestInvalidDataTypeStruct.class);}, "Unsupported field type.*");
|
converter.convert(new byte[1], TestInvalidDataTypeStruct.class);
|
||||||
|
}, "Unsupported field type.*");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,12 @@ public class TestEmbeddedStruct implements Struct {
|
|||||||
|
|
||||||
private byte[] embedded = DEFAULT_ARRAY;
|
private byte[] embedded = DEFAULT_ARRAY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the provided object is equivalent to this class.
|
||||||
|
*
|
||||||
|
* @param o object to compare
|
||||||
|
* @return true if the provided object is equal to this class
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object o) {
|
public boolean equals(final Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@ -53,6 +59,7 @@ public class TestEmbeddedStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public byte[] getEmbedded() {
|
public byte[] getEmbedded() {
|
||||||
@ -61,6 +68,7 @@ public class TestEmbeddedStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public short getEmbeddedShort() {
|
public short getEmbeddedShort() {
|
||||||
@ -69,6 +77,7 @@ public class TestEmbeddedStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public int getEmbeddedSize() {
|
public int getEmbeddedSize() {
|
||||||
|
@ -54,6 +54,7 @@ public class TestStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public byte getTestByte() {
|
public byte getTestByte() {
|
||||||
@ -62,6 +63,7 @@ public class TestStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public short getTestShort() {
|
public short getTestShort() {
|
||||||
@ -70,6 +72,7 @@ public class TestStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public TestEmbeddedStruct getTestEmbeddedStruct() {
|
public TestEmbeddedStruct getTestEmbeddedStruct() {
|
||||||
@ -78,6 +81,7 @@ public class TestStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public TestVariableStruct getTestVariableStruct() {
|
public TestVariableStruct getTestVariableStruct() {
|
||||||
@ -86,6 +90,7 @@ public class TestStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public int getTestVariableStructLength() {
|
public int getTestVariableStructLength() {
|
||||||
|
@ -14,6 +14,7 @@ public class TestVariableStruct implements Struct {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter.
|
* Getter.
|
||||||
|
*
|
||||||
* @return value
|
* @return value
|
||||||
*/
|
*/
|
||||||
public byte[] getTestArray() {
|
public byte[] getTestArray() {
|
||||||
|
Loading…
Reference in New Issue
Block a user