Incompatible Java 8 change in the Collections classes

Suppose that we want to implement a HashMap (let’s call it OrderedHashMap where the values can also retrieved by their index, where the index refers to the order in which the key/value pairs have been inserted. The following unit test shows how such a class could be used:

@Test
public void testPutGet() {
    OrderedHashMap<string, string=""> ohm = new OrderedHashMap<>();
    ohm.put("Z", "LetterZ");    // 0
    ohm.put("A", "LetterA");    // 1
    ohm.put("D", "LetterD");    // 2
    ohm.put("K", "LetterK");    // 3

    assertEquals(ohm.get(0), "LetterZ");
    assertEquals(ohm.get(3), "LetterK");
}
</string,>

The straight forward implementation of the class OrderedHashMapis to inherit from one of the existing Collection classes and simply implement the additional functionality, e.g. like

package com.example;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class OrderedHashMap<k, v=""> extends LinkedHashMap<k, v=""> {
    private static final long serialVersionUID = 6354582314971513369L;

    private List<v> items = new ArrayList<>();

    public V get(int index) {
        return items.get(index);
    }

    @Override
    public V put(K key, V value) {
        items.add(value);
        return super.put(key, value);
    }
}
</v></k,></k,>

With this code, the above unit test succeeds. Lets add an additional unit tests for one of the other methods inherited from LinkedHashMap, putAll():

@Test
public void testPutAll() {
    OrderedHashMap<string, string=""> ohm = new OrderedHashMap<>();
    ohm.put("Z", "LetterZ");    // 0
    ohm.put("A", "LetterA");    // 1
    ohm.put("D", "LetterD");    // 2
    ohm.put("K", "LetterK");    // 3

    OrderedHashMap<string, string=""> ohm2 = new OrderedHashMap<>();
    ohm2.putAll(ohm);

    assertEquals(ohm2.get(0), "LetterZ");
    assertEquals(ohm2.get(3), "LetterK");
}
</string,></string,>

This unit test also succeeds and shows that we can copy the elements from one OrderedHashMap to a second one, using putAll(). Unless we switch to Java 8. If we run the above sample with the Java 8 runtime, the test fails with an exception:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(Unknown Source)
	at java.util.ArrayList.get(Unknown Source)
	at com.example.OrderedHashMap.get(OrderedHashMap.java:13)
	at com.example.test.OrderedHashMapTest.testPutAll(OrderedHashMapTest.java:37)
	...

This is an issue I came across when I was trying to translate a grammar with ANTLR (not using the latest version) – see also https://github.com/antlr/antlr4/issues/337. The background is that the LinkedHashMap class (or one of its super classes) changed the implementation of putAll() so that it no longer calls put() to insert the new elements in the destination map. Hence, our own implementation is no longer able to catch this call to update the List at the same time a new key/value pair is added to the map. If you do similar things, you should check if there are such differences between the Java 7 and the Java 8 runtime which might affect your code. In general, this is a good example to Favor Composition over Inheritance in Java and Object Oriented Programming. The main problem is that the sub class depends on the behaviour of the superclass, and hence becomes fragile (and breaks when the superclass behaviour changes in an incompatible way). Collections are always a bad candidate for subclassing – instead, create a new class which implements the desired interfaces and then delegates to the necessary Collection classes. This is more effort at the beginning (to implement all the necessary interface methods), but makes the class much more resistent against modifications in the runtime classes. See also Effective Java, Second Edition: Item 16: Favor composition over inheritance.

Implicit default constructors

As you certainly know, if there is no constructor implemented in a class, the compiler implicitly synthesizes a default constructor. Hence, the following code compiles:

public class ClassA {

    public static void main(String[] args) {
        new ClassA();
    }
}

The disassembled code shows that there is indeed a constructor which essentially calls its super constructor, java.lang.Object.<init>:

$ javap -c ClassA.class
Compiled from "ClassA.java"
public class ClassA {
public ClassA();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #1                  // class ClassA
       3: invokespecial #16                 // Method "<init>":()V
       6: return
}

Now, there is one detail which even long-time Java developers are not always aware of: if any other constructor overload is added to the class, the compiler will not synthesize a default constructor – hence, the following code does not compile:

public class ClassA {

    public ClassA(int a) { }

    public static void main(String[] args) {
        new ClassA();
    }
}

ClassA.java:7: error: constructor ClassA in class ClassA cannot be applied to given types;
        new ClassA();
        ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

This is in accordance to the Java Language Specification: 8.8.9. Default Constructor where it says:

If a class contains no constructor declarations, then a default constructor is implicitly declared.
...

Means, if there are any constructor declarations, then no default constructor will be created by the compiler. Note that there is also the same behavior in C++. Note also that the term “default constructor” as it is used by the language specification only refers to the parameterless constructor created by the compiler – hence, strictly spoken, a parameterless constructor which is explicitly implemented in the class is not a default constructor (even though developers often call it like that). Normally, this is not an issue, since the compiler will emit the above error when we try to instantiate the class through the default constructor. However, if the object is created dynamically (e.g. through reflection), there will be no compile time error – it will become a runtime error then:

public class ClassA {

    public ClassA(int a) { }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class clazz = Class.forName("ClassA");
        ClassA obj = (ClassA) clazz.newInstance();
    }
}

Exception in thread "main" java.lang.InstantiationException: ClassA
	at java.lang.Class.newInstance(Unknown Source)
	at ClassA.main(ClassA.java:9)
Caused by: java.lang.NoSuchMethodException: ClassA.<init>()
	at java.lang.Class.getConstructor0(Unknown Source)
	... 2 more
</init>

This can be important when using persistence frameworks which do these kind of things in the background. Other examples are dependency injection frameworks which might complain at the lack of default or empty constructor, if no constructor has the @Inject annotation.

Tracking Application Module Pool operations

To get a better understanding how the Application Module Pool works in Oracle ADF, it can be useful to log some of its methods as they are called. This can also be useful to track down Application Module Pool related issues, or even to collect usage and performance statistics. The entry point for such a logging facility is to define a custom Application Module Pool class. Note that this is not something you would or should do in the final application – it is usually not necessary, but can be convenient for the use cases mentioned above. The class to instantiate as the Application Module Pool is defined through the PoolClassName property on the Application Module’s configuration:

The configuration is stored in the bc4j.xml configuration file, and we could also set the property by simply editing this file:

  <appmoduleconfig deployplatform="LOCAL" jdbcname="ApplicationDB" jbo.project="model.Model" name="AppModuleLocal" applicationname="model.AppModule">
     <am-pooling poolclassname="test.CustomApplicationModulePool">
     <database jbo.locking.mode="optimistic">
     <security appmodulejndiname="model.AppModule">
  </security></database></am-pooling></appmoduleconfig>

Now, the ADF runtime will instantiate an object of the defined class as the Application Module’s pool. We simply need to implement this class, inheriting from ApplicationPoolImpl:

package test;

import java.util.Properties;
import oracle.jbo.ApplicationModule;
import oracle.jbo.common.ampool.ApplicationPoolImpl;
import oracle.jbo.common.ampool.SessionCookie;

public class CustomApplicationModulePool extends ApplicationPoolImpl {
    public CustomApplicationModulePool() {
        super();
    }

    ...

}

By overriding the methods which are of interest, we can add logging to the application module pool and for example track how Application Modules are checked out and released. Ideally a logging API such as java.util.logging should be used – for simplicity, I am using System.err in the examples. The most interesting methods to track Application Module usage are useApplicationModule() which is called by the framework to check out an application module from the pool, releaseApplicationModule() which is called to give back an application module to the pool and probably createSessionCookie() which creates a session cookie for a BC4J session:

    @Override
    public SessionCookie createSessionCookie(String applicationId,
                                             String sessionId,
                                             Properties properties) {
        System.err.printf("createSessionCookie(applicationId=%s, sessionId=%s, properties=%s)\n",
                           applicationId, sessionId, properties);

        SessionCookie result = super.createSessionCookie(applicationId, sessionId, properties);

        System.err.printf("    Result: %s\n", result.getSessionId());

        return result;
    }


    @Override
    public ApplicationModule useApplicationModule(final SessionCookie cookie,
                                                  final boolean checkout) {
        System.err.printf("useApplicationModule(session id=%s, checkout=%s)\n", 
                          cookie.getSessionId(), checkout);

        ApplicationModule result = super.useApplicationModule(cookie, checkout);

        System.err.printf("    Result: %s\n", System.identityHashCode(result));

        return result;
    }

    @Override
    public void releaseApplicationModule(SessionCookie cookie,
                                         boolean manageState) {
        System.err.printf("releaseApplicationModule(cookie=%s, manageState=%s)\n",
                          cookie, manageState);

        super.releaseApplicationModule(cookie, manageState);
    }

    @Override
    public void releaseApplicationModule(SessionCookie cookie,
                                         int releaseFlags) {
        System.err.printf("releaseApplicationModule(cookie=%s, releaseFlags=%s)\n",
                          cookie, releaseFlags);

        super.releaseApplicationModule(cookie, releaseFlags);
    }

We can now observe how and when the ADF runtime retrieves Application Modules form the pool. Remember that each HTTP request which requires BC4J will result in an Application Module checkout and a corresponding release before the request finishes. Also remember that each Application Module has its own database transaction (unless it is a nested Application Module, in which case it inherits the transaction from its parent). Hence, logging information about the associated database transaction can also be useful.

Enabling global breakpoints in JDeveloper

In larger ADF projects with more than one workspace and with many projects, it might happen that a breakpoint can not be set on a class from a different workspace or project, even though JDeveloper properly navigates to the source file. In this case, make sure that the “Scope for New Breakpoints” is set to “Global” in the “Debugger/Breakpoints” preferences (open the preferences dialog through “Tools/Preferences”):