Static Initialiser, . class and Retroguard

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gavin Andrews

    Static Initialiser, . class and Retroguard

    I use log4j for logging and tend to include the following snipet in
    all my classes...

    public class MyClass {

    // Logging Declarations
    private static String _className;
    private static Category _cat;

    static {
    _className = MyClass.class.g etName();
    _cat = Category.getIns tance(_classNam e);
    }

    ............

    }

    this works fine until you start to investigate obfuscation and in
    particular RetroGuard; Retroguard correctly changes MyClass to be
    some funny name but when the code executes the static initialiser
    fails with ClassNotFoundEx ception since MyClass.class.g etName is not
    myFunnyName.cla ss.getName

    What I really want is a way in a static intialiser to get .class but I
    don't have a this so can't do a this.class. Any ideas?

    I have a horrible hack... create a funny Constructor and create an
    instance using the funny Constructor;the n discard the instance.

    e.g.


    public class MyClass {

    // Logging Declarations
    private static String _className;
    private static Category _cat;

    static {
    _className = new MyClass(_cat).c lass.getName();
    _cat = Category.getIns tance(_classNam e);
    }

    private MyClass(Categor y c) {}

    ............

    }

    but I don't like it...

    So the big question is... How do I get .class in a static context?

    Thanks,
    Gavin
  • Silvio Bierman

    #2
    Re: Static Initialiser, . class and Retroguard

    This is a common problem with obfuscators that change class names. Javac
    changes ClassName.class with Class.forname(" ClassName") during compilation.
    Retroguard will change the name of ClassName to something cryptic, but it
    will not modify the string that is passed to class.forName() resulting in
    that call failing.

    I have suggested the makers to do also modify the String and they agreed
    that would be better, but there has not been a new release since.

    If it is not to inefficient you could replace 'ClassName.clas s' with 'new
    ClassName().get Class()' to be obfuscator-insensitive. This is not an option
    in your static initializer I am affraid.

    Regards,

    Silvio Bierman


    Comment

    • PerfectDayToChaseTornados

      #3
      Re: Static Initialiser, . class and Retroguard

      "Gavin Andrews" <loagw@yahoo.co m> wrote in message
      news:4a54b1c1.0 310100531.2eaa6 8f1@posting.goo gle.com...
      | I use log4j for logging and tend to include the following snipet in
      | all my classes...
      |
      | public class MyClass {
      |
      | // Logging Declarations
      | private static String _className;
      | private static Category _cat;
      |
      | static {
      | _className = MyClass.class.g etName();
      | _cat = Category.getIns tance(_classNam e);
      | }
      |
      | ............
      |
      | }
      |
      | this works fine until you start to investigate obfuscation and in
      | particular RetroGuard; Retroguard correctly changes MyClass to be
      | some funny name but when the code executes the static initialiser
      | fails with ClassNotFoundEx ception since MyClass.class.g etName is not
      | myFunnyName.cla ss.getName
      |
      | What I really want is a way in a static intialiser to get .class but I
      | don't have a this so can't do a this.class. Any ideas?
      |
      | I have a horrible hack... create a funny Constructor and create an
      | instance using the funny Constructor;the n discard the instance.
      |
      | e.g.
      |
      |
      | public class MyClass {
      |
      | // Logging Declarations
      | private static String _className;
      | private static Category _cat;
      |
      | static {
      | _className = new MyClass(_cat).c lass.getName();
      | _cat = Category.getIns tance(_classNam e);
      | }
      |
      | private MyClass(Categor y c) {}
      |
      | ............
      |
      | }
      |
      | but I don't like it...
      |
      | So the big question is... How do I get .class in a static context?
      |
      | Thanks,
      | Gavin

      You could use Logger.getRootL ogger() if you do not need to log to specific
      files for particular classes.


      --
      -P
      "Sometimes I feel so goddam' trapped by everything that I know"


      Comment

      • Raymond DeCampo

        #4
        Re: Static Initialiser, . class and Retroguard

        Gavin Andrews wrote:[color=blue]
        > I use log4j for logging and tend to include the following snipet in
        > all my classes...
        >
        > public class MyClass {
        >
        > // Logging Declarations
        > private static String _className;
        > private static Category _cat;
        >
        > static {
        > _className = MyClass.class.g etName();
        > _cat = Category.getIns tance(_classNam e);
        > }
        >
        > ............
        >
        > }
        >
        > this works fine until you start to investigate obfuscation and in
        > particular RetroGuard; Retroguard correctly changes MyClass to be
        > some funny name but when the code executes the static initialiser
        > fails with ClassNotFoundEx ception since MyClass.class.g etName is not
        > myFunnyName.cla ss.getName
        >
        > What I really want is a way in a static intialiser to get .class but I
        > don't have a this so can't do a this.class. Any ideas?
        >
        > I have a horrible hack... create a funny Constructor and create an
        > instance using the funny Constructor;the n discard the instance.
        >
        > e.g.
        >
        >
        > public class MyClass {
        >
        > // Logging Declarations
        > private static String _className;
        > private static Category _cat;
        >
        > static {
        > _className = new MyClass(_cat).c lass.getName();
        > _cat = Category.getIns tance(_classNam e);
        > }
        >
        > private MyClass(Categor y c) {}
        >
        > ............
        >
        > }
        >
        > but I don't like it...
        >
        > So the big question is... How do I get .class in a static context?
        >
        > Thanks,
        > Gavin[/color]

        Well, not to answer your question but maybe to solve your problem, why
        don't you just put the classname in yourself? It's a little more
        maintainence and error-prone, but it would work:

        public class MyClass
        {
        private static final Logger log =
        Logger.getLogge r("com.myCompan y.MyClass");
        }

        There's nothing magic in log4j about using the class reference to pass
        the name, it just wants a string.

        Ray

        Comment

        • Gavin Andrews

          #5
          Re: Static Initialiser, . class and Retroguard

          Thanks for the feedback.
          [color=blue][color=green]
          >> Javac changes ClassName.class with Class.forname(" ClassName")[/color][/color]
          during compilation.

          Aha! That explains the behaviour. I didn't realise that's why the
          literal was appearing.
          [color=blue]
          > There's nothing magic in log4j about using the class reference to pass
          > the name, it just wants a string.[/color]

          Using the literal myself is a possibility. I was just hoping not to
          use a literal if i could help it to aid maintainability (given I'm
          constantly renaming my classes).

          Regards,
          Gavin

          Comment

          Working...