Thursday, April 28, 2005

JavaPathFinder

NASA has released JavaPathFinder. What is JavaPathFinder?? here is an excerpt

"""If you are not familiar with formal methods, this basically means JPF is a Java virtual machine that executes your program not just once (like a normal VM), but theoretically in all possible ways, checking for property violations like deadlocks or unhandled exceptions along all potential execution paths. If it finds an error, JPF reports the whole execution that leads to it. Unlike a normal debugger, JPF keeps track of every step how it got to the defect."""

interesting ... need to figure out where Halting Problem theorem stands ..

Tuesday, April 26, 2005

My new office

when i came for the interview, i found the office amusing. There was cartoons over the walls, green outdoors .. one thing that interested me was there were no glass walls seperating my friend's work place and outside. Then i was involved in the interview process and forgot everything. well now im sitting at the exact same place, and its very enjoyable place in here .. what theyve done is that in the middle of the building, they built a small garden with glass roof .. so that explains the missing of glass.. there is a fountain near my workpace and there is a constant sound of water flowing .. talking of my workplace, my computer is a P4 3.2GHz with 2GB of ram .. pretty neat eh .. monitor is lcd, and finally i found the solution for flicker free screen .. i always used to wonder, the more bigger your screen, the more flicker you will have with higher resolution .. so what to do? .. well lcd's seem having no problem with that ... dress code is one other thing(now you know where this is heading;) ..you can wear casuals all week long! .. it may not be a new thing for a lot of people ... i have enough suffered enough formals .. more goodies .. music is not considered blasphemy .. and they will even give you a headphone! ...aarrghh ..enough of bragging ;)

Monday, April 25, 2005

Jimmy

Jimmy was of a breed similar to daschund. He was an important member of one of my relative's (dad's sister's husband or something) familiy. Twice during our(me and my brother's) summer vacation, we stayed there (chembur, bombay exactly). He was a good dog. During night, he would come and wrap (yes :) us with blanket... and would sleep along with us. Morning he would twril his tounge into my ear to wake me up (yukk??). He used to watch TV (yes he does!) .. and was very intelligent. He understood almost anything .. whenever we call for a 'bath', he would run away and hide inside bed. We used to drown him in big bucket of water ;) .. Once i remember, i was not drinking milk even after a lot of shouting by mother .. Jimmy came and scratched my face for that! .. We all had a very good time going beach etc with him. Alas not everything stays same :( .. they bought two other dogs too. One was a dalmatian named Moti(male) and another was a pomerian named Rosie. Motie was a small one. Jimmy used to terrorize him. Rosie was a quiet one, she was shipped from singapore .. because of that we thought. Time went by... Moti grew bigger and stonger.. now he is only afraid of his master, no one else. Even we cant stand his wagging of tail.. its that powerful. Moti had his fair share of revenge with Jimmy. Both of them are locked seperately. If Moti has a chance, i guess he will kill Jimmy. Last time i saw Jimmy was when i was in 10th standard. He was old, but he remembered me. I dont know whether he is dead or alive now..

Tuesday, April 19, 2005

The mysterious .class variable

Those who code in java will know that each class has a field called class which gets the class Object.
Say we have a class called Test,

Class cls = Test.class; gives the class object for Test.
This is equivalent of

Class cls = Class.forName("Test");

Lets write a class, which does reflection to find out what are all the fields and methods for that class.

========================================
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public Class cls = Test.class;
public static void main(String[] args) {
Field[] fields = Test.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i]);
}
Method[] methods = Test.class.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i]);
}
}
}
========================================
interestingly, if you run this program, it prints
----------------------------------------
public java.lang.Class Test.cls
static java.lang.Class Test.class$Test
public static void Test.main(java.lang.String[])
static java.lang.Class Test.class$(java.lang.String)
----------------------------------------
*Note, this class is compiled using Sun's JDK 1.4.2 compiler. If you are using eclipse, you might get a different result.

So as you see, there is no "class" field as such, but we got a field called class$Test, and a method called Class class$(String).
Lets disassemble the class to find what is happening inside!'

For simplicity, im disassembling this class

========================================
public class Test {
public Class cls = Test.class;
}
========================================
.source Test.java
.class public super Test
.super java/lang/Object

.field public cls Ljava/lang/Class;
.field static class$Test Ljava/lang/Class;

.method public ()V
.limit stack 3
.limit locals 1
.line 1
l0: aload_0
l1: invokespecial java/lang/Object/ ()V
.line 2
l4: aload_0
l5: getstatic Test/class$Test Ljava/lang/Class;
l8: ifnonnull l23
l11: ldc "Test"
l13: invokestatic Test/class$ (Ljava/lang/String;)Ljava/lang/Class;
l16: dup
l17: putstatic Test/class$Test Ljava/lang/Class;
l20: goto l26
l23: getstatic Test/class$Test Ljava/lang/Class;
l26: putfield Test/cls Ljava/lang/Class;
l29: return

.end method

.method static class$ (Ljava/lang/String;)Ljava/lang/Class;
.limit stack 3
.limit locals 2
.catch java/lang/ClassNotFoundException from l0 to l4 using l5
.line 2
l0: aload_0
l1: invokestatic java/lang/Class/forName (Ljava/lang/String;)Ljava/lang/Class;
l4: areturn
l5: astore_1
l6: new java/lang/NoClassDefFoundError
l9: dup
l10: aload_1
l11: invokevirtual java/lang/ClassNotFoundException/getMessage ()Ljava/lang/String;
l14: invokespecial java/lang/NoClassDefFoundError/ (Ljava/lang/String;)V
l17: athrow

.end method
========================================

Look at the constructor. What it does is
if (class$Test == null) {
class$Test = class$("Test")
}
cls = class$Test;

So it is pretty much evident that, the class object is once stored in class$Test.

And what happens in class$ method?
Well, this is what it does

return Class.forName(String) !! (Not counting the ClassNotFoundException)

So from this we can understand that the .class Field is just a syntactic sugar which the compiler provides, and not a real Field.

Friday, April 15, 2005

Masters of Doom Movie!!!!

Masters of Doom is going to be a movie!!! .. read from slashdot. Im so happy ... it was one of the best times in my life when i had a chance to read the book and play then newly released Doom3 ..
so much for a carmack fan ..


Tuesday, April 12, 2005

Of virtual machines

Of late ive been interested in virtual machine stuff. Naturally (for me), the first vm to explore was jvm.

JVM is a stack based vm. It means whenever u want to execute an operation, the operands are popped from stack and executed. For eg a simple addition for 11 + 12, you have to do these
(im using Joshua Engel's Oolong assembler and Gnoloo disassembler)

bipush 12
bipush 13
iadd

ie push 12 and 13 to stack and execute iadd. iadd will pop two operands from stack (the 12 and 13), compute the result and push it back to stack. So finally stack will end up having a value of 25.

There are other kinds of vm's like the register based ones. Parrot is an example. In these kind of vm's there is no need to spend time on pushing and popping operands, instead operands can be loaded/stored in registers.

Ok going back to jvm .. each method will have its own operand stack, its local variables. Local variables are place where you can store variables. It is numbered starting from 0 (upto 65536 i guess).
Usually in a non static method, "this" reference will be stored in location 0, then all the method parameters are stored starting from 1 etc

so for eg if there is a method

void test(int i, float j)

it means
this --> 0 //stored in local variable 0
i --> 1 //stored in local variable 1
j -->2 //stored in local variable 2

so i can be accessed by using "iload 1" instruction and j by using "fload 2".

This is different from C where parameters are pushed onto stack i believe.

There are some interesting things supported by jvm. One example is support of methods with same signatures, but different return types.

In java language, you cant have both these methods coexisting in same class

public Integer test() {..}
public String test() {..}

because method signature consists of method name and its parameteres and does not contain return types.
This makes sense as you will never specify the return type while invoking

like
i = test();

But the jvm perfectly supports the above two methods in same class. The thing there is whenever you invoke any method, you need to specify the return types as well.

so to invoke the first version, you would be saying

invokevirtual TestClass/test() Ljava/lang/Integer;

and to invoke the second

invokevirtual TestClass/test() Ljava/lang/String;

Here is a code which does exactly this

===============================================
.class T14
.method static test() Ljava/lang/String;
ldc "this is test"
areturn
.end method

.method static test() Ljava/lang/Integer;
new java/lang/Integer
dup
iconst_1
invokespecial java/lang/Integer/(I) V
areturn
.end method

.method public static main([Ljava/lang/String;) V
getstatic java/lang/System/out Ljava/io/PrintStream;
astore_0

aload_0
invokestatic T14/test() Ljava/lang/String;
invokevirtual java/io/PrintStream/println(Ljava/lang/Object;) V

aload_0
invokestatic T14/test() Ljava/lang/Integer;
invokevirtual java/io/PrintStream/println(Ljava/lang/Object;) V
return
.end method
.end class
===============================================

when you run this it prints both
--------------------------------------------------------------------
C:\shibin\src\jsm\inher>java COM.sootNsmoke.oolong.Oolong T14.j

C:\shibin\src\jsm\inher>java T14
this is test
1
--------------------------------------------------------------------

More things later