alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * CreateMethodElement.java
 *
 * Created on September 3, 2003, 4:50 PM
 */

package org.netbeans.modules.refactoring;

import java.text.MessageFormat;
import java.util.List;
import java.util.ListIterator;
import javax.swing.text.Position;
import org.netbeans.api.mdr.MDRepository;
import org.netbeans.modules.refactoring.api.RefactoringElement;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.openide.text.CloneableEditorSupport;
import org.openide.text.PositionBounds;
import org.openide.text.PositionRef;
import org.openide.util.NbBundle;

/**
 *
 * @author  Tomas Hurka
 */
public class CreateMethodElement implements RefactoringElement {
    private int modifiers;
    private String name;
    private TypeReference returnType;
    private String methodBody;
    private ClassDefinition declaringClass;
    private boolean enabled=true;
    private JavaModelPackage modelExtent;
    private List parameters;
    private Method newMethod;
    private PositionBounds bounds;
    private ClassMember insertPoint;
    
    /** Creates a new instance of CreateMethodElement */
    public CreateMethodElement(int mod,String n,TypeReference rdesc,List pars,String body,ClassMember insertPoint,ClassDefinition jcls) {
        modifiers=mod;
        name=n;
        returnType=rdesc;
        parameters=pars;
        methodBody=body;
        declaringClass=jcls;
        this.insertPoint=insertPoint;
        modelExtent=(JavaModelPackage)jcls.refOutermostPackage();
    }
    
    /** Returns text describing the refactoring formatted for display (using HTML tags).
     * @return Formatted text.
     *
     */
    public String getDisplayText() {
        return new MessageFormat(NbBundle.getMessage(CreateMethodElement.class, "DSC_CreateMethod")).format (
                    new Object[] {name}
                );
    }
    
    /** Returns Java element associated with this refactoring element.
     * @return MDR Java element.
     *
     */
    public Element getJavaElement() {
        return declaringClass;
    }
    
    /** Returns position bounds of the text to be affected by this refactoring element.
     *
     */
    public PositionBounds getPosition() {
        if (bounds==null) {
            PositionBounds b=JavaMetamodel.getManager().getElementPosition(declaringClass);
            CloneableEditorSupport ed=b.getBegin().getCloneableEditorSupport();
            PositionRef start=ed.createPositionRef(0, Position.Bias.Forward);
            PositionRef end=ed.createPositionRef(0, Position.Bias.Backward);

            bounds=new PositionBounds(start,end);
        }
        return bounds;
    }
    
    /** Returns the status of this refactoring element (whether it is a normal element,
     * or a warning.
     * @return Status of this element.
     *
     */
    public int getStatus() {
        return RefactoringElement.NORMAL;
    }
    
    /** Returns text describing the refactoring element.
     * @return Text.
     *
     */
    public String getText() {
        throw new UnsupportedOperationException();
    }
    
    /** Indicates whether this refactoring element is enabled.
     * @return true if this element is enabled, otherwise false.
     *
     */
    public boolean isEnabled() {
        return enabled;
    }
    
    /** Performs the change represented by this refactoring element.
     *
     */
    public void performChange() {
        if (enabled) {
            MDRepository repository = JavaMetamodel.getDefaultRepository();
            repository.beginTrans(true);
            boolean fail = true;
            try {                
                newMethod=modelExtent.getMethod().createMethod(
                    name, null, modifiers, null, null, null, methodBody, null, parameters, null, null, 0
                );
                if (returnType==null)
                    newMethod.setType(modelExtent.getType().resolve("void")); //NOI18N
                else
                    newMethod.setTypeName((TypeReference)returnType.duplicate());
                List contents = declaringClass.getContents();
                if (insertPoint == null) {
                    contents.add(newMethod);
                } else {
                    ListIterator iter = contents.listIterator(contents.size());
                    Object obj;
                    do {
                        obj = iter.previous();
                    } while (obj != insertPoint);
                    iter.add(newMethod);
                }
                fail = false;
            }
            finally {
                repository.endTrans(fail);
            }
        }
    }
    
    /** Enables/disables this element.
     * @param enabled If true the element is enabled, otherwise it is disabled.
     *
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.