private static void searchInNestedClass(List<TestItem> resultList, SearchTestItemParams params) throws JavaModelException { final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri()); for (final IType type : compilationUnit.getAllTypes()) { if (type.getFullyQualifiedName().equals(params.getFullName())) { resultList.addAll(searchTestMethodsOfType(type)); } } }
private static Range getRange(ICompilationUnit typeRoot, IJavaElement element) throws JavaModelException { ISourceRange r = ((ISourceReference) element).getNameRange(); return JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength()); }
/** * Returns uri for a compilation unit * * @param cu * @return */ public static String toURI(ICompilationUnit cu) { return getFileURI(cu.getResource()); }
/** * Given the uri string returns a {@link IPackageFragement}. May return null if * it can not associate the uri with a package fragment. * * @param uriString * @return package fragment */ public static IPackageFragment resolvePackage(String uriString) { return resolvePackage(toURI(uriString)); }
/** * Given the uri returns a {@link ICompilationUnit}. * May return null if it can not associate the uri with a Java * file. * * @param uriString * @return compilation unit */ public static ICompilationUnit resolveCompilationUnit(String uriString) { return resolveCompilationUnit(toURI(uriString)); }
/** * Convenience method that combines {@link #resolveClassFile(String)} and * {@link #resolveCompilationUnit(String)}. * * @param uri * @return either a class file or compilation unit */ public static ITypeRoot resolveTypeRoot(String uriString) { URI uri = toURI(uriString); if (uri == null) { return null; } if (JDT_SCHEME.equals(uri.getScheme())) { return resolveClassFile(uri); } return resolveCompilationUnit(uri); }
public void handleChanged(DidChangeTextDocumentParams params) { String uri = params.getTextDocument().getUri(); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); range = JDTUtils.toRange(unit, 0, length);
/** * Creates location to the given offset and length for the class file. * * @param unit * @param offset * @param length * @return location * @throws JavaModelException */ public static Location toLocation(IClassFile classFile, int offset, int length) throws JavaModelException { String uriString = toUri(classFile); if (uriString != null) { Range range = toRange(classFile, offset, length); return new Location(uriString, range); } return null; }
/** * Creates location to the given offset and length for the compilation unit * * @param unit * @param offset * @param length * @return location or null * @throws JavaModelException */ public static Location toLocation(ICompilationUnit unit, int offset, int length) throws JavaModelException { return new Location(ResourceUtils.toClientUri(toURI(unit)), toRange(unit, offset, length)); }
public void handleClosed(DidCloseTextDocumentParams params) { String uri = params.getTextDocument().getUri(); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null) { return; toReconcile.remove(unit); if (JDTUtils.isDefaultProject(unit) || !JDTUtils.isOnClassPath(unit) || unit.getResource().isDerived()) { new DiagnosticsHandler(connection, unit).clearDiagnostics(); } else if (unit.hasUnsavedChanges()) { if (JDTUtils.isDefaultProject(unit)) { File f = new File(unit.getUnderlyingResource().getLocationURI()); if (!f.exists()) {
String uri = JDTUtils.getFileURI(file); if (JavaCore.isJavaLikeFileName(file.getName())) { ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
IJavaSearchScope.SOURCES); case PACKAGE: final IJavaElement packageElement = JDTUtils.resolvePackage(params.getUri()); return SearchEngine.createJavaSearchScope(new IJavaElement[] { packageElement }, IJavaSearchScope.SOURCES); case CLASS: case NESTED_CLASS: final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri()); final IType[] types = compilationUnit.getAllTypes(); for (final IType type : types) { final String className = fullName.substring(0, fullName.lastIndexOf("#")); final String methodName = fullName.substring(fullName.lastIndexOf("#") + 1); final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getUri()); final IType[] allTypes = unit.getAllTypes(); for (final IType type : allTypes) {
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isCodeLensEnabled()) { return Collections.emptyList(); } final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); IClassFile classFile = null; if (unit == null) { classFile = JDTUtils.resolveClassFile(uri); if (classFile == null) { return Collections.emptyList(); } } else { if (!unit.getResource().exists() || monitor.isCanceled()) { return Collections.emptyList(); } } try { ITypeRoot typeRoot = unit != null ? unit : classFile; IJavaElement[] elements = typeRoot.getChildren(); LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length); collectCodeLenses(typeRoot, elements, lenses, monitor); if (monitor.isCanceled()) { lenses.clear(); } return new ArrayList<>(lenses); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e); } return Collections.emptyList(); }
return; IResource resource = JDTUtils.isFolder(uriString) ? JDTUtils.findFolder(uriString) : JDTUtils.findFile(uriString); if (resource == null) { return; URL url = getUrl(formatterUrl); URI formatterUri = url.toURI(); URI uri = JDTUtils.toURI(uriString); if (uri != null && uri.equals(formatterUri) && JavaLanguageServerPlugin.getInstance().getProtocol() != null) { if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); IJavaElement[] elements = JDTUtils.findElementsAtSelection(unit, params.getPosition().getLine(), params.getPosition().getCharacter(), this.preferenceManager, monitor); if (elements == null || elements.length == 0) { return edit;
public void handleOpen(DidOpenTextDocumentParams params) { String uri = params.getTextDocument().getUri(); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null || unit.getResource() == null || unit.getResource().isDerived()) { return; if (isDefaultProject || !JDTUtils.isOnClassPath(unit)) { Severity severity = preferenceManager.getPreferences(project).getIncompleteClasspathSeverity(); String msg;
ITypeRoot typeRoot = JDTUtils.resolveTypeRoot(uri); if (typeRoot != null) { IJavaElement element = JDTUtils.findElementAtSelection(typeRoot, position.getLine(), position.getCharacter(), this.preferenceManager, monitor); if (REFERENCES_TYPE.equals(type)) { try {
private static void searchInPackage(List<TestItem> resultList, SearchTestItemParams params) throws JavaModelException { final IPackageFragment packageFragment = JDTUtils.resolvePackage(params.getUri()); for (final ICompilationUnit unit : packageFragment.getCompilationUnits()) { for (final IType type : unit.getTypes()) { resultList.add(TestItemUtils.constructTestItem(type, TestLevel.CLASS)); } } }
private Location computeDefinitionNavigation(ITypeRoot unit, int line, int column, IProgressMonitor monitor) { try { IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column, this.preferenceManager, monitor); if (element == null) { return null; } ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null) ) { return fixLocation(element, JDTUtils.toLocation(element), unit.getJavaProject()); } if (element instanceof IMember && ((IMember) element).getClassFile() != null) { return fixLocation(element, JDTUtils.toLocation(((IMember) element).getClassFile()), unit.getJavaProject()); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem computing definition for" + unit.getElementName(), e); } return null; }
String packageName = getPackageName(javaProject, uri); String fileName = path.getName(path.getNameCount() - 1).toString(); String packagePath = packageName.replace(PERIOD, PATH_SEPARATOR); if (!file.isLinked()) { try { createFolders(file.getParent(), monitor); file.createLink(uri, IResource.REPLACE, monitor); } catch (CoreException e) {