473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
4 3210
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
Jul 17 '05 #2
"Gavin Andrews" <lo***@yahoo.co m> wrote in message
news:4a******** *************** ***@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"
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.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


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

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
2584
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 wondering if there was some kind of a "static constructor" that would do this automatically upon the construction of the first cell instance? Sure I...
5
1546
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 list<B> typeB ;
4
326
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 ---------------------------------- #include <iostream>
14
2848
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
1699
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
2110
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> class foo { public: foo( Type i )
7
1649
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
11310
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 always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
9
3401
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 to put them in main(), it's so far away. Thanks. -
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.