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

Bug with @TypeChecked and generic properties of Java classes used in binary expressions

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.5
    • Fix Version/s: 2.4.6
    • Component/s: Compiler
    • Labels:
      None

      Description

      I have the following two Java Classes:

      import java.math.BigDecimal;
      
      public abstract class Attribute<T> {
      
          private BigDecimal numericValue;
      
          public abstract T getValue();
      
          public abstract void setValue(T value);
      
          public BigDecimal getNumericValue() {
              return numericValue;
          }
      
          public void setNumericValue(BigDecimal numericValue) {
              this.numericValue = numericValue;
          }
      }
      
      import java.math.BigDecimal;
      
      public class LongAttribute extends Attribute<Long> {
          public Long getValue() {
              return getNumericValue() == null ? null : getNumericValue().longValue();
          }
          public void setValue(Long value) {
              setNumericValue(BigDecimal.valueOf(value));
          }
      }
      

      And a Groovy test class with @TypeChecked annotation:

      import groovy.transform.TypeChecked
      
      @TypeChecked
      class Scratch {
          public static void main(String[] args) {
              LongAttribute attr = new LongAttribute();
              attr.value = 0L
              println "getter: " + attr.value
              println "smaller as 0? " + (attr.value < 0)
          }
      }
      

      Compiling the class leads to the error message:

      Error:(10, 36) Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#compareTo(int). Please check if the declared type is right and if the method exists.
      

      Removing the @TypeChecked annotation or moving the Java classes into the Groovy class as static inner classes suppresses the compilation error.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              m.ewert Marc Ewert

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment