Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 682065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:33:14+00:00 2026-05-14T01:33:14+00:00

I want to generate custom methods in Eclipse like the way we can generate

  • 0

I want to generate custom methods in Eclipse like the way we can generate toString, getters and settlers (right click -> source ->). Could anyone tell me what is involved in doing this?
Thanks,
Dutch

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:33:15+00:00Added an answer on May 14, 2026 at 1:33 am

    You could add your own action after the recent (Eclipse Galileo 3.5) generate toString() action,

    • using the active contribution URI: menu:org.eclipse.jdt.ui.source.menu?after=additions
    • with the active action definition identifier: org.eclipse.jdt.ui.edit.text.java.generate.tostring
    • using the active contribution item class: GenerateToStringAction (package org.eclipse.jdt.ui.actions)
    • from the contributing plugin: org.eclipse.jdt.ui

    I cannot find the source of that class on the internet, so I will copy it here for you to examine its content:

    /*******************************************************************************
     * Copyright (c) 2008, 2009 Mateusz Matela and others.
     * All rights reserved. This program and the accompanying materials
     * are made available under the terms of the Eclipse Public License v1.0
     * which accompanies this distribution, and is available at
     * http://www.eclipse.org/legal/epl-v10.html
     *
     * Contributors:
     *     Mateusz Matela <mateusz.matela@gmail.com> - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070
     *     Mateusz Matela <mateusz.matela@gmail.com> - [toString] toString() generator: Fields in declaration order - https://bugs.eclipse.org/bugs/show_bug.cgi?id=279924
     *******************************************************************************/
    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.codemanipulation.CodeGenerationSettings;
    import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.GenerateToStringOperation;
    import org.eclipse.jdt.internal.corext.codemanipulation.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;
    
    /**
     * Adds method implementations for <code>{@link java.lang.Object#toString()}</code> The action opens a
     * dialog from which the user can choose the fields and methods to be considered.
     * <p>
     * Will open the parent compilation unit in a Java editor. The result is
     * unsaved, so the user can decide if the changes are acceptable.
     * <p>
     * The action is applicable to structured selections containing elements of type
     * {@link org.eclipse.jdt.core.IType}.
     * 
     * <p>
     * This class may be instantiated; it is not intended to be subclassed.
     * </p>
     * 
     * @since 3.5
     * 
     * @noextend This class is not intended to be subclassed by clients.
     */
    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;
                    }
                }
            }
        }
    
        /**
         * Creates a new generate tostring action.
         * <p>
         * The action requires that the selection provided by the site's selection
         * provider is of type
         * {@link org.eclipse.jface.viewers.IStructuredSelection}.
         * 
         * @param site the workbench site providing context information for this
         *            action
         */
        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);
        }
    
        /**
         * Note: This constructor is for internal use only. Clients should not call
         * this constructor.
         * 
         * @param editor the compilation unit editor
         * 
         * @noreference This constructor is not intended to be referenced by clients.
         */
        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;
        }
    
        /**
         * Populates <code>result</code> with the bindings from <code>membersToBindings</code>, sorted
         * in the order of <code>allMembers</code>.
         * 
         * @param result list of bindings from membersToBindings, sorted in source order
         * @param allMembers all member elements in source order
         * @param membersToBindings map from {@link IMember} to {@link IBinding}
         * @since 3.6
         */
        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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 354k
  • Answers 354k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to do this through VBScript and load in… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer EDIT: I tried to clarify I bit more ... 1.… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer Read each line, stick the contents of the line into… May 14, 2026 at 7:52 am

Related Questions

I want to fire some code when a property is accessed and changed. I
I am creating my own custom T4 Template that integrates with an ADO.NET Entity
When using the ObsoleteAtribute in .Net it gives you compiler warnings telling you that
Normally in an ASP.NET view one could use the following function to obtain a
Does anyone know if it possible to define the equivalent of a java custom

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.