3
public void foo(){
    throw new Exception("foo");
}


public void bar(){
    foo();
}

Is it possible to inspect the method bar() in order to know that foo() is called without a try catch inside bar()?

JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
  • This would fail to compile due to the uncaught checked exception. It would be more interesting if it were a RuntimeException. Static-code analysis might have some answers. – TJR Sep 24 '11 at 09:14

3 Answers3

6

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.

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26
  • +1 - good link to improve my knowledge too. Thanks. However, it can not guarantee that its shows that the called method has used try..catch or something. – Naved Sep 24 '11 at 09:08
  • @Vicente Plata does it mean that we need to add a try catch everywhere with the proxy? Because if a method already contains a try catch, it is not needed... – JohnJohnGa Sep 24 '11 at 09:13
  • +1 - My answer suggests against trying to validate method calls at runtime, but this is a great solution for the cases where it's really necessary (eg. unit tests.) – Rob Sep 24 '11 at 10:08
  • @JohnJohnGa there are many ways to achieve your goal. One, is adding a try catch, but it would have no sense actually. The one I can think of currently, is adding a validation so if the last called method was "foo" and immediately after that you go to call "bar", then you know foo is calling bar (at least within the context of the class). – Vicente Plata Sep 25 '11 at 09:14
  • Answer accepted because it is the more close to the thing I try to do. Thanks – JohnJohnGa Sep 26 '11 at 15:14
2

It seems like your intention is to have your application code check a method implementation, and conditional branch when that method fails to use try-catch internally.

Unless you are writing unit tests, let me discourage doing this for two reasons:

1. A developer should understand his application logic.

You should already know what your code is doing. If the method is part of a closed-source API, check the documentation for thrown Exception types.

2. It adds unnecessary complexity.

Because flow of execution depends on method implementation, you will have an application whose behavior is dependent upon the state of its own source. (eg. Changing the method can create side-effects, which makes debugging more difficult.)

If you can determine method behavior by checking the source code or API documentation, what is the need for verifying at run-time?

Community
  • 1
  • 1
Rob
  • 5,223
  • 5
  • 41
  • 62
0

As far as my knowledge is concern, there is no such reflection API which allows to see the inside implementations. You can only inspect the methods present in the Class, but can not the logic written in the method.

Naved
  • 4,020
  • 4
  • 31
  • 45