0

There is a Java application and I have no permission to alter the Java code apart from annotating classes or methods (with custom or existing annotations). Using annotations and annotations only I have to invoke code which means that every time an instance of an annotated class is created or an annotated method is called, some extra Java code must be executed (e.g a call to a REST Webservice). So my question is: how can I do this?

In order to prevent answers that I have already checked I will give you some solutions that seem to work but are not satisfying enough.

  1. Aspect Oriented Programming (e.g AspectJ) can do this (execute code before and after the call of an annotated method) but I don't really want the runtime overhead.

  2. Use the solution provided here which actually uses reflection. This is exactly what I need only that it alters the initial code further than just annotating and so I cannot use it.

  3. Use annotation processor for source code generation as suggested here by the last answer. However, still this means that I will alter the source code which I don't want.

What I would really like is a way to simply include a Java file that somehow will execute some Java lines every time the annotated element will be triggered.

Community
  • 1
  • 1
  • 2
    What "runtime overhead" of AspectJ are you trying to avoid and how would you imagine any other solution would call your new methods? – Marvin Jun 23 '15 at 13:12
  • 2
    Side note: this restriction sounds awfully **stupid**. So, you are allowed to to add any annotation ... but no other changes to source code? My personal recommendation: don't invest in potential technical "solutions" (that will be second class workarounds anyway); instead challenge the people that put this bizarre requirement on you. – GhostCat Jun 23 '15 at 13:14
  • Or you could [write a java agent.](http://stackoverflow.com/q/11898566/139010) But I also agree with the other comments here that this restriction is rubbish. – Matt Ball Jun 23 '15 at 13:17
  • Well I don't really know, just encountered such concerns, and that's why I am asking. I just don't want to alter much the way the application will function. So,if I get it right, you think that it is the best way to do this and any other solution would have more or less the same impact on performance? – Ailene rob Jun 23 '15 at 13:22
  • What I am saying is: if your goal is to successfully maintain this product in the future; make changes to it, add features, fix bugs ... whatever: then your "strategy" sounds very dangerous. If you are afraid of breaking things; then start by adding many many unit tests for example - that helps to exactly document and understand the current behavior of the systems. "Not changing the system while changing it nonetheless" simply sounds like an approach that will cause more harm than good in the long run. – GhostCat Jun 23 '15 at 13:25
  • The restriction is because these annotations will be offered for a sort of testing purposes upon application's (that I do not own) behavior and might need to be removed after a certain period of time. If there is no way, then I will make the minimum code changes. – Ailene rob Jun 23 '15 at 13:30

1 Answers1

0

Why not skip annotations completely and use byteman to inject code at runtime into the entry points of your code.

I have to agree with the comment above though, that this sort of restriction is ridiculous and should be challenged.

mikea
  • 6,537
  • 19
  • 36