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

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.refactoring;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import javax.jmi.reflect.RefObject;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.modules.javacore.JMManager;
import org.netbeans.modules.refactoring.api.AbstractRefactoring;
import org.netbeans.modules.refactoring.api.Problem;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.modules.refactoring.classpath.RefactoringClassPathImplementation;
import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.openide.text.PositionBounds;
import org.openide.util.Cancellable;

/**
 *
 * @author Martin Matula
 */
public abstract class NbAbstractRefactoring extends AbstractRefactoring {

    public final ClassPath classPath = ClassPathFactory.createClassPath(RefactoringClassPathImplementation.getDefault());

    protected static final Resource getClassResource(Feature feature) {
        ClassDefinition declaringClass = feature.getDeclaringClass();
        ClassDefinition outermostClass;

        if (declaringClass == null) {
            outermostClass = declaringClass = (ClassDefinition) feature;
        }
        else if (declaringClass instanceof JavaClass) {
            outermostClass = declaringClass;
            JavaClass cd = (JavaClass) declaringClass;
            while (cd != null) {
                outermostClass = cd;
                cd = (JavaClass) cd.getDeclaringClass();
            }
        } else {
            outermostClass = declaringClass;
        }
        return (Resource)outermostClass.refImmediateComposite();
    }
    
    protected static final Problem createProblem(Problem result, boolean isFatal, String message) {
        Problem problem = new Problem(isFatal, message);
        if (result == null) {
            return problem;
        } else if (isFatal) {
            problem.setNext(result);
            return problem;
        } else {
            //problem.setNext(result.getNext());
            //result.setNext(problem);
            
            // [TODO] performance
            Problem p = result;
            while (p.getNext() != null)
                p = p.getNext();
            p.setNext(problem);
            return result;
        }
    }
    
    /** Costs 5 progress steps */
    protected void collectUsages(Map classResMap, Map elementMap, RefObject jmiObject) {
        // [PENDING]
        /*
        Collection usages;
        fireProgressListenerStep();

        if (jmiObject instanceof JavaClass) {
            usages = cp.getClassUsages((JavaClass) jmiObject);
        } else {
            usages = cp.getFeatureUsages((ClassFeature) jmiObject);
        }

        fireProgressListenerStep();
        fireProgressListenerStep();
        
        for (Iterator it = usages.iterator(); it.hasNext();) {
            Object element = it.next();
            Iterator elementIt = null;
            FeatureReference ref = null;

            if (element instanceof FeatureReference) {
                ref = (FeatureReference) element;
                elementIt = ref.getClients().iterator();
            } else {
                elementIt = Collections.singleton(element).iterator();
            }

            while (elementIt.hasNext()) {
                Object el = elementIt.next();
                ClassResource classResource;

                if (el instanceof ImportStatement) {
                    classResource = ((ImportStatement) el).getSourceFile();
                } else {
                    classResource = getClassResource((ClassFeature) el);
                }

                Set s = (Set) classResMap.get(classResource);
                if (s == null) {
                    s = new HashSet(1);
                    classResMap.put(classResource, s);
                }
                s.add(el);

                Set refSet = (Set) elementMap.get(el);
                if (refSet == null) {
                    refSet = new HashSet();
                    elementMap.put(el, refSet);
                }
                if (ref instanceof BehavioralReference) {
                    refSet.add(ref);
                } else {
                    refSet.add(jmiObject);
                }
            }
        }

        fireProgressListenerStep();
        fireProgressListenerStep();
         */
    }

    /** Collects usages of a given element. "Costs" 3 progress steps.
     */
    protected Problem collectElements(Collection elements, Map classResMap, Map elementMap) {
        // [PENDING]
        /*
        for (Iterator it = classResMap.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();

            JavaSource resource = (JavaSource) entry.getKey();
            Set s = (Set) classResMap.get(resource);

            ReferencePositions rp = null;
            try {
                rp = new ReferencePositions(resource, factory);
            } catch (DataObjectNotFoundException e) {
                return new Problem(true, e.getMessage());
            }

            for (Iterator it2 = s.iterator(); it2.hasNext();) {
                Object feature = it2.next();
                Set refs = (Set) elementMap.get(feature);
                HashMap methodMap = null;
                for (Iterator it3 = refs.iterator(); it3.hasNext();) {
                    Object ref = it3.next();
                    if (ref instanceof BehavioralReference) {
                        if (methodMap == null) methodMap = new HashMap();
                        String name = ((BehavioralReference) ref).getSymbolName();
                        Set methods = (Set) methodMap.get(name);
                        if (methods == null) {
                            methods = new HashSet();
                            methodMap.put(name, methods);
                        }
                        methods.add(ref);
                    } else {
                        elements.addAll(rp.getIDRefs(feature, ref));
                    }
                }
                if (methodMap != null) {
                    for (Iterator it3 = methodMap.values().iterator(); it3.hasNext();) {
                        elements.addAll(rp.getIDRefs(feature, it3.next()));
                    }
                }
            }
        }

        fireProgressListenerStep();
        fireProgressListenerStep();
        fireProgressListenerStep();

         */
        return null;
    }
    
    public static PositionBounds getPositionBounds(RefObject element) {
        return JavaMetamodel.getManager().getElementPosition
            ((org.netbeans.jmi.javamodel.Element) element);
    }
    
    protected volatile boolean cancelRequest = false;
    protected volatile Iterator referencesIterator;
    public void cancelPrepare() {
        cancelRequest = true;
        if (referencesIterator != null && referencesIterator instanceof Cancellable) {
            ((Cancellable) referencesIterator).cancel();
        }
    }
    
    public static void setClassPath(Element el) {
        assert el != null;
        HashSet s = new HashSet();
        s.add(JavaMetamodel.getManager().getFileObject(el.getResource()));
        JMManager.getTransactionMutex().setClassPath(
            ClassPathFactory.createClassPath(
                RefactoringClassPathImplementation.getCustom(s)
            )
        );
    }
    
    public static void setClassPath(Collection elements) {
        JavaMetamodel m = JavaMetamodel.getManager();
        HashSet s = new HashSet(elements.size());
        for (Iterator i = elements.iterator(); i.hasNext();) {
            Element el = (Element) i.next();
            s.add(m.getFileObject(el.getResource()));
        }
        JMManager.getTransactionMutex().setClassPath(
            ClassPathFactory.createClassPath(
                RefactoringClassPathImplementation.getCustom(s)
            )
        );
    }
}
... 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.