1

I would annotate a Java method in order to run some code before the method body is executed and before the method returns to the caller. I read about code generation at compile time using Java annotation.

The signature of the method I would annotate is as in the following example.

public Output process(Input arg0) {
    // ...
    // body that creates output object as processing result of arg0
    // ...
    return output;
}

I would annotate the above method in order to log the input argument before the method body is executed and to log the output data before the method returns to the caller.

I hypothesized an annotation like the following one.

@Log(input = "arg0", inputDest = "log1.txt", outputDest = "log2.txt")

The above annotation should generate (at compile time) some code like the following.

public Output process(Input arg0) {
    SampleLogger.log(arg0, "log1.txt");   // <-- generated code
    // ...
    // body that creates Output object as processing result of arg0
    // ...
    SampleLogger.log(output, "log2.txt");   // <-- generated code
    return output;
}

How to achieve this?

enzom83
  • 7,500
  • 8
  • 55
  • 107
  • What have you tried? What is your research? I only see requirements, not an attempt at a solution. And as such the question is far too broad as there are any ways to achieve this. – Erwin Bolwidt Jun 21 '18 at 23:29
  • Duplicate to [annotation processor](https://stackoverflow.com/questions/13690272/code-replacement-with-an-annotation-processor) and [Project Lombok](https://stackoverflow.com/questions/36563807/can-i-add-a-method-to-a-class-from-a-compile-time-annotation) – Amith Kumar Jun 22 '18 at 00:03
  • Possible duplicate of [Code replacement with an annotation processor](https://stackoverflow.com/questions/13690272/code-replacement-with-an-annotation-processor) – Amith Kumar Jun 22 '18 at 00:05
  • Seems like overkill to me for a logger functionality. Why does plain old AOP not work? AspectJ? It also works with annotations , btw. – benji2505 Jun 22 '18 at 01:10

1 Answers1

0

Yes, you can. But you should not use compile code generation. As the comment above mentioned, project lombok do such genration in compile period. But it is trick. It uses so many private API which let the project complex and hard to maintain.

What you really need is to enter into the method invocation so that you can enhance the method implematation. That is what AOP do.

For your requirenemnt, there are many libraries can do it. Such as Cglib, AspectJ, Spring AOP. Here give you a simple Cglib sample

  public static void main(String[] arg) {
    YourClass actualInstance = new YourClass();
    YourClass instanceToUse = (YourClass) Enhancer.create(YourClass.class, (MethodInterceptor) (obj, method, args, proxy) -> {
      System.out.println("enter the method: " + method.getName());
      Object result = method.invoke(actualInstance, args);
      System.out.println("exit the method: " + method.getName());
      return result;
    });
    instanceToUse.process();
  }

  public static class YourClass {
    public Object process() {
      System.out.println("do something");
      return new Object();
    }
  }
Dean Xu
  • 3,133
  • 1
  • 12
  • 36