- my webspace

- my webspace

Latest Comment

Why I must believe in GOD
Testing time is almost passed..there are many colours in lif...
27/06/12 01:43 More...
By Tarun Shekhawat

Allama Iqbal - Selective verse...
Yahoouj
Really good work about this website was done. Keep trying mo...
07/03/10 15:04 More...
By Roderick

Allama Iqbal - Selective verse...
Great Job
You have dont a great job of collecting these... Even I had ...
25/08/09 01:01 More...
By Sikandar

O ye who don't believe !
It's like Lehman Brothers :grin
11/10/08 10:31 More...
By anurag Chaturvedi

I Protest
@Sikku
Thanks Sikku for the feedback. I never intend to blame, a...
29/07/08 11:06 More...
By Aminur Rashid

Login






Lost Password?
Home arrow Java
Prevent reflection to access private methods and members in java classes PDF Print E-mail
User Rating: / 1
Written by Aminur Rashid   
Thursday, 26 March 2009

Reflection is a nice evil. It can let you access private method/fields/constructor of a class.
import java.lang.reflect.Field;

public class UseReflection {
	public static void main(String args[]) {
		Object prey = new Prey();
		try {
			Field pf = prey.getClass().getDeclaredField("privateString");
			pf.setAccessible(true);
			pf.set(prey, "Aminur test");
			System.out.println(pf.get(prey));
		} catch (Exception e) {
			System.err.println("Caught exception " + e.toString());
		}

	}
}

class Prey {
	private String privateString = "privateValue";
}

Now in case you are wondering, that makes my class vulnerable to be modified, yes you are right. But then there is a way to prevent the caller from changing the modifier/changing the accessor. The easiest thing is to use the SecurityManager. Run the programme again using the default securitymanager provided by Java
 
java -Djava.security.manager UseReflection

For more on DefaultPolicy Implementation, you may want to read the document at sun.

Be first to comment this article
StumbleUponDigg This!Bookmark on Delicious

Add as favourites (456) | Quote this article on your site | Views: 2466 | E-mail

Last Updated ( Thursday, 26 March 2009 )
Read more...
 
JFormattedText Field with Date PDF Print E-mail
User Rating: / 3
Written by Aminur Rashid   
Wednesday, 25 March 2009


While trying JFormattedTextField, I had this requirement to show placeholder to the user to guide him while providing the date input. There was also a need to pop up user with the error message, if the input was incorrect. (Yeah, I understand that many prefer inline error messages than the pop-ups but then requirements are requirements.) One way could be to provide a MaskFormat to the JFormattedTextField and use directly validates the date, yourself using the pattern. What I tried is used both the DateFormat and MastFormat with the JFormattedTextField. The code can be modified for individual needs but the idea is to provide an input verifier to the JFormattedTextField. Use the formatter set for JFormattedTextField to verify the inputs. Along with these, also install a mask formatter to show placeholders to user.

Be first to comment this article
StumbleUponDigg This!Bookmark on Delicious

Add as favourites (382) | Quote this article on your site | Views: 1676 | E-mail

Last Updated ( Wednesday, 05 June 2013 )
Read more...
 
<< Start < Prev 1 2 3 Next > End >>

Results 19 - 20 of 20
Aminur Rashid