Monday, March 24, 2008

Abuse of Booleans ;-)

Today next chapter of abusing Java internals with using of reflections ;-)
In previous chapters we discussed problems: How many is 1+1 in Java and The Secret Life of String.

Two of well known facts about Boolean in Java are:
Boolean.valueOf(true) value is true
Boolean.valueOf(false) value is false

But how we will shown, not always ;p
import java.lang.reflect.Field;
public class HackBool {
static void fixBooleans() throws Exception {
Class boolClass = Boolean.class;
Field field = boolClass.getDeclaredField("value");
field.setAccessible(true);
field.setBoolean(Boolean.TRUE, false);
field.setBoolean(Boolean.FALSE, true);
}

public static void main(String[] args) throws Exception {
System.out.println("Boolean.valueOf("+true+")="+Boolean.valueOf(true));
System.out.println("Boolean.valueOf("+false+")="+Boolean.valueOf(false));
fixBooleans();
System.out.println("Boolean.valueOf("+true+")="+Boolean.valueOf(true));
System.out.println("Boolean.valueOf("+false+")="+Boolean.valueOf(false));
}
}


When we will try to execute this code we will see:

Boolean.valueOf(true)=true
Boolean.valueOf(false)=false
Boolean.valueOf(true)=false
Boolean.valueOf(false)=true


Two first lines looks good, but what with two last? ;-)

And what we will see after change of this code to this version:
import java.lang.reflect.Field;
public class HackBool {
static void fixBooleans() throws Exception {
Class boolClass = Boolean.class;
Field field = boolClass.getDeclaredField("value");
field.setAccessible(true);
field.setBoolean(Boolean.TRUE, false);
field.setBoolean(Boolean.FALSE, true);
}

public static void main(String[] args) throws Exception {
fixBooleans();
Boolean b = true;
if (b) System.out.println("TRUE :-)");
else System.out.println("FALSE :-(");
}
}

Correct answer is:
FALSE :-(

Why? Answer is easy, Boolean have two fields TRUE and FALSE, in fixBooleans() we changed values stored in variable value, for now when JVM tries to obtain value of TRUE it gets our change value false. That's whole secret ;-)

I will try to write next time something more interesting.


Similar postsbeta
How many i 1+1 in Java? ;-)
The Secret Life of String ;-)
How to create valid file name?
Recursion is evil ;-)
How we may use Google Earth Plugin? :-)

OpenOffice.org2GoogleDocs 0.9.3

New version is here :-)
This time Russian translation was added :-) Translation was made by Nikita aka Kolobok.

OpenOffice.org2GoogleDocs


Similar postsbeta
OpenOffice.org2GoogleDocs 1.1.1
OpenOffice.org2GoogleDocs 0.9.5
OpenOffice.org2GoogleDocs 1.0.2
OpenOffice.org2GoogleDocs 0.7.1
OpenOffice.org2GoogleDocs 0.8.1 - fixes :-)

Saturday, March 22, 2008

Sunday, March 16, 2008

How many i 1+1 in Java? ;-)

Last time we spoke about immutability of String in Java, today lets talk about adding Integers ;-)

Lets check how many is 1+1 in Java...

For first try we will use this code:
public class Integers { 
public static void main(String[] args) throws Exception{
Integer i1 = 1;
Integer i2 = 1;
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));
}
}

After compile and run we will see on the screen:

1+1=2

It was easy :-)

But to our next try we will add some additional code....

import java.lang.reflect.Field;
public class Integers {
public static void main(String[] args) throws Exception {
Integer i1 = 1;
Integer i2 = 1;
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));
fixIntegers();
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));

}

static void fixIntegers() throws Exception {
Class integerClass = Integer.class;
Field value = integerClass.getDeclaredField("value");
value.setAccessible(true);
value.setInt(Integer.valueOf(2), 3);
}

}


And now strange things happens....

After we compile and run we can see....

1+1=2
1+1=3

How it's possible? ;-)

Here short explanation ;-)
Method fixIntegers() changes value of int stored as "int representation" in some Integer... This some Integer is rather important ;-) In Sun Java was decided that conversion of some numbers [actually from -128 to 127] is rather often, so why to make this conversion so often? More reasonable will be to keep somewhere array of those Integers and when it will be needed use one of those objects. Method fixIntegers() first gets Integer object for int value of 2, and using reflections it changes value from 2 to 3 :-)
Next "magic" things are in line where we made second addition, we adds 1 to 1, and of course we got as a result 2, but we also wants to convert it to Integer, instead conversion we have here usage of value from "pool", we take this object :-) But because we wants to print String representation of this value method toString() of our object is called... and toString() looks to private variable value and finds in this place... 3 :-)
And it's whole secret ;-)


