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