tags:

views:

274

answers:

8

I use an java application which generates a class dynamically. Via an ant script the source code will be produced for a give classname and a class template.

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". Then in static main() of it I want to call the static method: 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?

A: 

You cannot use the key "this" in a static context.

Instead, if you want to call dynamically a static function, you can use java reflection.

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.

Melursus
+8  A: 

If you are creating the class via Ant then why not just generate a static method getClassName that returns the name of the class?

Visage
alright. I solved it as proposed. (the answer to the "why" is: I hoped there to be some convenient java method - as now i had to dig into the app which genereates teh source code...)
räph
+1  A: 

There's a nasty workaround:

public static final Class THIS_CLASS = new Object() {
  public Class getParentClass() {
    return getClass().getEnclosingClass();
  }
}.getParentClass();
Joachim Sauer
Definitely a nasty workaround, but it doesn't solve his problem completely. If he needs to reference ClassName.method(), having the Class object doesn't help, except as a starter for reflection.
Yishai
+1  A: 

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?

Ronald Wildenberg
+3  A: 

If your main method resides in the same class you just can call getId() in the main method.

rudolfson
sry for inaccurate description. tried to keep the example simple, but actually that was misleading. of course the main method was in another class
räph
A: 

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.

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:

Method m = THIS_CLASS.getDeclaredMethod("getId", null);
Object result = m.invoke(null, null);
Yishai
+2  A: 

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?

Tetsujin no Oni
+1  A: 

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());
    }

}
MatthieuF