Similar postsbeta
Abuse of Booleans ;-)
The Secret Life of String ;-)
How to create valid file name?
Recursion is evil ;-)
How we may use Google Earth Plugin? :-)

OpenOffice.org2GoogleDocs 0.9.2

Fixes:
  • more proper handling of files with spaces in names... strange but older method worked in Impress but not worked in Writer
  • increased size of message window

    Changes:
  • dialog windows for import and export now inherits from JFrame instead JDialog, thanks to this change those windows will be visible on StartBar

    Download


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.9.1
    OOo2GD 1.7.1
    OOo2GD 1.7.2
    OpenOffice.org2GoogleDocs 1.0.4
    OOo2GD 1.8.2
  • Sunday, March 9, 2008

    OpenOffice.org2GoogleDocs 0.9.1

    New version 0.9.1 of OpenOffice.org2GoogleDocs is available.

    Fixes:
  • configuration files should be stored only in User Home directory

    Enhancements:
  • conversion of Impress (ODP) presentation to Microsoft PowerPoint 97 (PPT) format - Google Docs supports only PPT not ODP
  • support for proxy servers - I hope this works, I wasn't able to test it

    Small changes:
  • small modifications in German, Polish and English translations

    Download 0.9.1


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.9 :-)
    OpenOffice.org2GoogleDocs 0.9.2
    Download your document in choosen format from Google Docs with OOo2GD 1.8.0 :-)
    OOo2GD 1.6.0 :-)
    OOo2GD 1.7.0
  • Thursday, March 6, 2008

    The Secret Life of String ;-)

    /mad scientist mode on

    They always said "String is immutable - you cannot change its value without changing reference!"

    But what They will say when I will use this code?
    import java.lang.reflect.Field;

    public class StringModificator {
    public static void trueModificator(String str)
    throws Exception {
    Field valueField = str.getClass().getDeclaredField("value");
    valueField.setAccessible(true);
    char[] value = (char[])valueField.get(str);
    char[] reversed = new char[value.length];
    for (int idx=0; idx<value.length; idx++) {
    reversed[idx]=value[value.length-idx-1];
    }
    for (int idx=0; idx<value.length; idx++) {
    value[idx]=reversed[idx];
    }
    }

    public static void falseModificator(String str) {
    str="toster";
    }

    public static void main(String[] args) throws Exception {
    String str = "it's a test";
    String subStr = str.substring(3);
    // prints "it's a test"
    System.out.println(str);
    System.out.println(subStr);

    falseModificator(str);
    // Still prints "it's a test"
    System.out.println(str);
    System.out.println(subStr);

    trueModificator(str);
    // But whats now? ;-)
    System.out.println(str);
    System.out.println(subStr);
    }
    }

    So this whole String isn't so immutable as They said!!!

    /mad scientist mode off


    Similar postsbeta
    Abuse of Booleans ;-)
    How many i 1+1 in Java? ;-)
    How to create valid file name?
    Recursion is evil ;-)
    How we may use Google Earth Plugin? :-)

    Sunday, March 2, 2008

    OpenOffice.org2GoogleDocs 0.9 :-)

    Yeah! ;-) Today we have next version ;-)
    In version 0.9 we have:
    1 fix - fixed storing of credentials for Google Account
    2 enhancements - icons instead of text in toolbar :-) and Google Docs menu was moved from main menu to File menu.

    As always new version is available at OOo2GD page.


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.9.1
    OpenOffice.org2GoogleDocs 1.0.1
    OpenOffice.org2GoogleDocs 1.0.4
    OpenOffice.org2GoogleDocs 0.8.1 - fixes :-)
    OOo2GD 2.1.1 is ready :-)

    Saturday, March 1, 2008

    OpenOffice.org2GoogleDocs 0.8.1 - fixes :-)

    New version of OOo2GD is available :-)
    This version provide important fix for downloading of files, also Bulgarian translation is fixed, last time it was broken [it was my fault]. In this version you will find also small correction in German translation.

    As always you may download new version on OpenOffice.org extension page.


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.8 :-)
    OOo2GD 1.8.2
    OpenOffice.org2GoogleDocs 0.9 :-)
    OOo2GD 1.7.0
    OpenOffice.org2GoogleDocs 0.7.1