0

So idea is to write a code which would add some predefined code snippets

e.g. I want to

  1. Generate class field com.ck.MyClass.DEBUGGER initialized as org.apache.commons.logging.LogFactory.getLog("DEBUGGER." + MyClass.CNAME);

  2. add these below lines at entry point and exit point of all methods except constructors in a class through java code for all the classes present in my project

    final String methodName = "process()";
    
    if (MyClass.DEBUGGER.isDebugEnabled())  {
        MyClass.DEBUGGER.debug(CommonLoggingConstants.METHOD_ENTRY +   MyClass.CNAME + CommonLoggingConstants.HASH + methodName);
    }
    

What would be the best approach and API to address this requirement?

Rafael Winterhalter
  • 38,221
  • 13
  • 94
  • 174
  • If you are using any IDE like Netbeans then you can create the templates. check the below links. https://platform.netbeans.org/tutorials/nbm-filetemplates.html, https://netbeans.org/competition/win-with-netbeans/customize-java-template.html – Naga Srinu Kapusetti Jul 04 '16 at 12:01
  • 1
    If the code does already exist you can have a look at Aspect Oriented Programming. Otherwise, maybe the IDE can help. – Henry Jul 04 '16 at 12:03
  • For templates in an IDE like eclipse, see: http://stackoverflow.com/a/1029304/3082272 – bobbel Jul 04 '16 at 12:03

2 Answers2

1

Yo can use Aspect Oriented Programming in java to do anything before and after of any function call in java.

Follow the follwing link to implement it. You can use without Spring also. http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html
http://kamalmeet.com/tag/aop-2/

Bhanu Pasrija
  • 127
  • 2
  • 8
1

You could try to use AOP to intercept all the methods in your application and achieve your goal. You can read more about it here.

Aditya Gupta
  • 453
  • 1
  • 3
  • 11