Configure SLF4J/Logback for a standalone application

Most examples for SLF4J/Logback you can find on the internet are about how web applications are configured. But if you have a command line or JavaFX application, you’ll require a different setup. Most probably you want a local ‘logback.xml’ that is located in your application’s folder or in the home directory of the user. But how to configure such a SLF4J/Logback for a Java SE application?

The good news is, that there is already a tiny library that does the trick: ext4logback. It allows you simply to specify the location of your ‘logback.xml’ and does all the initialization for you. It also creates a default logback XML configuration if no file exists at startup.

Simply add the dependency to your Maven POM:


Then you can just initialize the log in your applications ‘main’ method:

 * Starts the application.
 * @param args
 *            Only optional argument is the 'logback.xml' file path and
 *            name. If no argument is provided it's assumed that the file name 
 *            is 'logback.xml' and it's in the current directory.
public static void main(final String[] args) {
    try {
        // Initializes Logback by reading the XML config file.
        // If the file does not exist, it will be created with some defaults.
        // This is a convenience method that directly uses the main method's arguments.
        new LogbackStandalone().init(args, new NewLogConfigFileParams("", "myapp"));"Application running...");
        // Your code goes here...
    } catch (RuntimeException ex) {

Parsing a Maven POM III

Due to popular demand I moved the latest code for parsing a Maven POM (including it’s parent POMs) into a tiny Github Project:

Here is a short example how to use it:

// Reading the model loads everything from remote
// repository using JBoss Shrinkwrap Resolver!
Model model = MavenPomReader.readModel("org.fuin:objects4j:0.6.1");
// Should print 'Objects4J'

A full example can be found here:

Eclipse Maven Tycho Xtext Archetype




mvn archetype:generate \
  -DarchetypeGroupId=org.fuin.archetypes \
  -DarchetypeArtifactId=emt-xtext-archetype \

It will generate the following project structure:

|-- com.mycompany.mydsl.dsl
|-- com.mycompany.mydsl.repository
|-- com.mycompany.mydsl.sdk
|-- com.mycompany.mydsl.tests
|-- com.mycompany.mydsl.ui
`-- mydsl-test
.mydsl Maven parent project. [Maven project]
.mydsl-test Example DSL project. [Eclipse project]



Finding methods calls programmatically

In the current project a defect was raised with the following exception attached:

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

After I dug into the code, I found the cause was simple. There was no rounding mode given for calculations that might produce big decimals, like in this example:


Every senior developer knows about this trap, but it’s still easy to forget. A default of BigDecimal.ROUND_HALF_UP, applied when no rounding mode is defined explicitly, would have made the method more safe to use.

How is it possible to prevent such failures in the future and avoid dividing a single big decimal parameter ad infinitum? One could use Checkstyle rules, use FindBugs rules or create a simple unit test.

I chose the unit test, so the question became “How can I find all the methods that call a given method in Java?” This is not possible with the standard Reflection API, but I had already used OW2’s ASM for such tasks, so it wasn’t too hard to create a JUnit assertion for it.

Just include this in your unit tests:

// Path to your project's '*.class' files
final File classesDir = new File("target/classes");

// Can be used to exclude some files/packages
final FileFilter fileFilter = new FileFilter() {
    public boolean accept(File file) {
	return !file.getPath().contains("my/pkg/to/exclude");

// Define methods to find
final MCAMethod divide = new MCAMethod("java.math.BigDecimal",
	"java.math.BigDecimal divide(java.math.BigDecimal)");

final MCAMethod setScale = new MCAMethod("java.math.BigDecimal",
	"java.math.BigDecimal setScale(int)");

// Fails if any class calls one of the two methods
AssertUsage.assertMethodsNotUsed(classesDir, fileFilter, 
                                 divide, setScale);

The full source code can be found at GitHub.

EclipseLink: How to get the SQL translated with the arguments for a Query?

The documentation at the EclipseLink Wiki is not very helpful:

It says: “To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.“.

// BEGIN Snippet from the Wiki

Session session = em.unwrap(JpaEntityManager).getActiveSession();

DatabaseQuery databaseQuery = 

String sqlString = databaseQuery

// END Snippet from the Wiki

But where to get the recordWithValues variable? Actually the solution is hard to find, but simple: databaseQuery.getTranslationRow()

// This does the trick...

String sqlString = databaseQuery

// ... to replace the '?' 

Mixins with pure Java







Why not just inheritance?







Mixins with AOP




// === Listing 1 ===

/** Base-Interface */
public interface Named {
    public String getName();

/** Mixin-Interface */
public interface NamedMixin extends Named {

/** Mixin-Provider */
public aspect NamedAspect {
    private String;
    public final void NamedMixin.setName(String name) { = name;
    public final String NamedMixin.getName() {
        return name;

/** Mixin-User */
public class MyClass implements NamedMixin {
   // Could have more methods or use different mixins


MyClass myObj = new MyClass();





Appearance: Java Model Parser and Printer (JaMoPP)



Mixins with JaMoPP


  • @MixinIntf Indicates a mixin interface.
  • provider.


// === LISTING 2 ===

public interface Named<T> {
    public T getName();

/** Mixin-Interface */
public interface NamedMixin<T> extends Named<T> {

/** Mixin-Provider */

    private T name;

    public void setName(T name) { = name;

    public T getName() {
        return name;

public final class MyName {
    private final String name;

    public MyName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("name == null");
            throw new IllegalArgumentException("name is empty");
        } = name;

    public String toString() {
        return name;




// === LISTING 3 ===

/** Mixin-User */
public class MyClass implements NamedMixin<MyName> {
    // Could have more methods or use different mixins





// === LISTING 4 ===

/** Mixin-User */
public class MyClass implements NamedMixin<MyName> {

    private MyName name;

    public void setName(MyName name) { = name;

    public MyName getName() {
        return name;





Screenshot Eclipse




