Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
View | Details | Raw Unified | Return to bug 226657
Collapse All | Expand All

(-)a/api.java/apichanges.xml (+17 lines)
 Lines 76-81    Link Here 
76
<!-- ACTUAL CHANGES BEGIN HERE: -->
76
<!-- ACTUAL CHANGES BEGIN HERE: -->
77
77
78
<changes>
78
<changes>
79
        <change id="JDK8-profiles">
80
            <api name="queries"/>
81
            <summary>The <code>SourceLevelQuery</code> supports JDK 8 profiles</summary>
82
            <version major="1" minor="45"/>
83
            <date day="5" month="3" year="2013"/>
84
            <author login="tzezula"/>
85
            <compatibility semantic="compatible" source="compatible" binary="compatible"/>
86
            <description>
87
                <p>
88
                    The JDK 8 provides three limited profiles (compact1, compact2, compact3) in addition
89
                    to the full JDK. Each profile specifies a specific set of Java API packages and
90
                    contains all of the APIs of the smaller profile. The new API allows a client of the
91
                    <code>SourceLevelQuery</code> to query the required profile.
92
                </p>
93
            </description>
94
            <issue number="226657"/>
95
        </change>
79
        <change id="source-level-synonyms">
96
        <change id="source-level-synonyms">
80
            <api name="queries"/>
97
            <api name="queries"/>
81
            <summary>The <code>SourceLevelQuery</code> supports source level synonyms</summary>
98
            <summary>The <code>SourceLevelQuery</code> supports source level synonyms</summary>
(-)a/api.java/manifest.mf (-1 / +1 lines)
 Lines 1-6    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.44
3
OpenIDE-Module-Specification-Version: 1.45
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/java/queries/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/java/queries/Bundle.properties
5
AutoUpdate-Show-In-Client: false
5
AutoUpdate-Show-In-Client: false
6
6
(-)a/api.java/src/org/netbeans/api/java/queries/SourceLevelQuery.java (+20 lines)
 Lines 180-185    Link Here 
180
        }
180
        }
181
181
182
        /**
182
        /**
183
         * Returns the name of the required profile.
184
         * @return a name of the required profile or null if the profile is either unknown
185
         * or unsupported by actual source level.
186
         * <div class="nonnormative">
187
         * The JDK 8 provides three limited profiles (compact1, compact2, compact3) in addition
188
         * to the full JDK. Each profile specifies a specific set of Java API packages and
189
         * contains all of the APIs of the smaller profile, @see http://openjdk.java.net/jeps/161
190
         * </div>
191
         * @since 1.45
192
         */
193
        @CheckForNull
194
        public String getProfile() {
195
            final SourceLevelQueryImplementation2.Result delegate = getDelegate();
196
            if (!(delegate instanceof SourceLevelQueryImplementation2.Result2)) {
197
                return null;
198
            }
199
            return ((SourceLevelQueryImplementation2.Result2)delegate).getProfile();
200
        }
201
202
        /**
183
         * Add a listener to changes of source level.
203
         * Add a listener to changes of source level.
184
         * If {@link #supportsChanges} is false, the listener will never be notified
204
         * If {@link #supportsChanges} is false, the listener will never be notified
185
         * although {@link #getSourceLevel} may change from call to call.
205
         * although {@link #getSourceLevel} may change from call to call.
(-)a/api.java/src/org/netbeans/spi/java/queries/SourceLevelQueryImplementation2.java (+21 lines)
 Lines 107-110    Link Here 
107
         */
107
         */
108
        void removeChangeListener(@NonNull ChangeListener listener);
108
        void removeChangeListener(@NonNull ChangeListener listener);
109
    }
109
    }
110
111
    /**
112
     * Result of finding a source level and profile as well as the ability
113
     * to listen on changes.
114
     * @since 1.45
115
     */
