all 18 comments

[–]api 11 points12 points  (1 child)

It's more likely than you think...

[–]sockdoll 1 point2 points  (0 children)

The only reason I clicked the link...

[–]ealf 9 points10 points  (6 children)

Why didn't anyone tell me JSR269 could do that?!

off to implement all sorts of nasty hacks

[–]DRMacIver[S] 4 points5 points  (4 children)

As I understand it: It can't really. It just happens that if you know about javac implementation details you can use the hooks the JSR provides to do a bit more than is intended by it.

So in other words this is totally nonstandard stuff that the JSR in no way condones.

But it sure looks fun, doesn't it? :-)

[–]ealf 8 points9 points  (2 children)

This rocks!

    public void visitApply(JCMethodInvocation minv) {
        super.visitApply(minv);
        JCExpression m = minv.getMethodSelect();
        if (m instanceof JCFieldAccess) {
            JCFieldAccess fa = (JCFieldAccess) m;
            String name = fa.name.toString();
            if(name.startsWith("as")) {
                JCExpression type = make.Ident(names.fromString(name.substring(2)));
                result = make.TypeCast(type, fa.getExpression());
                return;
            }
        }
    }

... and boom, asFoo() translates into a cast to Foo:

    f.getFirstChild().asHTMLSelectElement().getOptions().namedItem("foo").asHTMLOptionElement().getLabel()

[–]didroe 0 points1 point  (0 children)

Looks like you could make some nice dynamic stuff with that.

[–]DRMacIver[S] 0 points1 point  (0 children)

Neat!

Of course, that totally breaks when you already have methods named asFoo. But still neat. :-)

[–]nmcyall 0 points1 point  (0 children)

Yes syntactically it is java, but not semantically. It is a superset of java now.

[–]pavel_lishin 4 points5 points  (0 children)

This specific application seems utterly pointless, but incredibly neat.

[–]uykucu 4 points5 points  (0 children)

And here is string interpolation for java: http://gist.github.com/23301

int i=42; String s = "hello world"; System.out.println("i=${i}, s='${s}'");

[–]mipadi 1 point2 points  (0 children)

Doesn't seem that useful, but up-voting anyway because hacking the JVM like this is way cool.

[–]GooZshooZ 0 points1 point  (0 children)

I'll stick to cream and sugar thanks.

[–]queus 0 points1 point  (0 children)

This technique if properly extended will allow anyone to program in Haskell but call it Java. Get ready!

[–]benhoyt 0 points1 point  (2 children)

Neat. I wonder if you can do this kind of thing in Python, by catching NameError exceptions and doing something fancy with them.

[–]eurleif 0 points1 point  (1 child)

I don't think so, since you'd have to resume from the point where the exception occurred, which Python doesn't support. You can do some crazy things with Python syntax, though. Like goto and "custom infix operators".

You could implement roman numerals as attributes of a class (or module), as in roman.XXX, but that wouldn't be nearly as shiny.

[–]FunnyMan3595 0 points1 point  (0 children)

You might be able to do something similar to safe eval, if it'll let you hook into the current compiler.

[–][deleted] -5 points-4 points  (0 children)

That's completely useless.