473,385 Members | 1,275 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.getName();
_cat = Category.getInstance(_className);
}

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

}

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 ClassNotFoundException since MyClass.class.getName is not
myFunnyName.class.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;then discard the instance.

e.g.
public class MyClass {

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

static {
_className = new MyClass(_cat).class.getName();
_cat = Category.getInstance(_className);
}

private MyClass(Category c) {}

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

}

but I don't like it...

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

Thanks,
Gavin
Jul 17 '05 #1
4 3197
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.class' with 'new
ClassName().getClass()' to be obfuscator-insensitive. This is not an option
in your static initializer I am affraid.

Regards,

Silvio Bierman
Jul 17 '05 #2
"Gavin Andrews" <lo***@yahoo.com> wrote in message
news:4a**************************@posting.google.c om...
| 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.getName();
| _cat = Category.getInstance(_className);
| }
|
| ............
|
| }
|
| 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 ClassNotFoundException since MyClass.class.getName is not
| myFunnyName.class.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;then discard the instance.
|
| e.g.
|
|
| public class MyClass {
|
| // Logging Declarations
| private static String _className;
| private static Category _cat;
|
| static {
| _className = new MyClass(_cat).class.getName();
| _cat = Category.getInstance(_className);
| }
|
| private MyClass(Category 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.getRootLogger() 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"
Jul 17 '05 #3
Gavin Andrews wrote:
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.getName();
_cat = Category.getInstance(_className);
}

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

}

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 ClassNotFoundException since MyClass.class.getName is not
myFunnyName.class.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;then discard the instance.

e.g.
public class MyClass {

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

static {
_className = new MyClass(_cat).class.getName();
_cat = Category.getInstance(_className);
}

private MyClass(Category c) {}

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

}

but I don't like it...

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

Thanks,
Gavin


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.getLogger("com.myCompany.MyClass");
}

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

Ray

Jul 17 '05 #4
Thanks for the feedback.
Javac changes ClassName.class with Class.forname("ClassName")
during compilation.

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


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
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jacek Dziedzic | last post by:
Still writing my 'cell' class that needs a look-up table to be created before any cells are accessed. Right now I have a static method cell::create_lookup_table() that does the job, but I was...
5
by: Surya Kiran | last post by:
Hi all, I've 2 classes class A, and class B. and i want to use list of type B in class A. like this list<B> typeB ; till now its fine. But i want to make it a static member. like static...
4
by: Agoston Bejo | last post by:
Hello there, is it possible to initialize such a static member that need some algorithm for initializing? What I mean is: ---------------------------------- Example: Platform VC++7.1...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
3
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
3
by: salem.ganzhorn | last post by:
The following code compiles cleanly, but it looks like gcc 3.4.0 does not emit the static data member into the object file (so I get a link error): #include <iostream> template <class Type>...
7
by: Quantum | last post by:
Hi, If I have this in a header file: //Part of the class myClass: static int myVar=99; but then in the source file have this: int myClass::myVar=77;
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.