Thursday, December 20, 2007

India Web 2.0

Came across this news site tracking India's contributions to Web 2.0.

avashya.com

Re: Growing Pains

I guess it started with the .Net guys getting aggressive with new features like annotations, generics, delegates and now Linq. They are becoming more dynamic. Something to watch out for.

I think the fact that .Net is a closed source platform controlled by Microsoft is a huge factor in them being able to be so aggressive. It seems like today, when everything is moving towards getting open sourced, this should be seen as a huge red flag. And in some sense I feel that way, but I mean you really have to marvel at the stuff they're doing. When you read the Evolution of LINQ you see some really neat concepts like Extension Methods and how they've used a previous language feature (Attributes) to actually implement it.

Regarding going the dynamic direction. I'm not sure that's necessarily true in the "core language/platform philosophy" sense. Both .Net and Java were designed from the ground up to be strong typed and will stay that way. I think it's more about abstracting out some tedious typing when it's possible for the compiler to infer the type... i.e. it's just syntactic sugar. It's especially useful when dealing with generics (and the endless <>s). So it's still going to all be the same under the covers, unlike in dynamic languages where there is no type safety at all. (Again it's interesting to see how var in C# came about as a necessity for another feature... Annonymous Types... which were needed for LINQ).

Java had to respond and got annotations and generics. Generics has been a mess with wildcards. Closures seems to be going down that route. If only we get something thats simpler and yet extensible. The memory model is simpler and concurrency API has also been a great addition.


Lets see how long before Java steals LINQ and related features ;) They're still arguing about closures and how the generics implementation was overly complicated (ppt). Wonder how long the insane backward compatibility requirement will continue. Also when Java was open sourced last year there was general optimism that there would be a lot more innovations to the language. But has it just cause a lot more arguments and disagreements about the direction? Java 5 was big release, but since then there hasn't been anything major.

My 2 cents for the year.

Sunday, December 16, 2007

Growing Pains

These are pretty exciting times. A lot of languages are being extended to add more features...

I guess it started with the .Net guys getting aggressive with new features like annotations, generics, delegates and now Linq. They are becoming more dynamic. Something to watch out for.

Java had to respond and got annotations and generics. Generics has been a mess with wildcards. Closures seems to be going down that route. If only we get something thats simpler and yet extensible. The memory model is simpler and concurrency API has also been a great addition.

C++ seems be following on Java's line with a similar memory model. I dont know what else is there but C++Ox should be big.

There seem to be a lot of changes coming into Javascript and they want to make it much more Java like. Just heard a talk on the proposed changes and it seems much more complicated (read crappy). There seem to be so many new keywords that it just does not make sense.

Is it time to move to a language like Scala? Cause I've not been able to leave static checking for Ruby yet. Hey why not just listen to Paul Graham and code in Lisp :)

Tuesday, November 20, 2007

Profiling - Just works

I've been trying to set up a profiler for Java development.

I use Eclipse primarily and my first choice was the Eclipse project TPTP. However the damn thing just refused to work on my machine(Windows 32). I needed to primarily attach to externally launched Java programs. The Agent Controller (which collects profiling data) sample scripts works. But Eclipse is still not able to find the Java process. And the same installation/setup steps worked on another machine. But memory profiling did not work there!

That's when I tried the latest RC build of Netbeans. They bundle their Profiler with the IDE so no need to install anything separately. They have a pretty nice UI wizard which gives directions to start the java application for profiling. And guess what.. it Just Works. Though some of their UI is not the best like UML interaction diagrams in Eclipse who cares. Atleast I am getting some profiling data.

Maybe I'll try Netbeans as my main IDE once its released.

Tuesday, September 04, 2007

Fishy Serialization

In Java, "Object Serialization supports the encoding of objects and the objects reachable from them, into a stream of bytes". This representation of the object can then be used for purposes like persisting on disk or passing of objects over the network. To serialize an object the class needs to implement the Serializable interface which is a marker interface with no methods defined. Optionally the class may define "readObject" and "writeObject" methods which will be used in the serialization process as defined in Specification.

