Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on
Updated: 8 hours 17 min ago

SLAP Your Methods and Don't Make Me Think!

Tue, 2016-08-16 15:31

In my recent post about creating a blogging platform I posted a piece of code like this:

public MarkdownPost(Resource resource) { try { this.parsedResource = parse(resource); this.metadata = extractMetadata(parsedResource); this.url = "/" + resource.getFilename().replace(EXTENSION, ""); } catch (IOException e) { throw new RuntimeException(e); } }

Never mind the smart constructor (which hasn't caused any problems so far!), there's an important rule that's been violated: the Single Level of Abstraction Principle.

Categories: Java

Spring Boot Secured By Let's Encrypt

Tue, 2016-08-16 13:31

In this article, we want to know how we can (1) generate a valid certificate for free; (2) configure a Spring Boot app with it; and lastly (3) how to renew it when it expires.

In my previous blog post, we became familiar with configuration of an Spring Boot Application with a self-signed certificate. Self-signed certificates are good for specific purposes such as test and development. But, if one needs to ship his application to production, certificates should be signed by known and legitimate Certificate Authorities (CA).

Categories: Java

Groovify CUBA Platform — An Overview of Groovy

Tue, 2016-08-16 10:31

Developing an application in CUBA Platform is mostly about productivity. It has other advantages, but productivity is probably one of the most important reasons. To ramp up productivity on all stages of CUBA app development, we can invite Groovy to the party.

In this first of two blog posts i will give you an overview about Groovy, so you get a understanding that this might be valuable for you. Why? Because in my opinion the productivity advantage of CUBA falls apart when turn from generating the UI or creating the domain model to the part of programming where you actually want to implement business logic. In this case you are back at the POJO model either in your controller logic or in the services that are just Spring beans.

Categories: Java

Java Concurrency ReentrantLock

Tue, 2016-08-16 08:31

In this article, we will discuss Java ReentrantLock. ReentrantLock is somewhat similar to synchronize block, but provides much more flexibility. I encourage all my readers to use ReentrantLock as it is extrinsic in nature and developers have much more control over it, unlike synchronize.

Benefits of Using ReentrantLock Over Synchronize.

Categories: Java

The Most Popular Java Tools for Every Phase of Development

Mon, 2016-08-15 15:31

Just because a tool is the most popular doesn’t mean it’s the “best.” What’s best is always context-dependent in programming. However, popularity is an undeniably important metric when choosing between a handful of similar tools. It’s not the popularity itself that adds to a tool’s value; it’s the greater supply of resources and support from the larger community of a given tool.

Popular tools are easier to learn because so many people can help you use them. And popular open-source tools tend to be less buggy because more eyes are on the code and more people are testing it in a greater number of scenarios.

Categories: Java

New Book — Microservices for Java Developers By Christian Posta

Mon, 2016-08-15 13:31

One of our Camel riders, Christian Posta, recently had his first book published.

Microservices for Java Developers

Categories: Java

Reduce Boilerplate Java Using try-with-resources

Mon, 2016-08-15 10:31

Java 8 has been out for a while, and Java 7 has been out even longer. But even so, many people still unfortunately are not taking advantage of some of the new features, many of which make reading and writing Java code much more pleasant. For example, Java 7 introduced some relatively simple things like strings in switch statements, underscores in numeric literals (e.g. 1_000_000 is easier to read and see the magnitude than just 1000000), and the try-with-resources statement. Java 8 went a lot further and introduced lambda expressions, the streams API, a new date/time API based on the Joda Time library, Optional, and more.

In this blog and in a few subsequent posts, I will take a simple snippet of code from a real project, and show what the code looked like originally and what it looked like after refactoring it to be more readable and maintainable. To start, this blog will actually tackle the try-with-resources statement introduced in Java 7. Many people even in 2016 still seem not to be aware of this statement, which not only makes the code less verbose, but also eliminates an entire class of errors resulting from failure to close I/O or other resources.

Categories: Java

Scala vs Kotlin: Operator Overloading

Mon, 2016-08-15 08:31

Last week, I started my comparison of Scala and Kotlin with the Pimp my library pattern. In the second part of this serie, I’d like to address operator overloading.


Before we dive into the nitty-gritty details, let’s try first to tell what it’s all about.

Categories: Java

The Missing Link of Software Engineering

Mon, 2016-08-15 07:31

One of the major challenges of Software Engineering is the numerous information gaps between different activities around it (requirements, design, algorithm, user interface, testing, bug tracking, documentation, etc). Can we easily check if all requirements are covered by a design? Can we identify if all design points are covered by an implementation or by user interface controls, or all requirement items by testing and documentation? Sometimes restrictions are found during coding and testing, so they should be considered as a part of requirements. Or imagine coverage of all options to generate all possible use-cases for test coverage. Wouldn't it be great if we could control all this and use it with Continuous Integration?

Why can't we fill these gaps today? Well, actually we do but only in our minds. What are the obstacles for doing this at least in semi-automatic mode? The most probable cause is our belief that meaning cannot be extracted from ambiguous natural language and partially it is enchantment by natural language itself. Concerning the first point, we do agree this is a difficult task but why can't we facilitate it by having some interim forms between natural language and formal data (and metadata)? The second point could be more tough but try to accept it: natural language forms are not ideal (though convenient) representation of meaning, it is just one of many others.

Categories: Java

If-Then-Else Is a Code Smell

Sun, 2016-08-14 10:31

In most cases (maybe even in all of them), if-then-else can and must be replaced by a decorator or simply another object. I've been planning to write about this for almost a year but only today found a real case in my own code that perfectly illustrates the problem. So it's time to demonstrate it and explain.

Fargo (1996) by the Coen Brothers

Take a look at the class DyTalk from yegor256/rultor and its method modify(). In a nutshell, it prevents you from saving any data to DynamoDB if there were no modifications of the XML document. It's a valid case, and it has to be validated, but the way it's implemented is simply wrong. This is how it works (an oversimplified example):

Categories: Java

Five Ways to Update Fields in a Thread Safe Way: Part 2

Sun, 2016-08-14 08:31

In part one, we looked at the first 3 ways to update a field in a threadsafe way in Java. Here are the 4th and 5th! 

CompareAndSet-Based Atomic UpdateWhen to Use?

Use compareAndSet when the lock in the solution described in part one becomes a bottleneck. Or use it if there exists a ready-made solution, for example, the AtomicInteger as shown below.

Categories: Java

Five Ways to Update Fields in a Thread Safe Way: Part 1

Sat, 2016-08-13 10:31

There are five ways to update a field in Java in a thread safe way. But before we start, what do you have to look at? If you access a field from many threads, you must make sure that:

  1. Changes are made visible to all threads
  2. The value is not changed during the update by the other threads
  3. Threads that are reading do not see the inconsistent intermediate state

You can achieve this by one of the following 5 ways:

Categories: Java

Build a Custom ODBC Driver in C/C++, Java, or C#

Sat, 2016-08-13 07:38

In today’s tech world, your success is directly proportional to your ability to integrate with other technologies and make more informed decisions. Well, everyone seems to know that, so how do you differentiate? You differentiate yourself by choosing the smartest way to integrate technologies—using standards based drivers. 

Can't find a driver for your application? You might be one of the following:

Categories: Java

Using JUnit 5 in IntelliJ IDEA

Fri, 2016-08-12 15:31

One of the new features in IntelliJ IDEA 2016.2 is support for the new JUnit 5 testing framework. Almost all Java developers will have used JUnit at some point, so it’s pretty exciting to find the newest version has evolved with the times and provides a number of new features, some of which may be familiar to those who have used other frameworks.

IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there’s no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency. Here we’re using Gradle to include the dependency to the M2 version:

Categories: Java

Useful Scala Compiler Options for Better Scala Development

Fri, 2016-08-12 13:31

At Threat Stack, we like to leverage our tools to the fullest. Since we use Scala, it only makes sense for us to always be looking into ways of getting the most out of the Scala Compiler to enhance our productivity. And, as it turns out, the Scala Compiler offers a number of features that makes our lives way better! 

A couple of resources make great recommendations about what options you should pass to the Scala Compiler. (Rob Norris' Blog comes to mind.) However, most of the resources don't really explain why you want to enable these options. In this series, I'll cover a number of Scala Compiler options that ease development. I'll discuss ones that we use at Threat Stack as well as other common options that we have opted not to use. Where applicable, I’ll show examples of issues in code that will be caught by the compiler once the correct options have been enabled.

Categories: Java

Does My Method Look Big in This?

Fri, 2016-08-12 10:31

How big is the following Java method?

public Collection getDescription() { SystemLibrary systemLib = registry.get(SystemLibrary.class); Analysis analysis = systemLib.getCurrentAnalysis(registry); return getDescription(analysis); }

This humble method locates some sort of system library, retrieves an Analysis, and returns a description of that Analysis.

Categories: Java

Remembering to Reset Thread Context Class Loader

Fri, 2016-08-12 08:31

I'm having a difficult time thinking of anything I like less about working with Java than working with class loaders. This is particularly true when working with application servers or OSGi where the use of multiple class loaders is prevalent and the ability to use class loaders transparently is reduced. I agree with the OSGI Alliance Blog post What You Should Know about Class Loaders that "in a modular environment, class loader code wreaks havoc."

Neil Bartlett has written the blog post The Dreaded Thread Context Class Loader in which he describes why the thread context class loader was introduced and why its use is not "OSGi-friendly." Bartlett states that there are rare cases in which "a library only consults the TCCL," but that in those rare cases "we are somewhat stuck" and "will have to explicitly set the TCCL from our own code before calling into the library."

Categories: Java

The Single Level of Abstraction Principle

Thu, 2016-08-11 15:36

It’s virtuous to separate out different perspectives in code because it helps make code more testable, extendable, and understandable. We want to use entities at the same level of perspective so that code is easier to read and understand code.

The same thing is true within entities where we want to do tasks at the same level of abstraction.

Categories: Java

Creating an Editor with Syntax Highlighting Using ANTLR and Kotlin

Thu, 2016-08-11 15:31

In this post we are going to see how to build a standalone editor with syntax highlighting for our language. The syntax highlighting feature will be based on the ANTLR lexer we have built in the first post. The code will be in Kotlin, however it should be easily convertible to Java. The editor will be named Kanvas.

Previous Posts

This post is part of a series on how to create a useful language and all the supporting tools.

Categories: Java

This Week in Spring: SpringOne Platform Wrapup

Thu, 2016-08-11 13:31

Welcome to another installment of This Week in Spring! This week I’m recovering from a crazy awesome week at SpringOne Platform while visiting customers here in summer-time London.

We’ve got a lot to cover so let’s get to it!

Categories: Java