how to create an eclipse plugin to auto create read/ writeExternal methods of existing class

254 Views Asked by At

How to create eclipse plugin to auto create the serialization code read/writeExternal on existing code java classes?

Steps needed get the class from active tab (and or info on class field info like one in outline window) and generate code for each field, maybe using reflection will also help.

1

There are 1 best solutions below

1
On

The easiest way is to build upon org.eclipse.jdt.ui.actions.GenerateMethodAbstractAction that is used by eclipse to implement GenerateToStringAction and GeneateHashCodeEqualsAction.

Basically:

  1. Build a basic sub-class of GenerateMethodAbstractAction
  2. Implement logic that enumerates all the fields / properties etc. you want to process in generateCandidates(). You also need to decide if you recurse into superclass or not.
  3. Implement logic that generates MethodDeclarations for readExternal/writeExternal methods using data collected in step 2.
  4. Wrap generated MethodDeclarations into an IWorkspaceRunnable that applies them as edits (see GenerateToStringOperation) and return it from createOperation(...).
  5. Register your new action to "Source" menu so it can be used

The code required is rather long and involved so it's better to follow the two existing action classes for guidance.

If you choose to put it somewhere else than "Source" menu, you can discover the active editor with

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()

See also: