Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ImportRewrite#addStaticImport()
The following examples show how to use
org.eclipse.jdt.core.dom.rewrite.ImportRewrite#addStaticImport() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoveCuUpdateCreator.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private void addStaticImport(ICompilationUnit movedUnit, IImportDeclaration importDecl, ImportRewrite rewrite) { String old = importDecl.getElementName(); int oldPackLength = movedUnit.getParent().getElementName().length(); StringBuilder result = new StringBuilder(fDestination.getElementName()); if (oldPackLength == 0) { result.append('.').append(old); } else if (result.length() == 0) { result.append(old.substring(oldPackLength + 1)); // cut "." } else { result.append(old.substring(oldPackLength)); } int index = result.lastIndexOf("."); //$NON-NLS-1$ if (index > 0 && index < result.length() - 1) { rewrite.addStaticImport(result.substring(0, index), result.substring(index + 1, result.length()), true); } }
Example 2
Source File: OrganizeImportsHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static void addImports(CompilationUnit root, ICompilationUnit unit, String[] favourites, ImportRewrite importRewrite, AST ast, ASTRewrite astRewrite, SimpleName node, boolean isMethod) throws JavaModelException { String name = node.getIdentifier(); String[] imports = SimilarElementsRequestor.getStaticImportFavorites(unit, name, isMethod, favourites); if (imports.length > 1) { // See https://github.com/redhat-developer/vscode-java/issues/1472 return; } for (int i = 0; i < imports.length; i++) { String curr = imports[i]; String qualifiedTypeName = Signature.getQualifier(curr); String res = importRewrite.addStaticImport(qualifiedTypeName, name, isMethod, new ContextSensitiveImportRewriteContext(root, node.getStartPosition(), importRewrite)); int dot = res.lastIndexOf('.'); if (dot != -1) { String usedTypeName = importRewrite.addImport(qualifiedTypeName); Name newName = ast.newQualifiedName(ast.newName(usedTypeName), ast.newSimpleName(name)); astRewrite.replace(node, newName, null); } } }
Example 3
Source File: MoveInnerToTopRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void addImportsToTargetUnit(final ICompilationUnit targetUnit, final IProgressMonitor monitor) throws CoreException, JavaModelException { monitor.beginTask("", 2); //$NON-NLS-1$ try { ImportRewrite rewrite= StubUtility.createImportRewrite(targetUnit, true); if (fTypeImports != null) { ITypeBinding type= null; for (final Iterator<ITypeBinding> iterator= fTypeImports.iterator(); iterator.hasNext();) { type= iterator.next(); rewrite.addImport(type); } } if (fStaticImports != null) { IBinding binding= null; for (final Iterator<IBinding> iterator= fStaticImports.iterator(); iterator.hasNext();) { binding= iterator.next(); rewrite.addStaticImport(binding); } } fTypeImports= null; fStaticImports= null; TextEdit edits= rewrite.rewriteImports(new SubProgressMonitor(monitor, 1)); JavaModelUtil.applyEdit(targetUnit, edits, false, new SubProgressMonitor(monitor, 1)); } finally { monitor.done(); } }
Example 4
Source File: RenamePackageProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public void rewriteImports(TextChangeManager changeManager, IProgressMonitor pm) throws CoreException { for (Iterator<Entry<ICompilationUnit, ImportChange>> iter= fImportChanges.entrySet().iterator(); iter.hasNext();) { Entry<ICompilationUnit, ImportChange> entry= iter.next(); ICompilationUnit cu= entry.getKey(); ImportChange importChange= entry.getValue(); ImportRewrite importRewrite= StubUtility.createImportRewrite(cu, true); importRewrite.setFilterImplicitImports(false); for (Iterator<String> iterator= importChange.fStaticToRemove.iterator(); iterator.hasNext();) { importRewrite.removeStaticImport(iterator.next()); } for (Iterator<String> iterator= importChange.fToRemove.iterator(); iterator.hasNext();) { importRewrite.removeImport(iterator.next()); } for (Iterator<String[]> iterator= importChange.fStaticToAdd.iterator(); iterator.hasNext();) { String[] toAdd= iterator.next(); importRewrite.addStaticImport(toAdd[0], toAdd[1], true); } for (Iterator<String> iterator= importChange.fToAdd.iterator(); iterator.hasNext();) { importRewrite.addImport(iterator.next()); } if (importRewrite.hasRecordedChanges()) { TextEdit importEdit= importRewrite.rewriteImports(pm); String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports; try { TextChangeCompatibility.addTextEdit(changeManager.get(cu), name, importEdit); } catch (MalformedTreeException e) { JavaLanguageServerPlugin.logError("MalformedTreeException while processing cu " + cu); //$NON-NLS-1$ throw e; } } } }
Example 5
Source File: MoveInnerToTopRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void updateReferenceInImport(ImportDeclaration enclosingImport, ASTNode node, CompilationUnitRewrite rewrite) { final IBinding binding= enclosingImport.resolveBinding(); if (binding instanceof ITypeBinding) { final ITypeBinding type= (ITypeBinding) binding; final ImportRewrite rewriter= rewrite.getImportRewrite(); if (enclosingImport.isStatic()) { final String oldImport= ASTNodes.asString(node); final StringBuffer buffer= new StringBuffer(oldImport); final String typeName= fType.getDeclaringType().getElementName(); final int index= buffer.indexOf(typeName); if (index >= 0) { buffer.delete(index, index + typeName.length() + 1); final String newImport= buffer.toString(); if (enclosingImport.isOnDemand()) { rewriter.removeStaticImport(oldImport + ".*"); //$NON-NLS-1$ rewriter.addStaticImport(newImport, "*", false); //$NON-NLS-1$ } else { rewriter.removeStaticImport(oldImport); final int offset= newImport.lastIndexOf('.'); if (offset >= 0 && offset < newImport.length() - 1) { rewriter.addStaticImport(newImport.substring(0, offset), newImport.substring(offset + 1), false); } } } } else rewriter.removeImport(type.getQualifiedName()); } }
Example 6
Source File: ChangeSignatureProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public void updateNode() throws JavaModelException { ImportRewrite importRewrite= fCuRewrite.getImportRewrite(); QualifiedName name= (QualifiedName) fImportDecl.getName(); //will be removed by importRemover if not used elsewhere ... importRewrite.removeStaticImport(name.getFullyQualifiedName()); importRewrite.addStaticImport(name.getQualifier().getFullyQualifiedName(), fMethodName, false); }
Example 7
Source File: RenamePackageProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public void rewriteImports(TextChangeManager changeManager, IProgressMonitor pm) throws CoreException { for (Iterator<Entry<ICompilationUnit, ImportChange>> iter= fImportChanges.entrySet().iterator(); iter.hasNext();) { Entry<ICompilationUnit, ImportChange> entry= iter.next(); ICompilationUnit cu= entry.getKey(); ImportChange importChange= entry.getValue(); ImportRewrite importRewrite= StubUtility.createImportRewrite(cu, true); importRewrite.setFilterImplicitImports(false); for (Iterator<String> iterator= importChange.fStaticToRemove.iterator(); iterator.hasNext();) { importRewrite.removeStaticImport(iterator.next()); } for (Iterator<String> iterator= importChange.fToRemove.iterator(); iterator.hasNext();) { importRewrite.removeImport(iterator.next()); } for (Iterator<String[]> iterator= importChange.fStaticToAdd.iterator(); iterator.hasNext();) { String[] toAdd= iterator.next(); importRewrite.addStaticImport(toAdd[0], toAdd[1], true); } for (Iterator<String> iterator= importChange.fToAdd.iterator(); iterator.hasNext();) { importRewrite.addImport(iterator.next()); } if (importRewrite.hasRecordedChanges()) { TextEdit importEdit= importRewrite.rewriteImports(pm); String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports; try { TextChangeCompatibility.addTextEdit(changeManager.get(cu), name, importEdit); } catch (MalformedTreeException e) { JavaPlugin.logErrorMessage("MalformedTreeException while processing cu " + cu); //$NON-NLS-1$ throw e; } } } }
Example 8
Source File: SourceProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void updateStaticReferences(ASTRewrite rewriter, CallContext context) { ImportRewrite importer= context.importer; for (Iterator<SimpleName> iter= fAnalyzer.getStaticsToImport().iterator(); iter.hasNext();) { Name element= iter.next(); IBinding binding= element.resolveBinding(); if (binding != null) { String s= importer.addStaticImport(binding); if (!ASTNodes.asString(element).equals(s)) { rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null); } } } }
Example 9
Source File: MoveCuUpdateCreator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void addStaticImport(ICompilationUnit movedUnit, IImportDeclaration importDecl, ImportRewrite rewrite) { String old= importDecl.getElementName(); int oldPackLength= movedUnit.getParent().getElementName().length(); StringBuffer result= new StringBuffer(fDestination.getElementName()); if (oldPackLength == 0) // move FROM default package result.append('.').append(old); else if (result.length() == 0) // move TO default package result.append(old.substring(oldPackLength + 1)); // cut "." else result.append(old.substring(oldPackLength)); int index= result.lastIndexOf("."); //$NON-NLS-1$ if (index > 0 && index < result.length() - 1) rewrite.addStaticImport(result.substring(0, index), result.substring(index + 1, result.length()), true); }
Example 10
Source File: OrganizeImportsOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void addStaticImports(List<SimpleName> staticReferences, ImportRewrite importsStructure) { for (int i= 0; i < staticReferences.size(); i++) { Name name= staticReferences.get(i); IBinding binding= name.resolveBinding(); if (binding != null) { // paranoia check importsStructure.addStaticImport(binding); } } }
Example 11
Source File: UnresolvedElementsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static void addStaticImportFavoriteProposals(IInvocationContext context, SimpleName node, boolean isMethod, Collection<ChangeCorrectionProposal> proposals) throws JavaModelException { IJavaProject project= context.getCompilationUnit().getJavaProject(); if (JavaModelUtil.is50OrHigher(project)) { String[] favourites = PreferenceManager.getPrefs(context.getCompilationUnit().getResource()) .getJavaCompletionFavoriteMembers(); if (favourites.length == 0) { return; } CompilationUnit root= context.getASTRoot(); AST ast= root.getAST(); String name= node.getIdentifier(); String[] staticImports= SimilarElementsRequestor.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites); for (int i= 0; i < staticImports.length; i++) { String curr= staticImports[i]; ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(root, true); ASTRewrite astRewrite= ASTRewrite.create(ast); String label; String qualifiedTypeName= Signature.getQualifier(curr); String elementLabel= BasicElementLabels.getJavaElementName(JavaModelUtil.concatenateName(Signature.getSimpleName(qualifiedTypeName), name)); String res= importRewrite.addStaticImport(qualifiedTypeName, name, isMethod, new ContextSensitiveImportRewriteContext(root, node.getStartPosition(), importRewrite)); int dot= res.lastIndexOf('.'); if (dot != -1) { String usedTypeName= importRewrite.addImport(qualifiedTypeName); Name newName= ast.newQualifiedName(ast.newName(usedTypeName), ast.newSimpleName(name)); astRewrite.replace(node, newName, null); label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_to_static_import_description, elementLabel); } else { label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_static_import_description, elementLabel); } ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT); proposal.setImportRewrite(importRewrite); proposals.add(proposal); } } }
Example 12
Source File: UnresolvedElementsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static void addStaticImportFavoriteProposals(IInvocationContext context, SimpleName node, boolean isMethod, Collection<ICommandAccess> proposals) throws JavaModelException { IJavaProject project= context.getCompilationUnit().getJavaProject(); if (JavaModelUtil.is50OrHigher(project)) { String pref= PreferenceConstants.getPreference(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, project); String[] favourites= pref.split(";"); //$NON-NLS-1$ if (favourites.length == 0) { return; } CompilationUnit root= context.getASTRoot(); AST ast= root.getAST(); String name= node.getIdentifier(); String[] staticImports= SimilarElementsRequestor.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites); for (int i= 0; i < staticImports.length; i++) { String curr= staticImports[i]; ImportRewrite importRewrite= StubUtility.createImportRewrite(root, true); ASTRewrite astRewrite= ASTRewrite.create(ast); String label; String qualifiedTypeName= Signature.getQualifier(curr); String elementLabel= BasicElementLabels.getJavaElementName(JavaModelUtil.concatenateName(Signature.getSimpleName(qualifiedTypeName), name)); String res= importRewrite.addStaticImport(qualifiedTypeName, name, isMethod, new ContextSensitiveImportRewriteContext(root, node.getStartPosition(), importRewrite)); int dot= res.lastIndexOf('.'); if (dot != -1) { String usedTypeName= importRewrite.addImport(qualifiedTypeName); Name newName= ast.newQualifiedName(ast.newName(usedTypeName), ast.newSimpleName(name)); astRewrite.replace(node, newName, null); label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_to_static_import_description, elementLabel); } else { label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_static_import_description, elementLabel); } Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT, image); proposal.setImportRewrite(importRewrite); proposals.add(proposal); } } }