The signature of the read/write Object methods is what struck me recently(this implies that I've been reading serialization related code before and not realised this). The modifier is private which means that no other instance should be able to invoke that method! And yet its invoked somehow. Time for some hacking...


For a dummy class Dog the stack trace to call the writeObject from SerializeTest.main was:

java.lang.RuntimeException
at foo.bar.Dog.writeObject(SerializeTest.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at foo.bar.SerializeTest.main(SerializeTest.java:23)

The ObjectOutputStream.writeObject is invoked from the main method and after lots of calls ObjectStreamClass.invokeWriteObject is called which does some reflection The writeObject method is dynamically invoked there. The interesting part of the implementation is:

void invokeWriteObject(Object obj, ObjectOutputStream out) {
writeObjectMethod.invoke(obj, new Object[]{ out });
}


writeObjectMethod is a member variable in ObjectStreamClass of type java.lang.reflect.Method
and is set as:

writeObjectMethod = getPrivateMethod(cl, "writeObject",
new Class[] { ObjectOutputStream.class },
Void.TYPE);


The definition of ObjectStreamClass.getPrivateMethod is:

/**
* Returns non-static private method with given signature defined by given
* class, or null if none found. Access checks are disabled on the
* returned method (if any).
*/
private static Method getPrivateMethod(Class cl, String name, Class[] argTypes,
Class returnType) {
Method meth = cl.getDeclaredMethod(name, argTypes);
meth.setAccessible(true);
int mods = meth.getModifiers();
return ((meth.getReturnType() == returnType) && ((mods & Modifier.STATIC) == 0) &&
((mods & Modifier.PRIVATE) != 0)) ? meth : null;
}
}


And there is the call to the method which does all that magic - Method.setAccessible.

The javadoc for the method says:
"A value of true indicates that the reflected object should suppress Java language access checking when it is used"

Using Reflection and with proper access its possible to even call private methods. This was something cool that I've learnt in a long time. Certainly makes Java more dynamic in nature. Now I'll have to read more on the Java security API soon.

Wednesday, May 09, 2007

wget Google videos

Google has some amazing tech videos at their site by the user Google engEDU. And they even allow for downloads which is great for me.

But I prefer downloading files using wget to be able to resume downloads later. You can't just wget the video link url's as is because they have & characters which cause wget to try to download them as separate files. Instead try something like this for the "OSS Speaker Series: The State of the Linux Kernel" video..

wget -c --output-document=MortonLinuxKernel.mp4 "http://vp10.video.l.google.com/videodownload?version=0&secureurl=twAAAOFdafTKyCsBI7E0BCCT6060NjqUP0-3g9pfM0xl5X1YO8a1zhU5ArUNYf8PLb44VqTIrTR2hntorTVWAEL6bqWkChEIIVPqNeHV5F4PRqoHXwlvZRC0_giNoVtliPIDsfE7zAzQSPok2b8ShvvgxJVI3T3WoPwGtr6Vvmwfjj18i9wTbaZD_JGXpTjD2kJrvVIenN5CSTRBbXYMKR49YyVYLdRNu9BuY924qZaDV4zEd9YFUsoCP42JkguszqVlOg&sigh=hEHjkZDlhZrMiZnAQrs8O55mFdo&begin=0&len=4897661&docid=1742374580386548257&rdc=1"

Friday, March 09, 2007

Playing with Javascript

I've pasted an early version of a recursive function to walk the DOM from a particular node in Javascript. If you run the code though.. your browser will hang as it goes into a recursive loop. So whats wrong with the code?

<html>
<head>
<script type="text/javascript" language="javascript">
function walk(node) {
if (node) {
//do something with node
for (i=0; i<node.childNodes.length; i++) {
walk(node.childNodes[i]);
}
}
}
</script>
</head>
<body onLoad="walk(document.body)">
<a href="http://www.parivartana.org">parivartana.org</a>
</body>
</html>

The variable i in the for loop should be declared as 'var i' to fix the behaviour. Thats when I realised that I should not really code in Javascript assuming that it is a subset of Java. (But being as lazy as I am never really studied the language).

Then I came across some fantastic videos on Javscript by Douglas Crockford at the YUI theatre which is a part of the Yahoo Javascript library YUI. Some of the features of the language are explained really well. Some key features are Objects as containers, Prototypal Inheritance and Lamda. He also explains some browser and Javascript quirks.

Now I am wondering if Javascript should be a part of my resume!

One of his recommendations is to favour minification ie removing of whitespace to reduce download size versus obfuscation. Google actually always heavily obfuscate their Javascript. Also as part of the Google Web Toolkit, the deployable code is also obfuscated. So thats a debateable topic. Time to read some GWT generated code then.