Collection<MavenClassSearchResult> (Set<MavenArtifactInfo> infos, String pattern, int maxResult) { if (pattern.isEmpty() || pattern.equals("*")) { pattern = "^/(.*)$"; } else { pattern = pattern.replace(".", "/"); int lastDot = pattern.lastIndexOf("/"); String packagePattern = lastDot == -1 ? "" : (pattern.substring(0, lastDot) + "/"); String classNamePattern = lastDot == -1 ? pattern : pattern.substring(lastDot + 1); packagePattern = packagePattern.replaceAll("\\*", ".*?"); classNamePattern = classNamePattern.replaceAll("\\*", "[^/]*?"); pattern = packagePattern + classNamePattern; pattern = ".*?/" + pattern; pattern = "^(" + pattern + ")$"; } Pattern p; try { p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); } catch (PatternSyntaxException e) { return Collections.emptyList(); } Map<String, MavenClassSearchResult> result = new HashMap<>(); for (MavenArtifactInfo each : infos) { if (each.getClassNames() == null) continue; Matcher matcher = p.matcher(each.getClassNames()); while (matcher.find()) { String classFQName = matcher.group(1); classFQName = classFQName.replace("/", "."); classFQName = StringUtil.trimStart(classFQName, "."); String key = classFQName; MavenClassSearchResult classResult = result.get(key); if (classResult == null) { int pos = classFQName.lastIndexOf("."); MavenRepositoryArtifactInfo artifactInfo = new MavenRepositoryArtifactInfo( each.getGroupId(), each.getArtifactId(), Collections.singletonList(each.getVersion())); if (pos == -1) { result.put(key, new MavenClassSearchResult(artifactInfo, classFQName, "default package")); } else { result.put(key, new MavenClassSearchResult(artifactInfo, classFQName.substring(pos + 1), classFQName.substring(0, pos))); } } else { List<String> versions = ContainerUtil.append(ContainerUtil.map(classResult.getSearchResults().getItems(), i -> i.getVersion()), each.getVersion()); MavenRepositoryArtifactInfo artifactInfo = new MavenRepositoryArtifactInfo( each.getGroupId(), each.getArtifactId(), versions); result.put(key, new MavenClassSearchResult(artifactInfo, classResult.getClassName(), classResult.getPackageName())); } if (result.size() > maxResult) break; } } result.values().forEach(a -> Arrays.sort(a.getSearchResults().getItems(), Comparator.comparing(MavenDependencyCompletionItem::getVersion, VersionComparatorUtil.COMPARATOR) .reversed()) ); return result.values(); }