How to Use Reflection to Inspect the Code in a Method

Can I use reflection to inspect the code in a method?

Basic Answer:

You can't with the reflection API (System.Reflection).

The reason is that the reflection api is designed to work on Metadata (Type of Classes, Name and Signature of Methods, ...) but not on the data level (which would be the IL-stream itself).

Extended Answer:

You can emit (but not read) IL with System.Reflection.Emit (e.g. ILGenerator Class).

Through MethodInfo.GetMethodBody() you can get the binary IL-stream for the implementation of a method. But thats usually completely useless by itself.

There are external libraries (like Cecil) that you can use to read/modify/add/delete code inside a method.

How to inspect a method using reflection

You may be interested in wrapping the whole class inside a Proxy and watch it with an InvocationHandler:

http://www.javalobby.org/java/forums/t18631.html

Your InvocationHandler would do something special if it sees that "foo" is called immediatly after "bar", I guess.

C# Reflection: Is it possible to view the code of a constructor gained using reflection?

Basically, code is only available in form of MSIL instructions. See Can I use reflection to inspect the code in a method? for more info.

use reflection to modify a method and gain access to its local variables

Look into Byteman. Using it, you can describe a rule that whenever the method getMessage() on the affected class is called, your own code will be called.

What is reflection and why is it useful?

The name reflection is used to describe code which is able to inspect other code in the same system (or itself).

For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

So, to give you a code example of this in Java (imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);

One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.

There are some good reflection examples to get you started at http://docs.oracle.com/javase/tutorial/reflect/index.html

And finally, yes, the concepts are pretty much similar in other statically typed languages which support reflection (like C#). In dynamically typed languages, the use case described above is less necessary (since the compiler will allow any method to be called on any object, failing at runtime if it does not exist), but the second case of looking for methods which are marked or work in a certain way is still common.

Update from a comment:

The ability to inspect the code in the system and see object types is
not reflection, but rather Type Introspection. Reflection is then the
ability to make modifications at runtime by making use of
introspection. The distinction is necessary here as some languages
support introspection, but do not support reflection. One such example
is C++

Reflection - Get the list of method calls inside a lambda expression

This is fairly easy as long as you use Expression<Action> instead of Action. For full code, including how to get the actual values implied, see here - in particular ResolveMethod (and how it is used by Invoke). This is the code I use in protobuf-net to do RPC based on lambdas.

Detecting whether a method/function exists in Java

You can find out if a method exists in Java using reflection.

Get the Class object of the class you're interested in and call getMethod() with the method name and parameter types on it.

If the method doesn't exist, it will throw a NoSuchMethodException.

Also, please note that "functions" are called methods in Java.

Last but not least: keep in mind that if you think you need this, then chances are that you've got a design problem at hand. Reflection (which is what the methods to inspect the actual Java classes is called) is a rather specialized feature of Java and should not generally be used in business code (although it's used quite heavily and to some nice effects in some common libraries).



Related Topics



Leave a reply



Submit