Interesting trick to finding Java caller 17

Posted by Warner Onstine on March 27, 2008

Have you ever wondered how to find the Class that has just called your class? Well, now you can (or rather you always could) by using this little trick that I got from my friend and former co-worker Leo.

Class.forName(new Throwable().getStackTrace()[2].getClassName()));

This will retrieve the caller name from the stacktrace and then you can get the actual class using Class.forName(). He uses this in an interesting way for logging.

private static final Log getLog() {
    try {
        return LogFactory.getLog(Class.forName(new Throwable().getStackTrace()[2].getClassName()));
    }
    catch (Exception e) {
        return LogFactory.getLog(FormattedLogger.class);
    }
}

Which he then imports as static methods so that he can just do:

info("Some intereting INFO logging going on here");

Pretty slick and an interesting use of the Java libraries.

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Thinking about picking the Groovy DSL book up again 6

Posted by Warner Onstine on March 26, 2008

But before I do I thought I’d ask everyone out there (who are interested in such a book), what would you like to see in it?

One possibility I’ve been tossing around is to turn it into a techniques or cookbook-style book. I’ve been reading through “Designing Interfaces” by Jennifer Tidwell. In this book she organizes the interface solutions into different sections and goes through each one with an example and what it solves and when to use it. I really like this idea (but others might not). Originally my book was a “How do I get started designing DSLs?” and “What in the world are they and why should I care?”.

Another thing I’ve been thinking about is ditching the Java part and making it Groovy only. This appeals to me because I feel that Groovy is much cleaner to implement an Internal DSL than Java is. Can you do DSLs in Java? Of course you can, but I’m not sure that I can give it the best treatment because it isn’t what I’m passionate about.

So, sound off in the comments, let me know what topics you’d like to see covered, what challenges have you faced in writing a DSL (or API/Framework) that you would have liked someone to trailblaze for you to show the dead-ends first?

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Groovy Roadmap

Posted by Warner Onstine on December 20, 2007

So Guillaume posted the current road map to the list a couple of days ago and I put in my .02 for fixing the (in my eyes) broken operator overloading. And the bug I really wanted is now marked to be fixed in 1.6!

I honestly can’t say how happy I am at this and will definitely test this functionality out with my Math DSL that I am working on.

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Overriding the ‘as’ operator 2

Posted by Warner Onstine on December 06, 2007

A friend of mine sent me a link to Charles Nutter’s blog on implementing interfaces using the as operator. While I was explaining how it worked to him I remembered that like many operators you can also override this one using asType(). This got us to thinking when would you use this functionality?

I have one project I’m working on right now (not ready to talk yet about as I want more code in place before I unveil it, :-P ) but this might be useful. Say that you wanted to duck-type your object for specific class instances. Overriding as may be one place to do it.

When would this make sense? Personally I think something like this makes the most sense in a library that provides some kind of functionality to a bunch of classes (rather than a one-off case). But I’m curious to see how others have used this functionality, would help to shed some light on other uses I hadn’t thought of.

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Math DSL Part Deux – Elementary Row Operations (EROs)

Posted by Warner Onstine on November 26, 2007

After sending out my query to the groovy user list I got back some ideas (no offense to Yann, just something I need to tackle on my own, so I didn’t look at your solution). What I ended up doing was two-fold:

  • Create a new MetaClass using ExpandoMetaClass
  • Define a new class that understood what row and matrix it was going to operate on (so it can have its own operator overloading)

Yes it was a little bit of extra work, but worth it. I also implemented my or operator overloading so that I can specify augmented matrices in three formats:

  • A|I for the identity matrix (only works on square matrices)
  • A|0 for the zero column, used for solving the set of linear equations
  • A|b for a vector column, also used for solving the set of linear equations

As always it helps to learn from others’ mistakes (and oddities) so I’ll share mine along the road:

  • When dealing with EMC (ExpandoMetaClass) watch out for the use of the this keyword
  • Closure delegates don’t always act the way you would hope when extending GroovyObjectSupport (at least I couldn’t get it to work right which is one reason why I switched to EMC)
  • Overriding the or operator works fine until you get to an object that has overridden the equals operator. In this case Groovy kept trying to find out if my Matrix was equal to my MatrixColumn. Once I figured this out I just returned false if the MatrixColumn was being compared against a Matrix then it worked as expected.

The next part I want to add is the ability to track the EROs through Elementary Matrices. These are matrices that start out as 1s along the diagonal and they get the same EROs applied to them (this will be useful further down the line for other operations).

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Math DSL beginning 1

