rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

IllegalArgumentException when parsing a file

Open uhafner opened this issue 1 year ago • 0 comments

What version of OpenRewrite are you using?

I am using

  • <rewrite-maven-plugin.version>5.39.2</rewrite-maven-plugin.version>
  • <rewrite-testing-frameworks.version>2.17.1</rewrite-testing-frameworks.version>
  • <rewrite-static-analysis.version>1.15.0</rewrite-static-analysis.version>
  • <rewrite-migrate-java.version>2.23.0</rewrite-migrate-java.version>
  • <rewrite-recommendations.version>1.8.4</rewrite-recommendations.version>

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project. I did run the command mvn -X clean rewrite:run.

See https://github.com/uhafner/codingstyle for details. The OpenRewrite configuration is visible in the pom.xml.

What is the smallest, simplest way to reproduce the problem?

package edu.hm.hafner.util;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.ProtectionDomain;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

class ResourceExtractorTest {
    void error() {
        ProtectionDomain protectionDomain = mock(ProtectionDomain.class);

        assertThatIllegalArgumentException()
                .isThrownBy(() -> new ResourceExtractor(ResourceExtractor.class, protectionDomain))
                .withMessageContainingAll("CodeSource for", "ResourceExtractor");
    }

    private String readToString(final Path output) {
        try {
            return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
        }
        catch (IOException exception) {
            throw new UncheckedIOException(exception);
        }
    }
}

What did you expect to see?

No exception.

What did you see instead?

Exception, see stacktrace below.

What is the full stack trace of any errors you encountered?

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.39.2:dryRun (default-cli) on project codingstyle: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.39.2:dryRun failed: Error while visiting src/test/java/edu/hm/hafner/util/ResourceExtractorTest.java: java.lang.IllegalArgumentException: Could not parse as Java
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:264)
  java.base/java.util.Optional.orElseThrow(Optional.java:403)
  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:264)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:176)
  org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:290)
  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:171)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.maybeReplaceStatement(JavaTemplateJavaExtension.java:451)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitStatement(JavaTemplateJavaExtension.java:445)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitStatement(JavaTemplateJavaExtension.java:55)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:936)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitVariableDeclarations(JavaTemplateJavaExtension.java:501)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitVariableDeclarations(JavaTemplateJavaExtension.java:55)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5785)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:147)
  ...
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:333)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)

Are you interested in [contributing a fix to OpenRewrite]

Not right now.

uhafner avatar Sep 12 '24 13:09 uhafner