3

i'm using java reflection to get method which are annotated with a specific Annotation . it returns two method , the one with modifier 4161 belongs to the interface . but i check the modifier specifications and can't find it anywhere ... help needed , tks :)

enter image description here

Adams.H
  • 1,603
  • 4
  • 20
  • 29
  • i didn't realise `4161` is a combination modifier and i've googled `4161` no relative result found at all . the duplicate you post is for those who have already know combination modifier . – Adams.H Mar 26 '19 at 08:15

1 Answers1

10

The modifiers integer is basically a combination of integer flags that form a bit field. You can use the static Modifier.toString() method to get a textual representation. If you would use this method, it would tell you that 4161 stands for public volatile, and it would be wrong.

To break it down, the bit field represented by 4161 is composed of 3 integer flags: 1, 64 and 4096. Looking up these values in the Modifier Javadoc, it will tell you that 1 stands for public and 64 stands for volatile. Surprising, because methods cannot be declared as volatile, and what about 4096? It's not even in the list!

The answer can be found in the JVM specification, where we find that:

  • 4096 (0x1000) indicates a synthetic method, i.e. a method that is not present in the source code.
  • 64 (0x0040) not only represents the volatile access modifier, but can also be used to signify that a method is a bridge method, i.e. a method that is generated by the compiler.

The conclusion is then that a method with a modifiers value of 4161 is a public synthetic bridge method. This article provides a fairly comprehensive overview.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • The `public` bit is `0x0001` and the `volatile` bit is `0x0040`, so what's the other `0x1000` bit correspond to, where (`4161` is `0x1041`)? The greatest bit in the [modifier values](https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.lang.reflect.Modifier.ABSTRACT) is only `0x0800`. – FThompson Mar 26 '19 at 08:05
  • 2
    @Vulcan Appears to be a synthetic flag: https://stackoverflow.com/questions/8540768/when-is-the-jvm-bytecode-access-modifier-flag-0x1000-hex-synthetic-set Will add to answer. – Robby Cornelissen Mar 26 '19 at 08:15
  • thanks for your reply , can you give a further explanation about `public volatile` , is this identify the `public` method in `Interface` ? – Adams.H Mar 26 '19 at 08:24
  • 1
    It's probably not a volatile method, but a bridge method. The value 0x40 is defined twice, once as `VOLATILE`, once as `BRIDGE`. See explanation [here](http://stas-blogspot.blogspot.com/2010/03/java-bridge-methods-explained.html). You could call `Method.isBridge()` to find out. – Robby Cornelissen Mar 26 '19 at 08:35