Moved getCallingClass function to ClassUtil

This commit is contained in:
Ziver Koc 2017-09-21 23:41:24 +02:00
parent c5c135320f
commit 8ce73e3f50
3 changed files with 24 additions and 16 deletions

View file

@ -27,6 +27,7 @@ package zutil;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
/** /**
@ -162,4 +163,23 @@ public class ClassUtil {
} }
return new Class[0]; return new Class[0];
} }
/**
* @return the first class in the stack that do not match the filter
*/
public static String getCallingClass(Class... filter){
ArrayList filterStr = new ArrayList();
for (Class clazz : filter)
filterStr.add(clazz.getName());
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(int i=1; i<stackTraceElements.length ;++i){
String className = stackTraceElements[i].getClassName();
if( !filterStr.contains(className) ){
return className;
}
}
return null;
}
} }

View file

@ -24,6 +24,7 @@
package zutil.log; package zutil.log;
import zutil.ClassUtil;
import zutil.io.file.FileUtil; import zutil.io.file.FileUtil;
import java.io.File; import java.io.File;
@ -45,21 +46,7 @@ public class LogUtil {
* @return a new Logger for the calling class * @return a new Logger for the calling class
*/ */
public static Logger getLogger(){ public static Logger getLogger(){
return Logger.getLogger(getCallingClass()); return Logger.getLogger(ClassUtil.getCallingClass(LogUtil.class));
}
/**
* @return the parent class other than Logger in the stack
*/
public static String getCallingClass(){
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(int i=1; i<stackTraceElements.length ;++i){
String name = stackTraceElements[i].getClassName();
if( !name.equals( LogUtil.class.getName() ) ){
return name;
}
}
return null;
} }
/** /**

View file

@ -24,6 +24,7 @@
package zutil.net.ssdp; package zutil.net.ssdp;
import zutil.ClassUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import java.io.IOException; import java.io.IOException;
@ -36,7 +37,7 @@ import java.util.logging.Level;
public class SSDPClientTest { public class SSDPClientTest {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println(LogUtil.getCallingClass()); System.out.println(ClassUtil.getCallingClass());
LogUtil.setGlobalLevel(Level.FINEST); LogUtil.setGlobalLevel(Level.FINEST);
SSDPClient ssdp = new SSDPClient(); SSDPClient ssdp = new SSDPClient();
ssdp.requestService("upnp:rootdevice"); ssdp.requestService("upnp:rootdevice");