在Eclipse中生成自定义方法

在Eclipse中生成自定义方法,第1张

在Eclipse中生成自定义方法

您可以在最近(Eclipse Galileo 3.5)生成

toString()
动作之后添加自己的动作,

  • 使用主动贡献URI:
    menu:org.eclipse.jdt.ui.source.menu?after=additions
  • 具有活动 *** 作定义标识符:
    org.eclipse.jdt.ui.edit.text.java.generate.tostring
  • 使用有效贡献项目类:(
    GenerateToStringAction
    org.eclipse.jdt.ui.actions
  • 来自贡献插件:org.eclipse.jdt.ui

我无法在互联网上找到该类的来源,因此我将其复制到此处以供您检查其内容:

package org.eclipse.jdt.ui.actions;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import org.eclipse.swt.widgets.Shell;import org.eclipse.core.resources.IWorkspaceRunnable;import org.eclipse.ui.IWorkbenchSite;import org.eclipse.ui.PlatformUI;import org.eclipse.ltk.core.refactoring.RefactoringStatus;import org.eclipse.jdt.core.IField;import org.eclipse.jdt.core.IJavaElement;import org.eclipse.jdt.core.IMember;import org.eclipse.jdt.core.IType;import org.eclipse.jdt.core.JavaCore;import org.eclipse.jdt.core.JavaModelException;import org.eclipse.jdt.core.dom.IBinding;import org.eclipse.jdt.core.dom.IMethodBinding;import org.eclipse.jdt.core.dom.ITypeBinding;import org.eclipse.jdt.core.dom.IVariableBinding;import org.eclipse.jdt.core.dom.Modifier;import org.eclipse.jdt.internal.corext.premanipulation.CodeGenerationSettings;import org.eclipse.jdt.internal.corext.premanipulation.tostringgeneration.GenerateToStringOperation;import org.eclipse.jdt.internal.corext.premanipulation.tostringgeneration.ToStringGenerationSettings;import org.eclipse.jdt.internal.corext.util.JavaModelUtil;import org.eclipse.jdt.internal.corext.util.Messages;import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;import org.eclipse.jdt.internal.ui.actions.ActionMessages;import org.eclipse.jdt.internal.ui.actions.SelectionConverter;import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog;import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog;import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;public class GenerateToStringAction extends GenerateMethodAbstractAction {    private static final String METHODNAME_TO_STRING= "toString"; //$NON-NLS-1$    private List fFields;    private List fInheritedFields;    private List fSelectedFields;    private List fMethods;    private List fInheritedMethods;    private GenerateToStringOperation operation;    private class ToStringInfo {        public boolean foundToString= false;        public boolean foundFinalToString= false;        public ToStringInfo(ITypeBinding typeBinding) { IMethodBinding[] declaredMethods= typeBinding.getDeclaredMethods(); for (int i= 0; i < declaredMethods.length; i++) {     if (declaredMethods[i].getName().equals(METHODNAME_TO_STRING) && declaredMethods[i].getParameterTypes().length == 0) {         this.foundToString= true;         if (Modifier.isFinal(declaredMethods[i].getModifiers()))  this.foundFinalToString= true;     } }        }    }        public GenerateToStringAction(IWorkbenchSite site) {        super(site);        setText(ActionMessages.GenerateToStringAction_label);        setDescription(ActionMessages.GenerateToStringAction_description);        setToolTipText(ActionMessages.GenerateToStringAction_tooltip);        PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GENERATE_TOSTRING_ACTION);    }        public GenerateToStringAction(CompilationUnitEditor editor) {        this(editor.getEditorSite());        fEditor= editor;        setEnabled((fEditor != null && SelectionConverter.canOperateOn(fEditor)));    }    RefactoringStatus checkMember(Object object) {        // no conditions need to be checked         return new RefactoringStatus();    }    RefactoringStatus checkGeneralConditions(IType type, CodeGenerationSettings settings, Object[] selected) {        return operation.checkConditions();    }     RefactoringStatus checkSuperClass(ITypeBinding superclass) {        RefactoringStatus status= new RefactoringStatus();        if (new ToStringInfo(superclass).foundFinalToString) { status.addError(Messages.format(ActionMessages.GenerateMethodAbstractAction_final_method_in_superclass_error, new String[] {         Messages.format(ActionMessages.GenerateMethodAbstractAction_super_class, BasicElementLabels.getJavaElementName(superclass.getQualifiedName())),         ActionMessages.GenerateToStringAction_tostring }), createRefactoringStatusContext(superclass.getJavaElement()));        }        return status;    }    SourceActionDialog createDialog(Shell shell, IType type) throws JavaModelException {        IVariableBinding[] fieldBindings= (IVariableBinding[]) fFields.toArray(new IVariableBinding[0]);        IVariableBinding[] inheritedFieldBindings= (IVariableBinding[]) fInheritedFields.toArray(new IVariableBinding[0]);        IVariableBinding[] selectedFieldBindings= (IVariableBinding[]) fSelectedFields.toArray(new IVariableBinding[0]);        IMethodBinding[] methodBindings= (IMethodBinding[]) fMethods.toArray(new IMethodBinding[0]);        IMethodBinding[] inheritededMethodBindings= (IMethodBinding[]) fInheritedMethods.toArray(new IMethodBinding[0]);        return new GenerateToStringDialog(shell, fEditor, type, fieldBindings, inheritedFieldBindings, selectedFieldBindings, methodBindings, inheritededMethodBindings);    }    IWorkspaceRunnable createOperation(Object[] selectedBindings, CodeGenerationSettings settings, boolean regenerate, IJavaElement type, IJavaElement elementPosition) {        return operation= GenerateToStringOperation.createOperation(fTypeBinding, selectedBindings, fUnit, elementPosition, (ToStringGenerationSettings)settings);    }    CodeGenerationSettings createSettings(IType type, SourceActionDialog dialog) {        ToStringGenerationSettings settings= ((GenerateToStringDialog) dialog).getGenerationSettings();        super.createSettings(type, dialog).setSettings(settings);        settings.createComments= dialog.getGenerateComment();        settings.useBlocks= useBlocks(type.getJavaProject());        String version= fUnit.getJavaElement().getJavaProject().getOption(JavaCore.COMPILER_SOURCE, true);        settings.is50orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_5);        settings.is60orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_6);        return settings;    }    boolean generateCandidates() throws JavaModelException {        IVariableBinding[] candidateFields= fTypeBinding.getDeclaredFields();        HashMap fieldsToBindings= new HashMap();        HashMap selectedFieldsToBindings= new HashMap();        for (int i= 0; i < candidateFields.length; i++) { if (!Modifier.isStatic(candidateFields[i].getModifiers())) {     fieldsToBindings.put(candidateFields[i].getJavaElement(), candidateFields[i]);     if (!Modifier.isTransient(candidateFields[i].getModifiers()))         selectedFieldsToBindings.put(candidateFields[i].getJavaElement(), candidateFields[i]); }        }        IType type= (IType)fTypeBinding.getJavaElement();        IField[] allFields= type.getFields();        fFields= new ArrayList();        populateMembers(fFields, allFields, fieldsToBindings);        fSelectedFields= new ArrayList();        populateMembers(fSelectedFields, allFields, selectedFieldsToBindings);        IMethodBinding[] candidateMethods= fTypeBinding.getDeclaredMethods();        HashMap methodsToBindings= new HashMap();        for (int i= 0; i < candidateMethods.length; i++) { if (!Modifier.isStatic(candidateMethods[i].getModifiers()) && candidateMethods[i].getParameterTypes().length == 0         && !candidateMethods[i].getReturnType().getName().equals("void") && !candidateMethods[i].getName().equals("toString") && !candidateMethods[i].getName().equals("clone")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$     methodsToBindings.put(candidateMethods[i].getJavaElement(), candidateMethods[i]); }        }        fMethods= new ArrayList();        populateMembers(fMethods, type.getMethods(), methodsToBindings);        fInheritedFields= new ArrayList();        fInheritedMethods= new ArrayList();        ITypeBinding typeBinding= fTypeBinding;        while ((typeBinding= typeBinding.getSuperclass()) != null) { type = (IType)typeBinding.getJavaElement(); candidateFields= typeBinding.getDeclaredFields(); for (int i= 0; i < candidateFields.length; i++) {     if (!Modifier.isPrivate(candidateFields[i].getModifiers()) && !Modifier.isStatic(candidateFields[i].getModifiers()) && !contains(fFields, candidateFields[i])  && !contains(fInheritedFields, candidateFields[i])) {         fieldsToBindings.put(candidateFields[i].getJavaElement(), candidateFields[i]);     } } populateMembers(fInheritedFields, type.getFields(), fieldsToBindings); candidateMethods= typeBinding.getDeclaredMethods(); for (int i= 0; i < candidateMethods.length; i++) {     if (!Modifier.isPrivate(candidateMethods[i].getModifiers())  && !Modifier.isStatic(candidateMethods[i].getModifiers())  && candidateMethods[i].getParameterTypes().length == 0  && !candidateMethods[i].getReturnType().getName().equals("void") && !contains(fMethods, candidateMethods[i]) && !contains(fInheritedMethods, candidateMethods[i]) && !candidateMethods[i].getName().equals("clone")) { //$NON-NLS-1$ //$NON-NLS-2$         methodsToBindings.put(candidateMethods[i].getJavaElement(), candidateMethods[i]);     } } populateMembers(fInheritedMethods, type.getMethods(), methodsToBindings);        }        return true;    }        private static void populateMembers(List result, IMember[] allMembers, HashMap membersToBindings) {        for (int i= 0; i < allMembers.length; i++) { Object memberBinding= membersToBindings.remove(allMembers[i]); if (memberBinding != null) {     result.add(memberBinding); }        }    }    private static boolean contains(List inheritedFields, Object member) {        for (Iterator iterator= inheritedFields.iterator(); iterator.hasNext();) { Object object= iterator.next(); if (object instanceof IVariableBinding && member instanceof IVariableBinding)     if (((IVariableBinding) object).getName().equals(((IVariableBinding) member).getName()))         return true; if (object instanceof IMethodBinding && member instanceof IMethodBinding)     if (((IMethodBinding) object).getName().equals(((IMethodBinding) member).getName()))         return true;        }        return false;    }    String getAlreadyImplementedErrorMethodName() {        return ActionMessages.GenerateToStringAction_tostring;    }    boolean isMethodAlreadyImplemented(ITypeBinding typeBinding) {        return new ToStringInfo(typeBinding).foundToString;    }    String getErrorCaption() {        return ActionMessages.GenerateToStringAction_error_caption;    }    String getNoMembersError() {        //no members error never occurs        return null;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5565822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存