Created
September 20, 2016 10:38
-
-
Save PiotrCL/1bf235abe74172baca88915fb3f0af38 to your computer and use it in GitHub Desktop.
Test class that detects how the strictfp modifier is propagated in different scenarios. The test is based on Java Reflection API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Modifier; | |
strictfp interface StrictInterface { | |
void someInterfaceMethod(); | |
default void someInterfaceDefaultMethod() {} | |
class InnerTest { | |
public static void innerMethod() {} | |
} | |
} | |
class Impl implements StrictInterface { | |
@Override | |
public void someInterfaceMethod() {} | |
public strictfp void someClassMethod() {} | |
public void someClassMethod2() {} | |
} | |
public class StrictfpReflectionTest { | |
public static void main(String argv[]) { | |
//The comments next to checkModifiers method call are showing the output on jdk1.8.0_91 on OSX | |
checkModifiers(Impl.class, "someInterfaceMethod"); // someInterfaceMethod modifiers: public | |
checkModifiers(Impl.class, "someClassMethod"); // someClassMethod modifiers: public strictfp | |
checkModifiers(Impl.class, "someClassMethod2"); // someClassMethod2 modifiers: public | |
checkModifiers(Impl.class.getInterfaces()[0], "someInterfaceDefaultMethod"); // someInterfaceDefaultMethod modifiers: public strictfp | |
checkModifiers(StrictInterface.InnerTest.class, "innerMethod"); // innerMethod modifiers: public static strictfp | |
} | |
public static void checkModifiers(Class clazz, String m) { | |
try { | |
int mod = clazz.getDeclaredMethod(m, new Class[0]).getModifiers(); | |
String res = m + " modifiers: " + Modifier.toString(mod); | |
System.out.println(res); | |
} catch (Exception e) { | |
e.printStackTrace(System.out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment