Hecl Java Integration

Yesterday, I took the Java integration I’d been building into the Hecl Android port, and moved it into its own directory, so now it’s built in to the command line/j2se (non JavaME) version of Hecl for people to experiment with. Here’s a slightly modified version of my posting on the subject:

I created a java/ subdirectory with an org.hecl.java package that does a few things. The core of the system is in JavaCmd and Reflection. The first creates commands out of Java classes. It utilizes, obviously, the reflection stuff in Reflection. Both are, at the moment, sort of hacky and tentative, but work. For instance, I checked in this bit of test suite today:

test java-2 {
   java java.util.Hashtable ht
   set hasht [ht -new {}]
   $hasht put foo "bar"
   $hasht get foo
} {bar}

The ‘java’ command takes a class, and string, and creates a new Hecl command ‘ht’ that is tied to the Hashtable class.

ht -new {} (could have been -new [list]) is how we call the constructor. After this, $hasht points to a Hashtable, and we can utilize its methods. Up to a point, at least – there have to be mappings in Reflection to deal correctly with types. For instance, there isn’t a mapping for Enumeration right now, so calling the
.keys() method wouldn’t do the right thing.

There are some more bits and pieces of magic:

  • Static methods are called like this:

      java java.lang.System sys
      sys gc
    

    Which calls System.gc().

  • Static fields, for now, are called like this, although once again, I’m still thinking about the syntax:

      sys -field err
    

    Which is of course System.err

  • In addition to -new, when you instantiate an object, you can pass in things like -text "foo", and that will attempt to call setText("foo"). This is a convenient way, in Android, at least, of setting up an object from within Hecl, without requiring the use of the XML layout files.

  • Furthermore, in order to deal with constants in a convenient way, something that’s reasonably common in certain types of API’s (like Android), the following pieces of code are equivalent:

      layout.setOrientation(LinearLayout.VERTICAL);
      $layout setorientation VERTICAL
    

    What’s happening in the Hecl code is that VERTICAL is looked up in the context of the class that’s calling it, so as to avoid having to do [layout -field VERTICAL] to fetch the value. This won’t work everywhere, and I suppose it could be criticized as too much ‘magic’. Naturally, these sorts of things will evolve as the code does, and survive if they’re useful and don’t create problems, or die out if they cause bugs and aren’t utilized.

There are some open issues with the ongoing work:

  • I like the API for already instantiated objects, but I’m not 100% wedded to the idea of -new for instantiation. The fact that it takes a list is also a bit tricky, because if you want to do something like construct an argument to be passed in, you might end up with something like this:

      callback -new [list [list SelectDemo $spinner]]
    

    which I don’t find all that pleasant. You can’t just do -new [list
    SelectDemo $spinner]
    because then it thinks it’s got two arguments,
    when it really only has one, which happens to be a list.

  • The APIs created like this tend to follow Java pretty closely, for obvious reasons, and can be a bit verbose at times. I’m still thinking about this. One idea might be to provide Hecl wrappers that make common operations easier.

Interest in a simple open source newsletter system for Rails?

I’m putting together a very simple newsletter system for a client, based on Rails. It’s meant to handle around 1000 addresses, and be simple to use: you create a newsletter with fckeditor, look at it, then send it if everything looks good. It also ads links so that people can unsubscribe, or subscribe from the site (with confirmation provided via a link in an email).

It’s not nearly as extensive as a lot of what I’ve seen out there in PHP, but I really needed something in Rails and didn’t find anything that worked for me. I’m considering open sourcing the code, but am only likely to do so if there is interest from people willing to work on the code. I’m just not in the mood to put code out there that just sits there. If you are interested, email me and we can work something out.

One reason to think Rails is “all that”

The economics of programming languages point to Rails being significantly better than what went before it.

I got to thinking about this when reading a comment on a site I like to read, which said:

Rails in itself is, to me, not that impressive. It does a lot of things right, but it does probably just as many wrong. Not the least of which is scaling.

