Eclipse重构问询:如何为类型层级添加中间父类
Great question—this is exactly the kind of repetitive task that Eclipse’s built-in tools are designed to handle without manual drudgery. Here’s how to solve it efficiently:
1. First, Lock in Your Abstract Class
Make sure you’ve already created org.mycompany.MySystemJob with all your shared system-specific logic, and confirmed it correctly extends org.quartz.Job. This will be the new parent for all your batch job classes.
2. Use Structural Search and Replace (SSR) for Batch Updates
Eclipse Kepler includes SSR, a powerful tool for modifying code structures at scale. This is perfect for your use case:
- Open the search dialog with
Search > Search..., then switch to the Structural Search tab. - Click New Search, select Java as the target language.
- Paste this search template to match all classes directly extending
org.quartz.Job:class $Class$ extends org.quartz.Job { // Class content (ignored by the template) } - Click the Variables button to refine the search:
- Select
$Class$, then under Exclude Types, addorg.mycompany.MySystemJob(so we don’t modify your new abstract class itself). - You can also add constraints like limiting to your project’s package if needed.
- Select
- Run the search to verify you’re only targeting the batch job classes you want to update.
- Switch to the Structural Replace tab, then paste this replacement template:
class $Class$ extends org.mycompany.MySystemJob { // Preserves original class content } - Click Preview to double-check all the changes that will be made. If everything looks correct, hit Replace All to update all classes in one go.
Why Extract Superclass Isn’t Ideal Here
You’re right that the Extract Superclass refactor isn’t a good fit here—it’s designed to pull common code from existing child classes into a new parent, which requires manually selecting all sibling classes. Since you’ve already built your abstract class upfront, SSR is the far more efficient choice.
Alternative: Type Hierarchy + Batch Refactor (For Smaller Codebases)
If you only have a handful of classes to update, this simpler method works too:
- Open the Type Hierarchy for
org.quartz.Job(select the class, press F4). - In the hierarchy view, expand the Direct Subclasses section and select all your batch job classes (hold Ctrl/Cmd to multi-select).
- Right-click the selection, then go to
Refactor > Change Superclass. - Enter
org.mycompany.MySystemJobas the new superclass, confirm, and Eclipse will update all selected classes at once.
内容的提问来源于stack exchange,提问作者Brian

