Is there a way in Java to find out which class I'm in without referencing an object? I need the current class name from a static point. Here is an example:
Java中是否有一种方法可以在不引用对象的情况下找出我所在的类?我需要静态点的当前类名。这是一个例子:
public class MyClass {
// is this possible?
private static final String myClassName = ???;
// not like this
void someMethod(){
String className = this.getClass();
}
// or this
private static final String myClassName = MyClass.class;
}
The overall problem is logging. The logger of the project I'm working on needs the class name and is supposed to be declared like this:
整体问题是记录。我正在处理的项目的记录器需要类名,并且应该像这样声明:
private static final Logger LOGGER = LogHelper.getLogger(MyClass.class);
If there is a more generic way, it would be easier to copy the logger.
如果有更通用的方法,则复制记录器会更容易。
I need this code in multiple classes, and it would be nice to just copy the code instead of always replacing "MyClass.class" every time.
我需要在多个类中使用此代码,并且只需复制代码而不是每次都替换“MyClass.class”会很好。
Unfortunately, different approaches wouldn't be compatible with the conventions of this project.
不幸的是,不同的方法与该项目的惯例不相容。
2 个解决方案
#1
1
Some compilers like eclipse lets you do it, for exsamples look this page:
像eclipse这样的一些编译器允许你这样做,例如看看这个页面:
http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/
and use this in your new template:
并在新模板中使用它:
private static final Logger LOGGER = LogHelper.getLogger(${primary_type_name}.class
#2
0
To put it simply, there likely isn't a simpler or more generic approach to doing this. While I'm uncertain of the exact logger library you're using, the main fact remains that you must supply the class that you want to log to the logger.
简而言之,可能没有更简单或更通用的方法来做到这一点。虽然我不确定您正在使用的确切记录器库,但主要的事实仍然是您必须提供要记录到记录器的类。
It does mean extra typing, but there's no realistic approach that is simpler, easier to understand, or safer - anything that's overly complex and uses a lot of reflection to extract the class name would need to be tested to ensure that it always returns the correct class, which is substantially more effort than simply using MyClass.class
.
它确实意味着额外的输入,但没有更简单,更容易理解或更安全的现实方法 - 任何过于复杂并使用大量反射来提取类名的东西都需要进行测试以确保它始终返回正确的class,比简单地使用MyClass.class要多得多。
#1
1
Some compilers like eclipse lets you do it, for exsamples look this page:
像eclipse这样的一些编译器允许你这样做,例如看看这个页面:
http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/
and use this in your new template:
并在新模板中使用它:
private static final Logger LOGGER = LogHelper.getLogger(${primary_type_name}.class
#2
0
To put it simply, there likely isn't a simpler or more generic approach to doing this. While I'm uncertain of the exact logger library you're using, the main fact remains that you must supply the class that you want to log to the logger.
简而言之,可能没有更简单或更通用的方法来做到这一点。虽然我不确定您正在使用的确切记录器库,但主要的事实仍然是您必须提供要记录到记录器的类。
It does mean extra typing, but there's no realistic approach that is simpler, easier to understand, or safer - anything that's overly complex and uses a lot of reflection to extract the class name would need to be tested to ensure that it always returns the correct class, which is substantially more effort than simply using MyClass.class
.
它确实意味着额外的输入,但没有更简单,更容易理解或更安全的现实方法 - 任何过于复杂并使用大量反射来提取类名的东西都需要进行测试以确保它始终返回正确的class,比简单地使用MyClass.class要多得多。