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

2009-09-28

Sneak Attack: Functional Java

How is this the first time I've heard of Functional Java?

Back to flipping out...

2009-09-22

Sneak Attack: Python is not Java

One of the (in Internet time, at least) golden oldies. Re-reading this makes me cringe when I think back.

Back to flipping out...

2009-06-01

Five Things I Hate About Java

I'm sure you've all seen the Five Things meme. Here's the five things I hate about Java.

1. Lack of Type Inferencing

There's just enough generics-related type inferencing to leave me wanting more.

2. No First-class Functions

I don't care if there are patterns for doing this, it's not very idiomatic, and the boilerplate obscures the intent of the code, which largely defeats the purpose.

3. No Read-Eval-Print Loop (REPL)

I can't praise the REPL enough. Java should have had one of these in the JDK in 1.0.

4. Checked Exceptions

The standard library is riddled with checked exceptions that really should have been unchecked, and third-party libraries have followed suit. In fact, Exception should have been unchecked and we should have CheckedException as a subclass instead of RuntimeException.

5. Array Covariance

It's broken. Especially now that Java has covariant returns (thanks Java 5!), this bothers me just often enough that I've forgotten how broken it is right before I need/want it.

Honorable Mention: No Tail-call Elimination

This very nearly bumped Array Covariance. I really wish tail-call elimination would catch on in mainstream languages.

Back to flipping out...

2008-11-01

Flexjson Hijinks

Flexjson has been good to me, but every once in a while it jumps up and surprises me. Here's an example.

AbstractFoo.java


abstract class AbstractFoo {
        private Float a = 1.1f;
        private Float b = 2.2f;
        private Float c = 3.3f;

        public Float getA() {
                return a;
        }

        public Float getB() {
                return b;
        }

        public Float getC() {
                return c;
        }
}

Foo.java


import flexjson.JSONSerializer;

public final class Foo extends AbstractFoo {
        public static void main(final String[] args) {
                final Foo myFoo = new Foo();
                final JSONSerializer serializer = new JSONSerializer();
                System.out.println(serializer.serialize(myFoo));
        }
}

Can you guess what you're going to get when you compile this and run java Foo? If you guessed:


{"a":1.1,"b":2.2,"c":3.3,"class":"Foo"}

you were wrong. You really get:

Exception in thread "main" flexjson.JSONException: Error trying to serialize path: [ a ]
        at flexjson.JSONSerializer$ObjectVisitor.bean(JSONSerializer.java:603)
        at flexjson.JSONSerializer$ObjectVisitor.json(JSONSerializer.java:471)
        at flexjson.JSONSerializer$ObjectVisitor.visit(JSONSerializer.java:435)
        at flexjson.JSONSerializer.serialize(JSONSerializer.java:222)
        at Foo.main(Foo.java:7)
Caused by: java.lang.IllegalAccessException: Class flexjson.JSONSerializer$ObjectVisitor can not access a member of class AbstractFoo with modifiers "public"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
        at java.lang.reflect.Method.invoke(Method.java:578)
        at flexjson.JSONSerializer$ObjectVisitor.bean(JSONSerializer.java:579)
        ... 4 more

It appears that Flexjson's serialization algorithm can't handle a package-visible, abstract super class. Changing it to public fixes the problem and you can continue on your merry way.

Back to flipping out...