Skip to content

Commit 766cd95

Browse files
committedMar 20, 2018
Initial commit
1 parent dbb176a commit 766cd95

9 files changed

+686
-0
lines changed
 

‎.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

+570
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎LoggerTest.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

‎src/LogMethodCall.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.lang.annotation.ElementType;
2+
import java.lang.annotation.Retention;
3+
import java.lang.annotation.RetentionPolicy;
4+
import java.lang.annotation.Target;
5+
6+
@Retention (RetentionPolicy.RUNTIME) // The annotation will be available during runtime
7+
@Target (ElementType.METHOD) // This can just used in methods
8+
public @interface LogMethodCall {
9+
Logger logLevel() default Logger.INFO;
10+
}

‎src/Logger.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public enum Logger {
2+
INFO,
3+
DEBUG
4+
}

‎src/Main.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Main {
2+
3+
public static void main(String[] args) {
4+
5+
Person p = new Person();
6+
Utils.log(p, "foo");
7+
p.foo(2);
8+
Utils.log(p, "bar");
9+
p.bar(3);
10+
11+
}
12+
13+
}

‎src/Person.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Person {
2+
3+
// Will use the default log level (INFO)
4+
@LogMethodCall
5+
public void foo(int a) {
6+
System.out.println("foo! " + a);
7+
}
8+
9+
@LogMethodCall (logLevel = Logger.DEBUG)
10+
public void bar(int b) {
11+
System.out.println("bar! " + b);
12+
}
13+
14+
}

‎src/Utils.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.lang.annotation.Annotation;
2+
import java.lang.reflect.Method;
3+
4+
public class Utils {
5+
6+
public static void log(Object o, String methodName) {
7+
8+
// Gets the object class
9+
Class klass = o.getClass();
10+
11+
// Iterate over its methods
12+
for (Method m : klass.getMethods()) {
13+
14+
// Verify if the method is the wanted one
15+
if (m.getName().equals(methodName)) {
16+
17+
// Yes, it is
18+
// So, iterate over its annotations
19+
for (Annotation a : m.getAnnotations()) {
20+
21+
// Verify if it is a LogMethodCall annotation
22+
if (a instanceof LogMethodCall) {
23+
24+
// Yes, it is
25+
// So, cast it
26+
LogMethodCall lmc = (LogMethodCall) a;
27+
28+
// Verify the log level
29+
switch (lmc.logLevel()) {
30+
case INFO:
31+
System.out.println("performing info log for \"" + m.getName() + "\" method");
32+
break;
33+
case DEBUG:
34+
System.out.println("performing debug log for \"" + m.getName() + "\" method");
35+
break;
36+
}
37+
38+
}
39+
}
40+
41+
// Method encountered, so the loop can be break
42+
break;
43+
44+
}
45+
46+
}
47+
48+
}
49+
50+
}

0 commit comments

Comments
 (0)