It seems that these sorts of “after the fact” “I know better” comments are a dime a dozen in the world of programming discussions. It’s easy to come along after something’s been built and puff yourself up by pointing to defects in existing systems and show that, therefore, by comparison, you’re a clever fellow.

That’s not my point, though – what I wish to explain is that yes, Rails really was that much better than what was around before it came onto the scene:

“Switching costs” between languages are high. Less so for really sharp programmers, but for the masses that use one or two languages, learning a new language, tools, deployment, etc… is a big step to take, with potentially high risks. Even most A-list programmers I know use a few languages at a time – it’s simply easier if you’re not tripping over your own feet by switching to a different system every day. “Flow” is easier to attain when you’re ensconced in the thinking of one language. For companies, this effect is magnified, and switching to something new is not done lightly.

Since companies are beginning to explore Rails, successfully, I might add, you have to conclude that the big step into the unknown was worth it for some reason. Especially considering that a number of other languages rushed to copy various nice aspects of Rails, lessening the need for users of those systems to consider taking the leap.

Of course, that’s not to say it’s a perfect system, without reproach, or has no negative aspects, but in the spirit of honesty, and credit where credit is due, Rails really did move things a step forward, and the willingness of people to incur high switching costs to obtain its benefits is strong evidence of that.

Android, Hecl and scripting

Aside from my hard drive crash, I’ve been working to figure out the best way to integrate Android and Hecl. Since Hecl has been a small, simple, humble language suitable for J2ME (or JavaME or whatever), that has meant in the past that it hasn’t really been able to utilize more complex or advanced Java features like reflection, because they aren’t present in the J2ME API. Android’s more complete implementation of “Java” (yes, I know about the VM, but the programming language I’m writing in is still Java) opens up a number of possibilities.

First on my list of things to do was aim for a generic access layer for Java objects using reflection, so that you can do things like this:

$button settext "blah blah"

Now that we have that working, I also wanted to be able to create new Android widgets from Hecl, which is where things get tricky. The “problem” is that I want something that feels like a scripting language, not simply a reflection of the underlying Java. For instance, the XML description of a button looks like this:

<Button id="@+id/execute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Execute"
/>

Which is actually not so bad in terms of verbosity. The Java code is uglier, in my opinion:

Button button = new Button(activity);
button.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button.setText("Execute");

Especially the LayoutParams business, that is nowhere near as clear as the XML, where it’s very obvious what values you’re giving to what parameters. You wouldn’t know that from the Java code without looking up the constructor for the LinearLayout.LayoutParams class.

Now, with Hecl I could of course write the code by hand to make things ‘friendlier’, but I’m worried that if I have to do that too much, it will take a long time to cover the rather extensive API, and it will also bloat the code. The ideal solution would be subvert the XML stuff for my own purposes, however, that’s proving tricky. My first idea was to utilize the AttributeSet parameter that you pass to many View derived objects’ constructors, in order to transform code like this:

button -text hello -layout_width 100 -layout_height 34

Into something that Android can digest. Unfortunately, that’s proving difficult, as the internals of Android barf on the class I’m passing them, even though it, to my knowledge, implements the AttributeSet interface without any problems.

This really made me wish that they would hurry up and release the source code, because I would have a far better understanding of the relationship between the XML attributes and the Java methods if I had access to what’s going on under the hood. Harumph.

My next attempt was a hack that takes things like -text and transforms them into calls to setText, but that isn’t as good, because you need a little bit more custom code for things like the layout parameters, which require special handling. However, lacking the source code, or some help from the Google folks with access to it (which isn’t likely over the next few days as they’re doubtless off consuming turkeys), it looks like that’s what I’ll have to do.

And we’re back in action!

I’m impressed. I reported the problem with my laptop (purchased in the US) to Dell Germany yesterday morning at about 10AM. Today (Tuesday), a technician with a new drive stopped by my house at about noon. Now I’m busy restoring from backups, after installing Ubuntu Gutsy.

While it’s certainly been a big disruption, I’m impressed with Dell’s customer service.

My laptop’s HD appears to have died

