Forget Guava: 5 Google Libraries Java Developers Should Know
What are some of the most useful yet unknown Google Java libraries?
If we would stop someone during an intense hackathon and ask her to name a popular Google Java library, the answer would probably be Guava. Itâs no wonder, since its main focus is improving developer productivity. But what about other Google libraries? Have you had a chance to check them out?
In the following post weâll go over some interesting Java libraries from Google, and give you a peek as to what you can do with them.
Adding another library to the mix
First, letâs be clear: we love Guava. It contains some of Googleâs core libraries for Java based projects, such as collections, caching, common annotations, I/O and others. These are useful not just for Google developers, but for the wider community as well.
Although itâs not the only cool thing that came out of Google. Weâre not talking about driverless cars, Google fiber or project Ara (and weâre still waiting for that last one) â weâre talking about more libraries.
The good news are that thereâs no need to comb through every available library in order to find the right one, since we did that for you. We chose 5 Google libraries that could make your life easier and code a little bit nicer. Check them out:
1. Dependency Injection â Guice
Guice is a lightweight dependency injection (DI) framework for Java, performing a similar role to Springâs core Inversion of Control (IOC) library. Itâs almost as popular as Guava. If youâre not familiar with it, Guice eases the need for factories and simplifies the use of ânewâ in your code. In Googleâs own words, Guice @Inject is the new new.
While this library wonât absolve you from writing factories, it will help make sure your code is not directly dependent on them. That way, it will be easier to change, unit test and reuse your code.
The cool factor
This library aims to make development and debugging easier and faster. The documentation even claims to help you get a better understanding of your code. Another cool feature Guice offers is generating helpful messages when errors occur, so youâll have an idea of what happened and how you can solve it.
2. Static Analysis â Error Prone
This open sourced library is a static analysis tool for Java, allowing you to catch common programming mistakes at compile-time. You can be the best developer in your team, but you or one of your colleagues might be pre-occupied by the latest Game of Thrones episode, and make a mistake.
In other words, this tool will help you catch mistakes before they become bugs in production and mess up your application.
For example, one of the bug patterns it can detect is CannotMockFinalClass â which indicates that Mockito cannot mock final classes:
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.Mockito;
/**
* Test for CannotMockFinalClass.
*/
@RunWith(JUnit4.class)
public class CannotMockFinalClassPositiveCases {
static final class FinalClass {}
// BUG: Diagnostic contains: Mockito cannot mock
@Mock FinalClass impossible;
public void method() {
// BUG: Diagnostic contains: Mockito cannot mock
FinalClass local = Mockito.mock(FinalClass.class);
}
}The cool factor
On the official library page youâll find a list of common errors and their suggested fixes. This means that each error has a solution that someone already thought of, and you can implement it easily in your own code. By the way, if youâre interested in common production errors, check out our analysis on the top 10 exception types in production.
3. Meaningful Errors â Truth
Truth is a testing framework, made to help your test and error messages be more readable and discoverable. It generate meaningful error messages that report on why tests fail, while being extensible to new types of objects. Oh, and it comes with the awesome catch phrase âWeâve made failure a strategyâ.
You can use Truth in place of JUnitâs assertions, FEST, or Hamcrestâs matchers classes, or simply use it the way you see fit for your code. It presents you with a test verb that asserts on a subject, the value or object under test.
You can use Truth in order to declare JUnit-style assumptions (and skip test on failure), assertions and exceptions.
import static com.google.common.truth.Truth.assertThat;
Set < Foo > foo = ...;
assertThat(foo).isEmpty()
org.truth0.FailureStrategy$ThrowableAssertionError: Not true that is empty
at org.truth0.FailureStrategy.fail(FailureStrategy.java: 33)
...4. Language-Agnostic Protocols â Kythe
Kythe provides and supports tools and standards to encourage interoperability (sharing, exchanging and using information between systems) among programs that manipulate source code. An easier way to explain it is to say that this library gives an ecosystem for building tools that work with source code.
The main goal of this library is to provide a standard, language-agnostic interchange mechanism, that will work on any source code. It includes build systems, compilers, interpreters, static analyses, editors, code-review applications and more. That way, youâll be able to share information with any other system youâd like.
The cool factor
Kythe comes with a rich community that will answer any question you have. Need help with Kythe on Python, JavaScript or Java? Want to know how to model Python modules? Or do you simply want to know how to pronounce Kythe? Just ask the community.
5. Serializing Structured Data â Protobuf
Protobuf, the library thatâs also known as Protocol Buffers is Googleâs language-neutral, platform-neutral, extensible mechanism for serializing structured data. If youâre not sure what it means, Google explains that itâs like XML, only smaller, faster and simpler.
If we had to choose a slogan for this library, it would probably be âstructure once, use anywhereâ, and thatâs the best way to describe it. Once you define how you want your data to be structured, you can use special generated source code to read and write from a variety of data streams and using a variety of languages.
The cool factor
Full disclosure is in order here: protobuf is one of our favorite libraries here at Takipi, and we can guarantee it will help you make complicated elements a little bit easier.
Did we forget anyone?
We know what youâre thinking â so many libraries and not a single word about GWT. GWT is a development toolkit for browser based applications, enabling the creation of high performance web apps even if JavaScript is not your strong suit. Itâs basically a Java to JavaScript compiler.
This free, open sourced library became popular due to the many Google products that used it. That list included Google Inbox, AdWords, AdSense, Flights, Hotel Finder, Offers, Wallet and Blogger, all very known and powerful products that came out of Google.
There are a few well known companies beside Google that use GWT, such as Ruxit, Go Grid, Whirled and others.
Additional Google Libraries
Following a comment on twitter weâve decided to add 2 more useful libraries that werenât mentioned here, thanks @joschi83!
1. AutoValue â AÂ collection of code generators that automate generation of repetitive Java code.
2. GSON â The popular Java serialization/deserialization library that can convert Java Objects into JSON and back.
Final thoughts
There are a lot of Java libraries that can help your code and make it smarter and easier to use. All you have to do is understand what youâre looking for, and see how a certain library can help.
If youâre a hardcore Guava fan and couldnât find anything useful in these other libraries, you should check out some of the lesser known features of Google Guava that every developer could use.
| Reference: | Forget Guava: 5 Google Libraries Java Developers Should Know from our JCG partner Henn Idan at the Takipi blog. |

