Tag Archives: java

Holiday Hack: reading Java class files

I spent a week by the pool in Egypt with my girlfriend, relaxing and…hacking. The lack of WiFi meant I had to rely on instinct and intellect to get me through my pet project and it proved a worthy exercise in problem solving and perseverance…

I had the crazy idea of practicing my C programing skills by reading the binary .class file format and…doing something with it. A friend even suggesting writing my own Java Virtual Machine, which I’m probably going to do at some point.

Anyhow, a week by the pool meant no WiFi, lots of food and drink, sunburn and…boredom. I set it upon myself to rely purely on the documentation I had to hand — at first just a cached Wikipedia page, later on the JVM specification — to read .class files and practice C.

It turns out this basic task proved to be a useful exercise not only in practicing C but in perseverance and relying on one’s intuition and experience to solve a problem, not just on “googling it” and Stackoverflow.

Lesson #1: having the source makes things easier

Though I’ve learned this many times before in other languages, having the source to hand means you can truly debug an issue.

I happened to be using uthash.h for a hash table as part of my data structures. The mistake I’d made was define my id type as a uint16_t — i.e. a 2-byte unsigned integer type — and then use the HASH_ADD_INT and HASH_FIND_INT convenience macros for manipulating/searching the hash table. Of course an int is 4-bytes on my Debian x86 laptop, so when the macro expanded to something along the lines of HASH_FIND(hh, table, id, sizeof(int), ptr); I was never finding items because the generated memcmp call was comparing the wrong amount of bytes. A rookie error, but a lesson nonetheless.

Lesson #2: don’t quit

Building on lesson #1, I was reminded that I am only limited by time, my own stamina/patience and ability to think around a problem. My limited experience with C and the advanced nature of uthash.h‘s source code meant it was daunting trying to debug and understand what it was doing and ultimately where I was going wrong. Alas, by the end of the debugging exercise I was much the wiser as to the workings of C and its true power. Reading the source code of others is always a useful task and had I quit when seeing the wall of #defines I’d have not better understood macros, C or the inner-workings of data types in C. The lesson? Keep chugging away – even when it’s frustrating you and you think you’re lost it can pay wonders to take 5 minute swim in the pool and a coffee to re-tackle a bug.

Lesson #3: DVCS is really useful

The only time I had WiFi was when we ventured up to the dedicated area near reception. Our room and the majority of facilities were nowhere near it, which meant we’d have to dedicate half an hour to trekking up to reception, hoping the connection’s working (Egypt’s infrastructure isn’t what we’re used to here in the west!) and frantically downloading what I can whilst the bandwidth isn’t saturated by Facebook pokes.

The point, though, is that I had versioning control on all the time. I didn’t have to commit to a remote server to save my small changes, improvements or fixes, I had it right where I needed it: on my hard-drive. This meant I could push to the Github repository irregularly but still have the power of versioning whenever I needed it: in this case in the form of Git.

Lesson #4: the UNIX mentality a.k.a. “do one thing and do it well”

I often get into the habit of concentrating on one programming task and sticking to it. Though I also stray from the task — by hyper-focusing for a bit and changing several things at once, ultimately confusing myself — one is almost always better off just following the UNIX mantra of doing one thing and doing it well: in my case implementing and committing the parsing code in one step and then the utilisation of that feature in the next.

Lesson #5: stop the features and tidy the code

It doesn’t always have to be a frantic race to the finish line for implementing all the features. In my process of learning and trial and error I wrote some crappy code. Once I’d felt I had a handle on what I was working on and ensured my understanding of the structure of the .class files, I spent time removing bits I hated, simplifying the complex parts of the codebase and satisfying my pedantic nature. The added benefit of this is that you stop the head-wind for how you’re implementing features which when the break for refactoring is over can mean you change your approach for a better solution and thus better code.

Lesson #6: cut the WiFi

Seriously: stop relying on Google so much and solve it yourself. Your skills, knowledge and ego will thank you.

My code’s over on Github if you’re interested.

The final keyword in Java

Whilst many will frown at the use of the final keyword in Java code, I find it a breath of fresh air. Perhaps it’s that I tend to lean on the side of caution, conservatism and safety when it comes to code: immutability being one of many examples of such. In this post I argue using it better than not, much like avoidance of doubt lends itself to stronger solutions.

Immutability is therefore the strongest reason I quote when one asks why I declare as much as possible as final in my code, but as with many things the use of final does have its caveats.

final Variables
According to the Java Language Specification:

A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

which means local or class scope, if you declare a variable final it must be assigned and (according to certain rules) and the compiler complains on subsequent attempts.

To me, this is useful because if something cannot change then why let it? If I can let the compiler do something for me reliably and consistently, then why shouldn’t I? Again: if a value shouldn’t change then make sure it doesn’t.

Readability
Some might find the following code more readable than an equivalent with the use of final:

public float calculateAverageAge(Collection personCol) {
float ageSum = 0;
for (Person p : personCol) {
ageSum += p.getAge();
}
return ageSum / personCol.size();
}

Yet, when we compare, there’s little difference:

Java final keyword code diff

With that said, the old adage “code is read many more times than it’s written” is a strong case against; though I personally feel it’s not actually *that* unreadable: but I risk venturing into an argument of subjectivity which we all know is futile. Perhaps it’s only less readable because you’re not used to it? Perhaps if it were commonplace we’d never have a problem. If it’s a screen real estate issue: final has the same number of letters as the C keywords const and there are those who argue that using const is good practice too; not to mention that we’re no longer in the days of 80-character-wide terminals.

Immutability
I think it’s fair to state that software is complex. Reducing complexity where possible makes it easier to reason about solutions. Solutions that are easier to reason about are therefore easier to implement at a programming level.

One assault on complexity in any given codebase is the mutation of state: changing properties of some entity that some aspect of the system’s logic relies on leads to a growth in what has to be considered. One could argue then, that reducing state mutation reduces complexity and this therefore leads to an easier solution. It is here where final can aid. My reference cannot change. Recursively, the properties within that object reference cannot change, which in turns means I have less surprise and less to reason about when using those objects. It is a solution that can cascade.* If you don’t agree: replace “easier” with “simpler” and reconsider.

Again: if something cannot change then why let it? In a block of code that declares 5 references and only one can be change (reassigned) then it is that one we have to worry about. It is that one that the unit test should cover in more cases. It is that one that the new programmer reading the code 6 months after it was written has to watch.

Functional Programming
In the functional programming world the idea of purity is a fundamental tenet. Functions are pure if they have no side effects. They are idempotent in nature: the same input produces the same output. Whereas in contrast the OO world does not have such a practice, at least in the same way. To satisfy encapsulation we have mutators which provide an interface to some property of an object. Coupled with abstraction these allow the internal structure of that object to change without forcing change on its clients; but herein lies the problem. Compile time changes are but one fraction of change. Of equal concern are the semantics of that dependency. If the code doesn’t have to be recompiled then great, but what about the actual intent behind that link? Has the logic changed? What impact does that have? How can I tell? The answer in either case is that you cannot tell without testing or without analysing the code. I don’t see that as a huge problem: with change we need to assert its validity. If we can encourage code to “do what it says on the tin” then we have simpler solutions. If side effects are non-existent then we have another string to our bow of simplicity. K.I.S.S., right?

Conclusion
It’s never easy to argue a case for doing something absolute in the professional software development “arena”. One learns either the hard way or through teamwork that these absolute rules are few and far between. Similarly, applying a concept or pattern blindly or where it is inappropriate for the solution leads to problems. Whilst I’d argue — as I have above — that reducing complexity is always something a solution should head towards, sometimes it’s unavoidable. Why then, would we not reduce it where we can, letting us spend energy on the elements that are complex, on the components that cannot be diluted further?

* Update: as stated on reddit, the final keyword does not extend to the fields of an object instance, unlike C’s const on a struct. I omitted this deliberately for a follow up post ;)

Is findbugs-maven-plugin onlyAnalyze broken?

I cannot seem to get onlyAnalyze working for my multi-module project: regardless of what package (or pattern) I set, maven-findbugs-plugin either parses everything or nothing and it doesn’t evaluate sub-packages as I’d expect from passing it packagename.*.

To prove either myself or the plugin at fault (though I always assume it’s the former!), I setup a small Maven project with the following structure:


pom.xml
src/
main/java/acme/App.java
main/java/acme/moo/App.java
main/java/no_detect/App.java

which is very simple!