So it looks like I will have to use the other computer, and hope that Dell sorts things out quickly. I’m pretty disappointed, as I just purchased the machine (an Inspiron 1505 with Ubuntu) in July, and haven’t really traveled with it much.

[440525.460000] ata2.00: configured for UDMA/33
[440525.460000] ata2: EH complete
[440942.908000] ata2.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x2 frozen
[440942.908000] ata2.00: cmd a0/01:00:00:00:00/00:00:00:00:00/a0 tag 0 cdb 0x43 data 12 in
[440942.908000]          res 50/00:01:00:00:00/00:00:00:00:00/a0 Emask 0x2 (HSM violation)
[440942.908000] ata2: soft resetting port

It required fsck’ing, and that was pretty much all she wrote.

The disk itself, according to smartctl:

Device: ATA      ST980813AS       Version: 3.AD
Serial number:             5NH0301G

It doesn’t support SMART though, so I don’t know if the recent fuss about Ubuntu and Laptop HD’s has anything to do with it. I never ran it much without power in any case, so I doubt it.

It’s especially frustrating, because I was making good progress on the Android port for Hecl, and will in the best of cases have to get a new computer “broken in”, even though I do have good backups.

My fingers are crossed that the money I spent for extra coverage will be worth something and that Dell Austria will be able to resolve the problem in a hurry, without requiring that I send the machine to the US or anything else that takes a lot of time.

Sigh:-(

Google’s Android is out!

Video introducing it:

http://youtube.com/watch?v=1FJHYqE0RDg

SDK is available, and all free software (or will be shortly, at least):

http://code.google.com/android/

The GUI API is not like MIDP2.0, so it’s not compatible with existing J2ME apps, like Hecl. Hrmph. Seems like Java 5 constructs are supported (I also wonder what those hex addresses are doing in there):

http://code.google.com/android/intro/hello-android.html

In any case, very interesting, and very cool. It certainly changes things a lot, though – until this becomes commonplace, it will just add to the number of platforms that need to be supported, which might actually make life tougher for a while. All things considered, though, you can’t complain about getting something that cool under an open source license!

The Economist on Business Books

Kicking ass in an unflat world is The Economist’s take on recent efforts in the world of business books. As the creator of Squeezed Books, their opinion was of interest to me.

As is often the case, they don’t disappoint, with lines like

Things to bear in mind as you do, he says, include the need for your company to have purpose, to seek out ideas from the fringes, and to embrace the democratising power of the internet—none of which will be news to anyone who has been in business for more than a few minutes.

What really caught me eye, though, was this:

There is also growing demand from overloaded executives for shorter books. So in January Harvard will launch a new hardback series, “Memo to the CEO”, covering subjects such as lessons from private equity and the future of strategy. Each book will be about 100 pages long and will cost $20. In a similar vein, Harvard has adopted the approach taken by iTunes to pop albums, and has started to sell individual chapters of books online—an idea that has been a hit with academics in particular.

A lot of good business books take a sensible central idea, and then proceed to fluff it up to a length where it can be published as a book. Often, this makes for a good read, as the anecdotes and case studies and whatnot are interesting, but for those in a hurry, or simply wanting to know what the basic point of the book is about, it’s just padding.

So, it looks as if the idea behind Squeezed Books is a good one, now I just need to get better at attracting more people to contribute summaries, and discuss the books on the site. One thing I’ve done to that end is stipulate that the summaries are available, unless otherwise noted, under a creative commons license.

Javascript fiddling

I’ve been playing around more with Javascript, as it seems to be increasingly important for doing interesting web applications.

I wrote this stupid hack over the weekend:

http://www.dedasys.com/pagedump.js

Which you can test here (warning: it kills the page you’re on, so if you’re reading this from a feed reader or something, becareful).

meltdown

There is, however a serious problem – the above code crashes Internet Explorer, so please use caution when running it. I can’t figure out what the problem might be, as it works ok in Firefox and Konqueror. Now I remember why I didn’t like Javascript before – “it’s the implementation(s), stupid!”. Advice appreciated.