Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8434

ClassCastException: Class cannot be cast to type-of-with's-delegate

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 2.4.12
    • None
    • Compiler
    • None
    • Windows 10, Gradle 4.2.1

    Description

      I'm sorry for the hard repro, but I couldn't figure it out with a smaller one.
      1. https://github.com/TWiStErRob/net.twisterrob.gradle.quality/tree/groovy-cast-bug
      2. gradlew test (if you're lucky it runs and fails, otherwise don't fret there's an other way)

      Test fails:

      Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to org.gradle.api.reporting.ConfigurableReport
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig$_setupReports_closure2$_closure3.doCall(CheckStyleTask.groovy:95)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig$_setupReports_closure2$_closure3.call(CheckStyleTask.groovy)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig$_setupReports_closure2.doCall(CheckStyleTask.groovy:94)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig$_setupReports_closure2.call(CheckStyleTask.groovy)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig.setupReports(CheckStyleTask.groovy:91)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig.execute(CheckStyleTask.groovy:42)
      	at net.twisterrob.gradle.checkstyle.CheckStyleTask$TaskConfig.execute(CheckStyleTask.groovy)
      	at org.gradle.api.internal.tasks.DefaultTaskContainer.create(DefaultTaskContainer.java:148)
      

      Now, that's obviously not my error, because 1) it worked in the previous commit (when the class wasn't an inner class) and 2) I'm using ast(groovy.transform.CompileStatic) to build.

      Another way: gradlew groovyCompile, and then observe the build directory:

      static def setupReports(CheckStyleTask task) {
      	task.reports.with {
      		def reportsDir = task.project.extensions.findByType(ReportingExtension).baseDir
      		xml.with {
      			enabled = true
      			setDestination(new File(reportsDir, 'checkstyle.xml'))
      

      compiles to

      public Object doCall(Object it) {
                                  boolean var2 = true;
                                  ((ConfigurableReport)CheckStyleTask.class).setEnabled(var2);
                                  Object var10001 = null;
                                  ((ConfigurableReport)((_closure4)this).getDelegate()).setDestination(new File((File)ScriptBytecodeAdapter.castToType(this.reportsDir.get(), File.class), "checkstyle.html"));
                                  Object var10000 = null;
      

      Note: the first cast is wrong, the second one is using getDelegate properly.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              twisterrob Robert Papp
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: