• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

javap is not printing annotations

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using JDK1.5 I have written and compiled a java class using annotations.

Code snippet:

@Stateless
@Remote(Concat.class)
public class ConcatBean implements Concat{
---
---
---

After compiling this, when I give the javap command like follows, I did not get any line with annotation entry like @Stateless.

Compiled from "ConcatBean.java"
public class ConcatBean extends java.lang.Object implements Concat{
public ConcatBean();
public java.lang.String concateString(java.lang.String, java.lang.String);
}
How to get annotation entries using javap or any other tool. I have chekced with JAD also, it is not printing.

Any idea guys?

Thanks,
Guru
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, annotations are discarded by the compiler, so they will not be available in the class file. Only if the annotation itself is annotated with @Retention(RetentionPolicy.RUNTIME), the information will be preserved in the class file.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
By default, annotations are discarded by the compiler, so they will not be available in the class file. Only if the annotation itself is annotated with @Retention(RetentionPolicy.RUNTIME), the information will be preserved in the class file.



I don't think that's correct. The default is RetentionPolicy.CLASS, which causes the annotations to be present in the class file; they're just not accessible through the JVM at runtime (that's the difference to RetentionPolicy.RUNTIME). But an external tool like javap or jad should be able to read them.

My guess would be that javap (as well as decompilers like JAD or JODE that have not been updated in years) predate Java 5, and consequently simply don't know about annotations.
[ November 06, 2007: Message edited by: Ulf Dittmer ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf's guess seems to be correct. The javap in JDK 6 doesn't give any info on generic methods either, even though that info is definitely part of the class file. IntelliJ, for example, can show both generics and annotations for a class that it only has the class file for. It's annoying that javap hasn't been updated in this respect.
[ November 06, 2007: Message edited by: Jim Yingst ]
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose one might say that javap is meant for showing the bytecode, and it does that just fine. Both generics and annotations are not actually bytecode, but class (or field or method) attributes, so in that sense they're outside the realm of javap.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I understand. Both generics and annotations are represented in bytecode (well, depending on the retention policy of the annotation). Even if the info is dropped at runtime, it's needed by the compiler when you compile with a library with no source, and that library uses generics or annotations. The default function of javap (if you don't use the -c option) is to just show you the method declarations (sans body) - and there in particular, it seems like you should be shown the generic part of the signatures. There's a difference in bytecode between

and

but javap shows both as

You can find documentation on the changes made to the class file format for JDK 5 here. To see what's different, look for black change bars, and text in blue or red. Those are all changes in bytecode related to new language constructs such as generics and annotations.
[ November 06, 2007: Message edited by: Jim Yingst ]
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what exactly one means by "bytecode". Is it everything that's inside of a class file? Or just the actual JVM instructions that are part of the Code attributes?

Method signatures and annotation information are not part of the Code attribute, they're in Signature and Runtime[In]visibleAnnotations attributes, respectively. There are various other attributes that javap has never displayed, like Deprecated, SourceFile and LineNumberTable.

In any case, javap sure could use an update to at least display some of this information. As a GUI alternative, jclasslib is a great tool for viewing the innards of class files.
[ November 07, 2007: Message edited by: Ulf Dittmer ]
 
Your mother was a hamster and your father was a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic