I'm trying to get the line number of annotations from .class file, but I can get only the list of annotations, not the lines. Is it possible to do this?
Asked
Active
Viewed 983 times
6
-
1did you mean annotation used in source code(.java file) that line number? – user404 Jan 11 '19 at 08:21
-
@user404 yes, IMO and obviously. – Vishwa Ratna Jan 11 '19 at 08:22
-
1@OP, try with javassist – Vishwa Ratna Jan 11 '19 at 08:23
-
1Hi, you can try with ASM or JavAssist apis to scan the .class file – pasquy73 Jan 11 '19 at 08:24
-
Would you please explain why you need this info? It is really interesting to understand the use case. Thanks. – Hamid Ghasemi Jan 11 '19 at 08:29
-
Yes, I've got the log file and I've to remove the line (in the log file) if refers to the annotation. My input is the .class file – Domenica Jan 11 '19 at 08:34
-
1If I understand your use case, you have to scan (on annotation) to get the line numbers and remove all lines in the log file if it refers to that lines. Is it right? – pasquy73 Jan 11 '19 at 09:03
-
Yes, it's right – Domenica Jan 11 '19 at 09:05
-
1the `.class` file only records line number for debugging purposes. Since you can't debug this metadata, you can only infer a line number which is it on or before. The only way to know is to read the original source. – Peter Lawrey Jan 11 '19 at 09:22
-
1I'm agree with Peter and I think that it's hard (difficult or impossible) to retrieve the lines of the annotations. But you said before that you have a log file, so I suppose that log refers to an exception ... right? – pasquy73 Jan 11 '19 at 10:19
-
Yes, in my error log, I've the stack trace that refers at line of annotation – Domenica Jan 11 '19 at 10:45
-
1I'm not sure, but can the annotation throw an exception? – pasquy73 Jan 11 '19 at 10:52
-
1I suggest you to post this question – pasquy73 Jan 11 '19 at 11:12
-
Thanks, I added a new question [here](https://stackoverflow.com/questions/54145778/annotation-and-exception) – Domenica Jan 11 '19 at 11:39
2 Answers
4
Looking through the Java Class File Specification it seems that for annotations no line number information is recorded.
Since the line number information is not present within the class file you cannot extract it from the class file.
Line numbers are only available for code segments: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.12

Thomas Kläger
- 17,754
- 3
- 23
- 34
0
You can use Java Decompiler. Follow this JD GUI, download the jar file and run it, keep the class files you want to decompile in same place with the jd-gui jar's directory and you will find if any annotation is used in source code there.

user404
- 1,934
- 1
- 16
- 32
-
3As far as I know, to decompile a .class file is not right copy of original .java file, I mean that you cannot get the right line number ... you can try! – pasquy73 Jan 11 '19 at 08:31
-
1most probably(I did and forgot actually) it excludes the commented codeblocks. – user404 Jan 11 '19 at 08:34