@Override public IProject[] getProjects() { return new IProject[] {fUnit.getJavaProject().getProject() }; } @Override
private void saveChanges(ICompilationUnit copy, CompilationUnit unit) throws JavaModelException, MalformedTreeException, BadLocationException { Document document= new Document(copy.getBuffer().getContents()); TextEdit edits = unit.rewrite(document, copy.getJavaProject().getOptions(true)); edits.apply(document); copy.getBuffer().setContents(document.get()); copy.save(null, false); }
private static Collection<String> getTopLevelTypeNames(ICompilationUnit iCompilationUnit) throws CoreException { IType[] types= iCompilationUnit.getTypes(); List<String> result= new ArrayList<>(types.length); for (int i= 0; i < types.length; i++) { result.add(types[i].getElementName()); } return result; }
public static IResource getResource(IJavaElement element){ if (element instanceof ICompilationUnit) return ((ICompilationUnit)element).getPrimary().getResource(); else return element.getResource(); }
private boolean canOverwrite(ICompilationUnit cu) { if (fDestination instanceof IPackageFragment){ IPackageFragment destination= (IPackageFragment)fDestination; return ! destination.equals(cu.getParent()) && destination.getCompilationUnit(cu.getElementName()).exists(); } else { return willOverwrite(ReorgUtils.getResource(cu)); } }
private IType[] getDestinationPackageTypes() throws JavaModelException { List<IType> types= new ArrayList<>(); if (fDestination.exists()) { ICompilationUnit[] cus= fDestination.getCompilationUnits(); for (int i= 0; i < cus.length; i++) { types.addAll(Arrays.asList(cus[i].getAllTypes())); } } return types.toArray(new IType[types.size()]); }
public static boolean isOnClassPath(ICompilationUnit unit) { if (unit != null && unit.getJavaProject() != null && !unit.getJavaProject().getProject().equals(JavaLanguageServerPlugin.getProjectsManager().getDefaultProject())) { return unit.getJavaProject().isOnClasspath(unit); } return false; }
private static void indexCompilationUnitSync(ICompilationUnit icu) { IJavaProject jp = icu.getJavaProject(); IndexManager im = JavaModelManager.getIndexManager(); IPath containerPath = jp.getProject().getFullPath(); SourceElementParser parser = im.getSourceElementParser(jp, null/*requestor will be set by indexer*/); im.addSource((IFile) icu.getResource(), containerPath, parser); AbstractJob.waitForJobFamily(containerPath.toString()); }
private static boolean isPrimaryType(IType type) { String cuName = type.getCompilationUnit().getElementName(); String typeName = type.getElementName(); return type.getDeclaringType() == null && JavaCore.removeJavaLikeExtension(cuName).equals(typeName); }
private boolean isOnClassPath(ICompilationUnit element) { IJavaProject project= element.getJavaProject(); if (project == null || !project.exists()) return false; return project.isOnClasspath(element); }
private static IResource getResource(IJavaElement element){ if (element.getElementType() == IJavaElement.COMPILATION_UNIT) return ((ICompilationUnit) element).getResource(); else if (element instanceof IOpenable) return element.getResource(); else return null; } }
private static IType convertToEnclosingOrPrimaryType(IJavaElement element) throws JavaModelException { if (element instanceof IType) return (IType)element; IType result= (IType)element.getAncestor(IJavaElement.TYPE); if (result != null) return result; if (element instanceof ICompilationUnit) return ((ICompilationUnit)element).findPrimaryType(); if (element instanceof IOrdinaryClassFile) return ((IOrdinaryClassFile)element).getType(); return null; }
private static IType findContextType(ICompilationUnit icu, int offset) throws JavaModelException { IJavaElement element = icu.getElementAt(offset); if (element == null) { return null; } if (element.getElementType() == IJavaElement.TYPE) { return (IType) element; } return (IType) element.getAncestor(IJavaElement.TYPE); } }
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException { // ensure cu is consistent (noop if already consistent) cu.makeConsistent(this.progressMonitor); this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); ASTNode node = ((JavaElement) elementToRemove).findNode(astCU); if (node == null) Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$ AST ast = astCU.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); rewriter.remove(node, null); TextEdit edits = rewriter.rewriteAST(); applyTextEdit(cu, edits); }
protected static BodyDeclaration createPlaceholderForProtectedTypeDeclaration(final BodyDeclaration bodyDeclaration, final CompilationUnit declaringCuNode, final ICompilationUnit declaringCu, final ASTRewrite rewrite, final boolean removeIndentation) throws JavaModelException { String text= null; try { final ASTRewrite rewriter= ASTRewrite.create(bodyDeclaration.getAST()); ModifierRewrite.create(rewriter, bodyDeclaration).setVisibility(Modifier.PROTECTED, null); final ITrackedNodePosition position= rewriter.track(bodyDeclaration); final IDocument document= new Document(declaringCu.getBuffer().getText(declaringCuNode.getStartPosition(), declaringCuNode.getLength())); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.UPDATE_REGIONS); text= document.get(position.getStartPosition(), position.getLength()); } catch (BadLocationException exception) { text= getNewText(bodyDeclaration, declaringCu, removeIndentation); } return (BodyDeclaration) rewrite.createStringPlaceholder(text, ASTNode.TYPE_DECLARATION); }
private String getEnclosingTypeName(int codeAssistOffset, ICompilationUnit compilationUnit) throws JavaModelException { IJavaElement element= compilationUnit.getElementAt(codeAssistOffset); if (element == null) return null; element= element.getAncestor(IJavaElement.TYPE); if (element == null) return null; return element.getElementName(); }
private boolean isPrimaryType() { String cuName= fType.getCompilationUnit().getElementName(); String typeName= fType.getElementName(); return Checks.isTopLevel(fType) && JavaCore.removeJavaLikeExtension(cuName).equals(typeName); }
public static NLSLine[] scan(ICompilationUnit cu) throws JavaModelException, BadLocationException, InvalidInputException { IJavaProject javaProject= cu.getJavaProject(); IScanner scanner= null; if (javaProject != null) { String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true); scanner= ToolFactory.createScanner(true, true, true, sourceLevel, complianceLevel); } else { scanner= ToolFactory.createScanner(true, true, false, true); } return scan(scanner, cu.getBuffer().getCharacters()); }
/** * Returns the element at the given position in the given type */ protected static IJavaElement getElementAt(IType type, int pos) throws JavaModelException { if (type.isBinary()) { return type.getClassFile().getElementAt(pos); } return type.getCompilationUnit().getElementAt(pos); }
public static boolean isImplicitImport(String qualifier, ICompilationUnit cu) { if ("java.lang".equals(qualifier)) { //$NON-NLS-1$ return true; } String packageName= cu.getParent().getElementName(); if (qualifier.equals(packageName)) { return true; } String typeName= JavaCore.removeJavaLikeExtension(cu.getElementName()); String mainTypeName= JavaModelUtil.concatenateName(packageName, typeName); return qualifier.equals(mainTypeName); }