isSynthetic()

Discussion in 'Java' started by The Abrasive Sponge, Jan 21, 2005.

  1. 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 Abrasive Sponge, Jan 21, 2005
    #1
    1. Advertising

  2. The Abrasive Sponge wrote:
    > 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.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Jan 21, 2005
    #2
    1. Advertising

  3. The Abrasive Sponge wrote:

    > 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

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Jan 21, 2005
    #3
  4. Thomas Fritsch wrote:
    > The Abrasive Sponge wrote:
    >
    >> 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
    >


    Thanks for your help, I saw that link too.
    The Abrasive Sponge, Jan 21, 2005
    #4
  5. Daniel Sjöblom wrote:

    > The Abrasive Sponge wrote:
    >
    >> 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.
    >


    Thanks, I think when you come across an anonymous class, it is synthetic.
    The Abrasive Sponge, Jan 21, 2005
    #5
  6. The Abrasive Sponge wrote:
    > Daniel Sjöblom wrote:
    >
    >> The Abrasive Sponge wrote:
    >>
    >>> 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.
    >>

    >
    > 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
    John C. Bollinger, Jan 21, 2005
    #6
  7. John C. Bollinger wrote:
    >> 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.


    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().
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Jan 21, 2005
    #7
  8. "Daniel Sjöblom" <_NOSPAM> wrote in message
    news:csrhsr$14q0$...
    > The Abrasive Sponge wrote:
    > > 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.


    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) {
    }
    } }
    Larry Barowski, Jan 22, 2005
    #8
  9. The Abrasive Sponge

    Chris Uppal Guest

    Daniel Sjöblom wrote:

    > > 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.).


    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
    Chris Uppal, Jan 22, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page