#Abstract method parameters with generics

1 messages ยท Page 1 of 1 (latest)

noble scarabBOT
#

<@&987246652869971988> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

e subclasses have the same method signature.

Yes, it is possible to achieve this using a superclass with generics. You can define an abstract method in the superclass with generic parameters, and then implement it in each subclass with specific types.

Here's an example:

abstract class SuperClass<T> {
    public abstract void myMethod(T param);
}

class SubClass1 extends SuperClass<String> {
    @Override
    public void myMethod(String param) {
        // Implementation for SubClass1
    }
}

class SubClass2 extends SuperClass<Integer> {
    @Override
    public void myMethod(Integer param) {
        // Implementation for SubClass2
    }
}

In this example, SuperClass is a generic class with a single type parameter T. It defines an abstract method myMethod that takes a parameter of type T. The subclasses SubClass1 and SubClass2 extend SuperClass and provide their own implementations of myMethod, specifying the specific type for the generic parameter.

By using this approach, you don't need to manually create one method per subclass as all subclasses will inherit the abstract method from the superclass.

wicked basalt
#

Please ping if you respond :)

timid rivet
#

Did the bot resolve it @wicked basalt ?

wicked basalt
#

No but it was poorly written so I tired remaking the post. Apparently I failed to delete it. Let me rephrase it.

Is it possible to automatically create a method like this with the help of a super class. I don't want to have to manually create one per subclass as all of them call a single static method. I'd like it if the method in the super class is created per instance with that generic and that in turn calls the static method. I need it to be the actual class of the generic, and not the generic itself. I assume you can do it with byte buddy or smth?

public abstract class Foo<E extends Event> {
  
  @EventHandler
  public final void onEvent(E event) {
    Baz.doStruff(event);
  }
}

public class Bar extends Foo<MyEventClass> {
  
  // I don't want this in the class, it should work with only 
  // the super method.
  // V - Has this by default inherited by Foo - V
  @EventHandler
  public void onEvent(MyEventClass event) {
    Baz.doStruff(event);
  }
}
#

I need to annote the method, use the generic as the argument, and call a single static method with the argument.

timid rivet
#

I can take a look later, lunch break's about over. But why not just create an event handler for the parent class?

wicked basalt
#

Well this is for minecraft. And the way that works is that an event is called, and when it is it looks for methods annoted EventHandler with the event class as parameter. All methods that match that will get called with the event. I can't listen for the abstract event, for whatever reason they don't allow that, so I need to listen for each subclass of it. I made my own funnel for all events to get distributed further which is the static method, but I need to listen for the event according to how the API works, then call Driver.dispatch(<event>) with the event.

#

And since there are like 200+ events I need to listen for I don't really want to do them all manually and would like them generated by eg. bytebuddy. I think I've managed to create a method but I can't for the life of me redirect it properly. If there is no other way to do it I'll have to fix the bytebuddy code but I'd prefer like any alternative as my brain is just fried from it. If there is no viable alternative a nudge in the right direction with this bytebuddy redirection would be much appreciated.

acoustic bone
#

For example

class MyListener implements Listener, EventExecutor{

  public void execute(Listener listener, Event event) {/*Do some logic*/}
  
  public void register(Plugin plugin, PluginManager pluginmanager){
    List.of(SomeEvent.class, SomeOtherEvent.class).forEach(eventClass -> {
      pluginManager.registerEvent(eventClass, this, EventPriority.NORMAL, this, plugin);
    }
  }
}
#

Just an example, syntax may be wrong

#

Implementing Listener here is useless but necessary so you can pass it as the second argument for registerEvent.

#

Also, the event system may be garbage, but I do agree with the fact that it doesn't just allow you to listen to multiple events that easily

#

There are events that get called very frequently so you should really be careful doing something like this

wicked basalt
wicked basalt
# acoustic bone There are events that get called *very frequently* so you should really be caref...

Yeah I know, I NEED to listen to around 150 different events, and they NEED to be funnelled through the static method. However, the method is blazing fast. The first thing it does is discarding unused events, so if the event isn't currently used it will return immidietly. After a lot of testing I've found that using the hash code in a Int2ObjectMap is the fastest way of doing this. Currently it does 1 million calls in about 6ms which is fast enough :)

acoustic bone
wicked basalt
acoustic bone
#

I knew where this was going

wicked basalt
#

Thought I needed to explian more but I could keep it short xd

wicked basalt
acoustic bone
#

Dw

#

I've tried about the same thing

#

I even did some reflection scanning all packages for even subclasses

#

But I went the manual route in the end, because it has more freedom

#

not entirely manual with EventHandler annotation

wicked basalt
#

Yeah I don't need to, or event want to. I tried using bytebuddy but it's a headache ngl. This works perfectly :)

acoustic bone
#

๐Ÿ‘ epic

wicked basalt
#

Thank you btw :>

acoustic bone
#

have fun coding ;]

wicked basalt
#

Unless it's byte buddy again I will!