This accessibility bypass should be removed. - Reflection should not be used to increase accessibility of classes, methods, or fields

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");

    }

}

Hi Lorenzo,

About the rule:

  • S3011 Reflection should not be used to increase accessibility of classes, methods, or fields

This rule easily finds when you try to force the accessibility using setAccessible(true), but about the reflection setters, it’s more complicated. The rule should check the private or protected modifiers of fields, but in the context of reflection, it’s not possible, there is no semantic information, so it’s a limitation.

My recommendation is to manually check that the code is safe here and change the code smells using "Resolve as won't fix" in SonarCloud.

Thank you for your answer. I think I will set won’t to fix because the code is safe.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.