简体   繁体   English

更改框架类/方法的访问级别

[英]Changing the access level of a framework class/method

In this given example, AbstractAnimal has lots of implementations outside the framework. 在这个给定的示例中,AbstractAnimal在框架之外具有许多实现。 The framework, however, is refactored so that now WalkingAnimals exist. 但是,该框架已经过重构,因此现在存在WalkingAnimals。 It's intended that Dog, amongst all other animals, will now inherit WalkingAnimal instead. 打算让Dog和其他所有动物现在一起继承WalkingAnimal。 For the sake of example, there are no animals that don't walk. 举例来说,没有动物不会走路。 All inheriters should be refactored. 所有继承者都应该重构。

UML

This is a Java framework, so I'll be referring to it as such. 这是一个Java框架,因此我将这样称呼它。 But feel free to interpret the question more generally. 但是请随意更一般地解释这个问题。 Because it's quite large, we would like our IDE to provide some kind of indication that the classes that currently inherit AbstractAnimal should be changed. 因为它很大,所以我们希望我们的IDE提供某种指示,表明应该更改当前继承AbstractAnimal的类。 Therefore we thought of the @Deprecated annotation. 因此,我们想到了@Deprecated注释。 However, the framework itself will then throw warnings, so it must not be the cleanest method. 但是,框架本身将随后发出警告,因此它一定不是最干净的方法。

In essence, there's classes and methods whose access level will change so that only the framework will be able to use it. 从本质上讲,有些类和方法的访问级别将发生变化,因此只有框架才能使用它。 We'd like to announce this in the to-be-refactored classes in a manner as clear as possible (such as warnings and strikethroughs provided by @Deprecated). 我们希望在重构类中以尽可能清晰的方式宣布这一点(例如@Deprecated提供的警告和删除线)。 Our IDE is IntelliJ IDEA and we're working with Java 1.8. 我们的IDE是IntelliJ IDEA,我们正在使用Java 1.8。

We're looking for some kind of automatic task generation in the sense that developers can take a look at a list of warnings and know what they'll still need to change. 从某种意义上说,我们正在寻找某种自动任务生成,开发人员可以查看警告列表并知道他们仍然需要更改什么。 Also bossman would like to see progress being made, so making these countable would be a great plus. 另外,Bossman希望看到正在取得的进展,因此使这些数字可数将是一个很大的优势。

Thank you in advance for any advice. 预先感谢您的任何建议。

What you could do is add an abstract dummy method to AbstractAnimal called youShouldNotExtendAbstractAnimalDirectly() that only has an implementation in WalkingAnimal . 您可以做的是向AbstractAnimal添加一个抽象的虚拟方法,名为youShouldNotExtendAbstractAnimalDirectly() ,该方法仅在WalkingAnimal具有实现。 This will cause a compile failure in Dog , and when the developers try fixing it they'll run into that method, and either take the hint or shoot themselves in the foot. 这将导致Dog的编译失败,并且当开发人员尝试修复它时,他们将遇到该方法,并且要么暗示要么射击自己。

You could just give the walk method to the AbstractAnimal class, with a default implementation of throwing an OperationNotSupportedException . 您可以将walk方法提供给AbstractAnimal类,其默认实现是抛出OperationNotSupportedException

public void walk() {
    throw new OperationNotSupportedException();
}

It really depends on the circumstances whether this is a good idea. 这真的是一个好主意,要视情况而定。

EDIT: To be more exact, as Jeroen points out, you'll only find out whether the new method is properly implemented at run time. 编辑:更确切地说,正如Jeroen指出的那样,您只会发现新方法是否在运行时正确实施。 Therefore, it's best to catch that exception wherever it is used; 因此,最好在任何地方都捕获该异常。 the more often you have to do that, the less desirable this solution is. 您执行此操作的次数越多,此解决方案就越不可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM