Access Java Collections/Dictionaries from .NET Using J-Integra® for .NET

Java/J2EE .NET Interoperability Products Page

This example demonstrates how to access Java Collections/Dictionaries from .NET client. J-Integra® for .NET is a Java interoperability component that bridges Java/J2EE and Microsoft .NET. It provides bi-directional access of Java objects and .NET components.

Synopsis

Use this example as a guideline to show you how to access Java collections/dictionaries (from the java.util package) using J-Integra® for .NET.

There is no actual code to deploy in this example, as it is basically the same as the Access Java from .NET Using J-Integra® for .NET example, but with the use of Java collections/dictionaries. This example will only highlight the differences that you need to be aware of when dealing with such objects. As it is not a step-by-step tutorial, take a look at the Access Java from .NET Using J-Integra® for .NET example if you are not familiar with J-Integra® for .NET.

Note: $USER_INSTALL_DIR$ is the directory where J-Integra® for .NET has been installed. If this location contains whitespace characters (e.g. C:\Program Files\...), you may need to include it within "" when referencing it at the command-line.

Prerequisites

  1. A platform with Microsoft Visual Studio .NET 2003 (7.1.3088 or higher) installed, or at least the .NET Framework 1.0.3705 or higher. This will be known throughout the example as the .NET platform.

  2. A platform with J-Integra® for .NET 1.5.1286 (or higher) installed. This will be known throughout the example as the Java platform.

  3. A version of JDK (1.3.1 or higher) installed on the Java platform, and the bin directory included in the path. This is required to compile and run the Java server.

  4. Verify that GenService is installed correctly on the .NET platform.

Java Collections/Dictionaries and J-Integra® for .NET

By default, when generating C# proxies for your Java objects, GenService will automatically map certain Java collections/dictionaries it encounters to a corresponding .NET collection interface - either System.Collections.IList or System.Collections.IDictionary. See Type Mapping Between Java Types and .NET Types for a full description of the collections/dictionary mappings.

However, the default behaviour will cause a System.InvalidCastException: Return argument has an invalid type error if it encounters parameter types that return an interface of the actual Java collections/dictionary rather than the actual collection/dictionary type. To see the reason why, let's examine a simple example. If a Java method had the following signature:

// in the body of the method, it actually returns a java.util.ArrayList
java.util.Collection getStudents(int number)

the C# generated method will look like this:

java.util.Collection getStudents(int number)

If the default behaviour is used, during runtime J-Integra® for .NET will convert the java.util.ArrayList returned from getStudents to a System.Collections.IList object (as described in the type mapping above). At the receiving end, the .NET Framework will try to cast this type into a java.util.Collection object (as described by the generated proxy method signature) and a this is where the exception will occur. To avoid this, the default behaviour must be disabled. This behaviour must be disabled in the runtime and during proxy generation.

Disable Collections/Dictionaries Mapping in Janetor

Perform this step on the Java platform.

You will disable J-Integra® for .NET's default behaviour to automatically convert Java collections/dictionaries it encounters at runtime to .NET collection interfaces. This is done through the Janetor tool.

In Janetor, and uncheck the Map Collections/Dictionaries checkbox under Runtime.

Setup Runtime and Exception in Janetor configuration tool

Before you exit Janetor, save the configuration by going to File - Save. If you are deploying J-Integra® for .NET as a WAR file (see any of our EJB examples for a tutorial), you will need to redeploy as the configuration file has been changed.

Disable Collections/Dictionaries Mapping in GenNet

Perform this step on the Java platform.

In this step, you will disable GenService's default behaviour to automatically generate a .NET collection interface for every Java collections/dictionary type it encounters. This is done through the GenNet tool.

On the third screen in GenNet, uncheck the Map Collections/Dictionaries checkbox.

Select Java classes to be exported to .NET in GenNet

The bottom line is that if you have any Java method signature that returns a Java collection/map interface, but the actual returned object might be any of the Java collections/dictionaries that J-Integra® for .NET has a .NET mapping for, then you will need to disable J-Integra® for .NET's default behaviour for collections/dictionaries.

© 2007 Intrinsyc Software International, Inc. All rights reserved. Legal