• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Call Object (Generic method calls? ) passing a method call?  RSS feed

 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so maybe this is not possible but it would be really handy. is there anyway to pass a method call then use it. like store a method call in an object or in a variable and then use it?

Like

Methodcall m = new Methodcall("toString");
Methodcall q = new Methodcall("getClass");
Methodcall s = new Methodcall("subString",2,8);// maybe it puts 2 and 8 in the params for the subString call

and then you could do
String foo="foodamins";
foo.m;
which would give me the toString of foo?


any ideas or am i thinking of something for the ideal world? because i would like to pass method callsto a generic class based on the type. (basically im testing different kinds of objects and i want to make a general tester based on the type of object however im limited to only 50% of my dtos because only those share the same getters and setters, any ideas?)

ty all
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Reflection.
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an interface! Ex:







note : Above may be slightly inaccurate but you should get the picture on how interfaces can be used in place of what you're trying to do (ie: function pointers).
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String foo="foodamins";
foo.m;


Your example isn't clear.
foo is a String. String does not have a field m.
is m a field or a method? As coded it appears to be a field.
And String is a poor choice because it is final (can't be extended)
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you will think I am fussy, but please avoid abbreviations like "ty" and "dtos". I presume "ty" means "Thank you," but a lot of people who didn't grow up speaking English might not understand it. And what does "dtos" mean?
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data Transfer Objects makes the most sense in context.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amaru Shakur:
...any ideas or am i thinking of something for the ideal world? because i would like to pass method callsto a generic class based on the type. (basically im testing different kinds of objects and i want to make a general tester based on the type of object however im limited to only 50% of my dtos because only those share the same getters and setters, any ideas?)

ty all


If by dtos you mean DTOs == Data Transfer Objects, then those objects should generally be JavaBeans and you should be able to use the java.beans.Itrospector (see API) to get at the values inside.

Or, as was mentioned earlier, Reflection would be another bet, as would be an interface that exposes the contents. By 'testing different kinds of objects' what do you mean? What do all these objects have in common that makes them all capable (or meaningfully) tested with this single generic class?
 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the poor wording. im looking into reflection but let me explain the background and why i dont want to / wouldnt use an interface.
i have about 360 data transfer objects that i am writing tests for. a good portion of these tests have to do with routine sql queries or operations (as simple as a insert, and can get more complicated).

i was able to make generic classes for the dtos who have overlapping fields (the variables are the same and the getter and setter methods are the same as per hibernates reverse engineering. (also because the sql file they are based off of uses the same name for the column attributes)). therefore i was able to use a generic test class for these by passing in the data transfer object type, and hard coding the method calls because they shared the same methods. For about 180 of the dtos they dont share the same methods, however the calls are relatively similiar operation wise, as in i would set the primary key (whether it is ID, name, rowID, SID, PK, ect, i set it at the same time and get it at the same time, the only difference is the setter and getter methods).

so if you will i was looking to make my method calls generic. for example just pass in method call 1 2 and 3. and throughout the code say dto.1();
would perform method call 1 (lets say it returns a primary key).
and dto.2 (lets say this would display the first coloumns attribute value that wasnt the key). see it would all be the same or similiar however, technically its different because the 2nd column could be name, routertype, liquerchoice, shoesize, ect... but in reality im just looking at the second column.


better wording of my example or desires.
Methodcall m = new Methodcall("toString"); // line 1
Methodcall q = new Methodcall("getClass");
Methodcall s = new Methodcall("subString",2,8);// maybe it puts 2 and 8 in the params for the subString call

and then you could do
Class foo="foodamins"; // this is probally illegal syntax but lets say i get // a class type and instantiate an object from it which is called dto
foo dto= new foo();
foo.m; // now i am doing a m method call but in reality the method called //by m , is whatever method call i stored in the Methodcall m in line 1


thankyou all, i really wish i know about this sites forum in 2001 (i was an major rookie in high school and we used the java ranch roundup from time to time, but im finding alot of neat stuff here especially in the SCJP section.)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection is still the answer. In particular, look at the Method class. I think it will do everything you need.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amaru Shakur:
sorry about the poor wording. im looking into reflection but let me explain the background and why i dont want to / wouldnt use an interface.
i have about 360 data transfer objects that i am writing tests for. a good portion of these tests have to do with routine sql queries or operations (as simple as a insert, and can get more complicated).


This sounds more like Data Access Objects, if there is any SQL involved. A Data Transfer Object should be a value holder only.

Originally posted by Amaru Shakur:
i was able to make generic classes for the dtos who have overlapping fields ... For about 180 of the dtos they dont share the same methods, however the calls are relatively similiar operation wise, as in i would set the primary key ... i was looking to make my method calls generic. for example just pass in method call 1 2 and 3. and throughout the code say .. see it would all be the same or similiar however, technically its different because the 2nd column could be name, routertype, liquerchoice, shoesize, ect... but in reality im just looking at the second column.


Reflection is a good call here, but a really generic interface may help you make the call with simpler-looking code. As an example:



Then you would implement it on the DTOs with a big call method that would translate the key-name for the property and the argument list into normal method calls:


Finally you could 'call' any of the properties you expose via the Callable interface:


This smells a little to me because of the long if/else blocks that the Callable implementation would require... but it is an option to consider which you may be able to clean up.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!