How can I get a class object in a static method without using the class name?

I have

public class A { static X s_x = new X(A.class); } 

and

 public class B { static X s_x = new X(B.class); } 

etc. for many classes without any special relationship or community. I really would like s_x to be initialized in the superclass, but with descendant specific code; this is not possible because static code is not redefined. Therefore, I want to at least simplify my copy. I want a magic expression that evaluates an object of a class, i.e. Writes:

  static X s_x = new X(/* magic expression here */); 

where the magic expression is the same regardless of the class in which I declare my X, but does the same as the above examples. The second option is a static method with the same effect.

Notes:

  • Java 6, if possible.
  • This question is not required (required) about registration ...
+4
source share
8 answers

Some answers have an idea for a solution, but not a completely necessary piece of code. So here he is, working on any JVM:

 static X s_x = new X(getClassStatic()); public static Class<?> getClassStatic() { try { // we're using the third highest stack element, since the // first highest is the getStackTrace() method, followed by this method itself. We // want the calling code class. String name = Thread.currentThread().getStackTrace()[2].getClassName(); return Class.forName(name); } catch (ClassNotFoundException e) { // shouldn't be able to get here... return null; } } 

or, for individual JVMs, as suggested by Peter Laurie:

 static X s_x = new X(getCallerClass(1)); 
0
source

I also had this strange requirement once, and I tried to search, but did not find anything, so I think the answer is impossible.

But I was told to rethink my approach, and when I tried, I understood the same solution without this strange requirement. Therefore, please rethink your approach. I am sure that you will be able to solve this as I do. Also, if you can post the problem / script you are trying to solve, maybe I can help.

+1
source

In Java 7, you can use the java.lang.invoke.MethodHandles class to do this:

 Class c = MethodHandle.lookup().lookupClass() 

You will probably receive a warning about using a raw type instead of a parameterized version of Class<X> , but I don’t see how you can avoid this by not returning to hard coding the class name yourself.

+1
source

In HotSpot / OpenJDK you can use

  Class c = Reflection.getCallerClass(1) 

Note. This is an internal API and may not work on all JVMs.

+1
source

Here is a single line that should work in the Java 5+ JVM and does not add any additional import data to your code:

 new Object(){}.getClass().getEnclosingClass() 

Creates an anonymous inner class, gets its Class object, and then gets its instance of the wrapper class, which should be your class. For instance:

 public class HelloClass { static final Class<?> THIS_CLASS = new Object(){}.getClass().getEnclosingClass(); public static void main(String[] args) { System.out.println(THIS_CLASS); // prints "class HelloClass" } } 

In the context of your question:

 static X s_x = new X(new Object(){}.getClass().getEnclosingClass()); 
+1
source

I think the only solution is to extract the class name from the current stack trace (see Thread.getStackTrace() ), which will give it to you as a String , from which you can get a Class object using Class.forName(String) , but much more ugly than your current approach.

0
source

You can do this, but some kind of re-factoring is needed in your approach:

Try it:

 class X { public X(String cName) { try { Class.forName(cName); }catch(ClassNotFoundException cne) {} } } public class Test { static X x1 = new X(Thread.currentThread().getStackTrace()[1].getClassName()); } 
0
source

If you want to create a logging object, you can use lombok to enter a logging object for you. Example from the page:

 @Log public class LogExample { public static void main(String... args) { log.error("Something wrong here"); } } 
0
source

All Articles