116
    interface Result2 extends Result {
117
118
        /**
119
         * Returns the name of the required profile.
120
         * @return a name of the required profile or null if the profile is either unknown
121
         * or unsupported by actual source level.
122
         * <div class="nonnormative">
123
         * The JDK 8 provides three limited profiles (compact1, compact2, compact3) in addition
124
         * to the full JDK. Each profile specifies a specific set of Java API packages and
125
         * contains all of the APIs of the smaller profile, @see http://openjdk.java.net/jeps/161
126
         * </div>
127
         */
128
        @CheckForNull
129
        String getProfile();
130
    }
110
}
131
}
(-)a/java.api.common/nbproject/project.xml (-1 / +1 lines)
 Lines 29-35    Link Here 
29
                    <compile-dependency/>
29
                    <compile-dependency/>
30
                    <run-dependency>
30
                    <run-dependency>
31
                        <release-version>1</release-version>
31
                        <release-version>1</release-version>
32
                        <specification-version>1.30</specification-version>
32
                        <specification-version>1.45</specification-version>
33
                    </run-dependency>
33
                    </run-dependency>
34
                </dependency>
34
                </dependency>
35
                <dependency>
35
                <dependency>
(-)a/java.api.common/src/org/netbeans/modules/java/api/common/queries/SourceLevelQueryImpl.java (-23 / +2 lines)
 Lines 43-52    Link Here 
43
 */
43
 */
44
package org.netbeans.modules.java.api.common.queries;
44
package org.netbeans.modules.java.api.common.queries;
45
45
46
import org.netbeans.modules.java.api.common.util.CommonProjectUtils;
47
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
46
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
48
import org.netbeans.spi.project.support.ant.PropertyUtils;
49
import org.netbeans.spi.project.support.ant.EditableProperties;
50
import org.openide.filesystems.FileObject;
47
import org.openide.filesystems.FileObject;
51
48
52
/**
49
/**
 Lines 66-90    Link Here 
66
    
63
    
67
    @Override
64
    @Override
68
    public String getSourceLevel(FileObject javaFile) {
65
    public String getSourceLevel(FileObject javaFile) {
69
        return findSourceLevel(evaluator);
66
        return SourceLevelQueryImpl2.findSourceLevel(evaluator);
70
    }
67
    }    
71
72
    static String findSourceLevel (final PropertyEvaluator eval) {
73
        final String activePlatform = eval.getProperty("platform.active"); //NOI18N
74
        if (CommonProjectUtils.getActivePlatform(activePlatform) != null) {
75
            String sl = eval.getProperty("javac.source"); //NOI18N
76
            if (sl != null && sl.length() > 0) {
77
                return sl;
78
            }
79
            return null;
80
        }
81
82
        EditableProperties props = PropertyUtils.getGlobalProperties();
83
        String sl = props.get("default.javac.source"); //NOI18N
84
        if (sl != null && sl.length() > 0) {
85
            return sl;
86
        }
87
        return null;
88
    }
89
    
68
    
90
}
69
}
(-)a/java.api.common/src/org/netbeans/modules/java/api/common/queries/SourceLevelQueryImpl2.java (-4 / +55 lines)
 Lines 44-53    Link Here 
44
44
45
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeEvent;
46
import java.beans.PropertyChangeListener;
46
import java.beans.PropertyChangeListener;
47
import java.util.regex.Pattern;
47
import javax.swing.event.ChangeListener;
48
import javax.swing.event.ChangeListener;
48
import org.netbeans.api.annotations.common.NonNull;
49
import org.netbeans.api.annotations.common.NonNull;
50
import org.netbeans.modules.java.api.common.util.CommonProjectUtils;
49
import org.netbeans.spi.java.queries.SourceLevelQueryImplementation2;
51
import org.netbeans.spi.java.queries.SourceLevelQueryImplementation2;
52
import org.netbeans.spi.project.support.ant.EditableProperties;
50
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
53
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
54
import org.netbeans.spi.project.support.ant.PropertyUtils;
51
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileObject;
52
import org.openide.util.ChangeSupport;
56
import org.openide.util.ChangeSupport;
53
import org.openide.util.Parameters;
57
import org.openide.util.Parameters;
 Lines 58-63    Link Here 
58
 */
