5

In software development we are all using the libraries by software providers. Consider in class A there are four functions viz., x,y,z. I just want my development team to avoid using the function x. So instead of telling them not to use, I found an idea. Inherit the class and override all the functions and for the function x an unsupportedmethod exception is thrown and for the rest I'm calling the super methods. There also I found a problem, developers can use the base class A directly, how to avoid the class A being used directly. I found a similar functionality in OSGi, the lib bundles can be brought in and then not exported and so on. Is there are any way to achieve this is java?

vvekselva
  • 803
  • 3
  • 17
  • 34
  • I'm guessing you cannot edit the source? – Jivings Jul 27 '12 at 08:28
  • 4
    Subclass it and throw a exception, IToldYouNotToUseThisFunctionException in the first line of the function. – Siddharth Jul 27 '12 at 08:29
  • you might want to have a look at Java's SecurityManager and this [stackoverflow post][1] [1]: http://stackoverflow.com/questions/5486797/how-to-prevent-public-methods-from-being-called-from-specific-classes/5490106#5490106 – Korgen Jul 27 '12 at 08:58
  • The SecurityManager has to be explicitly invoked for a check. I tried a few things - my only guess would be to implement a custom `ClassLoader`, but I wasn't able to figure out how exactly, as a custom system class loader is not asked for all classes. – aRestless Jul 27 '12 at 10:34

4 Answers4

2

I suppose code reviews exist for these reasons. Consider situation where you can not edit the source of a third party, what would you do ? Like Siddharth says, sub class it and throw a meaningful exception and document it with a clear reasons. If someone is using base class even after that, mostly it may not out of ignorance,but it may out of curiosity. That kind of thing can be appreciated personally and for learning, but for the project sake developer has to follow the guidelines.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
1

I think simply telling your developers what to do is preferred over a complex software solution. Sometimes the simple thing is better.

But, if you insist on going down this path, you can enforce your architecture standards using aspects if you're a Spring user. Weave the offending methods with an aspect that throws an exception if they're called.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You can edit library class file in hex editor and modify its access modifier from public to package private. Also you can rename it and then use inheritance to wrap this class. Here you can find class file specification. Once I've tried this technique to substitute jdbc driver class with wraper class that provide some additional logging and other useful tricks.

gkuzmin
  • 2,414
  • 17
  • 24
  • Most license agreements will forbid editing the "binaries", won't they? – aRestless Jul 27 '12 at 08:54
  • I am not an expert in licenses, but may be some of them accept editing binaries for compilation/testing purpose? In this case production libraries will be original. – gkuzmin Jul 27 '12 at 09:04
0

There is a variety of tools that check source code for adherence to certain rules, such as formatting, dead code, naming conventions for variables etc. Popular ones for Java include the Maven Enforcer plugin, checkstyle and PMD.

These might allow you to write a rule that forbids certain method calls. Then you could check automatically at compile time. As far as I can tell, unfortunately none of the tools above support "illegal method calls" out-of-the-box; however, at least for PMD writing new checks is fairly simple.

sleske
  • 81,358
  • 34
  • 189
  • 227