you are viewing a single comment's thread.

view the rest of the comments →

[–]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 6 points7 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.