熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Eclipse重構功能:擴展點的使用

2022-06-13   來源: Java開源技術 

  Eclipse中提供了幾個擴展點方便擴展重構功能

  基本的重構功能有

  RenameMoveCreateDeleteCopy對應擴展點即為

  

  rerefactoringrenameParticipants
rerefactoringmoveParticipants
rerefactoringcreateParticipants
rerefactoringdeleteParticipants
pyParticipants

  以ReName為例其余項與ReName大同小異

  實現這個擴展點的基本語法

  

  < extension point=rerefactoringrenameParticipants>
< renameParticipant
    id=intramartappproducerrefactoringrenameTypeParticipant
    name=Ebuilder RenameTypeParticipant
    class=intramartappproducerrefactoringTypeRenameParticipant>
    < enablement>
    < /enablement>
< /renameParticipant>
< /extension>

  這裡默認對響應所有改名事件如果需要過濾可以在元素< enablement/>中加以定義不贅述實現改名擴展的關鍵在實現類必須是rerefactoringparticipantsRenameParticipant的子類

  下面代碼進行了簡單的Eclipse重構功能實現

  

  import reresourcesIFile;
import reresourcesResourcesPlugin;
import reruntimeCoreException;
import reruntimeIProgressMonitor;
import reruntimeOperationCanceledException;
import rerefactoringChange;
import rerefactoringRefactoringStatus;
import rerefactoringTextFileChange;
import rerefactoringparticipantsCheckConditionsContext;
import rerefactoringparticipantsRenameParticipant;
import orgeclipsetexteditsReplaceEdit;

  public class TypeRenameParticipant extends RenameParticipant {

  public TypeRenameParticipant() {
    }

  @Override
    public RefactoringStatus checkConditions(IProgressMonitor pm
            CheckConditionsContext context) throws OperationCanceledException {
        return new RefactoringStatus();
    }

  @Override
    public Change createChange(IProgressMonitor pm) throws CoreException
            OperationCanceledException {
        IFile file = ResourcesPlugingetWorkspace()getRoot()getProject(a)
                getFile(a);
        TextFileChange textFileChange = new TextFileChange(File Changed
                file);

  ReplaceEdit edit = new ReplaceEdit( haha);
        textFileChangesetEdit(edit);
        return textFileChange;
    }

  @Override
    public String getName() {
        return Ebuilder RenameTypeParticipant;
    }

  @Override
    protected boolean initialize(Object element) {
        // need sub
        return true;
    }

  }

  CreateChange方法內實現過於粗糙僅僅是為了可以讓大家看到結果

  Eclipse重構功能結果預覽

  通過利用擴展點我們就自然的將重構時的差異比較警告preview重構historyredo/undo等eclipse平台提供的基本功能加以利用了

  Preview的結果如下圖

  Preview的結果Java開源技術免費提供,內容來源於互聯網,本文歸原作者所有。

推薦文章
Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.