The POM has the following findbugs configuration:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>2.4.0</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals><goal>findbugs</goal><goal>check</goal></goals>
                    </execution>
                </executions>
                <configuration>
                    <debug>true</debug>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                    <onlyAnalyze>acme.*</onlyAnalyze>
                </configuration>
            </plugin>
        </plugins>
    </build>

and every App.java has the following code with two obvious violations:


package acme;
import java.io.Serializable;

public class App implements Serializable
{
private static final class NotSer {
private String meh = "meh";
}

private static final NotSer ns = new NotSer();// Violation: not serializable field

public static void main( String[] args )
{
ns.meh = "hehehe";// Vilation: unused
System.out.println( "Hello World!" );
}
}

Note that no_detect.App has the same content as above, but my expectation is that it wouldn’t be
evaluated by findbugs because I have the “onlyAnalyze” option set to acme.* which I assume would
evaluate acme.App and acme.moo.App and nothing else.

I now execute a mvn clean install to clean, build, test, run findbugs, package, install, which
produces the following findbugs report (snipped for brevity) and results in a build failure which is expected
because acme.App and acme.moo.App:


<BugInstance category='BAD_PRACTICE' type='SE_NO_SERIALVERSIONID' instanceOccurrenceMax='0'>
<ShortMessage>Class is Serializable, but doesn't define serialVersionUID</ShortMessage>
<LongMessage>acme.App is Serializable; consider declaring a serialVersionUID</LongMessage>
<Details>
<p> This field is never read.&nbsp; Consider removing it from the class.</p>
</Details>
<BugPattern category='BAD_PRACTICE' abbrev='SnVI' type='SE_NO_SERIALVERSIONID'><ShortDescription>Class is Serializable, but doesn't define serialVersionUID</ShortDescription><Details>
<BugCode abbrev='UrF'><Description>Unread field</Description></BugCode><BugCode abbrev='SnVI'><Description>Serializable class with no Version ID</Description></BugCode>

To summarise: only acme.App is analysed, acme.moo.App (bad) isn’t and neither is
no_detect.App (good).

I tried with two wildcards in the onlyAnalyze option but that produces a **successful build** but
with a findbugs error (Dangling meta character '*' etc).

I tried with onlyAnalyze set to acme.*,acme.moo.* which analyzes all the expected
classes (acme.App and acme.moo.App) which means it “works” but not as I expect; i.e. I have to explicitly declare all parent-packages for the classes I want to analyze: that could get large and difficult to maintain on a multi-module project!

Must. Keep. Trying.

Modern Service Oriented Architecture: the basics

Just some of my notes on looking over SOA related theory.

What is “SOA”?
Service Oriented Architecture is a means of exposing business services
separately from their platform and codebase in order to provide local
and/or remote invokation of said services through abstracted data types
and signatures.

Put simply: SOA allows you to talk to alternate platforms by removing
the bespoke or language-specific features and data types to a
higher/specified representation providing interoperability between
platforms without the need for their awareness of eachother.

As a real world example, an organisation can provide interoperation
between legacy and new systems by abstracting the manner of how they
talk and the underlying content: new code can use technologies like XML
to define the data and the corresponding types and the two systems can
work together through that abstraction. This scenario is commonplace
among business where different streams are responsible for systems and
so a common format for data interchange is necessary to allow the two to
co-exist separately.

SOA also provides a more modular approach to service integration: if a
web service can “proxy” requests from one system to another, the two can
change independently without the need for related changes to the
services themselves. Such a scenario – one of proxying – can encourage
an ecosystem of services allowing each to communicate with seemingly
unrelated counterparts through a common set of data types and methods.

Benefits of SOA
Typically, the “promises” of SOA are numerous.

SOA allows for system agility
As aluded to previously, the ability for bespoke systems – those written
for specific purposes in a language of choice – to communicate with each
other allows for greater flexibility and scalability with an
organisation’s data. For example, if you can have your sales systems
talking to your suppliers through an aggregation service to warn of
unusual sales volumes, out-of-stock items and so-on, you can better
support the volume of a growing business and provide real-time response
to customer needs.

Consider an organisation where a sales system cannot speak with the MI
to ascertain whether a customer on the phone holds an account with you.
Crazy, no? In scenarios like this where legacy, decades-old and
difficult to support systems hold the key to sales success but are the
hindrance to it, SLA can be leveraged to quickly and easily have the two
communicating and providing front-line departments with the information
they need.

