Reflecting method's actions in Java

I'd like to know how to - if even possible - reflect what method calls are executed inside the method during execution. I'm especially interested in either external method calls (that is, methods in other classes) or calling some specific method like getDatabaseConnection().

My intention would be to monitor predefined objects' actions inside methods and execute additional code if some specific conditions are met like some method is called with specific values. The monitor would be completely external class or a set of classes with no direct access to the object to be monitored by no other way than reflection.

Asked by: Leonardo993 | Posted: 23-01-2022

Answer 1

Aspect J will solve your problem.

Try to define a pointcut like this:

pointcut profilling(): execution(public * *(..)) && (
            within(com.myPackage..*) ||

In this way you will catch all the call to any public method within the package com.myPackage. Add as many within clauses you need.

Then add the following code:

Object around(): profilling() {

    //Do wherever you need before method call
    //Do wherever you need after method call


IF you want to learn something more about aspectJ follow this guide.

Answered by: Anna306 | Posted: 24-02-2022

Answer 2

I'd expect BCEL to be able to do this. From the web site:

The Byte Code Engineering Library is intended to give users a convenient possibility to analyze, create, and manipulate (binary) Java class files (those ending with .class).

The "analyze" part being the important bit here. The JavaDoc isn't visible on the web site (as far as I can see) so I can't easily be sure whether or not it'll help you, but it's a reasonable starting point.

Answered by: Kirsten872 | Posted: 24-02-2022

Answer 3

BCEL should offer this capability, but ...

... your requirements sound a lot like Aspect-Oriented Programming (AOP), so you should probably also look at AspectJ (with Eclipse tooling).

The main advantage of AspectJ is that it offers a well-designed way to express your specific conditions.

Answered by: Briony216 | Posted: 24-02-2022

Similar questions

reflection - Java reflecting subclasses

I have a class in Java, I wish to reflect all subclasses of this class, how would I do this? In this specific case, all subclasses are in the same package, and only subclasses are in this package, so an equivalent solution is to fetch all classes in a package.

reflection - Java reflecting on method scope variables

Using reflection you can get pretty much everything relating to a class. You can get all the declared methods, fields and classes (and possibly even more), but i couldn't find a way to reflect on a method so i could find out what classes that method might be using. Essentially i would like to find out all dependencies to other classes that a given class has. Example: Given the following code: ...

reflection - Java reflecting nested anonymous classes

Why does this code return "class java.lang.Object" ? Object a = new Object() { public Object b = new Object(){ public int c; }; }; System.out.println(a.getClass().getField("b").getType()); Why does the inner-inner type get lost? How can I reflect the c field ? Edit: This one works (as pointed out in some answers): a.getClass().getField("b").get(a) ....

reflection - Reflecting on Java

This question already has answers here:

java - How can I check if a method is static using reflection?

I want to discover at run-time ONLY the static Methods of a class, how can I do this? Or, how to differentiate between static and non-static methods.

Is there a general "back-end" library for Java reflection

I'm currently working with a specialized, interpreted, programming language implemented in Java. As a very small part of the language, I'd like to add the ability to make calls into Java. Before I dive into all of the nitty-gritty of reflection, I was wondering if anyone knew of a general library for doing the "back-end" part of invoking Java code reflectively. That is, I parse a string (I define the gram...

reflection - Validate reflected method return type and parms in Java

I have a generic Callback object which provides a (primitive) callback capability for Java, in the absence of closures. The Callback object contains a Method, and returns the parameter and return types for the method via a couple of accessor methods that just delegate to the equivalent methods in Method. I am trying to validate that a Callback I have been supplied points to a valid method. I need the return type ...

Can I discover a Java class' declared inner classes using reflection?

In Java, is there any way to use the JDK libraries to discover the private classes implemented within another class? Or do I need so use something like asm?

reflection - How do I intercept a method invocation with standard java features (no AspectJ etc)?

I want to intercept all method invocations to some class MyClass to be able to react on some setter-invocations. I tried to use dynamic proxies, but as far as I know, this only works for classes implementing some interface. But MyClass does not have such an interface. Is there any other way, besides implementing a wrapper class, that delegates all invocations to a member, which is an instance of the MyClass...

java - Calling a method using reflection

Is it possible to call a method by reflection from a class? class MyObject { ... //some methods public void fce() { //call another method of this object via reflection? } } Thank you.

java - How do reflection and remoting work internally?

I am curious to know how Reflection and Remoting in .net work internally. I also hear that .net can use remoting to communicate with applications written in other languages (such as Java). How does that work? This is probably a large question so an answer that briefly touches upon each question is reasonable.

reflection - Dumping a java object's properties

Is there a library that will recursively dump/print an objects properties? I'm looking for something similar to the console.dir() function in Firebug. I'm aware of the commons-lang ReflectionToStringBuilde...

c# - How does "static reflection" work in java? (ex. in mockito or easymock)

I'm a .NET guy - and I mainly code in C#. Since C# 3.0, we can leverage lambda expressions and expression trees to use static reflection. For example, it is possible to implement GetMethodName in the following snippet to return the name of the method passed ...

Is Java Reflection on Inherited Methods Different in Windows and Linux?

While setting up Hudson for continous integration testing (on a JeOS server), I've come across some strange behaviour I'm hoping the fine people at SO can explain to me. Our unit tests depend heavily on the use of domain objects, with lots of properties that must be set (due to null constraints in the database). In order to keep our tests readable, we have created a class InstantiationUtils that can instantiate an ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)