0

I want to do something like this:

public class Parent {

    private static final Map<Class<?>, Object> map;

    static {
        map = new IdentityHashMap<>();
    }

    public Parent() {
        if (map.get(getKey()) == null) {
            map.put(getKey(), new Object());
        }
    }

    private static Class<?> getKey() {
        return /*something*/;
    }

    public static Object getValue() {
        return map.get(getKey());
    }

}

public class Child extends Parent {


}

Child.getValue() // returns an object
Parent.getValue() // returns other object

I tried to use MethodHandles.lookup().lookupClass() in getKey(), but it returns Parent class when calling Child.getValue() (eg:

public class Parent {

    private static final Map<Class<?>, Object> map;

    static {
        map = new IdentityHashMap<>();
    }

    public Parent() {
        if (map.get(getKey()) == null) {
            map.put(getKey(), new Object());
        }
    }

    private static Class<?> getKey() {
        Class<?> clazz = MethodHandles.lookup().lookupClass();
        System.out.println(clazz.getName());
        return clazz;
    }

    public static Object getValue() {
        return map.get(getKey());
    }

}

public class Child extends Parent {


}

Child.getValue() 
// console output: my.project.package.Parent
// i want: my.project.package.Child

)

How i can do this? I tried to use all cases from this: Getting the class name from a static method in Java, and in every case it prints in console parent class name.

UPD: the getValue() method should be static, i can't do anything with it. UPD2: I know that such code is bad practice, and I know why this problem arises, but I did not write the getValue() method and it does not depend on me, and I want to find out any way to perform the task described in the question, even using reflection, the MethodHandles or something else

Iondering
  • 11
  • 2
  • Do the `getKey()` and `getValue()` methods have to be `static` or can they be non-static? – Progman May 30 '22 at 15:41
  • Yes, they should be static, it not depends on me – Iondering May 30 '22 at 15:42
  • 2
    Does this answer your question? [Why doesn't Java allow overriding of static methods?](https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods) – Progman May 30 '22 at 15:51
  • I know that such code is bad practice, and I know why this problem arises, but I did not write the getValue() method and it does not depend on me, and I want to find out any way to perform the task described in the question, even using reflection, the MethodHandles or something else – Iondering May 30 '22 at 15:59
  • 2
    It seems like a wrong way to to whatever you need to do. If you want and can share the real code and a concrete example, maybe we can propose a better solution. With the current formulation of the problem, I don't think what you want is possible, or perhaps using extremely dirty hacks. Seems better to take a step back and try to rethink the problem. – Dici May 30 '22 at 16:07
  • What can you modify? One option is to get the `key` of the classes as a static value and "override" it in the constructor of each child class, but that will just generate new problems... I'd also suggest considering a different approach to the problem. – Jetto Martínez May 30 '22 at 16:08
  • Calling `Child.getValue()` ends up executing code from/in `Parent.getValue()` so it will return `Parent` as this is the place from which `MethodHandles.lookup().lookupClass()` was called. Also trying to "override" `getValue()` in *each* Child class makes no sense, since lack of polymorphism would force us to call them via `ChildX.getValue()`, but that means we already know about `ChildX` class, so using directly `ChildX.class` would be easier. Solution you are looking for would be something like `self-type` mechanism, but unfortunately Java doesn't have it. – Pshemo May 30 '22 at 16:12
  • ok, the real code: im making plugin for minecraft using bukkit framework, and, to make custom event for bukkit events api, i need to add HandlerList directly to the event, like this: pastebin.com/MBRRUbEH i want to do superclass for all events, whose child classes would not have to write these methods and create a list of handlers, so event class will be small, like this: https://pastebin.com/L7ZQNbpa – Iondering May 30 '22 at 16:17
  • Why does `getHandlerList` have to be static? Can't you just have an abstract class with a non-static handler list and a default constructor that creates it, and then all sub-classes benefit from it? – Dici May 30 '22 at 18:43
  • It have to be static, because bukkit framework forces do it static, it access it by the reflection. – Iondering Jun 01 '22 at 11:01

0 Answers0