[英]How can a class access its own classname?
I use an java application which generates a class dynamically. 我使用一个动态生成类的java应用程序。 Via an ant script the source code will be produced for a give classname and a class template.
通过ant脚本,将为给定的类名和类模板生成源代码。
In the template for the class I need to know the name of even this class, to call a static method of the class. 在类的模板中,我需要知道甚至这个类的名称,来调用类的静态方法。
Example. 例。 The class will be named "VersionInfo".
该类将命名为“VersionInfo”。 Then in static main() of it I want to call the static method: VersionInfo.getId().
然后在静态main()中我想调用静态方法:VersionInfo.getId()。 But I don't know the class-name.
但我不知道班级名称。
Is there an equivalent to "this" for static contexts or some Utility-Class for such a purpose? 对于静态上下文或某些实用程序类是否有相当于“this”的目的?
如果您是通过Ant创建类,那么为什么不生成一个返回类名的静态方法getClassName?
如果你的main方法驻留在同一个类中,你可以在main方法中调用getId()
。
So you're saying that it should generate this? 所以你说它应该生成这个?
public class VersionInfo{ // VersionInfo class name changes, per problem description
public static void main(){
System.out.println(getId());
// but in the main within the class,we don't need the classname to call a static method
}
public static string getId(){
return "what's the problem?";
}
}
Is there something missing from the description, that you're calling some OTHER class' static method by an unknown-to-the-template name? 在描述中是否缺少某些东西,你是通过一个未知的模板名称调用一些OTHER类的静态方法?
There's a nasty workaround: 有一个令人讨厌的解决方法:
public static final Class THIS_CLASS = new Object() {
public Class getParentClass() {
return getClass().getEnclosingClass();
}
}.getParentClass();
I'm not sure I understand. 我不确定我理解。 If you generate the class
VersionInfo
yourself, why can't you get the class name from the code that generates the class? 如果您自己生成类
VersionInfo
,为什么不能从生成类的代码中获取类名?
Try this: 尝试这个:
package uk.co.farwell.stack_overflow;
public class Test_847708 {
private final static String getId() {
return "string";
}
public static void main(String args[]) {
System.out.println("getId=" + getId());
}
}
You cannot use the key "this" in a static context. 您不能在静态上下文中使用键“this”。
Instead, if you want to call dynamically a static function, you can use java reflection . 相反,如果要动态调用静态函数,可以使用java反射 。
I cannot help you further for java reflection because I never use it, but I already use it in .Net and it's a powerful tools. 我无法帮助你进一步了解java反射,因为我从不使用它,但我已经在.Net中使用它,它是一个强大的工具。
The cleanest answer to the question might be to make a third class with a static, known, name that is generated by the ANT script which references the dynamic class name, and then have your main method reference that known class. 对问题最干净的答案可能是使用由引用动态类名称的ANT脚本生成的静态已知名称创建第三个类,然后让主方法引用该已知类。
If for some reason that isn't enough, then combine Joachim Sauer and Melursus answer, and get the class name, and then get the method via reflection: 如果由于某种原因还不够,那就结合Joachim Sauer和Melursus的答案,得到类名,然后通过反射得到方法:
Method m = THIS_CLASS.getDeclaredMethod("getId", null);
Object result = m.invoke(null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.