@@ -67,6 +67,13 @@ public class PackageCommand {
67
67
68
68
private static final String DEFAULT_PACKAGE_DISPLAYNAME = "(default package)" ;
69
69
70
+ private static final String REFERENCED_LIBRARIES_PATH = "REFERENCED_LIBRARIES_PATH" ;
71
+
72
+ private static final String REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries" ;
73
+
74
+ private static final ContainerNode REFERENCED_LIBRARIES_CONTAINER = new ContainerNode (REFERENCED_LIBRARIES_CONTAINER_NAME , REFERENCED_LIBRARIES_PATH ,
75
+ NodeKind .CONTAINER , IClasspathEntry .CPE_CONTAINER );
76
+
70
77
private static final Gson gson = new GsonBuilder ().registerTypeAdapterFactory (new CollectionTypeAdapterFactory ())
71
78
.registerTypeAdapterFactory (new EnumTypeAdapterFactory ()).create ();
72
79
@@ -97,7 +104,6 @@ public static List<PackageNode> getChildren(List<Object> arguments, IProgressMon
97
104
throw new IllegalArgumentException ("Should have at least one arugment for getChildren" );
98
105
}
99
106
PackageParams params = gson .fromJson (gson .toJson (arguments .get (0 )), PackageParams .class );
100
-
101
107
BiFunction <PackageParams , IProgressMonitor , List <PackageNode >> loader = commands .get (params .getKind ());
102
108
if (loader == null ) {
103
109
throw new CoreException (new Status (IStatus .ERROR , JdtlsExtActivator .PLUGIN_ID , String .format ("Unknown classpath item type: %s" , params .getKind ())));
@@ -144,7 +150,13 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
144
150
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot ) packageFragment .getParent ();
145
151
PackageNode rootNode = null ;
146
152
if (typeRoot instanceof IClassFile ) {
147
- rootNode = new PackageRootNode (pkgRoot .getElementName (), pkgRoot .getPath ().toPortableString (), NodeKind .PACKAGEROOT , pkgRoot .getKind ());
153
+ IClasspathEntry entry = pkgRoot .getRawClasspathEntry ();
154
+ // Process Referenced Variable
155
+ if (entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
156
+ rootNode = getNodeFromClasspathVariable (entry );
157
+ } else {
158
+ rootNode = new PackageRootNode (pkgRoot .getElementName (), pkgRoot .getPath ().toPortableString (), NodeKind .PACKAGEROOT , pkgRoot .getKind ());
159
+ }
148
160
} else {
149
161
rootNode = new PackageRootNode (ExtUtils .removeProjectSegment (typeRoot .getJavaProject ().getElementName (), pkgRoot .getPath ()).toPortableString (),
150
162
pkgRoot .getPath ().toPortableString (), NodeKind .PACKAGEROOT , pkgRoot .getKind ());
@@ -154,8 +166,13 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
154
166
if (typeRoot instanceof IClassFile ) {
155
167
IClasspathEntry entry = pkgRoot .getRawClasspathEntry ();
156
168
IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), typeRoot .getJavaProject ());
157
- PackageNode containerNode = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), NodeKind .CONTAINER ,
158
- entry .getEntryKind ());
169
+ PackageNode containerNode = null ;
170
+ if (entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
171
+ containerNode = REFERENCED_LIBRARIES_CONTAINER ;
172
+ } else {
173
+ containerNode = new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), NodeKind .CONTAINER ,
174
+ entry .getEntryKind ());
175
+ }
159
176
result .add (containerNode );
160
177
}
161
178
result .add (rootNode );
@@ -174,36 +191,80 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
174
191
*/
175
192
private static List <PackageNode > getContainers (PackageParams query , IProgressMonitor pm ) {
176
193
IJavaProject javaProject = getJavaProject (query .getProjectUri ());
177
-
178
194
if (javaProject != null ) {
179
195
try {
180
196
IClasspathEntry [] references = javaProject .getRawClasspath ();
181
- return Arrays .stream (references ).map (entry -> {
182
- try {
183
- entry = JavaCore .getResolvedClasspathEntry (entry );
184
- IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
185
- // HACK: There is an initialization issue for the first container.
186
- if (container == null ) {
187
- container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
188
- }
189
-
190
- if (container != null ) {
191
- return new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), NodeKind .CONTAINER ,
192
- entry .getEntryKind ());
193
- }
194
- } catch (CoreException e ) {
195
- // Ignore it
196
- }
197
- return null ;
198
- }).filter (containerNode -> containerNode != null ).collect (Collectors .toList ());
197
+ List <PackageNode > result = Arrays .stream (references )
198
+ .filter (entry -> entry .getEntryKind () != IClasspathEntry .CPE_LIBRARY && entry .getEntryKind () != IClasspathEntry .CPE_VARIABLE )
199
+ .map (entry -> getNodeFromClasspathEntry (entry , javaProject , NodeKind .CONTAINER )).filter (containerNode -> containerNode != null )
200
+ .collect (Collectors .toList ());
201
+ boolean isReferencedLibrariesExist = Arrays .stream (references )
202
+ .anyMatch (entry -> entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE );
203
+ if (isReferencedLibrariesExist ) {
204
+ result .add (REFERENCED_LIBRARIES_CONTAINER );
205
+ }
206
+ return result ;
199
207
} catch (CoreException e ) {
200
208
JdtlsExtActivator .logException ("Problem load project library " , e );
201
209
}
202
210
}
203
211
return Collections .emptyList ();
204
212
}
205
213
214
+ /**
215
+ * Get the correspond node of classpath, it may be container or a package root
216
+ *
217
+ * @param classpathEntry
218
+ * classpath entry
219
+ * @param javaProject
220
+ * correspond java project
221
+ * @param nodeKind
222
+ * could be CONTAINER or PACKAGEROOT(for referenced libraries)
223
+ * @return correspond PackageNode of classpath entry
224
+ */
225
+ private static PackageNode getNodeFromClasspathEntry (IClasspathEntry classpathEntry , IJavaProject javaProject , NodeKind nodeKind ) {
226
+ try {
227
+ IClasspathEntry entry = JavaCore .getResolvedClasspathEntry (classpathEntry );
228
+ IClasspathContainer container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
229
+ // HACK: There is an initialization issue for the first container.
230
+ if (container == null ) {
231
+ container = JavaCore .getClasspathContainer (entry .getPath (), javaProject );
232
+ }
233
+ if (container != null ) {
234
+ switch (nodeKind ) {
235
+ case CONTAINER :
236
+ return new ContainerNode (container .getDescription (), container .getPath ().toPortableString (), nodeKind , entry .getEntryKind ());
237
+ case PACKAGEROOT :
238
+ // Use package name as package root name
239
+ String [] pathSegments = container .getPath ().segments ();
240
+ return new PackageRootNode (pathSegments [pathSegments .length - 1 ], container .getPath ().toPortableString (), nodeKind ,
241
+ IPackageFragmentRoot .K_BINARY );
242
+ default :
243
+ return null ;
244
+ }
245
+ }
246
+ } catch (CoreException e ) {
247
+ JdtlsExtActivator .logException ("Problems when convert classpath entry to package node " , e );
248
+ }
249
+ return null ;
250
+ }
251
+
252
+ /**
253
+ * Get correspond node of referenced variable
254
+ *
255
+ * @param classpathEntry
256
+ * referenced cariable's classpath entry
257
+ * @return correspond package node
258
+ */
259
+ private static PackageNode getNodeFromClasspathVariable (IClasspathEntry classpathEntry ) {
260
+ IClasspathEntry entry = JavaCore .getResolvedClasspathEntry (classpathEntry );
261
+ String name = classpathEntry .getPath ().toPortableString ();
262
+ String path = entry .getPath ().toPortableString ();
263
+ return new PackageRootNode (name , path , NodeKind .PACKAGEROOT , IPackageFragmentRoot .K_BINARY );
264
+ }
265
+
206
266
private static List <PackageNode > getPackageFragmentRoots (PackageParams query , IProgressMonitor pm ) {
267
+ ArrayList <PackageNode > children = new ArrayList <>();
207
268
IJavaProject javaProject = getJavaProject (query .getProjectUri ());
208
269
209
270
if (javaProject != null ) {
@@ -217,7 +278,6 @@ private static List<PackageNode> getPackageFragmentRoots(PackageParams query, IP
217
278
}
218
279
}
219
280
if (containerEntry != null ) {
220
- ArrayList <PackageNode > children = new ArrayList <>();
221
281
IPackageFragmentRoot [] packageFragmentRoots = javaProject .findPackageFragmentRoots (containerEntry );
222
282
for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots ) {
223
283
String displayName = fragmentRoot .getElementName ();
@@ -232,6 +292,16 @@ private static List<PackageNode> getPackageFragmentRoots(PackageParams query, IP
232
292
}
233
293
}
234
294
return children ;
295
+ } else if (query .getPath ().equals (REFERENCED_LIBRARIES_PATH )) {
296
+ // Process referenced libraries
297
+ List <PackageNode > referLibs = Arrays .stream (references ).filter (entry -> entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY )
298
+ .map (classpath -> getNodeFromClasspathEntry (classpath , javaProject , NodeKind .PACKAGEROOT )).filter (entry -> entry != null )
299
+ .collect (Collectors .toList ());
300
+ List <PackageNode > referVariables = Arrays .stream (references ).filter (entry -> entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE )
301
+ .map (classpath -> getNodeFromClasspathVariable (classpath )).filter (entry -> entry != null ).collect (Collectors .toList ());
302
+ children .addAll (referLibs );
303
+ children .addAll (referVariables );
304
+ return children ;
235
305
}
236
306
} catch (CoreException e ) {
237
307
JdtlsExtActivator .logException ("Problem load project JAR entries " , e );
@@ -243,9 +313,7 @@ private static List<PackageNode> getPackageFragmentRoots(PackageParams query, IP
243
313
244
314
private static List <PackageNode > getPackages (PackageParams query , IProgressMonitor pm ) {
245
315
IJavaProject javaProject = getJavaProject (query .getProjectUri ());
246
-
247
316
if (javaProject != null ) {
248
-
249
317
try {
250
318
IPackageFragmentRoot packageRoot = javaProject .findPackageFragmentRoot (Path .fromPortableString (query .getRootPath ()));
251
319
if (packageRoot == null ) {
@@ -274,7 +342,7 @@ private static List<PackageNode> getRootTypes(PackageParams query, IProgressMoni
274
342
if (packageFragment != null ) {
275
343
IJavaElement [] types = packageFragment .getChildren ();
276
344
Object [] nonJavaResources = packageFragment .getNonJavaResources ();
277
- List <PackageNode > rootTypeNodes = Arrays .stream (types ).filter (typeRoot -> !typeRoot .getElementName ().contains ("$" )).map (typeRoot -> {
345
+ List <PackageNode > rootTypeNodes = Arrays .stream (types ).filter (typeRoot -> !typeRoot .getElementName ().contains ("$" )).map (typeRoot -> {
278
346
PackageNode item = new TypeRootNode (typeRoot .getElementName (), typeRoot .getPath ().toPortableString (), NodeKind .TYPEROOT ,
279
347
typeRoot instanceof IClassFile ? TypeRootNode .K_BINARY : TypeRootNode .K_SOURCE );
280
348
if (typeRoot instanceof ICompilationUnit ) {
@@ -289,20 +357,20 @@ private static List<PackageNode> getRootTypes(PackageParams query, IProgressMoni
289
357
}
290
358
// when .java files and other .properties files are mixed up
291
359
rootTypeNodes .addAll (
292
- Arrays .stream (nonJavaResources ).filter (resource -> resource instanceof IFile || resource instanceof JarEntryFile ).map (resource -> {
293
- if (resource instanceof IFile ) {
294
- IFile file = (IFile ) resource ;
295
- PackageNode item = new PackageNode (file .getName (), file .getFullPath ().toPortableString (), NodeKind .FILE );
296
- item .setUri (JDTUtils .getFileURI (file ));
297
- return item ;
298
- } else {
299
- JarEntryFile file = (JarEntryFile ) resource ;
300
- PackageNode entry = new PackageNode (file .getName (), file .getFullPath ().toPortableString (), NodeKind .FILE );
301
- entry .setUri (ExtUtils .toUri ((JarEntryFile ) resource ));
302
- return entry ;
303
- }
304
-
305
- }).collect (Collectors .toList ()));
360
+ Arrays .stream (nonJavaResources ).filter (resource -> resource instanceof IFile || resource instanceof JarEntryFile ).map (resource -> {
361
+ if (resource instanceof IFile ) {
362
+ IFile file = (IFile ) resource ;
363
+ PackageNode item = new PackageNode (file .getName (), file .getFullPath ().toPortableString (), NodeKind .FILE );
364
+ item .setUri (JDTUtils .getFileURI (file ));
365
+ return item ;
366
+ } else {
367
+ JarEntryFile file = (JarEntryFile ) resource ;
368
+ PackageNode entry = new PackageNode (file .getName (), file .getFullPath ().toPortableString (), NodeKind .FILE );
369
+ entry .setUri (ExtUtils .toUri ((JarEntryFile ) resource ));
370
+ return entry ;
371
+ }
372
+
373
+ }).collect (Collectors .toList ()));
306
374
return rootTypeNodes ;
307
375
}
308
376
} catch (CoreException e ) {
0 commit comments