Eclipse再構成機能:拡張点の使用


転載先:http://developer.51cto.com/art/200908/143871.htm
 
Eclipseにはいくつかの拡張点が提供され、再構成機能を拡張しやすい。
基本的な再構成機能は、
Rename、Move、Create、Delete、Copy。対応する拡張点は、次の通りです。
 

  
  
  
  
  1. org.eclipse.ltk.core.refactoring.renameParticipants    
  2. org.eclipse.ltk.core.refactoring.moveParticipants    
  3. org.eclipse.ltk.core.refactoring.createParticipants    
  4. org.eclipse.ltk.core.refactoring.deleteParticipants    
  5. org.eclipse.ltk.core.refactoring.copyParticipants   
 
ReNameを例にとって、残りの4つはReNameと大同小異です。
この拡張点を実現するための基本的な文法:
 

  
  
  
  
  1. < extension point="org.eclipse.ltk.core.refactoring.renameParticipants">    
  2. < renameParticipant    
  3.     id="jp.co.intramart.app.producer.refactoring.renameTypeParticipant"    
  4.     name="Ebuilder RenameTypeParticipant"     
  5.     class="jp.co.intramart.app.producer.refactoring.TypeRenameParticipant">     
  6.     < enablement>    
  7.     < /enablement>        
  8. < /renameParticipant>    
  9. < /extension>   
ここではデフォルトですべての改名イベントに対応しています。フィルタリングが必要なら、要素の中で定義できます。贅を要しない。改名拡張を実現するための鍵となる実現カテゴリは、org.eclipspe.ltk.com re.refactoring.participnts.Rename Particparticpartでなければなりません。のサブクラス
以下のコードは簡単なEclipse再構成機能を実現します。
 

  
  
  
  
  1. import org.eclipse.core.resources.IFile;    
  2. import org.eclipse.core.resources.ResourcesPlugin;    
  3. import org.eclipse.core.runtime.CoreException;    
  4. import org.eclipse.core.runtime.IProgressMonitor;    
  5. import org.eclipse.core.runtime.OperationCanceledException;    
  6. import org.eclipse.ltk.core.refactoring.Change;    
  7. import org.eclipse.ltk.core.refactoring.RefactoringStatus;    
  8. import org.eclipse.ltk.core.refactoring.TextFileChange;    
  9. import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;    
  10. import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;    
  11. import org.eclipse.text.edits.ReplaceEdit;    
  12.     
  13. public class TypeRenameParticipant extends RenameParticipant {    
  14.     
  15.     public TypeRenameParticipant() {    
  16.     }    
  17.     
  18.     @Override    
  19.     public RefactoringStatus checkConditions(IProgressMonitor pm,    
  20.             CheckConditionsContext context) throws OperationCanceledException {    
  21.         return new RefactoringStatus();    
  22.     }    
  23.     
  24.     @Override    
  25.     public Change createChange(IProgressMonitor pm) throws CoreException,    
  26.             OperationCanceledException {    
  27.         IFile file = ResourcesPlugin.getWorkspace().getRoot().getProject("a")    
  28.                 .getFile("a");    
  29.         TextFileChange textFileChange = new TextFileChange("File Changed ",    
  30.                 file);    
  31.     
  32.         ReplaceEdit edit = new ReplaceEdit(01"haha");    
  33.         textFileChange.setEdit(edit);    
  34.         return textFileChange;    
  35.     }    
  36.     
  37.     @Override    
  38.     public String getName() {    
  39.         return "Ebuilder RenameTypeParticipant";    
  40.     }    
  41.     
  42.     @Override    
  43.     protected boolean initialize(Object element) {    
  44.         // need sub    
  45.         return true;    
  46.     }    
  47.     
  48. }   
 
CreateChangeメソッドでは、あまりにも粗い実現は、結果を見ることができるようにするためだけです。
Eclipse再構成機能結果プレビュー
拡張点を利用することで、再現時の違いを自然に比較し、警告、preview、再構築history、redo/undoなど、eclipseプラットフォームが提供する基本機能を利用しました。
Prevewの結果は下図の通りです。
 
Eclipse再構成機能:特殊需要
ここで紹介します。拡張点によって特別な需要が実現されます。
増加、削除、変更、シフトなどの基本的な再構成に加えて、JDTでのペア、方法、変数名の再構成など、特別な需要を増やすことができます。
特殊な需要を実現するには、自分のRefactoring類を実現し、クラスorg.eclipse.ltk.com re.refactoring.Refactoringに関連する方法を実現します。このような構造はRenamePartcipartなどの構造とほぼ一致しています。
 

  
  
  
  
  1. import org.eclipse.core.runtime.CoreException;    
  2. import org.eclipse.core.runtime.IProgressMonitor;    
  3. import org.eclipse.core.runtime.OperationCanceledException;    
  4. import org.eclipse.ltk.core.refactoring.Change;    
  5. import org.eclipse.ltk.core.refactoring.Refactoring;    
  6. import org.eclipse.ltk.core.refactoring.RefactoringStatus;    
  7.     
  8. public class ProducerRefactoring extends Refactoring {    
  9.     
  10.     @Override    
  11.     public RefactoringStatus checkFinalConditions(IProgressMonitor pm)    
  12.             throws CoreException, OperationCanceledException {    
  13.         // need sub    
  14.         return new RefactoringStatus();    
  15.     }    
  16.     
  17.     @Override    
  18.     public RefactoringStatus checkInitialConditions(IProgressMonitor pm)    
  19.             throws CoreException, OperationCanceledException {    
  20.         // need sub    
  21.         return new RefactoringStatus();    
  22.     }    
  23.     
  24.     @Override    
  25.     public Change createChange(IProgressMonitor pm) throws CoreException,    
  26.             OperationCanceledException {    
  27.         // need sub    
  28.         return null;    
  29.     }    
  30.     
  31.     @Override    
  32.     public String getName() {    
  33.         return "ProducerRefactoring";    
  34.     }    
  35.     
  36. }   
 
このクラスは特殊な需要と再構成の特殊な論理を処理する責任があります。
論理層の他に、表現層の実現が必要である。
Refactoring Wizard及びRefactorngWizardPage。
Refactoring,Wizard,WizardPageを実現すると,UIが論理的に実現される。
対応するアクションの構成により、RefactorngWizard OpenOperationを使用する。特殊な再構成ニーズの開発が完了しました。
特殊需要のRefactoring論理部分の再利用を容易にするために、eclipseは拡張点を提供する:
org.eclipse.ltk.com re.refactoring.refactorigonConttributions
拡張点の配置により、使用時にIDでいつでもRefactoringオブジェクトが得られます。
この記事はeclipseの肩に立ちます。