• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating any kind of objects using reflection  RSS feed

 
Da Schack
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I try to write a java programm which can create a instance of a various objects. But I don't know these objects, so the need to be created dynamicly. This works fine with reflection but I've to problems:

I've wrote an utility class which contains a Map<Class, Object>. This map contains all primitiv types e.g. 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 oder MAX_VALUE is not a good idea. Does anyone know better values for the `types or is there a special framework?

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

Thanks in advance,
Da
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What an appropriate default value is for primitive values depends entirely on what your goal with this is.

Ofcourse you cannot create an instance of an interface, but you could create a dynamic proxy.

What do you need this functionality for? It reminds me of EasyMock, a framework for creating mock objects automatically which you can use to create unit tests, where you replace services which the class you are testing uses by mock objects that check if the class you are testing makes the expected calls to those services.
 
Da Schack
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasper,

thanks for the quick response! The objective is to write a tool which checks if your class is immutable. 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). Therefore I must be able to create an instance of this class and check the method return values. Do you think that is possible with EasyMock?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what EasyMock was made for, so I don't think you can do that with EasyMock.
 
Da Schack
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any idea which values are the best to use for the primitive types?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without knowing anything about the classes or interfaces for which you are creating instances of dynamically in this way, nothing can be said about what appropriate values are to pass to the constructor.

There might not even be a single value that can be used in all cases. Suppose that I make a class which takes a month number between 1 and 12 as an int. And I have another class which expects a number between 1000 and 2000. Then there is no single default value that you could use for both those classes, because the ranges 1 to 12 and 1000 to 2000 don't have any common numbers.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Da Schack wrote:

Please check your private messages for an important administrative matter.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!