javastrictfp

Java class declared with strictfp modifier fails test with Modifier.isStrict(int)


To be clear, this question is strictly hypothetical. Personally, I have no Real World need for this behaviour; I discovered it accidentally.

Inspired by: How to make a (static) initializer block strictfp?

If I declare a Java class with strictfp modifier:

public strictfp class PublicStrictfpClass
{
    public double f() { return 2d / 3d; }
}

... then I test using Modifier.isStrict(int):

public static void main(String[] argArr)
{
    if (! Modifier.isStrict(PublicStrictfpClass.class.getModifiers()))
    {
        throw new IllegalStateException("Unreachable code");
    }
}

The above code fails: Method main() throws IllegalStateException. I tried a class declaration with and without method PublicStrictfpClass.f() -- both fail.

Do I misunderstand ... ?

I am running this code from Linux using the latest patch for OpenJDK 8.

Finally, I did a bunch of Googling before asking this question. The keyword strictfp is very rare in the wild!


Solution

  • It's probably because the class modifiers in the class file do not include the strictfp (don't know why, must ask Java developers). This is what the getModifiers() method looks into, its documentation states: "The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface". (so no mentioning of strictfp)

    You can still get the modifiers of the method f. The method modifiers do include strictfp:

    Modifier.isStrict(PublicStrictfpClass.class.getMethod("f").getModifiers())