Hi everyone, I have this problem to ask.
Sonarcloud gives error “This accessibility bypass should be removed” to lines with **. how can I solve this problem?
The lines are:
field.set(containingObj, value);
field.setInt(containingObj, (Integer) value);
field.setBoolean(containingObj, (Boolean) value);
field.setByte(containingObj, (Byte) value);
field.setChar(containingObj, (Character) value);
Help me!!
void setFieldValue(final Object containingObj, final Object value) {
try {
if (value == null) {
setFieldValueMethod1();
return;
}
switch (primitiveType) {
case NON_PRIMITIVE:
**field.set(containingObj, value);**
break;
case CLASS_REF:
setFieldValueMethod2(value);
break;
case INTEGER:
setFieldValueMethod3(value);
**field.setInt(containingObj, (Integer) value);**
break;
case LONG:
setFieldValueMethod4(value);
break;
case SHORT:
setFieldValueMethod5(value);
break;
case DOUBLE:
setFieldValueMethod6(value);
break;
case FLOAT:
setFieldValueMethod7(value);
break;
case BOOLEAN:
setFieldValueMethod8(value);
**field.setBoolean(containingObj, (Boolean) value);**
break;
case BYTE:
setFieldValueMethod9(value);
**field.setByte(containingObj, (Byte) value);**
break;
case CHARACTER:
setFieldValueMethod10(value);
**field.setChar(containingObj, (Character) value);**
break;
default:
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException(
"Could not set field " + field.getDeclaringClass().getName() + "." + field.getName(), e);
}
}
private void setFieldValueMethod10(final Object value) {
if (!(value instanceof Character)) {
throw new IllegalArgumentException(
"Expected value of type Character; got " + value.getClass().getName());
}
}
private void setFieldValueMethod9(final Object value) {
if (!(value instanceof Byte)) {
throw new IllegalArgumentException(
"Expected value of type Byte; got " + value.getClass().getName());
}
}
private void setFieldValueMethod8(final Object value)
throws IllegalArgumentException {
if (!(value instanceof Boolean)) {
throw new IllegalArgumentException(
"Expected value of type Boolean; got " + value.getClass().getName());
}
}
private void setFieldValueMethod7(final Object value) {
if (!(value instanceof Float)) {
throw new IllegalArgumentException(
"Expected value of type Float; got " + value.getClass().getName());
}
}
private void setFieldValueMethod6(final Object value) {
if (!(value instanceof Double)) {
throw new IllegalArgumentException(
"Expected value of type Double; got " + value.getClass().getName());
}
}
private void setFieldValueMethod5(final Object value) {
if (!(value instanceof Short)) {
throw new IllegalArgumentException(
"Expected value of type Short; got " + value.getClass().getName());
}
}
private void setFieldValueMethod4(final Object value) {
if (!(value instanceof Long)) {
throw new IllegalArgumentException(
"Expected value of type Long; got " + value.getClass().getName());
}
}
private void setFieldValueMethod3(final Object value) {
if (!(value instanceof Integer)) {
throw new IllegalArgumentException(
"Expected value of type Integer; got " + value.getClass().getName());
}
}
private void setFieldValueMethod2(final Object value) {
if (!(value instanceof Class)) {
throw new IllegalArgumentException(
"Expected value of type Class<?>; got " + value.getClass().getName());
}
}
private void setFieldValueMethod1() {
if (primitiveType != PrimitiveType.NON_PRIMITIVE) {
throw new IllegalArgumentException("Tried to set primitive-typed field "
+ field.getDeclaringClass().getName() + "." + field.getName() + " to null value");
}
}