typetools / typetools/checker-framework

Units Checker: custom qualifiers cannot be nested classes

Open
#2,090 0 comments 0 reactions 1 assignee View on GitHub

@jyluo is already working on this.

Since Aug 4, 2018.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Take this example (from https://groups.google.com/d/msg/checker-framework-discuss/M0tPOCT9RGA/1YrR_ndCDAAJ):

import org.checkerframework.checker.units.qual.*;

import org.checkerframework.framework.qual.SubtypeOf;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Test {
    /**
     * Fraction
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
    @SubtypeOf(UnknownUnits.class)
    public static @interface frac {
        Prefix value() default Prefix.one;
    }
    
    /**
     * Percent
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
    @SubtypeOf(UnknownUnits.class)
    @UnitsMultiple(quantity=frac.class, prefix=Prefix.centi)
    public static @interface pct {
    }

    public @pct Float test(final String param) {
        if (param.equals("foo")) {
            return null;
        }      
        return toFrac(3f);
    }
   
    @SuppressWarnings("units")
    @pct Float toFrac(float number) {
        return number;
    }       
}

One gets:

error: Could not load class 'Test.frac' for field 'quantity' in annotation @org.checkerframework.checker.units.qual.UnitsMultiple(quantity=Test.frac.class, prefix=org.checkerframework.checker.units.qual.Prefix.centi)
  Compilation unit: Test.java
  Last visited tree at line 35 column 5:
      public @pct Float test(final String param) {
  Exception: java.lang.ClassNotFoundException: Test.frac; Stack trace: java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  java.lang.Class.forName0(Native Method)
  java.lang.Class.forName(Class.java:348)
  org.checkerframework.javacutil.AnnotationUtils.getElementValueClass(AnnotationUtils.java:662)
  org.checkerframework.checker.units.UnitsAnnotatedTypeFactory.aliasedAnnotation(UnitsAnnotatedTypeFactory.java:107)

Adding -AunitsDirs=. or -cp . has no effect.
If this is an inherent restrictions, we should document it clearly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.