3636import org .eclipse .jdt .core .IType ;
3737import org .eclipse .jdt .core .JavaCore ;
3838import org .eclipse .jdt .core .JavaModelException ;
39+ import org .eclipse .jdt .internal .compiler .classfmt .ClassFileConstants ;
40+ import org .eclipse .jdt .internal .compiler .impl .CompilerOptions ;
3941import org .eclipse .jdt .internal .core .JrtPackageFragmentRoot ;
4042import org .eclipse .jdt .ls .core .internal .JDTUtils ;
4143import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
@@ -68,9 +70,11 @@ public class PackageNode {
6870 public static final String REFERENCED_LIBRARIES_PATH = "REFERENCED_LIBRARIES_PATH" ;
6971 private static final String REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries" ;
7072 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 ,
7275 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 ,
7478 REFERENCED_LIBRARIES_PATH , NodeKind .CONTAINER , IClasspathEntry .CPE_CONTAINER );
7579
7680 /**
@@ -85,6 +89,8 @@ public class PackageNode {
8589 */
8690 private static final String UNMANAGED_FOLDER_NATURE_ID = "org.eclipse.jdt.ls.core.unmanagedFolder" ;
8791
92+ private static final String MAX_SOURCE_VERSION = "MaxSourceVersion" ;
93+
8894 /**
8995 * The name of the PackageNode.
9096 */
@@ -164,7 +170,8 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
164170 return null ;
165171 }
166172 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 );
168175 projectNode .setUri (ProjectUtils .getProjectRealFolder (proj ).toFile ().toURI ().toString ());
169176 try {
170177 List <String > natureIds = new ArrayList <>(Arrays .asList (proj .getDescription ().getNatureIds ()));
@@ -173,6 +180,10 @@ public static PackageNode createNodeForProject(IJavaElement javaElement) {
173180 projectNode .setMetaDataValue (UNMANAGED_FOLDER_INNER_PATH , proj .getLocationURI ().toString ());
174181 }
175182 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 );
176187 } catch (CoreException e ) {
177188 // do nothing
178189 }
@@ -201,7 +212,8 @@ public static PackageNode createNodeForResource(IResource resource) {
201212 }
202213
203214 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 );
205217 fragmentNode .setHandlerIdentifier (packageFragment .getHandleIdentifier ());
206218 if (packageFragment .getResource () != null ) {
207219 fragmentNode .setUri (packageFragment .getResource ().getLocationURI ().toString ());
@@ -215,16 +227,19 @@ public static PackageNode createNodeForVirtualContainer(IPackageFragmentRoot pkg
215227 IClasspathEntry entry = pkgRoot .getRawClasspathEntry ();
216228 IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), pkgRoot .getJavaProject ());
217229 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 ) {
219232 containerNode = REFERENCED_LIBRARIES_CONTAINER ;
220233 } 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 ());
222236 }
223237 return containerNode ;
224238
225239 }
226240
227- public static PackageRootNode createNodeForPackageFragmentRoot (IPackageFragmentRoot pkgRoot ) throws JavaModelException {
241+ public static PackageRootNode createNodeForPackageFragmentRoot (IPackageFragmentRoot pkgRoot )
242+ throws JavaModelException {
228243 PackageRootNode node ;
229244 String displayName = pkgRoot .getElementName ();
230245 boolean isSourcePath = pkgRoot .getKind () == IPackageFragmentRoot .K_SOURCE ;
@@ -271,14 +286,16 @@ public static PackageRootNode createNodeForPackageFragmentRoot(IPackageFragmentR
271286 * Get the correspond node of classpath, it may be container or a package root.
272287 *
273288 * @param classpathEntry
274- * classpath entry
289+ * classpath entry
275290 * @param javaProject
276- * correspond java project
291+ * correspond java project
277292 * @param nodeKind
278- * could be CONTAINER or PACKAGEROOT(for referenced libraries)
293+ * could be CONTAINER or PACKAGEROOT(for referenced
294+ * libraries)
279295 * @return correspond PackageNode of classpath entry
280296 */
281- public static PackageNode createNodeForClasspathEntry (IClasspathEntry classpathEntry , IJavaProject javaProject , NodeKind nodeKind ) {
297+ public static PackageNode createNodeForClasspathEntry (IClasspathEntry classpathEntry , IJavaProject javaProject ,
298+ NodeKind nodeKind ) {
282299 try {
283300 IClasspathEntry entry = JavaCore .getResolvedClasspathEntry (classpathEntry );
284301 IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
@@ -289,17 +306,18 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
289306 if (container != null ) {
290307 PackageNode node = null ;
291308 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 ());
293311 final URI containerURI = ExtUtils .getContainerURI (javaProject , container );
294312 node .setUri (containerURI != null ? containerURI .toString () : null );
295313 } else if (nodeKind == NodeKind .PACKAGEROOT ) { // ClasspathEntry for referenced jar files
296314 // Use package name as package root name
297315 String [] pathSegments = container .getPath ().segments ();
298316 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 );
303321 }
304322 return node ;
305323 }
@@ -310,7 +328,8 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
310328 }
311329
312330 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 );
314333
315334 try {
316335 if (type .isEnum ()) {
@@ -332,7 +351,7 @@ public static PackageNode createNodeForPrimaryType(IType type) {
332351 * Get correspond node of referenced variable.
333352 *
334353 * @param classpathEntry
335- * referenced variable's classpath entry
354+ * referenced variable's classpath entry
336355 * @return correspond package node
337356 */
338357 public static PackageRootNode createNodeForClasspathVariable (IClasspathEntry classpathEntry ) {
0 commit comments