This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 93767
Collapse All | Expand All

(-)api/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.api.java/1
2
OpenIDE-Module: org.netbeans.api.java/1
3
OpenIDE-Module-Specification-Version: 1.11
3
OpenIDE-Module-Specification-Version: 1.12
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/java/classpath/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/java/classpath/Bundle.properties
5
5
(-)api/src/org/netbeans/api/java/queries/BinaryForSourceQuery.java (+130 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.api.java.queries;
20
21
import java.net.URL;
22
import java.util.HashSet;
23
import java.util.Set;
24
import javax.swing.event.ChangeListener;
25
import org.netbeans.api.java.classpath.ClassPath;
26
import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
27
import org.openide.filesystems.FileObject;
28
import org.openide.filesystems.FileStateInvalidException;
29
import org.openide.filesystems.URLMapper;
30
import org.openide.util.Exceptions;
31
import org.openide.util.Lookup;
32
33
/**
34
 *
35
 * The query is used for finding binaries for sources.
36
 * @see BinaryForSourceQueryImplementation
37
 * @since org.netbeans.api.java/1 1.12
38
 * @author Tomas Zezula
39
 * 
40
 */
41
public final class BinaryForSourceQuery {
42
    
43
    private static final Lookup.Result<? extends BinaryForSourceQueryImplementation> implementations =
44
        Lookup.getDefault().lookupResult (BinaryForSourceQueryImplementation.class);
45
    
46
    /** Creates a new instance of BInaryForSOurceQuery */
47
    private BinaryForSourceQuery() {
48
    }
49
    
50
    /**
51
     * Returns the binary root for given source root.
52
     * @param sourceRoot the source path root.
53
     * @return a result object encapsulating the answer (never null)
54
     */
55
    public static Result findBinaryRoots (final URL sourceRoot) {
56
       assert sourceRoot != null;
57
       for (BinaryForSourceQueryImplementation impl : implementations.allInstances()) {
58
           BinaryForSourceQuery.Result result = impl.findBinaryRoots (sourceRoot);
59
           if (result != null) {
60
               return result;
61
           }
62
       }
63
       return new DefaultResult (sourceRoot);
64
    }
65
    
66
    /**
67
     * Result of finding binaries, encapsulating the answer as well as the
68
     * ability to listen to it.
69
     */
70
    public static interface Result {
71
        
72
        /**
73
         * Get the binary roots.         
74
         * @return array of roots of compiled classes (may be empty but not null)
75
         */
76
        URL[] getRoots();
77
        
78
        /**
79
         * Add a listener to changes in the roots.
80
         * @param l a listener to add
81
         */
82
        void addChangeListener(ChangeListener l);
83
        
84
        /**
85
         * Remove a listener to changes in the roots.
86
         * @param l a listener to remove
87
         */
88
        void removeChangeListener(ChangeListener l);
89
    }        
90
    
91
    private static class DefaultResult implements Result {
92
        
93
        private final URL sourceRoot;
94
        
95
        DefaultResult (final URL sourceRoot) {
96
            this.sourceRoot = sourceRoot;
97
        }
98
    
99
        public URL[] getRoots() {
100
            FileObject fo = URLMapper.findFileObject(sourceRoot);
101
            if (fo == null) {
102
                return new URL[0];
103
            }
104
            ClassPath exec = ClassPath.getClassPath(fo, ClassPath.EXECUTE);
105
            if (exec == null) {
106
                return new URL[0];
107
            }           
108
            Set<URL> result = new HashSet<URL>();
109
            for (ClassPath.Entry e : exec.entries()) {
110
                FileObject[] roots = SourceForBinaryQuery.findSourceRoots(e.getURL()).getRoots();
111
                for (FileObject root : roots) {
112
                    try {
113
                        if (sourceRoot.equals (root.getURL())) {
114
                            result.add (e.getURL());
115
                        }
116
                    } catch (FileStateInvalidException fsie) {
117
                        Exceptions.printStackTrace(fsie);
118
                    }
119
                }
120
            }
121
            return result.toArray(new URL[result.size()]);
122
        }
123
124
        public void addChangeListener(ChangeListener l) {            
125
        }
126
127
        public void removeChangeListener(ChangeListener l) {            
128
        }
129
    }
130
}
(-)api/src/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation.java (+57 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.spi.java.queries;
20
21
import java.net.URL;
22
import org.netbeans.api.java.queries.BinaryForSourceQuery;
23
24
/**
25
 * Information about where binaries (classfiles) corresponding to 
26
 * Java sources can be found.
27
 * @since org.netbeans.api.java/1 1.12
28
 * @see BinaryForSourceQuery
29
 * @author Tomas Zezula
30
 */
31
public interface BinaryForSourceQueryImplementation {
32
    
33
    /**
34
     * Returns the binary root(s) for a given source root.
35
     * <p>
36
     * The returned BinaryForSourceQuery.Result must be a singleton. It means that for
37
     * repeated calling of this method with the same recognized root the method has to
38
     * return the same instance of BinaryForSourceQuery.Result.<br>
39
     * The typical implemantation of the findBinaryRoots contains 3 steps:
40
     * <ol>
41
     * <li>Look into the cache if there is already a result for the root, if so return it</li>
42
     * <li>Check if the sourceRoot is recognized, if not return null</li>
43
     * <li>Create a new BinaryForSourceQuery.Result for the sourceRoot, put it into the cache
44
     * and return it.</li>
45
     * </ol>
46
     * </p>
47
     * <p>
48
     * Any absolute URL may be used but typically it will use the <code>file</code>
49
     * protocol for directory entries and <code>jar</code> protocol for JAR entries
50
     * (e.g. <samp>jar:file:/tmp/foo.jar!/</samp>).
51
     * </p>
52
     * @param sourceRoot the source path root
53
     * @return a result object encapsulating the answer or null if the sourceRoot is not recognized
54
     */
55
    public BinaryForSourceQuery.Result findBinaryRoots(URL sourceRoot);
56
    
57
}
(-)api/test/unit/src/org/netbeans/api/java/queries/BinaryForSourceQueryTest.java (+179 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.netbeans.api.java.queries;
21
22
import java.io.File;
23
import java.io.IOException;
24
import java.net.URL;
25
import java.util.HashMap;
26
import java.util.Map;
27
import javax.swing.event.ChangeListener;
28
import junit.framework.Assert;
29
import org.netbeans.api.java.classpath.ClassPath;
30
import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
31
import org.netbeans.junit.NbTestCase;
32
import org.netbeans.spi.java.classpath.ClassPathProvider;
33
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
35
import org.openide.filesystems.FileObject;
36
import org.openide.filesystems.FileUtil;
37
import org.openide.filesystems.URLMapper;
38
import org.openide.util.Lookup;
39
import org.openide.util.lookup.Lookups;
40
import org.openide.util.lookup.ProxyLookup;
41
42
/**
43
 * @author Tomas Zezula
44
 */
45
public class BinaryForSourceQueryTest extends NbTestCase {
46
    
47
    private FileObject srcRoot1;
48
    private FileObject srcRoot2;
49
    private FileObject binaryRoot2;
50
    
51
    
52
    
53
    static {
54
        BinaryForSourceQueryTest.class.getClassLoader().setDefaultAssertionStatus(true);
55
        System.setProperty("org.openide.util.Lookup", BinaryForSourceQueryTest.Lkp.class.getName());
56
        Assert.assertEquals(BinaryForSourceQueryTest.Lkp.class, Lookup.getDefault().getClass());
57
    }   
58
    
59
    public static class Lkp extends ProxyLookup {
60
        
61
        private static Lkp DEFAULT;
62
        
63
        public Lkp () {
64
            Assert.assertNull(DEFAULT);
65
            DEFAULT = this;
66
            ClassLoader l = Lkp.class.getClassLoader();
67
            this.setLookups(
68
                 new Lookup [] {
69
                    Lookups.fixed (CPProvider.getDefault(), SFBQImpl.getDefault()),
70
                    Lookups.metaInfServices(l),
71
                    Lookups.singleton(l),
72
            });
73
        }
74
        
75
        public void setLookupsWrapper(Lookup... l) {
76
            setLookups(l);
77
        }        
78
    }
79
    
80
81
    public BinaryForSourceQueryTest (String n) {
82
        super(n);
83
    }        
84
    
85
    @Override
86
    protected void setUp () throws IOException {
87
        this.clearWorkDir();
88
        File wd = this.getWorkDir();
89
        FileObject root = FileUtil.toFileObject(wd);
90
        assertNotNull(root);
91
        srcRoot1 = root.createFolder("src1");
92
        assertNotNull(srcRoot1);
93
        srcRoot2 = root.createFolder("src2");
94
        assertNotNull(srcRoot2);
95
        binaryRoot2 = root.createFolder("binary2");
96
        assertNotNull(binaryRoot2);       
97
        SFBQImpl.getDefault().register(srcRoot2.getURL(), binaryRoot2.getURL());
98
        CPProvider.getDefault().register(srcRoot2, ClassPath.SOURCE, ClassPathSupport.createClassPath(new FileObject[] {srcRoot2}));
99
        CPProvider.getDefault().register(srcRoot2, ClassPath.EXECUTE, ClassPathSupport.createClassPath(new FileObject[] {binaryRoot2}));
100
    }
101
    
102
    public void testQuery() throws Exception {
103
        BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(srcRoot1.getURL());
104
        assertEquals(0,result.getRoots().length);        
105
        result = BinaryForSourceQuery.findBinaryRoots(srcRoot2.getURL());
106
        assertEquals(1,result.getRoots().length);
107
        assertEquals(binaryRoot2.getURL(), result.getRoots()[0]);
108
    }
109
    
110
    private static class SFBQImpl implements SourceForBinaryQueryImplementation {        
111
        
112
        private static SFBQImpl instance;
113
        
114
        private final Map<URL,URL> data = new HashMap<URL,URL>();
115
        
116
        void register (URL source, URL binary) {
117
            data.put (binary,source);
118
        }
119
            
120
        public Result findSourceRoots(URL binaryRoot) {
121
            URL src = data.get (binaryRoot);
122
            if (src == null) {
123
                return null;
124
            }
125
            final FileObject fo = URLMapper.findFileObject(src);
126
            if (fo == null) {
127
                return null;
128
            }
129
            return new SourceForBinaryQuery.Result () {                
130
                public FileObject[] getRoots() {
131
                    return new FileObject[] {fo};
132
                }                
133
                public void addChangeListener (ChangeListener l) {}
134
                
135
                public void removeChangeListener (ChangeListener l) {}
136
            };
137
        }
138
        
139
        static synchronized SFBQImpl getDefault () {
140
            if (instance == null) {
141
                instance = new SFBQImpl ();
142
            }
143
            return instance;
144
        }
145
    }
146
    
147
    private static class CPProvider implements ClassPathProvider {
148
        
149
        private static CPProvider instance;
150
        
151
        private final Map<FileObject,Map<String,ClassPath>> data = new HashMap<FileObject,Map<String,ClassPath>>();
152
        
153
        void register (FileObject fo, String type, ClassPath cp) {
154
            Map<String,ClassPath> m = data.get (fo);
155
            if (m == null) {
156
                m = new HashMap<String,ClassPath>();
157
                data.put (fo,m);
158
            }
159
            m.put (type,cp);
160
        }
161
            
162
        public ClassPath findClassPath(FileObject file, String type) {
163
            Map<String,ClassPath> m = data.get (file);
164
            if (m == null) {
165
                return null;
166
            }
167
            return m.get (type);
168
        }
169
        
170
        public static  synchronized CPProvider getDefault () {
171
            if (instance == null) {
172
                instance = new CPProvider ();
173
            }
174
            return instance;
175
        }
176
    }
177
    
178
    
179
}
(-)j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProject.java (-2 / +4 lines)
Lines 13-19 Link Here
13
 * "Portions Copyrighted [year] [name of copyright owner]"
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
18
 */
19
19
Lines 49-54 Link Here
49
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
49
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
50
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesClientSupport;
50
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesClientSupport;
51
import org.netbeans.modules.java.j2seproject.jaxws.J2SEProjectJAXWSClientSupport;
51
import org.netbeans.modules.java.j2seproject.jaxws.J2SEProjectJAXWSClientSupport;
52
import org.netbeans.modules.java.j2seproject.queries.BinaryForSourceQueryImpl;
52
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesSupportProvider;
53
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesSupportProvider;
53
import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
54
import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
54
import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
55
import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
Lines 254-260 Link Here
254
            UILookupMergerSupport.createPrivilegedTemplatesMerger(),
255
            UILookupMergerSupport.createPrivilegedTemplatesMerger(),
255
            UILookupMergerSupport.createRecommendedTemplatesMerger(),
256
            UILookupMergerSupport.createRecommendedTemplatesMerger(),
256
            LookupProviderSupport.createSourcesMerger(),
257
            LookupProviderSupport.createSourcesMerger(),
257
            new J2SEPropertyEvaluatorImpl(evaluator())
258
            new J2SEPropertyEvaluatorImpl(evaluator()),
259
            new BinaryForSourceQueryImpl(this.sourceRoots, this.testRoots, this.helper, this.eval) //Does not use APH to get/put properties/cfgdata
258
        });
260
        });
