Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, March 15, 2010

Simple stuff missing in .NET and Visual Studio

I'm a Java programmer, but today I had to deal with C#.NET to create a simple Windows Forms application which had some mathematical and algorithmic stuff.

I've heard a lot of positive comments about Visual Studio, but what I experienced today was not up to the level of usual praises it gets from a lot of developers. Compared to IntelliJ IDEA, which is my favorite IDE, there are several stuff missing in Visual Studio (2008 edition). First thing was auto-complete. Of course it does have a reasonable auto-complete facility, but it's partial (at least with the default settings - I don't know whether there's a way to change it). Whenever a method name is completed, we have to manually code '(' and ')' parts of the method call, which I found a bit inconvenient. The same seems to be the case with strings, once I open a double-quote, the IDE doesn't automatically close it - it's inconvenient when there's a lot of such coding to be done.

Second noticeable thing was that most often it doesn't automatically point out the coding errors unless we build it manually, this is troublesome when there's a lot of coding to be done. Then there was no way of altering editor's background color - it's set to bright white by default and can be really troublesome for the eyes in the long run.

Finally, the most weird part, I wanted to use a Hashtable to have access to some values using keys. I defined one and then wrote code to add a lot of stuff. Then when I was looking for a method to retrieve them based on keys......there was no such simple method retrieve elements like in Java's Hashtables. (there was a method to check whether an item exists, but can't fetch it directly - how terrible is it?) After a Google search, I found a lot of articles teaching how to fetch an item in C# Hashtable - iterate through all elements....! This is really useless and a terrible hashtable implementation I've ever seen. Of course they might have written efficient methods to put stuff (I assume at least those Add methods are efficient), but there was no efficient or an easy way of retrieving them back. For algorithms which have a lot of get calls rather than inserting elements, this will cause unnecessary overhead.

Although I have no idea of moving to .NET, hope these will be fixed in upcoming Visual Studio 2010 edition.

Sunday, February 14, 2010

Is 'private' really 'private'?

I've got the following Java class with private methods.

class ClassX {

private void methodX() {
System.out.println("methodX: this is private.");
}

private void methodY() {
System.out.println("methodY: this is private.");
}
}


How to invoke these methods from an outside class without modifying this code?
If you've studied Java, you might have learnt that it's impossible.

Truth is, you can access anything from anywhere.
Just use Reflection.

class Main {
public static void main(String[] args) throws Exception {
ClassX cx = new ClassX();
Method method = ClassX.class.getDeclaredMethod("methodX", null);
method.setAccessible(true);
method.invoke(cx, null);
}
}

All you have to do is to take the method using 'getDeclaredMethod', then set it as 'accessible' (previously it was not accessible since it was private). Now you can do any damn thing with it. How simple is it?

Still not enough? You can iterate through all declared methods in a class. (in case it is a compiled one and you don't already know what private methods are there)

Method[] ms = ClassX.class.getDeclaredMethods();
for (Method m: ms) {
System.out.println(m.toString()); // see what it is.
m.setAccessible(true);
m.invoke(cx, null); // invoking each method.
}

You can do the same thing for variables as well.

Is 'private' really 'private'? No.
Then how to make something really 'private'? Who knows?
That's not the important point here. If you don't know about reflection, then go ahead and learn it. Reflection is what enables real 'dependency injection' in Java.