Skip to content

Commit dc56ade

Browse files
maarztctrueden
authored andcommitted
LogService: tweak getCallingClass method
Extract code for calling class detection to new class CallingClassUtils. Classes that should be ignored by the calling class detection should be annotated with IgnoreAsCallingClass.
1 parent 3a530d0 commit dc56ade

File tree

5 files changed

+182
-11
lines changed

5 files changed

+182
-11
lines changed

src/main/java/org/scijava/log/AbstractLogService.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Johannes Schindelin
4545
* @author Curtis Rueden
4646
*/
47+
@IgnoreAsCallingClass
4748
public abstract class AbstractLogService extends AbstractService implements
4849
LogService
4950
{
@@ -88,7 +89,7 @@ public AbstractLogService(final Properties properties) {
8889
public int getLevel() {
8990
if (!classAndPackageLevels.isEmpty()) {
9091
// check for a custom log level for calling class or its parent packages
91-
String classOrPackageName = callingClass();
92+
String classOrPackageName = CallingClassUtils.getCallingClass().getName();
9293
while (classOrPackageName != null) {
9394
final Integer level = classAndPackageLevels.get(classOrPackageName);
9495
if (level != null) return level;
@@ -119,16 +120,6 @@ protected String getPrefix(final int level) {
119120

120121
// -- Helper methods --
121122

122-
private String callingClass() {
123-
final String thisClass = AbstractLogService.class.getName();
124-
for (final StackTraceElement element : new Exception().getStackTrace()) {
125-
final String className = element.getClassName();
126-
// NB: Skip stack trace elements from other methods of this class.
127-
if (!thisClass.equals(className)) return className;
128-
}
129-
return null;
130-
}
131-
132123
private String parentPackage(final String classOrPackageName) {
133124
final int dot = classOrPackageName.lastIndexOf(".");
134125
if (dot < 0) return null;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.log;
33+
34+
/**
35+
* Utility class for getting the calling class of a method.
36+
*
37+
* @author Matthias Arzt
38+
*/
39+
40+
@IgnoreAsCallingClass
41+
public final class CallingClassUtils {
42+
43+
private CallingClassUtils() {
44+
// prevent instantiation of utility class
45+
}
46+
47+
/**
48+
* Inspects the stack trace to return the class that calls this method, but
49+
* ignores every class annotated with @IgnoreAsCallingClass.
50+
*
51+
* @throws IllegalStateException if every method on the stack, is in a class
52+
* annotated with @IgnoreAsCallingClass.
53+
*/
54+
public static Class<?> getCallingClass() {
55+
try {
56+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
57+
for (int i = 1; i < stackTrace.length - 1; i++) {
58+
Class<?> clazz = Class.forName(stackTrace[i].getClassName());
59+
if (!clazz.isAnnotationPresent(IgnoreAsCallingClass.class))
60+
return clazz;
61+
}
62+
}
63+
catch (ClassNotFoundException ignore) {}
64+
throw new IllegalStateException();
65+
}
66+
67+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.log;
33+
34+
import java.lang.annotation.Retention;
35+
import java.lang.annotation.RetentionPolicy;
36+
37+
/**
38+
* Classes annotated with {@link IgnoreAsCallingClass} are ignored by
39+
* {@link CallingClassUtils#getCallingClass()}.
40+
*
41+
* @author Matthias Arzt
42+
*/
43+
@Retention(RetentionPolicy.RUNTIME)
44+
public @interface IgnoreAsCallingClass {}

src/main/java/org/scijava/log/Logger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @see LogLevel
4949
* @see LogService
5050
*/
51+
@IgnoreAsCallingClass
5152
public interface Logger {
5253

5354
default void debug(final Object msg) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.log;
33+
34+
import org.junit.Test;
35+
36+
import static org.junit.Assert.assertEquals;
37+
38+
/**
39+
* Tests {@link CallingClassUtils}.
40+
*
41+
* @author Matthias Arzt
42+
*/
43+
public class CallingClassUtilsTest {
44+
@Test
45+
public void testGetCallingClass() {
46+
Class<?> callingClass = CallingClassUtils.getCallingClass();
47+
assertEquals(this.getClass(), callingClass);
48+
}
49+
50+
@Test
51+
public void testIgnoreAsCallingClass() {
52+
assertEquals(ClassA.class, ClassA.returnGetCallingClass());
53+
assertEquals(this.getClass(), ClassB.returnGetCallingClass());
54+
}
55+
56+
public static class ClassA {
57+
static Class<?> returnGetCallingClass() {
58+
return CallingClassUtils.getCallingClass();
59+
}
60+
}
61+
62+
@IgnoreAsCallingClass
63+
private static class ClassB {
64+
static Class<?> returnGetCallingClass() {
65+
return CallingClassUtils.getCallingClass();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)