259
        return LookupProviderSupport.createCompositeLookup(base, "Projects/org-netbeans-modules-java-j2seproject/Lookup"); //NOI18N
261
        return LookupProviderSupport.createCompositeLookup(base, "Projects/org-netbeans-modules-java-j2seproject/Lookup"); //NOI18N
260
    }
262
    }
(-)j2seproject/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImpl.java (+133 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.netbeans.modules.java.j2seproject.queries;
21
22
import java.beans.PropertyChangeEvent;
23
import java.beans.PropertyChangeListener;
24
import java.io.File;
25
import java.net.MalformedURLException;
26
import java.net.URL;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.concurrent.CopyOnWriteArrayList;
31
import javax.swing.event.ChangeEvent;
32
import javax.swing.event.ChangeListener;
33
import org.netbeans.api.java.queries.BinaryForSourceQuery;
34
import org.netbeans.api.java.queries.BinaryForSourceQuery.Result;
35
import org.netbeans.modules.java.j2seproject.SourceRoots;
36
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
37
import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
38
import org.netbeans.spi.project.support.ant.AntProjectHelper;
39
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
40
import org.openide.util.Exceptions;
41
42
/**
43
 *
44
 * @author Tomas Zezula
45
 */
46
public class BinaryForSourceQueryImpl implements BinaryForSourceQueryImplementation {        
47
    
48
    
49
    private final Map<URL,BinaryForSourceQuery.Result>  cache = new HashMap<URL,BinaryForSourceQuery.Result>();
50
    private final SourceRoots src;
51
    private final SourceRoots test;
52
    private final PropertyEvaluator eval;
53
    private final AntProjectHelper helper;
54
    
55
    /** Creates a new instance of BinaryForSourceQueryImpl */
56
    public BinaryForSourceQueryImpl(SourceRoots src, SourceRoots test, AntProjectHelper helper, PropertyEvaluator eval) {
57
        assert src != null;
58
        assert test != null;
59
        assert helper != null;
60
        assert eval != null;        
61
        this.src = src;
62
        this.test = test;
63
        this.eval = eval;
64
        this.helper = helper;
65
    }
66
    
67
    public Result findBinaryRoots(URL sourceRoot) {
68
        assert sourceRoot != null;
69
        BinaryForSourceQuery.Result result = cache.get(sourceRoot);
70
        if (result == null) {
71
            for (URL root : this.src.getRootURLs()) {
72
                if (root.equals(sourceRoot)) {
73
                    result = new R (J2SEProjectProperties.BUILD_CLASSES_DIR);
74
                    cache.put (sourceRoot,result);
75
                    break;
76
                }
77
            }
78
            for (URL root : this.test.getRootURLs()) {
79
                if (root.equals(sourceRoot)) {
80
                    result = new R (J2SEProjectProperties.BUILD_TEST_CLASSES_DIR);
81
                    cache.put (sourceRoot,result);
82
                    break;
83
                }
84
            }
85
        }
86
        return result;
87
    }
88
    
89
    class R implements BinaryForSourceQuery.Result, PropertyChangeListener {
90
        
91
        private final String propName;
92
        private final List<ChangeListener> listeners = new CopyOnWriteArrayList<ChangeListener>();
93
        
94
        R (final String propName) {
95
            assert propName != null;
96
            this.propName = propName;
97
            eval.addPropertyChangeListener(this);
98
        }
99
        
100
        public URL[] getRoots() {
101
            String val = eval.getProperty(propName);
102
            if (val != null) {                
103
                File f = helper.resolveFile(val);
104
                if (f != null) {
105
                    try {
106
                        return new URL[] {f.toURI().toURL()};
107
                    } catch (MalformedURLException e) {
108
                        Exceptions.printStackTrace(e);
109
                    }
110
                }
111
            }
112
            return new URL[0];
113
        }
114
115
        public void addChangeListener(ChangeListener l) {
116
            assert l != null;
117
            this.listeners.add (l);
118
        }
119
120
        public void removeChangeListener(ChangeListener l) {
121
            assert l != null;
122
            this.listeners.remove (l);
123
        }
124
125
        public void propertyChange(PropertyChangeEvent event) {
126
            ChangeEvent ce = new ChangeEvent (this);
127
            for (ChangeListener l : listeners) {
128
                l.stateChanged(ce);
129
            }
130
        }
131
}
132
133
}
(-)j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImplTest.java (+103 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.netbeans.modules.java.j2seproject.queries;
21
22
import java.io.IOException;
23
import java.util.Properties;
24
import org.netbeans.api.java.queries.BinaryForSourceQuery;
25
import org.netbeans.api.project.Project;
26
import org.netbeans.api.project.ProjectManager;
27
import org.netbeans.junit.NbTestCase;
28
import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
29
import org.openide.filesystems.FileObject;
30
import org.netbeans.api.project.TestUtil;
31
import org.netbeans.spi.project.support.ant.AntProjectHelper;
32
import org.openide.filesystems.FileUtil;
33
import org.openide.modules.SpecificationVersion;
34
import org.openide.util.Lookup;
35
36
/**
37
 * Tests for BinaryForSourceQueryImpl
38
 *
39
 * @author Tomas Zezula
40
 */
41
public class BinaryForSourceQueryImplTest extends NbTestCase {
42
    
43
    public BinaryForSourceQueryImplTest(String testName) {
44
        super(testName);
45
    }
46
    
47
    private FileObject scratch;
48
    private FileObject projdir;
49
    private FileObject sources;
50
    private FileObject buildClasses;
51
    private ProjectManager pm;
52
    private Project pp;
53
    AntProjectHelper helper;
54
    
55
    protected void setUp() throws Exception {
56
        super.setUp();
57
        TestUtil.setLookup(new Object[] {
58
            new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
59
            new org.netbeans.modules.java.project.ProjectSourceForBinaryQuery(),
60
            new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
61
        });
62
        Properties p = System.getProperties();
63
    }
64
65
    protected void tearDown() throws Exception {
66
        scratch = null;
67
        projdir = null;
68
        pm = null;
69
        TestUtil.setLookup(Lookup.EMPTY);
70
        super.tearDown();
71
    }
72
    
73
    
74
    private void prepareProject () throws IOException {
75
        scratch = TestUtil.makeScratchDir(this);
76
        projdir = scratch.createFolder("proj");        
77
        J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4"));   //NOI18N
78
        helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null);
79
        J2SEProjectGenerator.setDefaultSourceLevel(null);   //NOI18N
80
        pm = ProjectManager.getDefault();
81
        pp = pm.findProject(projdir);
82
        sources = projdir.getFileObject("src");
83
        FileObject fo = projdir.createFolder("build");
84
        buildClasses = fo.createFolder("classes");        
85
    }
86
    
87
    public void testBinaryForSourceQuery() throws Exception {
88
        this.prepareProject();
89
        FileObject folder = scratch.createFolder("SomeFolder");
90
        BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(folder.getURL());
91
        assertEquals("Non-project folder does not have any source folder", 0, result.getRoots().length);
92
        folder = projdir.createFolder("SomeFolderInProject");
93
        result = BinaryForSourceQuery.findBinaryRoots(folder.getURL());
94
        assertEquals("Project non build folder does not have any source folder", 0, result.getRoots().length);
95
        result = BinaryForSourceQuery.findBinaryRoots(sources.getURL());        
96
        assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
97
        assertEquals("Project build folder must have source folder",buildClasses.getURL(),result.getRoots()[0]);        
98
        assertEquals(BinaryForSourceQueryImpl.R.class, result.getClass());
99
        BinaryForSourceQuery.Result result2 = BinaryForSourceQuery.findBinaryRoots(sources.getURL());
100
        assertTrue (result == result2);
101
    }               
102
                    
103
}
(-)project/src/META-INF/services/org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation (+2 lines)
Added Link Here
1
org.netbeans.modules.java.project.ProjectBinaryForSourceQuery
2
#position=100
(-)project/src/org/netbeans/modules/java/project/ProjectBinaryForSourceQuery.java (+56 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
package org.netbeans.modules.java.project;
20
21
import java.net.URISyntaxException;
22
import java.net.URL;
23
import org.netbeans.api.java.queries.BinaryForSourceQuery.Result;
24
import org.netbeans.api.project.FileOwnerQuery;
25
import org.netbeans.api.project.Project;
26
import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
27
import org.openide.util.Exceptions;
28
29
/**
30
 * Finds binary roots corresponding to source roots.
31
 * Assumes an instance of BinaryForSourceQueryImplementation is in project's lookup.
32
 * @author Tomas Zezula
33
 */
34
public class ProjectBinaryForSourceQuery implements BinaryForSourceQueryImplementation {
35
    
36
    /** Creates a new instance of ProjectBinaryForSourceQuery */
37
    public ProjectBinaryForSourceQuery() {
38
    }
39
    
40
    public Result findBinaryRoots(URL sourceRoot) {
41
        try {
42
            Project p = FileOwnerQuery.getOwner(sourceRoot.toURI());
43
            if (p != null) {
44
                BinaryForSourceQueryImplementation impl = p.getLookup().
45
                    lookup(BinaryForSourceQueryImplementation.class);
46
                if (impl != null) {
47
                    return impl.findBinaryRoots(sourceRoot);
48
                }
49
            }
50
        } catch (URISyntaxException e) {
51
            Exceptions.printStackTrace(e);
52
        }
53
        return null;
54
    }
55
56
}

Return to bug 93767