经过短暂的研究,恐怕这是不可能的。请参阅对此问题的第一条评论
如果您查看反编译的类,您会看到编译器从所有最终参数中删除了 final 关键字。这是合理的,因为 final 仅在编译时才有意义。
我试图在这方面找到更多证据,但没有成功。无论如何,我根据第一个建议的配音答案做了一个小测试。
有像这样的测试课
public class TestClass {
public void testMethod(final Integer param) {} // could be also int
}
并运行此测试/记录
@Test
public void testIsFinal() throws NoSuchMethodException, SecurityException {
Method method = TestClass.class.getMethod("testMethod", Integer.class);
log.info("method {}", method);
Parameter[] params = method.getParameters();
log.info("params {}", params);
log.info("params[0] {}", params[0]);
log.info("modifiers {}", params[0].getModifiers());
log.info("final {}", Modifier.isFinal( params[0].getModifiers() ));
}
日志
2017-12-11 13:11:24.512 INFO org.example.jpa.JUnitTest:33 - 方法公共无效 org.example.jpa.JUnitTest$TestClass.testMethod(java.lang.Integer)
2017-12-11 13:11 :24.521 INFO org.example.jpa.JUnitTest:36 - 参数 [0] java.lang.Integer arg0
2017-12-11 13:11:24.521 INFO org.example.jpa.JUnitTest:37 - 修饰符 0
2017-12- 11 13:11:24.522 信息 org.example.jpa.JUnitTest:38 - 最终错误
所以似乎final
编译的方法声明中不存在唯一允许的方法参数修饰符。请注意,它既不存在于记录的方法签名中
public void org.example.jpa.JUnitTest$TestClass.testMethod(java.lang.Integer)
从源代码Modifier
public static final int FINAL = 0x00000010;
和
/**
* The Java source modifiers that can be applied to a method or constructor parameter.
* @jls 8.4.1 Formal Parameters
*/
private static final int PARAMETER_MODIFIERS = Modifier.FINAL;
和
public static boolean isFinal(int mod) {
return (mod & FINAL) != 0;
}
所以告诉方法参数被声明为final
第二位应该是1。