Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Eclipse extension Point

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the Eclipse extension point, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Several extension points are provided in Eclipse to facilitate the extension of refactoring capabilities.

The basic refactoring functions are

Rename,Move,Create,Delete,Copy . The corresponding expansion point is:

Org.eclipse.ltk.core.refactoring.renameParticipants org.eclipse.ltk.core.refactoring.moveParticipants org.eclipse.ltk.core.refactoring.createParticipants org.eclipse.ltk.core.refactoring.deleteParticipants org.eclipse.ltk.core.refactoring.copyParticipants

Take ReName as an example, the other four items are more or less the same as ReName.

The basic syntax for implementing this extension point:

< extension point="org.eclipse.ltk.core.refactoring.renameParticipants">

< renameParticipant id="jp.co.intramart.app.producer.refactoring.renameTypeParticipant" name="Ebuilder RenameTypeParticipant" class="jp.co.intramart.app.producer.refactoring.TypeRenameParticipant">

< enablement>

< /enablement>

< /renameParticipant>

< /extension>

This is the default response to all renaming events, and if you need to filter, you can use the element

< enablement/>

It is defined in the. I won't repeat it. The key to implementing the renamed extension is the implementation class, which must be a subclass of org.eclipse.ltk.core.refactoring.participants.RenameParticipant;

The following code implements a simple Eclipse refactoring function.

Import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.TextFileChange; import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext Import org.eclipse.ltk.core.refactoring.participants.RenameParticipant; import org.eclipse.text.edits.ReplaceEdit; 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 = ResourcesPlugin.getWorkspace () .getRoot () .getProject ("a") .getFile ("a"); TextFileChange textFileChange = new TextFileChange ("File Changed", file) ReplaceEdit edit = new ReplaceEdit (0,1, ""); textFileChange.setEdit (edit); return textFileChange;} @ Override public String getName () {return "Ebuilder RenameTypeParticipant";} @ Override protected boolean initialize (Object element) {/ / need sub return true }}

The implementation in the CreateChange method is too rough, just so that you can see the results.

Preview of the result of Eclipse refactoring function

By using extension points, we naturally take advantage of the basic functions provided by the eclipse platform, such as differences in refactoring, warning, preview, refactoring history,redo/undo, etc.

The results of Preview are shown below.

Eclipse refactoring function: special requirements

Let me introduce you to the implementation of special requirements through extension points.

In addition to basic refactorings such as adding, deleting, changing, and moving, you can add refactorings for special requirements, such as class, method, and variable names in JDT.

To achieve special requirements, it is necessary to implement their own Refactoring class, inherit the class org.eclipse.ltk.core.refactoring.Refactoring to achieve the relevant methods, the structure of this class is basically the same as the structure of RenameParticipant and other classes, directly on the code, do not repeat.

Import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.Refactoring; import org.eclipse.ltk.core.refactoring.RefactoringStatus Public class ProducerRefactoring extends Refactoring {@ Override public RefactoringStatus checkFinalConditions (IProgressMonitor pm) throws CoreException, OperationCanceledException {/ / need sub return new RefactoringStatus ();} @ Override public RefactoringStatus checkInitialConditions (IProgressMonitor pm) throws CoreException, OperationCanceledException {/ / need sub return new RefactoringStatus () @ Override public Change createChange (IProgressMonitor pm) throws CoreException, OperationCanceledException {/ / need sub return null;} @ Override public String getName () {return "ProducerRefactoring";}}

This class is responsible for dealing with special requirements and the special logic of refactoring.

In addition to the logical layer, you also need to implement the presentation layer:

RefactoringWizard and RefactoringWizardPage.

After the implementation of Refactoring,Wizard,WizardPage, it is completed, UI to the implementation of the logic.

Use RefactoringWizardOpenOperation through the configuration of the corresponding Action. That is, the development of special refactoring requirements is completed.

To facilitate the reuse of the logical parts of Refactoring with special requirements, eclipse provides an extension point:

Org.eclipse.ltk.core.refactoring.refactoringContributions

Through the configuration of the extension point, the Refactoring object can be obtained at any time through ID.

The above is all the content of this article "how to use Eclipse extension points". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report