INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.

Home Page:http://spoon.gforge.inria.fr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: `<nulltype> cannot be found` when calling `nullType.isEnum()`

Luro02 opened this issue · comments

Describe the bug

This was discovered through this issue #5412. It does not crash in noclasspath mode, where it returns false.

Source code you are trying to analyze/transform

No response

Source code for your Spoon processing

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 testNullLiteralIsEnum() {
        // contract: even for the <nulltype> the methods should work
        Launcher launcher = new Launcher();
        launcher.getEnvironment().setNoClasspath(false);
        launcher.addInputResource(new VirtualFile("public class Example {}", "Example"));
        CtModel ctModel = launcher.buildModel();

        CtTypeReference<?> nullType = ctModel.getRootPackage().getFactory().Type().nullType();
        assertFalse(nullType.isEnum());
    }
}

Actual output

spoon.support.SpoonClassNotFoundException: <nulltype> cannot be found
	at spoon.support.reflect.reference.CtTypeReferenceImpl.isEnum(CtTypeReferenceImpl.java:621)

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

I think there is a solution that replaces the reference defined below in TypeFactory with a specific class to obtain the desired behavior.

public final CtTypeReference<?> NULL_TYPE = createReference(CtTypeReference.NULL_TYPE_NAME);