Merge pull request #876 from nsacyber/v3_issue_873-update-hirs_structs-for-checkstyle

[#873] Fixed Checkstyle Issues In HIRS_STRUCTS
This commit is contained in:
iadgovuser26 2024-11-15 09:27:52 -05:00 committed by GitHub
commit 70d198441c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 104 additions and 89 deletions

View File

@ -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",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,6 +14,7 @@ public class TestVariableStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public byte[] getTestArray() {