62
 */
59
class SourceLevelQueryImpl2 implements SourceLevelQueryImplementation2 {
63
class SourceLevelQueryImpl2 implements SourceLevelQueryImplementation2 {
60
    
64
    
65
    private static final String PLATFORM_ACTIVE = "platform.active";    //NOI18N
66
    private static final String JAVAC_SOURCE = "javac.source";  //NOI18N
67
    private static final String DEFAULT_SOURCE_LEVEL = "default.javac.source";  //NOI18N
68
    private static final String JAVAC_PROFILE = "javac.profile";    //NOI18N
69
    private static final Pattern SUPPORTS_PROFILES = Pattern.compile("(1\\.)?8");    //NOI18N
70
61
    private final PropertyEvaluator eval;
71
    private final PropertyEvaluator eval;
62
    private final Result result;
72
    private final Result result;
63
73
 Lines 72-78    Link Here 
72
        return this.result;
82
        return this.result;
73
    }
83
    }
74
84
75
    private class R implements Result, PropertyChangeListener {
85
    static String findSourceLevel (
86
            @NonNull final PropertyEvaluator eval) {
87
        final String activePlatform = eval.getProperty(PLATFORM_ACTIVE);
88
        if (CommonProjectUtils.getActivePlatform(activePlatform) != null) {
89
            String sl = eval.getProperty(JAVAC_SOURCE);
90
            if (sl != null && sl.length() > 0) {
91
                return sl;
92
            }
93
            return null;
94
        }
95
        final EditableProperties props = PropertyUtils.getGlobalProperties();
96
        String sl = props.get(DEFAULT_SOURCE_LEVEL);
97
        if (sl != null && sl.length() > 0) {
98
            return sl;
99
        }
100
        return null;
101
    }
102
103
    private static String findProfile(
104
            @NonNull final PropertyEvaluator eval) {
105
        if (supportsProfiles(findSourceLevel(eval))) {
106
            final String profile = eval.getProperty(JAVAC_PROFILE);
107
            if (profile != null && profile.length() > 0) {
108
                return profile;
109
            }
110
        }
111
        return null;
112
    }
113
114
    private static boolean supportsProfiles(
115
            @NonNull final String sl) {
116
        return sl != null &&
117
            SUPPORTS_PROFILES.matcher(sl).matches();
118
    }
119
120
    private class R implements Result2, PropertyChangeListener {
76
        
121
        
77
        private final ChangeSupport cs = new ChangeSupport(this);
122
        private final ChangeSupport cs = new ChangeSupport(this);
78
123
 Lines 83-89    Link Here 
83
128
84
        @Override
129
        @Override
85
        public String getSourceLevel() {
130
        public String getSourceLevel() {
86
            return SourceLevelQueryImpl.findSourceLevel(eval);
131
            return findSourceLevel(eval);
132
        }
133
134
        @Override
135
        public String getProfile() {
136
            return findProfile(eval);
87
        }
137
        }
88
138
89
        @Override
139
        @Override
 Lines 100-107    Link Here 
100
        public void propertyChange(PropertyChangeEvent evt) {
150
        public void propertyChange(PropertyChangeEvent evt) {
101
            final String name = evt.getPropertyName();
151
            final String name = evt.getPropertyName();
102
            if (name == null ||
152
            if (name == null ||
103
                "javac.source".equals(name) ||     //NOI18N
153
                JAVAC_SOURCE.equals(name) ||
104
                "platform.active".equals(name)) {  //NOI18N
154
                JAVAC_PROFILE.equals(name) ||
155
                PLATFORM_ACTIVE.equals(name)) {
105
                this.cs.fireChange();
156
                this.cs.fireChange();
106
            }
157
            }
107
        }
158
        }
(-)a/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/queries/SourceLevelQueryImplTest.java (-3 / +111 lines)
 Lines 55-64    Link Here 
55
import java.util.concurrent.atomic.AtomicInteger;
55
import java.util.concurrent.atomic.AtomicInteger;
56
import javax.swing.event.ChangeEvent;
56
import javax.swing.event.ChangeEvent;
57
import javax.swing.event.ChangeListener;
57
import javax.swing.event.ChangeListener;
58
import org.netbeans.api.annotations.common.NonNull;
59
import org.netbeans.api.annotations.common.NullAllowed;
58
import org.netbeans.api.java.classpath.ClassPath;
60
import org.netbeans.api.java.classpath.ClassPath;
59
import org.netbeans.api.java.platform.JavaPlatform;
61
import org.netbeans.api.java.platform.JavaPlatform;
60
import org.netbeans.api.java.platform.Specification;
62
import org.netbeans.api.java.platform.Specification;
61
import org.netbeans.api.java.queries.SourceLevelQuery;
62
import org.netbeans.api.project.Project;
63
import org.netbeans.api.project.Project;
63
import org.netbeans.api.project.ProjectManager;
64
import org.netbeans.api.project.ProjectManager;
64
import org.netbeans.junit.NbTestCase;
65
import org.netbeans.junit.NbTestCase;
 Lines 76-81    Link Here 
76
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
77
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
77
import org.openide.filesystems.FileUtil;
78
import org.openide.filesystems.FileUtil;
78
import org.openide.modules.SpecificationVersion;
79
import org.openide.modules.SpecificationVersion;
80
import org.openide.util.Mutex;
79
import org.openide.util.test.MockLookup;
81
import org.openide.util.test.MockLookup;
80
82
81
/**
83
/**
 Lines 100-105    Link Here 
100
        });
102
        });
101
    }
103
    }
102
104
105
    private static final String COMPACT1 = "compact1";  //NOI18N
106
    private static final String COMPACT2 = "compact2";  //NOI18N
107
    private static final String JDK_8 = "8";    //NOI18N
108
    private static final String JDK_8_ALIAS = "1.8";    //NOI18N
103
    private static final String JAVAC_SOURCE = "1.2";
109
    private static final String JAVAC_SOURCE = "1.2";
104
    private static final String DEFAULT_JAVAC_SOURCE = "17.2";
110
    private static final String DEFAULT_JAVAC_SOURCE = "17.2";
105
111
 Lines 136-142    Link Here 
136
    }
142
    }
137
143
138
144
139
    private void prepareProject(String platformName) throws IOException {
145
    private void prepareProject(
146
            @NonNull final String platformName) throws IOException {
147
        prepareProject(platformName, null, null);
148
    }
149
    private void prepareProject(
150
            @NonNull final String platformName,
151
            @NullAllowed final String sourceLevel,
152
            @NullAllowed final String profile) throws IOException {
140
        scratch = TestUtil.makeScratchDir(this);
153
        scratch = TestUtil.makeScratchDir(this);
141
        projdir = scratch.createFolder("proj");
154
        projdir = scratch.createFolder("proj");
142
        helper = ProjectGenerator.createProject(projdir, "test");
155
        helper = ProjectGenerator.createProject(projdir, "test");
 Lines 146-152    Link Here 
146
        EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
159
        EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
147
        props.setProperty("javac.source", "${def}");
160
        props.setProperty("javac.source", "${def}");
148
        props.setProperty("platform.active", platformName);
161
        props.setProperty("platform.active", platformName);
149
        props.setProperty("def", JAVAC_SOURCE);
162
        props.setProperty("def",
163
                sourceLevel != null ?
164
                sourceLevel :
165
                JAVAC_SOURCE);
166
        if (profile != null) {
167
            props.setProperty("javac.profile", profile);    //NOI18N
168
        }
150
        helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
169
        helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
151
        props = PropertyUtils.getGlobalProperties();
170
        props = PropertyUtils.getGlobalProperties();
152
        props.put("default.javac.source", DEFAULT_JAVAC_SOURCE);
171
        props.put("default.javac.source", DEFAULT_JAVAC_SOURCE);
 Lines 207-212    Link Here 
207
        assertEquals("1.7", result.getSourceLevel().toString());
226
        assertEquals("1.7", result.getSourceLevel().toString());
208
    }
227
    }
209
228
229
    public void testProfilesJDK8ProfileGiven() throws IOException {
230
        this.prepareProject(TEST_PLATFORM, JDK_8, COMPACT2);
231
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
232
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
233
        final SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
234
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
235
        assertEquals(COMPACT2, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
236
    }
237
238
    public void testProfilesJDK8AliasProfileGiven() throws IOException {
239
        this.prepareProject(TEST_PLATFORM, JDK_8_ALIAS, COMPACT2);
240
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
241
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
242
        final SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
243
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
244
        assertEquals(COMPACT2, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
245
    }
246
247
    public void testProfilesJDK8AliasProfileNotGiven() throws IOException {
248
        this.prepareProject(TEST_PLATFORM, JDK_8, null);
249
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
250
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
251
        final SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
252
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
253
        assertEquals(null, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
254
    }
255
256
    public void testProfilesOldJDKAliasProfileGiven() throws IOException {
257
        this.prepareProject(TEST_PLATFORM, JAVAC_SOURCE, COMPACT2);
258
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
259
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
260
        final SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
261
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
262
        assertEquals(null, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
263
    }
264
265
    public void testProfileChanges() throws Exception {
266
        prepareProject(TEST_PLATFORM, JDK_8, COMPACT1);
267
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
268
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
269
        SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
270
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
271
        assertEquals(COMPACT1, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
272
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
273
            @Override
274
            public Void run() throws Exception {
275
                final EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
276
                props.setProperty("javac.profile", COMPACT2);   //NOI18N
277
                helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
278
                ProjectManager.getDefault().saveProject(prj);
279
                return null;
280
            }
281
        });
282
        result = sourceLevelQuery.getSourceLevel(dummy);
283
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
284
        assertEquals(COMPACT2, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
285
286
    }
287
288
    public void testProfileListening() throws Exception {
289
        prepareProject(TEST_PLATFORM, JDK_8, COMPACT1);
290
        final FileObject dummy = projdir.createData("Dummy.java");  //NOI18N
291
        final SourceLevelQueryImplementation2 sourceLevelQuery = QuerySupport.createSourceLevelQuery2(eval);
292
        final SourceLevelQueryImplementation2.Result result = sourceLevelQuery.getSourceLevel(dummy);
293
        assertTrue(result instanceof SourceLevelQueryImplementation2.Result2);
294
        assertEquals(COMPACT1, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
295
        final AtomicInteger eventCount = new AtomicInteger();
296
        final ChangeListener listener = new ChangeListener() {
297
            @Override
298
            public void stateChanged(ChangeEvent e) {
299
                eventCount.getAndIncrement();
300
            }
301
        };
302
        result.addChangeListener(listener);
303
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
304
            @Override
305
            public Void run() throws Exception {
306
                final EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
307
                props.setProperty("javac.profile", COMPACT2);   //NOI18N
308
                helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
309
                ProjectManager.getDefault().saveProject(prj);
310
                return null;
311
            }
312
        });
313
        assertEquals(1, eventCount.get());
314
        assertEquals(COMPACT2, ((SourceLevelQueryImplementation2.Result2)result).getProfile());
315
316
    }
317
210
    private static class TestPlatformProvider implements JavaPlatformProvider {
318
    private static class TestPlatformProvider implements JavaPlatformProvider {
211
319
212
        private JavaPlatform platform;
320
        private JavaPlatform platform;

Return to bug 226657
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo