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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.refactoring.classpath;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.api.java.classpath.GlobalPathRegistryEvent;

import org.netbeans.api.java.classpath.GlobalPathRegistryListener;

import org.netbeans.api.java.queries.SourceForBinaryQuery;
import org.netbeans.spi.java.classpath.ClassPathImplementation;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;

import org.openide.util.WeakListeners;




public class RefactoringClassPathImplementation implements ClassPathImplementation, GlobalPathRegistryListener, PropertyChangeListener {

    private List resourceCache;
    private PropertyChangeSupport support;
    private Collection fileSet;

    private RefactoringClassPathImplementation(Collection set) {
        this.support = new PropertyChangeSupport(this);
        this.fileSet = set;
    }

    public synchronized List  getResources() {
        if (this.resourceCache == null) {
            this.resourceCache = Collections.unmodifiableList(this.createResources());
        }
        return this.resourceCache;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        this.support.addPropertyChangeListener (listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        this.support.removePropertyChangeListener(listener);
    }
    
    public void pathsAdded(GlobalPathRegistryEvent event) {
        resetCache();
    }

    
    public void pathsRemoved(GlobalPathRegistryEvent event) {
        resetCache();
    }

    public void propertyChange (PropertyChangeEvent event) {
        resetCache();
    }

    private List createResources () {
        List result = new ArrayList ();
        Set covered = new HashSet ();
        GlobalPathRegistry regs = GlobalPathRegistry.getDefault();
        regs.addGlobalPathRegistryListener((GlobalPathRegistryListener)WeakListeners.create(GlobalPathRegistryListener.class, this, regs));
        assert regs != null : "GlobalPathRegistry.getDefault() returned null";                      //NOI18N
        
        computeDependencies();

        for (Iterator it = sources.iterator(); it.hasNext();) {
            ClassPath cp = (ClassPath) it.next ();
            for (Iterator et = cp.entries().iterator(); et.hasNext();) {
                ClassPath.Entry entry = (ClassPath.Entry) et.next();
                URL url = entry.getURL();
                assert url != null : "ClassPath.Entry.getURL() returned null";        //NOI18N
                covered.add (url);
                result.add (ClassPathSupport.createResource(url));
            }
            cp.addPropertyChangeListener((PropertyChangeListener)WeakListeners.create(PropertyChangeListener.class,this,cp));
        }
        addResources(boot,covered,result);
        addResources(compile,covered,result);
        return result;
    }
    
    private Set sources;
    private Set compile;
    private Set boot;
    
    private void computeDependencies() {
        if (fileSet.isEmpty()) {
            GlobalPathRegistry regs = GlobalPathRegistry.getDefault();
            sources = regs.getPaths(ClassPath.SOURCE);
            assert sources != null : "GlobalPathRegistry.getPath() for SOURCES returned null";          //NOI18N
            compile = regs.getPaths(ClassPath.COMPILE);
            assert compile != null : "GlobalPathRegistry.getPath() for COMPILE returned null";          //NOI18N
            boot = regs.getPaths(ClassPath.BOOT);
            assert boot != null : "GlobalPathRegistry.getPath() for BOOT returned null";                //NOI18N
        } else {
            OpenProjects.getDefault().addPropertyChangeListener((PropertyChangeListener)WeakListeners.create(PropertyChangeListener.class,this,OpenProjects.getDefault()));
            
            sources = new HashSet();
            compile = new HashSet();
            boot = new HashSet();
            for (Iterator it = getRelevantProjects().iterator();it.hasNext();) {
                Project p = (Project) it.next();
                Sources src = (Sources) p.getLookup().lookup(Sources.class);
                SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
                for(int i=0; i*/ set) {
        assert set != null;
        if (!(customInstance != null && set.equals(customInstance.fileSet))) {
            customInstance = new RefactoringClassPathImplementation (set);
        }
        return customInstance;
    }
    
    private synchronized static void resetCache () {        
        defaultInstance = null;
        customInstance = null;
    }
    
    private static RefactoringClassPathImplementation defaultInstance;
    private static RefactoringClassPathImplementation customInstance;

}
... 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.