473,441 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Reflection

13
Hello.

I would like to write function which will invoke any method of any object in artificial way.

Code, I have already written:

Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.Method;
  2. import java.lang.Class;
  3. import java.lang.reflect.Type;
  4. import static java.lang.System.out;
  5.  
  6. class Calc
  7. {
  8.     public long add(long a, long b) { return a + b; }
  9. }
  10.  
  11. public class Main {
  12.  
  13.     public void manipulate (Object className, String methodName, String methodArg1, String methodArg2)
  14.     {
  15.         Class<?> c= className.getClass();
  16.         Method mlist[] = c.getDeclaredMethods();
  17.         for (Method m : mlist)
  18.         {
  19.             if (m.getName().equals(methodName))
  20.             {
  21.                 Type[] paramTypes = m.getParameterTypes();
  22.                 Type returnType = m.getReturnType();
  23.                 m.setAccessible(true);
  24.                 Object ReturnValue = null;
  25.  
  26.                 //out.format(returnType.toString(), ReturnValue);
  27.                 //out = m.invoke(className, ...);
  28.             }
  29.         }
  30.     }
  31.     public static void main(String[] args) {
  32.         Main main = new Main();
  33.         Calc c = new Calc();
  34.         main.manipulate(c, "add", "2", "4");
  35.  
  36.         System.out.println("end");
  37.         //System.out.println(c.add(-1,-2));
  38.     }
  39. }
  40.  
  41.  
Problem is with casting.
I have "2" and "4" (for example) as arguments. In paramTypes i have types, but i cannot initiate variables of those type.

I found in one solution Casting.ToType but it does not work.

Finally, I want to invoke method on object passed as parameter in method "manipulate".

I will be grateful for any idea. Thank You!
Paul
Jul 3 '10 #1
1 1580
Oralloy
988 Expert 512MB
pb2000,

First off, you should probably experiment with the java.lang.reflect.Method class.

I did my reflection work under Eclipse, so I was able to inject a break-point into the method loop and trivially inspect the objects. This was tremendously useful in understanding how things actually work and the overall structure. From the command line this is a bit more problematic, and will be left as an exercise for (you) the student. *smile*

Once you understand the structure, the invocation in your line 27 will look like (using implicit variadic-method object hoisting)
Expand|Select|Wrap|Line Numbers
  1. m.invoke(className, 2, 4);
or (if you invoke explicitly)
Expand|Select|Wrap|Line Numbers
  1. m.invoke(className, new Long(2), new Long(4));
In any case, the return is an object of type java.lang.Object, which may (or may not) require you to do an explicit cast to the type you want.

Why you're trying to replace the java.lang.System.out object in that same line is beyond me. It's declared as "final", so you shouldn't be able to modify it (except, perhaps, through reflection).

Also, "className" is a misnomer. That is an object of the class you're applying reflection to, and should likely be named such. Your example "Calc" class requires an object, so an appropriate object is required.

The documentation for "invoke(...)" is here: http://java.sun.com/javase/7/docs/ap...ang.Object...)

Good luck!
Jul 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.