Skip to content

Reflection with system classes is used to generate a Functional interface instance #1041

Open
@tyuldashev

Description

@tyuldashev

Description
For quite simple method which accepts interface Supplier<T> as a parameter very complex test code is generated.

To Reproduce

Steps to reproduce the behavior:

public class ExtractBug {
    Integer supplier(Supplier<Integer> supplier) {
        return supplier.get() * supplier.get();
    }
}
  1. Set test generation to 'Symbolic' only code generation
  2. Generate tests for code above

Expected behavior
"Quite simple" tests are generated.

Actual behavior
One of generated test looks like below, though as result it just throws NullPointerException.

    @Test
    @DisplayName("supplier: return supplier.get() * supplier.get() : True -> ThrowNullPointerException")
    public void testSupplier_ThrowNullPointerException_1() throws Throwable {
        ExtractBug extractBug = new ExtractBug();
        Object supplier = createInstance("java.util.ServiceLoader$ProviderImpl");
        setField(supplier, "java.util.ServiceLoader$ProviderImpl", "factoryMethod", null);
        AccessControlContext accessControlContext = ((AccessControlContext) createInstance("java.security.AccessControlContext"));
        setField(supplier, "java.util.ServiceLoader$ProviderImpl", "acc", accessControlContext);
        
        /* This test fails because method [org.test.lambda.ExtractBug.supplier] produces [java.lang.NullPointerException]
            java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:803)
            java.base/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:722)
            org.test.lambda.ExtractBug.supplier(ExtractBug.java:13) */
        Class extractBugClazz = Class.forName("org.test.lambda.ExtractBug");
        Class supplierType = Class.forName("java.util.function.Supplier");
        Method supplierMethod = extractBugClazz.getDeclaredMethod("supplier", supplierType);
        supplierMethod.setAccessible(true);
        Object[] supplierMethodArguments = new Object[1];
        supplierMethodArguments[0] = supplier;
        try {
            supplierMethod.invoke(extractBug, supplierMethodArguments);
        } catch (InvocationTargetException invocationTargetException) {
            throw invocationTargetException.getTargetException();
        }
    }

Do we need such test at all? Or any chance that we could simplify it? At least part of initialization?

Please convert to discussion and/or assign proper person.

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bugctg-questionFurther information is requestedpriority-top-focusTop priority chosen by dev team

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions