• Post Reply Bookmark Topic Watch Topic
  • New Topic

synthetic classes  RSS feed

 
Anoop Jaloree
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Can anyone tell me what are synthetic classes and what are they used for?

Thx

Anoop
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16046
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard of that term, it's not a common Java term.
Where did you hear about "synthetic classes"?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find isSynthetic() methods in many of the java.lang.reflection classes, such as Method, Field, Constructor, and of course Class (which is really in java.lang) since JDK 5. A synthetic attribute has been part of the class file format for much longer - see JVMS2 4.7.6. Basically a synthetic class/field/method/cionstructor is one that does not appear (in the same form) in the original source code, but has been added or modified by the compiler for some reason.

Most commonly synthetic fields occur in order to support nested classes - when an inner class has access to an instance of an outer class, this is implemented by creating a synthectic field in the inner class, generally named this$0, which holds a reference to the outer class instance.

Offhand I don't know of any good examples of synthetic classes (as opposed to synthetic fields). Maybe this is a feature which is theoretically possible, but not actually used by current compilers. Or more likely, it is used, somewhere, but it's fairly obscure. Or maybe it's used somewhere quite common, and I'm just ignorant of it.

Hope that helps...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Or more likely, it is used, somewhere, but it's fairly obscure. Or maybe it's used somewhere quite common, and I'm just ignorant of it.


Probably dynamic proxy classes (see here) use it. RMI can also create stubs and skeletons on the fly these days, so maybe those classes, too (although those might actually just be dynamic proxies as well.)
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a dynamic proxy class that tells me its not synthetic
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good guess, though, right?
[ April 18, 2006: Message edited by: Ernest Friedman-Hill ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Most commonly synthetic fields occur in order to support nested classes - when an inner class has access to an instance of an outer class, this is implemented by creating a synthectic field in the inner class, generally named this$0, which holds a reference to the outer class instance.


Also if an inner class accesses a private field of the outer class, the compiler creates a synthetic access method (because in byte code, every class is a top level class).


Offhand I don't know of any good examples of synthetic classes (as opposed to synthetic fields).


Just a wild guess, but perhaps anonymous inner classes are marked synthetic?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EFH]: Good guess, though, right?

I suppose it sounded plausible...

[Ilja]: Just a wild guess, but perhaps anonymous inner classes are marked synthetic?

Also a good guess, but apparently not, according to a test I just ran. This one makes sense to me, as we do see a source code declaration of the anonymous class; it's just a bit streamlined compared to a normal class.

I'm not sure how important it really is to know whether a given class or member is synthetic or not. There don't appear to be any rules governing what you can or can't do with a synthetic element. I think it's mostly just for informational purposes. Probably of interest if you're writing an IDE or a decompiler, and you want to be able to show source code for a given feature - you know that if it's marked synthetic, you shouldn't expect to actually show specific code.
 
Anoop Jaloree
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim, Ernest and Jeff for the insight!

Not much relevant reference is available at hand rgd the same, so I hope this thread will be useful to lots.

Anoop
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!