Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:30:33+00:00 2026-06-10T07:30:33+00:00

Java Gurus, I am pretty new for annotations and haven’t searched for this a

  • 0

Java Gurus,

I am pretty new for annotations and haven’t searched for this a lot, so please bear with me…

I would like to implement a Custom Annotation which will intercept a method call; to start with something very basic it can just print the methods name and parameters so that I could avoid the logger statement.

A sample call like this:

public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
    log.debug("in findMyAppObjectById(" + id + ")");
    //....
}   

can be converted into:

@LogMethodCall(Logger.DEBUG)
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
    //....
}   

Could I get some hints about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:30:34+00:00Added an answer on June 10, 2026 at 7:30 am

    Based in your answers of my comments, you will not be able to do this with just annotations. You can, of course, create your annotations and create some reflective code that will detected then and execute some code, but this will not change your code too much, because you will need to call the parser method before you call your methods and I think that will not help you too much, since you will need to call the parser method before each call.

    If you need the behavior that you mentioned (automatic call), you will need to combine your annotations with some AOP framework like Spring (plain Java) or AspectJ (AspectJ code). With then, you can set pointcuts and everytime this point is reached, some code may be executed. You can configure then to execute some code before and/or after method execution.

    If the first scenario is sufficient, you can do something like:

    Logger: enum

    public enum Logger {
        INFO,
        DEBUG;
    }
    

    LogMethodCall: annotation

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention( RetentionPolicy.RUNTIME ) // the annotation will be available during runtime
    @Target( ElementType.METHOD )         // this can just used in methods
    public @interface LogMethodCall {
    
        Logger logLevel() default Logger.INFO;
    
    }
    

    Person: annotated class

    public class Person {
    
        // will use the default log level (INFO)
        @LogMethodCall
        public void foo( int a ) {
            System.out.println( "foo! " + a );
        }
    
        @LogMethodCall( logLevel = Logger.DEBUG )
        public void bar( int b ) {
            System.out.println( "bar! " + b );
        }
    
    }
    

    Utils: class with the log static method (this will perform the “parsing”)

    public class Utils {
    
        public static void log( Object o, String methodName ) {
    
            // gets the object class
            Class klass = o.getClass();
    
            // iterate over its methods
            for ( Method m : klass.getMethods() ) {
    
                // verify if the method is the wanted one
                if ( m.getName().equals( methodName ) ) {
    
                    // yes, it is
                    // so, iterate over its annotations
                    for ( Annotation a : m.getAnnotations() ) {
    
                        // verify if it is a LogMethodCall annotation
                        if ( a instanceof LogMethodCall ) {
    
                            // yes, it is
                            // so, cast it
                            LogMethodCall lmc = ( LogMethodCall ) a;
    
                            // verify the log level
                            switch ( lmc.logLevel() ) {
                                case INFO:
                                    System.out.println( "performing info log for \"" + m.getName() + "\" method" );
                                    break;
                                case DEBUG:
                                    System.out.println( "performing debug log for \"" + m.getName() + "\" method" );
                                    break;
                            }
    
                        }
                    }
    
                    // method encountered, so the loop can be break
                    break;
    
                }
    
            }
    
        }
    
    }
    

    AnnotationProcessing: class with code to test the annotation processing

    public class AnnotationProcessing {
    
        public static void main(String[] args) {
    
            Person p = new Person();
            Utils.log( p, "foo" );
            p.foo( 2 );
            Utils.log( p, "bar" );
            p.bar( 3 );
    
        }
    }
    

    Of course, you will need to improve my code to fit your needs. It is just a start point.

    More about annotations:

    • http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html
    • http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
    • http://tutorials.jenkov.com/java-reflection/annotations.html

    More about AOP:

    • http://en.wikipedia.org/wiki/Aspect-oriented_programming
    • http://static.springsource.org/spring/docs/3.0.x/reference/aop.html
    • http://www.eclipse.org/aspectj/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to django but experienced in Python and java web programming
Mockito seems like a pretty sweet stubbing/mocking framework for Java. The only problem is
I want to use the new java.nio.file.Files.walkFileTree in Scala. And I was even successful:
This is a question for the generic collection gurus. I'm shocked to find that
I am still new to Java and Eclipse and I'm trying to get my
I tried to increase the heap size like the below C:\Data\Guru\Code\Got\adminservice\adminservice>java -Xms512m -Xmx512m Usage:
As a java guru, what would you suggest to abstract random access so that
Please note that I am not a java guru. I might not use the
I'm getting an error when trying to implement an onClickListener, this is the error:
Unix gurus! I have a Java program which passes some parameters to a Servlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.