Why?



  • I just found this pattern repeated in about a hundred different classes:

     

    public class A { public Class getTheClass() { return B.class; } }
    public class B { public Class getTheClass() { return C.class; } }
    public class C { public Class getTheClass() { return A.class; } }

    ...with no apparent rhyme or reason!

    The checkin comments don't mention anything about it and the developer is long gone.

    For those not familiar with Java, every object has a built-in final method: Class getClass() - which returns the underlying runtime class.



  • Is there any way you can find usage?

    Honestly, this seems like sabotage to me, though thinly veiled...  I would say it's outright stupidity/not understanding if it wasn't for the methods returning other classes.



  • Is it actually returning the wrong class in each one, or is that an anonymization artifact?


  • Discourse touched me in a no-no place

    "Why?" for this code falls in the category of questions you don't bother asking because you know in advance you won't like the answer. A better question might be: why isn't this on the front page?



  • I can't imagine something like that would compile in C++.


  • Discourse touched me in a no-no place

    @snoofle said:

    The checkin comments don't mention anything about it and the developer is long gone.
    A parting gift, maybe?



  • @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     



  • @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     

    Hmm, I just tried in C# and it won't work there, either.


  • @Nexzus said:

    I can't imagine something like that would compile in C++.

    Well, I can imagine.

    History has taught me reality doesn't pay much respect to my imagination, however.

     



  • @Someone You Know said:

    Is it actually returning the wrong class in each one, or is that an anonymization artifact?
    I anonymized the names of the classes, but it really returned the wrong class in each getTheClass() method. It's only used in one location for just one of the objects, but there are a whole lot of copy-paste classes all over the application.

    Interestingly, the one place where it's used just gets dumped to a log, so it's harmless, except that it's misleading.

    I was told not to spend the time cleaning it up because there was more important work to do.



  • @snoofle said:

    @Someone You Know said:

    Is it actually returning the wrong class in each one, or is that an anonymization artifact?
    I anonymized the names of the classes, but it really returned the wrong class in each getTheClass() method. It's only used in one location for just one of the objects, but there are a whole lot of copy-paste classes all over the application.

    Interestingly, the one place where it's used just gets dumped to a log, so it's harmless, except that it's misleading.

    I was told not to spend the time cleaning it up because there was more important work to do.

    I remember a company where I worked, where someone was trying to enforce security through obscurity. But it wasn't just making the code hard to read (they did it, and I made a thread about how they used human languages to try to accomplish that). They also had a special library that only deceptive code. It was nothing but a huge collection of objects that did nothing but nops, with fake documentation and misleading names for objects and methods. Sometimes I wonder if it's really a small world after all and you happen to be seeing something done by the same guy who had me translating code to Portuguese once.



  • built-in final method: Class getClass()

    And now I see how smart the guys who designed Java were for making it "final". You can thank them too.

    Why wouldn't this compile in C# or in C++?




  • The only thing that I could think of (which may or may not apply to you) is what happens with, for instance, Hibernate. It creates proxy classes (and injects the ORM code for you), so you're often not dealing with the class that you thought you were, but a subclass. So myObject.getClass() could have been returning some odd results, leading the developer to this half baked solution.

    There are, of course, ways to detect this, etc, but this seems like it would do the job, too, especially if you didn't have the patience to wade through Hibernate's javadocs or search results with mildly related blog entries. The downside is that you'd have to do this for every single class, of course, as opposed to a generic method.



  • Well, different from A.getClass() that returns A, A.getTheClass() will return B. So, yeah...



  • Consipiracy theory: the person who wrote this code only did so to make Java appear worse than it is. Its a smear campaign I tell you, and this thread is spreading it.



  • @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

    Hmm, I just tried in C# and it won't work there, either.
    Interestingly enough, it compiles in JavaScript...


  • @erikal said:

    Consipiracy theory: the person who wrote this code only did so to make Java appear worse than it is. Its a smear campaign I tell you, and this thread is spreading it.

    I think Java does a good enough job on its own...



  • @C-Octothorpe said:

    Interestingly enough, it compiles in JavaScript...
     

    It most certainly does not.

    SyntaxError: class is a reserved identifier




  • @ubersoldat said:

    built-in final method: Class getClass()
    And now I see how smart the guys who designed Java were for making it "final". You can thank them too. Why wouldn't this compile in C# or in C++?

    Because their objects don't have members named "class"?



  • @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     

    Hmm, I just tried in C# and it won't work there, either.
    It does compile in SnoofleOS.


  • @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     

    Hmm, I just tried in C# and it won't work there, either.
    No shit. C# uses typeof(A) versus A.class.

     



  • @AngelSL said:

    @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     

    Hmm, I just tried in C# and it won't work there, either.
    No shit. C# uses typeof(A) versus A.class.

     

    You must be new here.


  • @Sutherlands said:

    @AngelSL said:

    @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

    Hmm, I just tried in C# and it won't work there, either.
    No shit. C# uses typeof(A) versus A.class.
    You must be new here.

     Although he has a low post count, his join date is not that recent...So he can't be new....maybe he is either abstract or static?



  • @Sutherlands said:

    @AngelSL said:

    @Sutherlands said:

    @topspin said:

    @Nexzus said:

    I can't imagine something like that would compile in C++.

     

    It wont compile in Visual Basic either.

     

    Hmm, I just tried in C# and it won't work there, either.
    No shit. C# uses typeof(A) versus A.class.

     

    You must be new here.
    If you judge 'newness' by post count, then sure.

    Why not.

     



  • @AngelSL said:

    If you judge 'newness' by post count, then sure.

    Why not.

    I judge newness by a lot of things.  In this case, not knowing how people on the forums are, and also not reading tags.



  • @Sutherlands said:

    @AngelSL said:

    If you judge 'newness' by post count, then sure.

    Why not.

    I judge newness by a lot of things.  In this case, not knowing how people on the forums are, and also not reading tags.
    I did read the tags.

     I chose to ignore them. 

     



  • @AngelSL said:

    @Sutherlands said:

    @AngelSL said:

    If you judge 'newness' by post count, then sure.

    Why not.

    I judge newness by a lot of things.  In this case, not knowing how people on the forums are, and also not reading tags.
    I did read the tags.

    I chose to ignore them.

    Clearly you're not new because your douchebaginess level seems to be quite high.  You do however, need your sarcasm detector adjusted.

    Also, woosh...  (jeeze, do I have to do everything around here?!)


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.