0

So the title might be a bit confusing so let me clarify using the following example. There are two classes, class A and class B. Class A is a class that I wrote and it can be found in my project. Class B is in an external library that I have as a dependency on my project. I can reference class B but I can't modify it's source. Now lets say class B has a method. Lets call this method "method()" for demonstration purposes. Is there a way for me to listen to when method() gets called from class A? If so, is it possible to even override the method?

You guys might be saying, "Can't you just let class A extend class B?". Well to answer your question, no. The way in which I will be using this concept, I would need to extend the object, not the class which is not possible (at least I don't think it is).

EDIT:

I'm using Java SE.

---What I'm Trying To Accomplish---

Okay so I make Minecraft server mods (mostly known as Bukkit plugins). In the Bukkit library (a dependency you need to create Bukkit plugins), there is a class called PlayerConnection. Every Player has their own PlayerConnection instance. The instance is stored in the EntityPlayer class in a public field called "playerConnection". PlayerConnection is responsible for sending and receiving packets. I need to be able to receive packets sent from that player without overriding the "playerConnection" field in EntityPlayer. I could create my own PlayerConnection class that extends PlayerConnection and set the playerConnection field to an instance of my custom PlayerConnection class but that would interfere with other plugins that use this same method.

  • 1
    What are you looking for is called AOP (Aspect Oriented Programming) – Dimitri Jul 24 '15 at 16:01
  • 1
    You'll need to proxy class B, which will be simple if class B implements a suitable interface and a headache otherwise. – chrylis -cautiouslyoptimistic- Jul 24 '15 at 16:02
  • you always extend the object class, so i would encourage you to just just let class A extend class B if its useful to you! – QuakeCore Jul 24 '15 at 16:08
  • @Dimitri I'll look into that :) Thanks – ReadySetPawn Jul 24 '15 at 16:11
  • Could you edit your question and add some background on what environment are you using (plain Java SE, Java EE, Spring)? It would also be usefull to know, if you wish to add a single functionality to a single class or have more cross-cutting concerns (like logging for all classes from this package etc). – Apokralipsa Jul 24 '15 at 16:12
  • @Apokralipsa I will edit the post with that information as well as specifying what in general I'm trying to accomplish and why I need such a method. – ReadySetPawn Jul 24 '15 at 16:16
  • I have looked into the classes you mentioned. Unfortunately, as they suffer from poor encapsulation (due to performance issues ... or at least I hope so :) your best chance at adding functionality is to extend the class and use super#method() invocations in overriden methods :( If you can control the build process then take a look at AspectJ, used to provide powerfull AOP extensions to Java. – Apokralipsa Jul 24 '15 at 19:40
  • @Apokralipsa Thanks for the info :) I'll read more into AOP – ReadySetPawn Jul 24 '15 at 21:09

0 Answers0