11/*******************************************************************************
2- * Copyright (c) 2017 Microsoft Corporation and others.
2+ * Copyright (c) 2018 Microsoft Corporation and others.
33 * All rights reserved. This program and the accompanying materials
44 * are made available under the terms of the Eclipse Public License v1.0
55 * which accompanies this distribution, and is available at
@@ -61,7 +61,7 @@ public class PackageCommand {
6161 static {
6262 commands = new HashMap <>();
6363 commands .put (NodeKind .PROJECT , PackageCommand ::getContainers );
64- commands .put (NodeKind .CONTAINER , PackageCommand ::getJars );
64+ commands .put (NodeKind .CONTAINER , PackageCommand ::getPackageFragmentRoots );
6565 commands .put (NodeKind .JAR , PackageCommand ::getPackages );
6666 commands .put (NodeKind .PACKAGE , PackageCommand ::getClassfiles );
6767 commands .put (NodeKind .Folder , PackageCommand ::getFolderChildren );
@@ -73,8 +73,8 @@ public class PackageCommand {
7373 * @param arguments
7474 * List of the arguments which contain two entries to get class path
7575 * children: the first entry is the query target node type
76- * {@link NodeKind} and the second one is the query instance
77- * of type {@link PackageParams}
76+ * {@link NodeKind} and the second one is the query instance of type
77+ * {@link PackageParams}
7878 * @return the found ClasspathNode list
7979 * @throws CoreException
8080 */
@@ -86,8 +86,7 @@ public static List<PackageNode> getChildren(List<Object> arguments, IProgressMon
8686
8787 BiFunction <PackageParams , IProgressMonitor , List <PackageNode >> loader = commands .get (params .getKind ());
8888 if (loader == null ) {
89- throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID ,
90- String .format ("Unknown classpath item type: %s" , params .getKind ())));
89+ throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID , String .format ("Unknown classpath item type: %s" , params .getKind ())));
9190 }
9291 List <PackageNode > result = loader .apply (params , pm );
9392 return result ;
@@ -102,30 +101,34 @@ private static List<PackageNode> getContainers(PackageParams query, IProgressMon
102101 if (javaProject != null ) {
103102 try {
104103 IClasspathEntry [] references = javaProject .getRawClasspath ();
105- return Arrays .stream (references )
106- . map ( entry -> {
107- try {
108- entry = JavaCore .getResolvedClasspathEntry (entry );
109- IClasspathContainer container = JavaCore . getClasspathContainer ( entry . getPath (),
110- javaProject );
111- if ( container != null ) {
112- PackageNode containerNode = new PackageNode ( container . getDescription (),
113- container . getPath (). toPortableString (), NodeKind . CONTAINER );
114- return containerNode ;
104+ return Arrays .stream (references ). map ( entry -> {
105+ try {
106+ entry = JavaCore . getResolvedClasspathEntry ( entry );
107+ IClasspathContainer container = JavaCore .getClasspathContainer (entry . getPath (), javaProject );
108+ if ( container != null ) {
109+ if ( entry . getEntryKind () == IClasspathEntry . CPE_SOURCE ) {
110+ IPackageFragmentRoot [] packageFragmentRoots = javaProject . findPackageFragmentRoots ( entry );
111+ for ( IPackageFragmentRoot fragmentRoot : packageFragmentRoots ) {
112+ String rootName = ExtUtils . removeProjectSegment ( javaProject . getElementName (), fragmentRoot . getPath ()). toPortableString ( );
113+ return new PackageNode ( rootName , fragmentRoot . getPath (). toPortableString (), NodeKind . JAR ) ;
115114 }
116- } catch ( CoreException e ) {
117- // Ignore it
115+ } else {
116+ return new PackageNode ( container . getDescription (), container . getPath (). toPortableString (), NodeKind . CONTAINER );
118117 }
119- return null ;
120- }).filter (containerNode -> containerNode != null ).collect (Collectors .toList ());
118+ }
119+ } catch (CoreException e ) {
120+ // Ignore it
121+ }
122+ return null ;
123+ }).filter (containerNode -> containerNode != null ).collect (Collectors .toList ());
121124 } catch (CoreException e ) {
122125 JavaLanguageServerPlugin .logException ("Problem load project library " , e );
123126 }
124127 }
125128 return Collections .emptyList ();
126129 }
127130
128- private static List <PackageNode > getJars (PackageParams query , IProgressMonitor pm ) {
131+ private static List <PackageNode > getPackageFragmentRoots (PackageParams query , IProgressMonitor pm ) {
129132 IJavaProject javaProject = getJavaProject (query .getProjectUri ());
130133
131134 if (javaProject != null ) {
@@ -142,8 +145,7 @@ private static List<PackageNode> getJars(PackageParams query, IProgressMonitor p
142145 ArrayList <PackageNode > children = new ArrayList <>();
143146 IPackageFragmentRoot [] packageFragmentRoots = javaProject .findPackageFragmentRoots (containerEntry );
144147 for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots ) {
145- PackageNode node = new PackageNode (fragmentRoot .getElementName (),
146- fragmentRoot .getPath ().toPortableString (), NodeKind .JAR );
148+ PackageNode node = new PackageNode (fragmentRoot .getElementName (), fragmentRoot .getPath ().toPortableString (), NodeKind .JAR );
147149 children .add (node );
148150 if (fragmentRoot instanceof JrtPackageFragmentRoot ) {
149151 node .setModuleName (fragmentRoot .getModuleDescription ().getElementName ());
@@ -165,11 +167,9 @@ private static List<PackageNode> getPackages(PackageParams query, IProgressMonit
165167 if (javaProject != null ) {
166168
167169 try {
168- IPackageFragmentRoot packageRoot = javaProject
169- .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
170+ IPackageFragmentRoot packageRoot = javaProject .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
170171 if (packageRoot == null ) {
171- throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID ,
172- String .format ("No package root found for %s" , query .getPath ())));
172+ throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID , String .format ("No package root found for %s" , query .getPath ())));
173173 }
174174 Object [] result = getPackageFragmentRootContent (packageRoot , pm );
175175 return convertToClasspathNode (result );
@@ -184,21 +184,17 @@ private static List<PackageNode> getClassfiles(PackageParams query, IProgressMon
184184 IJavaProject javaProject = getJavaProject (query .getProjectUri ());
185185 if (javaProject != null ) {
186186 try {
187- IPackageFragmentRoot packageRoot = javaProject
188- .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
187+ IPackageFragmentRoot packageRoot = javaProject .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
189188 if (packageRoot == null ) {
190- throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID ,
191- String .format ("No package root found for %s" , query .getPath ())));
189+ throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID , String .format ("No package root found for %s" , query .getPath ())));
192190 }
193191 IPackageFragment packageFragment = packageRoot .getPackageFragment (query .getPath ());
194192 if (packageFragment != null ) {
195193 IJavaElement [] classFiles = packageFragment .getChildren ();
196- return Arrays .stream (classFiles ).filter (classFile -> !classFile .getElementName ().contains ("$" ))
197- .map (classFile -> {
198- PackageNode item = new PackageNode (classFile .getElementName (),
199- classFile .getPath ().toPortableString (), NodeKind .CLASSFILE );
200- return item ;
201- }).collect (Collectors .toList ());
194+ return Arrays .stream (classFiles ).filter (classFile -> !classFile .getElementName ().contains ("$" )).map (classFile -> {
195+ PackageNode item = new PackageNode (classFile .getElementName (), classFile .getPath ().toPortableString (), NodeKind .CLASSFILE );
196+ return item ;
197+ }).collect (Collectors .toList ());
202198
203199 }
204200 } catch (CoreException e ) {
@@ -212,11 +208,9 @@ private static List<PackageNode> getFolderChildren(PackageParams query, IProgres
212208 IJavaProject javaProject = getJavaProject (query .getProjectUri ());
213209 if (javaProject != null ) {
214210 try {
215- IPackageFragmentRoot packageRoot = javaProject
216- .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
211+ IPackageFragmentRoot packageRoot = javaProject .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
217212 if (packageRoot == null ) {
218- throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID ,
219- String .format ("No package root found for %s" , query .getPath ())));
213+ throw new CoreException (new Status (IStatus .ERROR , JavaLanguageServerPlugin .PLUGIN_ID , String .format ("No package root found for %s" , query .getPath ())));
220214 }
221215 // jar file and folders
222216 Object [] resources = packageRoot .getNonJavaResources ();
@@ -240,8 +234,7 @@ private static List<PackageNode> getFolderChildren(PackageParams query, IProgres
240234 return Collections .emptyList ();
241235 }
242236
243- private static Object [] getPackageFragmentRootContent (IPackageFragmentRoot root , IProgressMonitor pm )
244- throws CoreException {
237+ private static Object [] getPackageFragmentRootContent (IPackageFragmentRoot root , IProgressMonitor pm ) throws CoreException {
245238 ArrayList <Object > result = new ArrayList <>();
246239 for (IJavaElement child : root .getChildren ()) {
247240 IPackageFragment fragment = (IPackageFragment ) child ;
@@ -263,8 +256,7 @@ private static List<PackageNode> convertToClasspathNode(Object[] rootContent) th
263256 for (Object root : rootContent ) {
264257 if (root instanceof IPackageFragment ) {
265258 IPackageFragment packageFragment = (IPackageFragment ) root ;
266- PackageNode entry = new PackageNode (((IPackageFragment ) root ).getElementName (),
267- packageFragment .getPath ().toPortableString (), NodeKind .PACKAGE );
259+ PackageNode entry = new PackageNode (((IPackageFragment ) root ).getElementName (), packageFragment .getPath ().toPortableString (), NodeKind .PACKAGE );
268260 result .add (entry );
269261 } else if (root instanceof IClassFile ) {
270262 IClassFile classFile = (IClassFile ) root ;
@@ -284,11 +276,9 @@ private static List<PackageNode> convertToClasspathNode(Object[] rootContent) th
284276
285277 private static PackageNode getJarEntryResource (JarEntryResource resource ) {
286278 if (resource instanceof JarEntryDirectory ) {
287- return new PackageNode (resource .getName (), resource .getFullPath ().toPortableString (),
288- NodeKind .Folder );
279+ return new PackageNode (resource .getName (), resource .getFullPath ().toPortableString (), NodeKind .Folder );
289280 } else if (resource instanceof JarEntryFile ) {
290- PackageNode entry = new PackageNode (resource .getName (), resource .getFullPath ().toPortableString (),
291- NodeKind .FILE );
281+ PackageNode entry = new PackageNode (resource .getName (), resource .getFullPath ().toPortableString (), NodeKind .FILE );
292282 entry .setUri (ExtUtils .toUri ((JarEntryFile ) resource ));
293283 return entry ;
294284 }
0 commit comments