2015-04-27

Hiding protobuffer generated code in a Java library

In this post I describe how to hide Protocol Buffer's generated code in a Java library.

Recently, I was developing a Java library that used Google's Protocol Buffers as a serialization format. As part of the design of the library, we only expose some classes, the remaining implementation has package-private visibility. In particular, we de not want to exposed the generated object, as it was mapped into a domain class, but the protoc.exe executable produces public code. The solution was to perform a post-processing step invoking "protoc" to remove all public definitions used the sed command (if you are on Windows, you should have this command available by installing Git Bash). Here is an example of a batch file to this on Windows:

@echo off
:: compiling ProtoBuff definition
protoc.exe --java_out=java\ java\pt\invisibleobjects\MyObject.proto
:: setting package-private class visibility for classes
sed -i 's/^public final class/final class/' java\pt\invisibleobjects\MyObject.java
:: setting package-private class visibility for static classes
sed -i 's/^public static final class/static final class/' java\pt\invisibleobjects\MyObject.java
:: setting package-private class visibility for enums
sed -i 's/^public enum/enum/' java\pt\invisibleobjects\MyObject.java

Back to the Future

I've recently decided to close my old personal blog. However, I picked all the software development related posts (wrote somewhere between 2007 and 2008) and imported them into this blog.

2015-04-19

return java;

As mentioned previously in my blog, I'm back to Java development. And it's been a while since my MSc AspectJ prototype. So how does a .NET developer survive in a Java world?

First, read this article: Java Doesn’t Suck – You’re Just Using it Wrong

Second, if you are lucky enough, try to lobby for using a modern Java stack. We are a couple of interesting technologies:

Finally, a good IDE is also important. The Eclipse IDE hasn't changed much over the years, and so I've been using IntelliJ IDEA, which is quite better (not surprising, since it's developed by JetBrains, the same people that developed ReSharper). There is a nice Community Edition, which is free. As a bonus, if you want to use the same short-cuts as ReSharper, you can get them here.

Design by Contract for Java on github

I've decided to share on GitHub the Design by Contract for Java (DbC4J) library. This was a prototype I developed back in 2007 for my Master Thesis, "An Aspect Oriented Infrastructure for Design by Contract in Java".


The DbC4J library allowed enforcing contract clauses using regular Java methods via naming conventions. Internally, it was implemented using AspectJ, the original Aspect-Oriented Programming (AOP) framework, and a lot reflection. The library has been available on-line since 2007, but the since the research group I was part of closed their website, it has been unavailable. Currently the source code available at GitHub is the last release from 2007, unchanged, so I'm not sure how the code compiles under the latest versions of Java/AspectJ (this was originally implemented under JDK/AspectJ/AJDT 5 with Eclipse). Also available on GitHub is my Master Thesis dissertation, in case anyone in interested in the research underlying the prototype.

2015-04-02

The blog formely known as Sergio Agostinho .NET

I've recently started working professionally on Java and I would like to blog about it in the near future. As such, it makes on sense to create (yet) another blog, so I decided to rename this blog to "Sergio Agostinho Dev". This means I'll also rename the site URL to http://sergioagostinho-dev.blogspot.com Yes, this will break existing links and will not benefit the blog SEO, but it's a change I would have to do, sooner of later.