[Bug]: The type of all expressions in an array (`T[]`) init should be `T` and not `<nulltype>`
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
### Describe the bug
I expected the type of all expressions inside an array init expression to be set and not cause a crash. In my case I tried to call `isEnum` on a `` (I will report this in another issue).
It is confusing that null literals do not have the correct type set and instead have some dummy placeholder called ``. Especially, when the type is known based on the context. Additionally, having some unrelated type set, will result in wrong values returned by the methods on the `CtTypeReference`. The `isEnum` method for example, would most likely return `false`, but that is not correct in every context (see below code for an example).
I guess this is not only limited to array init expressions, but just spoon that sees a `null` literal and sets it's type to ``.
### Source code you are trying to analyze/transform
```Java
public class Example {
public static Fruit[] fruits = new Fruit[] { null };
public enum Fruit { APPLE, PEAR; }
}
```
### Source code for your Spoon processing
```Java
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.compiler.VirtualFile;
import static org.junit.jupiter.api.Assertions.*;
class TestSpoon {
@Test
void testNullLiteralTypeInArray() {
// contract: the type of an element in an array T[] is always T
CtClass ctClass = Launcher.parseClass(
"public class Example {\n" +
" public static Fruit[] fruits = new Fruit[] { null };\n" +
"\n" +
" public enum Fruit { APPLE, PEAR; }\n" +
"}\n"
);
CtField ctField = ctClass.getField("fruits");
CtNewArray ctNewArray = (CtNewArray) ctField.getDefaultExpression();
CtTypeReference initType = ctNewArray.getElements().get(0).getType();
CtTypeReference arrayType = ((CtArrayTypeReference) ctNewArray.getType()).getArrayType();
assertEquals("Example$Fruit", arrayType.getQualifiedName());
assertEquals(arrayType, initType);
}
}
```
### Actual output
```Java
spoon.support.SpoonClassNotFoundException: cannot be found
at spoon.support.reflect.reference.CtTypeReferenceImpl.isEnum(CtTypeReferenceImpl.java:621)
at org.example.playground.TestSpoon.testNullLiteralIsEnum(TestSpoon.java:26)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
```
### Expected output
_No response_
### Spoon Version
10.4.0
### JVM Version
openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment Temurin-17.0.1+12 (build 17.0.1+12) OpenJDK 64-Bit Server VM Temurin-17.0.1+12 (build 17.0.1+12, mixed mode, sharing)
### What operating system are you using?
Windows 10
Contributor guide
Assessment
This issue has not been assessed yet.