Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ImportRewrite#removeStaticImport()

The following examples show how to use org.eclipse.jdt.core.dom.rewrite.ImportRewrite#removeStaticImport() . 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: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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 2
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
	List<ICompilationUnit> cuList = Arrays.asList(fCus);
	SearchResultGroup[] references = getReferences(movedUnit, pm, status);
	for (int i = 0; i < references.length; i++) {
		SearchResultGroup searchResultGroup = references[i];
		ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
		if (referencingCu == null) {
			continue;
		}

		boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
		SearchMatch[] results = searchResultGroup.getSearchResults();
		for (int j = 0; j < results.length; j++) {
			// TODO: should update type references with results from addImport
			TypeReference reference = (TypeReference) results[j];
			if (reference.isImportDeclaration()) {
				ImportRewrite rewrite = getImportRewrite(referencingCu);
				IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
				if (Flags.isStatic(importDecl.getFlags())) {
					rewrite.removeStaticImport(importDecl.getElementName());
					addStaticImport(movedUnit, importDecl, rewrite);
				} else {
					rewrite.removeImport(importDecl.getElementName());
					rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
				}
			} else if (reference.isQualified()) {
				TextChange textChange = changeManager.get(referencingCu);
				String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
				TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
				TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
			} else if (simpleReferencesNeedNewImport) {
				ImportRewrite importEdit = getImportRewrite(referencingCu);
				String typeName = reference.getSimpleName();
				importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
			}
		}
	}
}
 
Example 3
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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 4
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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 5
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
	List<ICompilationUnit> cuList= Arrays.asList(fCus);
	SearchResultGroup[] references= getReferences(movedUnit, pm, status);
	for (int i= 0; i < references.length; i++) {
		SearchResultGroup searchResultGroup= references[i];
		ICompilationUnit referencingCu= searchResultGroup.getCompilationUnit();
		if (referencingCu == null)
			continue;

		boolean simpleReferencesNeedNewImport= simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
		SearchMatch[] results= searchResultGroup.getSearchResults();
		for (int j= 0; j < results.length; j++) {
			// TODO: should update type references with results from addImport
			TypeReference reference= (TypeReference) results[j];
			if (reference.isImportDeclaration()) {
				ImportRewrite rewrite= getImportRewrite(referencingCu);
				IImportDeclaration importDecl= (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
				if (Flags.isStatic(importDecl.getFlags())) {
					rewrite.removeStaticImport(importDecl.getElementName());
					addStaticImport(movedUnit, importDecl, rewrite);
				} else {
					rewrite.removeImport(importDecl.getElementName());
					rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
				}
			} else if (reference.isQualified()) {
				TextChange textChange= changeManager.get(referencingCu);
				String changeName= RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
				TextEdit replaceEdit= new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
				TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
			} else if (simpleReferencesNeedNewImport) {
				ImportRewrite importEdit= getImportRewrite(referencingCu);
				String typeName= reference.getSimpleName();
				importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
			}
		}
	}
}