36
36
import org .eclipse .jdt .core .IType ;
37
37
import org .eclipse .jdt .core .JavaCore ;
38
38
import org .eclipse .jdt .core .JavaModelException ;
39
+ import org .eclipse .jdt .internal .compiler .classfmt .ClassFileConstants ;
40
+ import org .eclipse .jdt .internal .compiler .impl .CompilerOptions ;
39
41
import org .eclipse .jdt .internal .core .JrtPackageFragmentRoot ;
40
42
import org .eclipse .jdt .ls .core .internal .JDTUtils ;
41
43
import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
@@ -68,9 +70,11 @@ public class PackageNode {
68
70
public static final String REFERENCED_LIBRARIES_PATH = "REFERENCED_LIBRARIES_PATH" ;
69
71
private static final String REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries" ;
70
72
private static final String IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries (Read-only)" ;
71
- public static final ContainerNode REFERENCED_LIBRARIES_CONTAINER = new ContainerNode (REFERENCED_LIBRARIES_CONTAINER_NAME , REFERENCED_LIBRARIES_PATH ,
73
+ public static final ContainerNode REFERENCED_LIBRARIES_CONTAINER = new ContainerNode (
74
+ REFERENCED_LIBRARIES_CONTAINER_NAME , REFERENCED_LIBRARIES_PATH ,
72
75
NodeKind .CONTAINER , IClasspathEntry .CPE_CONTAINER );
73
- public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode (IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME ,
76
+ public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode (
77
+ IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME ,
74
78
REFERENCED_LIBRARIES_PATH , NodeKind .CONTAINER , IClasspathEntry .CPE_CONTAINER );
75
79
76
80
/**
@@ -85,6 +89,8 @@ public class PackageNode {
85
89
*/
86
90
private static final String UNMANAGED_FOLDER_NATURE_ID = "org.eclipse.jdt.ls.core.unmanagedFolder" ;
87
91
92
+ private static final String MAX_SOURCE_VERSION = "MaxSourceVersion" ;
93
+
88
94
/**
89
95
* The name of the PackageNode.
90
96
*/
@@ -164,7 +170,8 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
164
170
return null ;
165
171
}
166
172
IProject proj = javaElement .getJavaProject ().getProject ();
167
- PackageNode projectNode = new PackageNode (proj .getName (), proj .getFullPath ().toPortableString (), NodeKind .PROJECT );
173
+ PackageNode projectNode = new PackageNode (proj .getName (), proj .getFullPath ().toPortableString (),
174
+ NodeKind .PROJECT );
168
175
projectNode .setUri (ProjectUtils .getProjectRealFolder (proj ).toFile ().toURI ().toString ());
169
176
try {
170
177
List <String > natureIds = new ArrayList <>(Arrays .asList (proj .getDescription ().getNatureIds ()));
@@ -173,6 +180,10 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
173
180
projectNode .setMetaDataValue (UNMANAGED_FOLDER_INNER_PATH , proj .getLocationURI ().toString ());
174
181
}
175
182
projectNode .setMetaDataValue (NATURE_ID , natureIds );
183
+ String sourceVersion = javaElement .getJavaProject ().getOption (JavaCore .COMPILER_SOURCE , true );
184
+ int jdkLevel = (int ) (CompilerOptions .versionToJdkLevel (sourceVersion , true ) >>> 16 );
185
+ int majorVersion = Math .max (0 , jdkLevel - ClassFileConstants .MAJOR_VERSION_0 );
186
+ projectNode .setMetaDataValue (MAX_SOURCE_VERSION , majorVersion );
176
187
} catch (CoreException e ) {
177
188
// do nothing
178
189
}
@@ -201,7 +212,8 @@ public static PackageNode createNodeForResource(IResource resource) {
201
212
}
202
213
203
214
public static PackageNode createNodeForPackageFragment (IPackageFragment packageFragment ) {
204
- PackageNode fragmentNode = new PackageNode (packageFragment .getElementName (), packageFragment .getPath ().toPortableString (), NodeKind .PACKAGE );
215
+ PackageNode fragmentNode = new PackageNode (packageFragment .getElementName (),
216
+ packageFragment .getPath ().toPortableString (), NodeKind .PACKAGE );
205
217
fragmentNode .setHandlerIdentifier (packageFragment .getHandleIdentifier ());
206
218
if (packageFragment .getResource () != null ) {
207
219
fragmentNode .setUri (packageFragment .getResource ().getLocationURI ().toString ());
@@ -215,16 +227,19 @@ public static PackageNode createNodeForVirtualContainer(IPackageFragmentRoot pkg
215
227
IClasspathEntry entry = pkgRoot .getRawClasspathEntry ();
216
228
IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), pkgRoot .getJavaProject ());
217
229
PackageNode containerNode = null ;
218
- if (entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
230
+ if (entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY
231
+ || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
219
232
containerNode = REFERENCED_LIBRARIES_CONTAINER ;
220
233
} else {
221
- containerNode = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), NodeKind .CONTAINER , entry .getEntryKind ());
234
+ containerNode = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (),
235
+ NodeKind .CONTAINER , entry .getEntryKind ());
222
236
}
223
237
return containerNode ;
224
238
225
239
}
226
240
227
- public static PackageRootNode createNodeForPackageFragmentRoot (IPackageFragmentRoot pkgRoot ) throws JavaModelException {
241
+ public static PackageRootNode createNodeForPackageFragmentRoot (IPackageFragmentRoot pkgRoot )
242
+ throws JavaModelException {
228
243
PackageRootNode node ;
229
244
String displayName = pkgRoot .getElementName ();
230
245
boolean isSourcePath = pkgRoot .getKind () == IPackageFragmentRoot .K_SOURCE ;
@@ -271,14 +286,16 @@ public static PackageRootNode createNodeForPackageFragmentRoot(IPackageFragmentR
271
286
* Get the correspond node of classpath, it may be container or a package root.
272
287
*
273
288
* @param classpathEntry
274
- * classpath entry
289
+ * classpath entry
275
290
* @param javaProject
276
- * correspond java project
291
+ * correspond java project
277
292
* @param nodeKind
278
- * could be CONTAINER or PACKAGEROOT(for referenced libraries)
293
+ * could be CONTAINER or PACKAGEROOT(for referenced
294
+ * libraries)
279
295
* @return correspond PackageNode of classpath entry
280
296
*/
281
- public static PackageNode createNodeForClasspathEntry (IClasspathEntry classpathEntry , IJavaProject javaProject , NodeKind nodeKind ) {
297
+ public static PackageNode createNodeForClasspathEntry (IClasspathEntry classpathEntry , IJavaProject javaProject ,
298
+ NodeKind nodeKind ) {
282
299
try {
283
300
IClasspathEntry entry = JavaCore .getResolvedClasspathEntry (classpathEntry );
284
301
IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
@@ -289,17 +306,18 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
289
306
if (container != null ) {
290
307
PackageNode node = null ;
291
308
if (nodeKind == NodeKind .CONTAINER ) {
292
- node = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), nodeKind , entry .getEntryKind ());
309
+ node = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (),
310
+ nodeKind , entry .getEntryKind ());
293
311
final URI containerURI = ExtUtils .getContainerURI (javaProject , container );
294
312
node .setUri (containerURI != null ? containerURI .toString () : null );
295
313
} else if (nodeKind == NodeKind .PACKAGEROOT ) { // ClasspathEntry for referenced jar files
296
314
// Use package name as package root name
297
315
String [] pathSegments = container .getPath ().segments ();
298
316
node = new PackageRootNode (
299
- pathSegments [pathSegments .length - 1 ],
300
- container .getPath ().toPortableString (),
301
- container .getPath ().toFile ().toURI ().toString (),
302
- nodeKind , IPackageFragmentRoot .K_BINARY );
317
+ pathSegments [pathSegments .length - 1 ],
318
+ container .getPath ().toPortableString (),
319
+ container .getPath ().toFile ().toURI ().toString (),
320
+ nodeKind , IPackageFragmentRoot .K_BINARY );
303
321
}
304
322
return node ;
305
323
}
@@ -310,7 +328,8 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
310
328
}
311
329
312
330
public static PackageNode createNodeForPrimaryType (IType type ) {
313
- PackageNode primaryTypeNode = new PackageNode (type .getElementName (), type .getPath ().toPortableString (), NodeKind .PRIMARYTYPE );
331
+ PackageNode primaryTypeNode = new PackageNode (type .getElementName (), type .getPath ().toPortableString (),
332
+ NodeKind .PRIMARYTYPE );
314
333
315
334
try {
316
335
if (type .isEnum ()) {
@@ -332,7 +351,7 @@ public static PackageNode createNodeForPrimaryType(IType type) {
332
351
* Get correspond node of referenced variable.
333
352
*
334
353
* @param classpathEntry
335
- * referenced variable's classpath entry
354
+ * referenced variable's classpath entry
336
355
* @return correspond package node
337
356
*/
338
357
public static PackageRootNode createNodeForClasspathVariable (IClasspathEntry classpathEntry ) {
0 commit comments