0

I am trying to write a java program which can create an instance of various objects. But I don't know these objects, so they need to be created dynamically. This works fine with reflection but I've to problems:

I wrote an utility class which contains a Map<Class, Object>. This map contains all primitive types e.g.

dummyValueMap.put(int.class, Integer.MIN_VALUE);

So I can query a dummy object for int to use this value in a constructor of the object which I don't know at compile time, to create a instance. Now my problem is that I need dummy values for the primitive types which can be used by all constructors, so Integer.MIN_VALUE or MAX_VALUE is not a good idea. Does anyone know of better values for the types or is there a special framework?

What can I do if the constructor parameter is an Interface? How do I get an implementation of this interface dynamically?

unholysampler
  • 17,141
  • 7
  • 47
  • 64

2 Answers2

1

As far as primitives go, you can instantiate the corresponding object and pass it to the constructor and through the magic of autoboxing/unboxing it will work (like pass an instance of Integer to a constructor that accepts an int argument).

For instanciating Interface implementations via reflections, that's not possible per se, but you can use a framework like Mockito :

MyInterface myInterfaceImplementation = Mockito.mock(MyInterface.class);
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • Or Google Guice, which is an injection framework (inject an implementation into an object being constructed using an interface as the guide). – Chris Dennett Oct 18 '11 at 19:27
  • Guice is a dependency injection framework primarely (a smaller version of Spring if you will). It is not taylored to creating unknown instances from interfaces. Mockito is better in that aspect, as it lets you minutely detail what you on-the-fly interface instance can do (what will the methods return, etc) – Shivan Dragon Oct 18 '11 at 19:33
  • Thanks for the Mockito hint! I will check this out! But which values are the most suitable for the primitve types? Because I need these objects of primitve types to construct all other and more complex types?! or am I wrong with this idea? – daschack Oct 18 '11 at 20:21
0

Those are things you have to handle yourself: primitive types, interface types, abstract types... For any given constructor argument, you have to be able to create and provide a value that's assignable to the argument's type. I don't know of any libraries to help with that, but I'd like to since I find myself doing this kind of thing on occasion. The non-concrete types are the hard ones since Java has no (easy) way of finding the subtypes of a type. To a certain extent, you can create some kind of "abstract type to concrete type" Map, where you'd register things like List => ArrayList and Set => HashSet. Then your code can query that for what type to create. Other options for handling interfaces would be dynamically creating an implementing object using JDK dynamic proxies or a mock framework like Mockito, and for handling abstract classes, Javassist, cglib, or, again, Mockito.

Community
  • 1
  • 1
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I also thought about that Collection framework but what is if in the class I'd like to test specifies an List or of an List does Mockito can cope with that? The intention is to write a tool which checks if a class is immutable or not. Immutable does not only mean that all variables are final it should also guaranty that the this reference of this class can not escape (directly or indirectly). – daschack Oct 18 '11 at 20:39
  • Parameterized types are unavailable at runtime in Java--another thing you have to cope with yourself if you need a collection to contain a certain type of object. I don't think you care about the `this` reference, if you actually meant that. You should care about internal mutable state escaping. I think you'll have a tough time proving that an object is immutable by inspection. – Ryan Stewart Oct 18 '11 at 23:30
  • Do you have a better solution? :) – daschack Oct 18 '11 at 23:37
  • Nope. One other option for constructing objects: skip the constructor. [Objenesis](http://objenesis.googlecode.com/svn/docs/index.html) specializes in that. – Ryan Stewart Oct 19 '11 at 05:00