1

I'm wondering what is completely lost during the Java compilation process.

For example, I know that annotation variables are not retained: http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html

Are there more things like this ?

  • Quick answer is everything you can't access via reflections. – Japhei Jul 03 '22 at 15:42
  • Please note that annotations are kept if you set the retention to runtime. – Japhei Jul 03 '22 at 15:44
  • Ermm .... no. You can't access the bytecodes themselves by reflection for a start. – Stephen C Jul 03 '22 at 15:44
  • 1
    Local variable annotations are not retained in class files (or at runtime) regardless of the retention policy set on the annotation type. See JLS 9.6.1.2. Reference: http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html – Idriss Riouak Jul 03 '22 at 15:45
  • @StephenC If I have understood the question correctly, it is not about accessing the bytecode, but rather knowing which elements of Java code are transferred to the bytecode and which are omitted. – Japhei Jul 03 '22 at 15:51
  • @Japhei, Yes exactly! I'm trying to understand what is completely lost during the building process. – Idriss Riouak Jul 03 '22 at 15:54
  • 2
    @IdrissRiouak I'm not a pro in compiled Java, but I can tell you that e.g. the names of variables are completely lost. There will be many things that get lost during the compilation. It would help if you could narrow down your question to what usecase you need this for. – Japhei Jul 03 '22 at 16:05
  • Thank you @Japhei ! I was wondering if "final" is retained in any way? – Idriss Riouak Jul 03 '22 at 16:11
  • Of the top of my head, the order of declarations of fields, methods etc in the class file are not guaranteed to be the same order as in the Java source. – Sweeper Jul 03 '22 at 16:13
  • `final` is retained, as long as the entire field is. Sometimes the field gets inlined though, if the value is a compile-time constant. – Sweeper Jul 03 '22 at 16:15
  • @IdrissRiouak I found a document explaining the compilation of Java. This might be a bit outdatet but the core should be the same: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html It sais that the final modifier is being retained. Out of my head I would add that effectively final variables have to be marked final in the byte code too, but I couldn't find any proof of that. Maybe you could just try to read into this in the document. – Japhei Jul 03 '22 at 16:22
  • I found this, and apparently `final` for local variables is not retained. [See reference](https://stackoverflow.com/questions/7704024/how-can-one-tell-if-a-local-variable-is-final-from-java-bytecode-related-to) "The finality of local variables is checked by the compiler and doesn't make it to the bytecode. This information isn't required at runtime, and hence isn't stored in the bytecode. The JVM treats final and non-final local variables in the same way." – Idriss Riouak Jul 03 '22 at 16:24
  • @Japhael - The question as written is ambiguous. But "everything" means everything. Your "Quick answer" is not an accurate answer. – Stephen C Jul 03 '22 at 22:48
  • 1
    “annotation variables” is not a thing. If you know the correct term, as shown in [this comment](https://stackoverflow.com/questions/72847839/what-is-not-retained-in-the-bytecode#comment128669917_72847839) you should [edit] your question to correct it. Generally, you should not use outdated 3rd party documents to discuss such things. The current authoritative source is [JLS, §9.6.4.2](https://docs.oracle.com/javase/specs/jls/se17/html/jls-9.html#jls-9.6.4.2) It used to be §9.6.1.2 a decade ago. – Holger Jul 04 '22 at 09:16
  • The latest JVM spec is here: https://docs.oracle.com/javase/specs/jvms/se18/html/index.html For a comprehensive analysis you would have to scan the specification for differences. Off the top of my head I am reasonably confident about (1) there are no annotations for column-level source positions, (2) no `import` declarations. More complex control flow (closures, finally) may be ambiguous. One approach to find more: decompile and check differences against e.g. the ExtendJ AST, but I'm not sure that that is worthwhile. Note that the ST group at KTH had papers on decompilation that may help. – creichen Aug 25 '22 at 17:27

0 Answers0