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

Compilation failure: ClassCastException

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.5.4, 2.5.14
    • Fix Version/s: 2.5.15, 4.0.0-beta-1, 3.0.9
    • Component/s: ast builder
    • Labels:
      None
    • Environment:
      JDK: openjdk version "1.8.0_265"(Zulu 8.48.0.53-CA-macosx) (build 1.8.0_265-b11)

      Description

      After upgrading 2.5.3 to 2.5.4, observing the following error in my project:

       

      General error during class generation: java.lang.Long cannot be cast to java.lang.String

      java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
      at org.apache.groovy.ast.tools.ExpressionUtils.transformBinaryConstantExpression(ExpressionUtils.java:86)
      at org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants(ExpressionUtils.java:248)
      at org.codehaus.groovy.classgen.Verifier.addFieldInitialization(Verifier.java:1163)
      at org.codehaus.groovy.classgen.Verifier.addInitialization(Verifier.java:1048)
      at org.codehaus.groovy.classgen.Verifier.addInitialization(Verifier.java:999)
      at org.codehaus.groovy.classgen.Verifier.visitClass(Verifier.java:250)
      at org.codehaus.groovy.control.CompilationUnit$18.call(CompilationUnit.java:811)
      at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1084)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:640)
      at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:618)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:595)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:574)
      ...

       

      This can be solved with:

      From:

      (String) ((ConstantExpression) left).getValue()

      To:

      String.valueOf(((ConstantExpression) left).getValue())

      Diff is as follows:

      diff --git a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
      index 4aa67a52cb..7057f14982 100644
      — a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
      +++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
      @@ -83,7 +83,7 @@ public final class ExpressionUtils {
      Expression left = transformInlineConstants(be.getLeftExpression(), targetType);
      Expression right = transformInlineConstants(be.getRightExpression(), targetType);
      if (left instanceof ConstantExpression && right instanceof ConstantExpression)

      { - return configure(be, new ConstantExpression((String) ((ConstantExpression) left).getValue() + + return configure(be, new ConstantExpression(String.valueOf(((ConstantExpression) left).getValue()) + ((ConstantExpression) right).getValue())); }

       

      If you want, I can raise a PR for this.

        Attachments

          Activity

            People

            • Assignee:
              daniel_sun Daniel Sun
              Reporter:
              dbhowmik Dipanjan Bhowmik
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 10m
                1h 10m