Skip to content

Compilation error with JDK 11 #4791

@magg

Description

@magg

Hello I am trying to upgrade an old project that used Spoon 5.9.0 to Spoon 10.1.0 and upgrade the JDK from 8 to 11.

Does Spoon work with a compliance level set to 11 if I want the compiler to work with JDK 11

Here's my code

    public void compileCode(Set<Path> sources, Set<Path> classpath, Path output) {
        if (log.isDebugEnabled()) {
            log.debug("Compiling code with spoon. Source folders: [{}]; output folder: {}; classpath: [{}]",
                    sources.stream().map(Path::toString).collect(joining(", ")),
                    output.toString(),
                    classpath.stream().map(Path::toString).collect(joining(", ")));
        }

        final SpoonModelBuilder compiler = new Launcher().createCompiler();
        sources.forEach(dir -> compiler.addInputSource(dir.toFile()));
        compiler.setBinaryOutputDirectory(output.toFile());
        compiler.setSourceClasspath(classpath.stream().map(Path::toString).toArray(String[]::new));
        compiler.getFactory().getEnvironment().setComplianceLevel(JAVA_VERSION);
        compiler.compile(InputType.FILES);
        checkCompilationSuccess(compiler);

        log.debug("Compilation with spoon is completed successfully.");
    }

    private void checkCompilationSuccess(SpoonModelBuilder compiler) {
        int errorCount = compiler.getFactory().getEnvironment().getErrorCount();
        if (errorCount != 0) {
            throw new CompilationException(
                    "Environment of spoon compiler has " + errorCount + " errors."
                            + " Source folders was " + compiler.getInputSources() + "."
                            + " Sources classpath was " + Arrays.toString(compiler.getSourceClasspath())
            );
        }
    }

But I get this error if JAVA_VERSION is set to 11, it works if I put 8

spoon.support.compiler.SnippetCompilationError: The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files at /tmp/junit145769342327212861/c5149660-9619-4e7e-a660-4768bd0c399a/sources/com/magg/psa/dex/auth/core/ui/pages/core/DynamicPage.java:1

	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.reportProblemsWhenCompiling(JDTBasedSpoonCompiler.java:613)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.compile(JDTBasedSpoonCompiler.java:168)
	at com.magg.pss.routine.service.compile.SpoonCompilationService.compileCode(SpoonCompilationService.java:41)
	at com.magg.pss.routine.service.compile.SpoonCompilationServiceTest.compileCode(SpoonCompilationServiceTest.java:46)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:46)
	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
	at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:258)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
	at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
	at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
	at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:54)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions