Skip to content
Chabanois Cédric edited this page Jul 1, 2013 · 9 revisions

Welcome to the transmorph wiki!

Information

Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).

Transmorph strong points are :

  • support conversion for primitives and objects
  • support conversion to multidimensional arrays
  • support conversion to parameterized collections and types
  • support conversion for beans that contain bi-directional relationships
  • you can choose exactly which converters you want to use
  • no dependencies
  • 40 converters included
  • easy to add more converters
  • objects can be modified after conversion using modifiers
  • can convert to a type given either its java type (Class) or signature
  • easy to use

Transmorph can also be used to inject properties from one object (Map or Bean) to a bean.

News

18 December 2012

Transmorph moved to github. The database used for mediawiki on sourceforge disappeared for no reason ...

6 February 2011

Transmorph 3.1.1 release

Getting transmorph

Using maven :

<dependency>
	<groupId>net.sf.transmorph</groupId>
	<artifactId>transmorph</artifactId>
	<version>3.1.3</version>
</dependency>

Download latest jar : transmorph-3.1.3.jar

Using transmorph

First, you need to choose which converters you want to use. You can use

new DefaultConverters()

which contains the main converters but you can also choose yourself the converters you need if you wish.

You can then create a Transmorph object :

Transmorph transmorph = new Transmorph(new DefaultConverters());

Create the object you want to convert :

Map map = new HashMap();
map.put("key1", new String[] { "value1-1", "value1-2" });
map.put("key2", new String[] { "value2-1", "value2-2" });
map.put("key3", null);
map.put("key4", new String[] { null, null });
map.put(null, new String[] { "value5-1", "value5-2" });

One of the limitations of generics is type erasure. Based on Neal Gafter's Type Tokens, is it possible to overcome this limitation:

TypeReference<Map<String,List<String>>> typeReference = new TypeReference<Map<String,List<String>>>() {};
Map<String, List<String>> converted = transmorph.convert(map,typeReference);

You can also use : (Here we convert an int[] to a List<Integer>

List<Integer> listOfInts = transmorph.convert(
	new int[] { 0, 1, 2, 3, 4, 5 }, List.class, new Class[] { Integer.class });

If you only know the type signature, use a parser (3 parsers available depending on the format of the signature)

JavaSyntaxTypeSignatureParser typeSignatureParser = new JavaSyntaxTypeSignatureParser("java.util.Map<java.lang.String, java.util.List<java.lang.String>>");
FullTypeSignature typeSignature = typeSignatureParser.parseTypeSignature();
TypeFactory typeFactory = new TypeFactory(getClass().getClassLoader());
Type type = typeFactory.getType(typeSignature);
Map<String, List<String>> converted = (Map<String, List<String>>)transmorph.convert(map,type);

User documentation