isSynthetic()

  • Thread starter The Abrasive Sponge
  • Start date
T

The Abrasive Sponge

This method is in the java.lang.Class class. The javadoc says that this
tests whether this class is sythetic as stated in the Java
Specification. Looks as if I cannot find it, but it looks like
something I am interested in. Anybody have info on this?
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

The said:
This method is in the java.lang.Class class. The javadoc says that this
tests whether this class is sythetic as stated in the Java
Specification. Looks as if I cannot find it, but it looks like
something I am interested in. Anybody have info on this?

A class may be marked as synthetic if it is generated by the compiler,
that is, it does not appear in the source code. I have never come across
a synthetic class.
 
T

Thomas Fritsch

The said:
This method is in the java.lang.Class class. The javadoc says that this
tests whether this class is sythetic as stated in the Java
Specification. Looks as if I cannot find it, but it looks like
something I am interested in. Anybody have info on this?
The Method Class#isSynthetic has been introduced with Java 5.
But the "Java Language Specification" for Java 5 seems not yet to be
officially available. See also
http://forum.java.sun.com/thread.jspa?threadID=575108&messageID=2871626
 
T

The Abrasive Sponge

Daniel said:
A class may be marked as synthetic if it is generated by the compiler,
that is, it does not appear in the source code. I have never come across
a synthetic class.

Thanks, I think when you come across an anonymous class, it is synthetic.
 
J

John C. Bollinger

The said:
Thanks, I think when you come across an anonymous class, it is synthetic.

I would be surprised. The _name_ assigned to an "anonymous" class is
pseudo-synthetic, but the class itself is fully defined by the source code.

If you happen to test your hypothesis then I, at least, would be
interested in the result.


John Bollinger
(e-mail address removed)
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

John said:
I would be surprised. The _name_ assigned to an "anonymous" class is
pseudo-synthetic, but the class itself is fully defined by the source code.

If you happen to test your hypothesis then I, at least, would be
interested in the result.

Well, I have now found a synthetic class, sort of. And it is indeed
anonymous. But this is something new in java 1.5. The InnerClasses
attribute for an anonymous class now has the synthetic flag set (it did
not exist prior to java 1.5. Instead 'synthetic' was marked by an
attribute, and the InnerClasses attribute could not have any extra
attributes.).

But the class itself is not marked synthetic, which is slightly strange,
and I will almost bet that the isSynthetic() method will return false if
the class itself is not marked synthetic. To illustrate what I mean
(this is a decompilation of some java 1.5 code, presumably compiled with
javac):

/* synthetic is missing here */
class javax.swing.plaf.basic.BasicIconFactory$1 extends java.lang.Object
{
@sourcefile = BasicIconFactory.java
@inner classes:
{
inner: javax.swing.plaf.basic.BasicIconFactory$1
outer: javax.swing.plaf.basic.BasicIconFactory
inner name: 0 (anonymous)
inner type: synthetic class /* but synthetic is present here */
}
}

Anyhow, the synthetic attribute/flag is completely optional, so I would
not rely on any information given by isSynthetic().
 
L

Larry Barowski

Daniel Sjöblom said:
A class may be marked as synthetic if it is generated by the compiler,
that is, it does not appear in the source code. I have never come across
a synthetic class.

Here is the one example that I know of: In order for an
outer class to call the private constructor of an inner class,
a synthetic default-access constructor is generated. To
avoid conflicts with other constructors, the first argument
is a dummy with the type of an anonymous inner class (of
the outer class). If the outer class already has an
anonymous inner class, that will be used. Otherwise, a
synthetic anonymous inner class will be created.
For example:

class MakeSynthetic {
private class Inner {
private Inner() {
} }

static public void main(String[] arg) {
(new MakeSynthetic()).new Inner();
try {
System.out.println(Class.forName
("MakeSynthetic$1").isSynthetic()); }
catch(ClassNotFoundException e) {
}
} }
 
C

Chris Uppal

Daniel said:
Well, I have now found a synthetic class, sort of. And it is indeed
anonymous. But this is something new in java 1.5. The InnerClasses
attribute for an anonymous class now has the synthetic flag set (it did
not exist prior to java 1.5. Instead 'synthetic' was marked by an
attribute, and the InnerClasses attribute could not have any extra
attributes.).

Yes, annoying isn't it. I wish they'd stop buggering about. (And, come to
that, I wish they'd check that the classfiles they distribute are actually
valid /before/ sending them out -- there are several .class files in the crypto
stuff where the 'EnclosingMethod' attribute incorrectly has "utf8" pool
constant references rather than classname and name&type references -- possibly
a bug in the obfuscator).

One case where a class is /completely/ synthetic occurs with code like:

==========
public class Test
{
public static class Nested
{
private final String m_name;
private Nested(String name) { m_name = name; }
}
public static final Nested NESTED = new Nested("NESTED");
}
===========

for which javac generates an empty "marker" class purely to ensure uniqueness
of
the (synthetic) non-private constructor for Test$Nested. Using jdk1.5.0 the
generated class (actually called Nested$1) is not marked synthetic at all (in
either way). Makes you think they just don't care...

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top