List all subclasses with fully qualified names

后端 未结 4 727
北海茫月
北海茫月 2021-01-13 08:18

I would like to get a list of all subclasses of a given class with their fully qualified names. I wanted to copy it from Eclipse and paste into a text file like this:

<
4条回答
  •  春和景丽
    2021-01-13 08:39

    Update: my original answer wouldn't work as there is no context to the structured selection.

    This answer shows how to contribute the action to the context menu and retrieve the structured selection. You can modify that type's execute method to process the Hierarchy (as VonC suggests, +1) and obtain all the sub-types and set the content to the clipboard as follows:

    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
        try {
            IStructuredSelection selection = SelectionConverter
                    .getStructuredSelection(activePart);
    
            IJavaElement[] elements = SelectionConverter.getElements(selection);
    
            if (elements != null && elements.length > 0) {
                if (elements[0] != null && elements[0] instanceof IType) {
                    IType type = (IType)elements[0];
    
                    ITypeHierarchy hierarchy =
                        type.newTypeHierarchy(new NullProgressMonitor());
    
                    IType[] subTypes = hierarchy.getAllSubtypes(type);
    
                    StringBuffer buf = new StringBuffer();
                    for (IType iType : subTypes) {
                        buf.append(iType.getFullyQualifiedName()).append("\n");
                    }
    
                    Shell shell = HandlerUtil.getActiveShell(event);
    
                    Clipboard clipboard = new Clipboard(shell.getDisplay());
    
                    clipboard.setContents(
                        new Object[]{buf.toString()}, 
                        new Transfer[]{TextTransfer.getInstance()});
                }
            }
        } catch (JavaModelException e) {
            logException(e);
        }
        return null;
    }
    

提交回复
热议问题