Posted by Warner Onstine on November 23, 2007

I decided to start working on this taking one of the examples I worked out for the book for math Sets (partially incomplete due to Groovy’s inability to directly override lessThan, lessThanEquals, greaterThan, greaterThanEquals) and to put into code some of the techniques I was learning in my Linear Algebra class.

So far I’ve implemented the following into my matrix math DSL:

  • Matrix/Row equality – allows you to test if two matrices or rows are equal (i.e. – contain the same ordered set of numbers)
  • Matrix addition/subtraction – You can add two matrices together if they are the same dimensions (2×2, 2×3, whatever, as long as the match in row and columns)
  • Matrix/Row scalar multiplication – You can multiply a scalar value against a row or matrix
  • Matrix multiplication – You can multiply two matrices together if the first matrix A’s (say 2×3) columns match B’s rows of the matrix you are multiplying against (say 3×2) which will produce a 2×2 matrix. If you did BxA then you would end up with a new matrix of 3×3
  • Matrix powers – If the matrix is square (say 3×3) you can multiply it by itself to get the power of the matrix
  • Left-shift operation – be able to dynamically create a Row or Matrix by adding new elements to the end

I still have a lot of work which is laid out on my Trac site for GroovyMath which leads me to my next conundrum.

When dealing with matrices you need to do a lot of Reduced Echelon Form (REF) or Row Reduced Echelon Form (RREF) this consists of taking one of two Elementary Row Operations (EROs) and applying it to a row to get a matrix in the form of a diagonal (all 1s down the upper-left to lower-right diagonal with 0s everywhere else) or getting it into an upper-diagonal form (0s below the diagonal and integers everywhere else). The two EROs that you can consist of a) row interchange, where you can exchange one row with another; and b) row addition, where you can add one row to another multiplying that row by a scalar. What I would like to be able to do is something like this:

def A = new Matrix(//rows go in here)
A.r1 + A.r2*2 //which will add 2 times the second row and add it to the first and replace it
A.r1 % A.r2 //which will exchange rows 1 and 2 with each other

Another thought I had was to do this in some kind of “rowOperations” closure:

def A = new Matrix(//rows go in here)
A.rowOperations {
    r1 + r2*2
    r1 % r2
}

Which definitely reads better than the first (and of course that’s all part of a DSL how well does it translate the native tongue into code). But the issue that I have with either of these approaches (maybe, still ironing out the second in my head) is that the properties r1 and r2 don’t exist. These property accessors will need to be intercepted. Ok, not a big deal. But if I choose to implement this in MatrixRow (i.e. – actually return the row itself) it doesn’t allow me to modify the originating matrix (A in this case) only change the row. For the addition problem I think I can get around it as it is executing in a closure, but the row interchange is an issue. The individual rows don’t know they’re a part of a matrix and I’m not sure that knowing it allows them to do anything.

Anyways, just wrestling with this concept right now as it is key to moving on to the more advanced functionality in my matrix math. Any suggestions are more than welcome. One new feature that I really like will be overriding the or operator to provide Augmented Matrices (another key part). Normally you define an augmented matrix in one of three ways:

  • A|0 – add a column of all zeros
  • A|b – add a vector as the augmented matrix
  • A|I – add an Identity matrix

The first two are used to solve the set of linear equations and a bunch of other things. The second is used to find the inverse of the matrix as you transform through EROs the matrix A so that it equals I (the identity matrix) and then the side that “I” was on now equals the inverse of A. I would say stop me if I’m boring you but it’s probably too late for that. I just wanted to point out how cool it was I can override the or operator to do something completely different that makes perfect sense in my given domain.

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

DSL book on hold…unfortunately

Posted by Warner Onstine on October 30, 2007

Well we got the first round of reviews back from the technical reviewers and there’s a lot of work to be done. So much so that we’ve decided to put the book on hold while we regroup and figure out if/how we are going to address the issues that the reviewers found with the book. In a lot of cases it is going to require a complete rewrite of the example (and hence the chapter itself). Personally I feel the core is there, but maybe the examples weren’t as fleshed out as they could have been.

Now of course I’m a little embarrassed that I announced the book before it was done, but that’s what happens sometimes. Not sure at this point whether or not the book will continue, taking some time to regroup/rethink things and see if it is something that I want to push forward with.

I will keep everyone up to date here on the progress of things once I’ve decided what to do. In the meantime I am going to get back to some of my back-burner projects (a few of which have some DSLs in them ;-) .

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

DSL book in progress 6

Posted by Warner Onstine on October 10, 2007

Now that things feel a little bit more final to me I thought I would take this opportunity to announce the book that’s been keeping me from blogging. The tentative title is Creating DSLs using Java and Groovy and it will be published by Pragmatic Programmers. I started working on this idea after last years No Fluff Just Stuff where I saw Neal Ford speak on DSLs and I talked to him briefly about a project I was working on. His talk inspired me to push forward with using DSLs for code generation specifically using Groovy.

The book will cover both Java and Groovy techniques for writing DSLs, as well as have some general guidelines for writing DSLs. We also have a chapter on ANTLR and JavaCC for writing external DSLs using Java. I tried to cover as much as I could of Groovy meta-programming and some of the additional capabilities of Groovy that make it a good language to write an internal DSL on top of, but there’s only so much space ;-) .

We just sent off the book for peer review, which means its about half-way finished, maybe a little more. We are currently shooting for a release date sometime early next year, which looks very doable at this point, but I expect a ton of edits to come back from this first review.

On a final note I just want to say what a pleasure it has been working with the PragProg crew. I love, love, love their book build system and my editor Susannah rocks. I have had a blast working with them so far on this book and am looking forward to finishing this up and getting it published.

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Next Language to Learn? 11

Posted by Warner Onstine on June 17, 2007

Definitely looking for feedback on this one. On my previous list I used to have the following as the next languages to learn and play around in. Of course the only true way to learn a language is to find a project to do in it (but I’ll figure that one out a bit later).

In no particular order:

  • Python
  • Ruby
  • Groovy
  • Objective-C

I went ahead and nailed Groovy (well still working on it but I have learned a lot in the short time I’ve dived into it). I still have my Programming Ruby book (and my Rails book) but haven’t had a chance to dive into those yet. Really would love to compare Groovy’s meta programming with Ruby’s in the near future. Python is still kinda on the radar, but mostly to learn how to manipulate tuples. Objective-C sits above Python for the moment.

Recently though some new (and not so new) languages have entered my list:

Here are some others that I might look into given the right impetus:

  • Lisp
  • OCaml – not so sure about this one

Functional languages are all the rage right now, so it would be good to know why ;-) . And Erlang and Scala both have Web frameworks that look interesting (Erlyweb and Lift respectively). Being a Web programmer I would like to investigate these as well.

What other languages are out there that would be good to learn? Why?

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Keeping a development journal 3

Posted by Warner Onstine on June 13, 2007

At last night’s presentation by Ted Neward one of the things he suggested during his debugging talk was to start keeping a development journal. This way you have a written record of things you’ve learned, tried, techniques, ideas, etc.

I like this idea for a number of reasons. I’ve always kept notes of meetings that I’ve been to or of ideas that I’ve had, but I’ve never done one specifically for development (at least at work). I also like the idea of a timeline, where I can go back through my journal and see what I was doing at a particular time, showing me the continuity of my development thought process. It also gives me the ability to track down items that I know happened during a particular time – “How did I end up tracking down that bug with accounting lines again?”

One of the problems I’ve always had is in seperating my personal projects from work projects (and personal projects from each other). I think that this approach, while on the surface looks good, is a bad idea. I periodically have gone through house cleaning and found several partially-filled notebooks of projects I’d started to think about but never implemented, but that had good ideas in them. So what do I do with those? They are in separate notebooks so I can’t combine them. What happens generally is I rip out the pages and recycle the notebooks, but I always end up with more partially-filled out notebooks.

I’ve also started using this blog as my own dev journal, but I don’t/can’t put everything up here (especially for all the work stuff I do as its mostly boring ;-) . I like to filter the stuff in my head (a bit) before posting it here. Starting today then I’m going to pick up a Moleskine and start my own dev journal with everything in it including work stuff. Then when that gets filled up I’ll start another. I don’t know how far to separate stuff yet (as I have other things besides development I’m interested in), I think I’ll get two journals, one for software and one for “other creative stuff”.

If you don’t like Moleskines you can always go for a lab notebook (the ones with the fake marble cover) as these have a sturdy cover that will last for a while and you can write the dates on the front cover (sorry couldn’t find a reliable link to these but you can find them in any office supply store).

If you’ve tried doing a development journal, what’s worked for you? what hasn’t?

Share and Enjoy:
  • Print
  • Digg
  • Reddit
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • DZone

Easy AdSense by Unreal