SOA can encourage innovation
In the post-Google era, notions of service “mash-ups” are commonplace.
These newfound, new-age businesses popularised the developer by
harnessing open source technologies and building public-facing APIs: all
built on service oriented architecture. They encouraged interoperation
between seemingly unrealted entities which built a platform for
innovation and a revolution in the software industry.

If you reduce this scale and manage to accomplish this very nature
within an organisation, then new products and alternatives to existing
ones spring from innovative and organic attitudes from developers and
system integrators.

You wouldn’t build a palace with breeze blocks

I strongly believe that if you want to build awesome software, then every aspect of it needs to be as good as you want the final product to be.

It is often the case that aged software shops will have put in place aspects of their software that they’ll “revisit later on”, which then doesn’t happen.

It gets lost to the crowd of “more important things to do”.

One of the most common neglects I see is how a shop will build, deploy and distribute its software. At best their solution is amateur. It’s usually home-baked and rigged with esoteric and often superfluous tasks and features that are “necessary” due to some poor design decision made previously. The build system — be it continuous integration or some poor sod in the corner — has to do 30 different things, copy 11 thousand files, just to compile the few.

And so the problem persists; the rot festers; the software continues to be sub-par and so do the profit margins.

I feel that if you start from the ground-up, that you build consistently and efficiently then your software becomes such. If it is easy for a developer to build and deploy or distribute your software, it’s then easy for them to do their job. Any developer knows the pains they’ll go through day-to-day when making a change that requires a 15-minute rebuild and 5 minute deployment wait just to check the first name field doesn’t fall over on a null pointer.

With poor practice comes poor software comes poor profit.

So if you’re heading up a development shop and your developers are crying for a more efficient, simpler, consistent build process, then heed their recommendations and concerns. Let them spend the time and money streamlining how they work, because you’ll get reliability, efficiency and less coffee breaks if you do.

Would you pay for that shiny new tablet PC if it came packaged in A4 paper?

How disappointed do you feel with a product when you hold it and you “know” its build quality is poor.

Why is software any different?

You wouldn’t build a palace with breeze blocks.

What’s an “Interface” in Java?

I was browsing reddit this afternoon and came across this post. It’s an interesting concept to explain. I mean, what is an interface in Java?

I thought through the best way to explain this to someone who doesn’t understand OO or Java in general, and I came up with the following.

The Letterbox Analogy
Think of a letterbox.

Doesn’t matter who makes the letterbox, it’ll always operate the same way. Doesn’t matter what country you go to, if you want to post a letter, you just put it through a letter box.

public interface Letterbox {
void post(Letter letter);
}

If I’m a manufacturer, I might want to make exquisite letterboxes. I’ll make ‘em out of gold, and they’ll be sturdy and look outstanding. The shape, though, will remain the same as any other. So I’ll implement the Letterbox interface:

public class GoldLetterBox implements Letterbox {
public void post(Letter letter) {
// some code...
}
}

and then people will buy them, and they’ll put them in doors, and their postal employees will put their letters and packages through the door. It’ll still be Super Special Gold Letterbox Co’s letterbox, but everyone will know how to use it.

Android Preferences and Custom Buttons

I delved into the Preference API for Android this weekend. It turns out that I needed a couple of “buttons” to allow the user to go to the main screen (for various genuine reasons!). The Android API doesn’t let you do it with buttons, but there’s an equally easy way.

I have found that using a combination of a “placeholder” Preference and then providing an onClickListener in the corresponding PreferenceActivity class provides what I needed.

In my instance, I needed a “Save” and “Clear” option in my preference screen, so I added the following PreferenceCatgory in my preferences.xml

<PreferenceCategory
android:title="Manage Your Preferences">
<Preference
android:title="Save"
android:summary="Store your preferences"
android:key="savePref" />
<Preference
android:title="Clear"
android:summary="Clear all your preferences"
android:key="clearPref" />
</PreferenceCategory>

and then inside my onCreate method, I just assigned listeners to the individual clear and save preferences:

findPreference("clearPref").setOnPreferenceClickListener(onPreferenceClickListener);

which is given an instance of my inner `Preference.OnPreferenceClickListener` class:

private class PreferenceOnClickListener implements Preference.OnPreferenceClickListener {
public boolean onPreferenceClick(Preference preference) {
//Do something...
return false;
}
}

easy?!