Friday, May 28, 2004

Instantiate a Java class that has private constructor

Java reflection is like double edged sword. If you don't use it properly, you would end up in a code mess. Remember the GOTO phenomenon during the BASIC era? Here is one example that I found today. Have you ever have a need to instantiate a class that has private constructor? There are two things you can do :

1. Sit back and think; if you ever have a need to instantiate that class, maybe that class should have a public constructor in the first place. Or maybe make it as interface or abstract class.

2. Or, you can use the mighty Java Reflection. Here is an example :


public class Secret {
   private Secret() {
      // some secret
   }
}

public class Hacker {
   public static void main(String args[]) throws Exception {
      Constructor[] cons = Secret.class.getDeclaredConstructors();
      // Change the accessible property of the constructor.
      cons[0].setAccessible(true);
      Secret secret = (Secret)cons[0].newInstance(null);
      // You are in !
   }
}


One caveat is it will not work if you have SecurityManager installed.

9 Comments:

At 6:48 PM, Blogger Prithvi said...

Hi,

Could you throw light on how to disable the security manager?

regards,
Prithvi

 
At 10:25 PM, Anonymous Anonymous said...

One problem you have is that the default constructor is not guaranteed to be at position 0 in the array.

You can easily set all of the constructors accessible with the static method:

AccessibleObject.setAccessible(cons, true);

 
At 10:27 PM, Anonymous Anonymous said...

I do realize that you only have one constructor in your example, but it might not be clear that if you were to add another constructor, your solution would not always work.

 
At 5:06 AM, Anonymous Steff said...

You could easily search for the default constructor by doing the following:

// Search for the default constructor
for (int i = 0; i < constructors.length; i++) {
if (constructors[i].getParameterTypes().length == 0) {
defaultConstructor = constructors[i];
break;
}
}

 
At 2:29 PM, Anonymous Ivan said...

This works for me only when I use getDeclaredConstructor().

No wonder, the javadoc states, that getConstructor() only returns public constructors.

 
At 1:19 AM, Anonymous Anonymous said...

Hello everyone! Who knows where to upload the film Avatar?
I even bought the film Avatar for a SMS to http://rsskino.ru/kinofilm/avatar.html , the link was, but download fails, the system will boot quite strange cocoa something.
Men, advise where to normal as quickly download film avatar?

 
At 5:43 AM, Anonymous Anonymous said...

[url=http://www.ganar-dinero-ya.com][img]http://www.ganar-dinero-ya.com/ganardinero.jpg[/img][/url]
[b]Necesitas ganar dinero y no sabes que hacer[/b]
Nosotros hemos hallado la mejor guia en internet de como ganar dinero desde casa. Como nos ha sido de utilidad a nosotros, tambien les puede ser de utilidad a ustedes. No son unicamente formas de ganar dinero con su pagina web, hay todo tipo de formas para ganar dinero en internet...
[b][url=http://www.ganar-dinero-ya.com][img]http://www.ganar-dinero-ya.com/dinero.jpg[/img][/url]Te recomendamos entrar a [url=http://www.ganar-dinero-ya.com/]Ganar dinero desde casa[/url][url=http://www.ganar-dinero-ya.com][img]http://www.ganar-dinero-ya.com/dinero.jpg[/img][/url][/b]

 
At 5:53 PM, Anonymous Anonymous said...

I sell a boat-program which will help you to outwit auction and to win, initially the boat was created for the Scandinavian auction http://internet-aukcion.ru/ but now the program can work with similar auctions: gagen ru, vezetmne ru and with ten.
The program-boat stakes for you, i.e. for this purpose it is not necessary to sit constantly at the monitor. The boat can set time when it is necessary to stake, thus you as much as possible will lower expenses for rates, and as much as possible increase the chances of a victory.

The price of the program a boat for the Scandinavian auctions 20$

For the first 10 clients the price 15$

To all clients free updating and support.

Behind purchases I ask in icq: 588889590 Max.

 
At 12:07 PM, Anonymous Anonymous said...

[b]Set software LoveBots v 5.2[/b]

All for a mass mailing dating http://24lux.ru/

The script is written in php5

Features:

[i]registration, account activation
manual input captures, or the solution through antikapchu
filling data accounts:
- Gulf desired photo
- Инфы about yourself
- Diary
- Sexual preference[/i]

gulyalka on questionnaires spammer on lichku
- Randomization Posts: replacement of Russian letters in Latin analogues

optimized to work in a continuous loop
check-activation-filling-spam check ..

Updates and support free of charge.

Price per set 100 wmz

For the first 10 buyers price 70 wmz (your feedback on the software).

For shopping I ask in icq: 588889590 Max.

Scrin program:

[IMG]http://i066.radikal.ru/1002/9d/a7a68e8c96ee.jpg[/IMG]

[IMG]http://i054.radikal.ru/1002/19/9db76967c0e5.jpg[/IMG]

[IMG]http://s003.radikal.ru/i202/1002/24/20716e86512e.jpg[/IMG]

Flooding in the subject no! Write to feedback after the purchase.

 

Post a Comment

<< Home