0

Is there a way to pass values through the @LogMethod annotation. I want to pass a String and an Object (DataFileVO). How Can I do it?

Something like this.

@LogMethod(logLevel = LoggerOne.DEBUG, duaNum = "23L", duaDataFile = myListObject)

LoggerOne.java

public enum LoggerOne {
    INFO, DEBUG;
}

LogMethod.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogMethod {
   LoggerOne logLevel() default LoggerOne.INFO;
 }

Util.java

public class Util {

public static void log(Object o, String methodName) {
    Class klass = o.getClass();

    for (Method m : klass.getMethods()) {
        if (m.getName().equals(methodName)) {
            for (Annotation a : m.getAnnotations()) {
                if (a instanceof LogMethod) {
                    LogMethod lm = (LogMethod) a;
                    switch (lm.logLevel()) {
                      case INFO:
                        System.out.println("Performing Custom INFO logging for " + m.getName());
                        break;
                      case DEBUG:
                        System.out.println("Performing Custom DEBUG logging for " + m.getName());
                    }
                }
            }
            break;
        }
    }
   }
 }

DataFileDaoImpl.java

    @LogMethod(logLevel = LoggerOne.DEBUG)
public List<DuaDataFileVO> getDuaByDuaAndShipperCode(String duaNum, Long shipperCode) {
     List<DuaDataFileVO> list = new ArrayList<DuaDataFileVO>();
     // Some code
        return list;
}
5
  • Not sure what you like to reach. Something like this would be possible: @LogMethod(logLevel = LoggerOne.DEBUG, message = "Debug Prefix:"). But attaching any dynamic data to an annotation or something like this is obviously not possible (the annotation is attachted to a method, not to a specific call ...). Commented Jan 29, 2015 at 15:24
  • So, I cannot do something like this? @LogMethod(logLevel = LoggerOne.DEBUG, duaNum = "23", duaDataFile = "somelistObject") Commented Jan 29, 2015 at 15:25
  • Sure. You can provide any static data you like to the annotation. Just add methods like "int duaNum();" to the annotation declaration and your code can then do whatever it wants with that data. Commented Jan 29, 2015 at 15:28
  • and what about an entire Object? Commented Jan 29, 2015 at 15:29
  • No, only primitives, Strings, Class objects ... no other instances. Commented Jan 29, 2015 at 15:30

1 Answer 1

4

Annotations are only 'metadata' and not part of the concrete program. So, if you're compile a program, the metadata will not exist any more.

Read Lesson: Annotations - ORACLE for more informations.

I think you can solve your problem with 'Aspect-oriented programming'. Here is a Tutorial Java Method Logging with AOP and Annotations.

The following code comes from this tutorial.

The important code is the aspect:

@Aspect
public class MethodLogger {
  @Around("execution(* *(..)) && @annotation(Loggable)")
  public Object around(ProceedingJoinPoint point) {
    long start = System.currentTimeMillis();
    Object result = point.proceed();
    Logger.info(
      "#%s(%s): %s in %[msec]s",
      MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
      point.getArgs(),
      result,
      System.currentTimeMillis() - start
    );
    return result;
  }
}

This aspect is working around an execution (method) - which has any modifier, any name and any arguments - that is annotated with @Loggable. The MethodLogger prints a 'Info' with the classpath, method-name, arguments, the result and the execution-time from the executed method.

You can use this annotation like this:

public class Foo {
  @Loggable
  public int power(int x, int p) {
    return Math.pow(x, p);
  }
}

With this example-output:

[INFO] com.example.Foo #power(2, 10): 1024 in 12μs

You need this dependencies to compile:

<dependency>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-aspects</artifactId>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>

The tutorial is very hepful. To unsterstand why it works, read it completely.

I hope it helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

For compiling single aspects, you can read stackoverflow-article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.