PlcEntityInterceptor { @SuppressWarnings({"squid:S3776", "squid:MethodCyclomaticComplexity"}) static Object getTyped(Class<?> clazz, PlcReadResponse response, String sourceFieldName) { LOGGER.debug("getTyped clazz: {}, response: {}, fieldName: {}", clazz, response, sourceFieldName); if (response.getResponseCode(sourceFieldName) != PlcResponseCode.OK) { throw new PlcRuntimeException(String.format("Unable to read specified field '%s', response code was '%s'", sourceFieldName, response.getResponseCode(sourceFieldName))); } if (clazz.isPrimitive()) { if (clazz == boolean.class) { return response.getBoolean(sourceFieldName); } else if (clazz == byte.class) { return response.getByte(sourceFieldName); } else if (clazz == short.class) { return response.getShort(sourceFieldName); } else if (clazz == int.class) { return response.getInteger(sourceFieldName); } else if (clazz == long.class) { return response.getLong(sourceFieldName); } } if (clazz == Boolean.class) { return response.getBoolean(sourceFieldName); } else if (clazz == Byte.class) { return response.getByte(sourceFieldName); } else if (clazz == Short.class) { return response.getShort(sourceFieldName); } else if (clazz == Integer.class) { return response.getInteger(sourceFieldName); } else if (clazz == Long.class) { return response.getLong(sourceFieldName); } else if (clazz == BigInteger.class) { return response.getBigInteger(sourceFieldName); } else if (clazz == Float.class) { return response.getFloat(sourceFieldName); } else if (clazz == Double.class) { return response.getDouble(sourceFieldName); } else if (clazz == BigDecimal.class) { return response.getBigDecimal(sourceFieldName); } else if (clazz == String.class) { return response.getString(sourceFieldName); } else if (clazz == LocalTime.class) { return response.getTime(sourceFieldName); } else if (clazz == LocalDate.class) { return response.getDate(sourceFieldName); } else if (clazz == LocalDateTime.class) { return response.getDateTime(sourceFieldName); } Object responseObject = response.getObject(sourceFieldName); if (clazz.isAssignableFrom(responseObject.getClass())) { return responseObject; } throw new ClassCastException("Unable to return response item " + responseObject + "(" + responseObject.getClass() + ") as instance of " + clazz); } private  PlcEntityInterceptor(); @SuppressWarnings({"unused", "squid:S00107"}) @RuntimeType static Object interceptGetter(@This Object proxy, @Origin Method method, @SuperCall Callable<?> callable,
                                         @FieldValue(PlcEntityManager.PLC_ADDRESS_FIELD_NAME) String address,
                                         @FieldValue(PlcEntityManager.DRIVER_MANAGER_FIELD_NAME) PlcDriverManager driverManager,
                                         @FieldValue(PlcEntityManager.ALIAS_REGISTRY) AliasRegistry registry,
                                         @FieldValue(PlcEntityManager.LAST_FETCHED) Map<String, Instant> lastFetched,
                                         @FieldValue(PlcEntityManager.LAST_WRITTEN) Map<String, Instant> lastWritten); @SuppressWarnings({"unused", "squid:S00107"}) @RuntimeType static Object interceptSetter(@This Object proxy, @Origin Method method, @SuperCall Callable<?> callable,
                                         @FieldValue(PlcEntityManager.PLC_ADDRESS_FIELD_NAME) String address,
                                         @FieldValue(PlcEntityManager.DRIVER_MANAGER_FIELD_NAME) PlcDriverManager driverManager,
                                         @FieldValue(PlcEntityManager.ALIAS_REGISTRY) AliasRegistry registry,
                                         @FieldValue(PlcEntityManager.LAST_FETCHED) Map<String, Instant> lastFetched,
                                         @Argument(0) Object argument); static PlcWriteResponse getPlcWriteResponse(PlcWriteRequest request);  }
@Test public void getTyped_notOkResponse_throws() { DefaultPlcReadResponse response = new DefaultPlcReadResponse(null, Collections.singletonMap("field", new ResponseItem<>(PlcResponseCode.NOT_FOUND, null))); assertThatThrownBy(() -> PlcEntityInterceptor.getTyped(Long.class, response, "field")) .isInstanceOf(PlcRuntimeException.class) .hasMessage("Unable to read specified field 'field', response code was 'NOT_FOUND'"); }