本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ImportRewrite.rewriteImports方法的典型用法代码示例。如果您正苦于以下问题:Java ImportRewrite.rewriteImports方法的具体用法?Java ImportRewrite.rewriteImports怎么用?Java ImportRewrite.rewriteImports使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.rewrite.ImportRewrite
的用法示例。
在下文中一共展示了ImportRewrite.rewriteImports方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewriteImports
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
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;
}
}
}
}
示例2: addGeneratedAnnotation
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* @param file
* @param info
* @param monitor
* @throws MalformedTreeException
* @throws BadLocationException
* @throws CoreException
*/
@SuppressWarnings("deprecation")
public static void addGeneratedAnnotation(IFile file, IFile graphFile, IProgressMonitor monitor)
throws MalformedTreeException, BadLocationException, CoreException {
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(file);
try {
String source = compilationUnit.getSource();
Document document = new Document(source);
compilationUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1));
astRoot.recordModifications();
final ImportRewrite importRewrite = ImportRewrite.create(astRoot, true);
importRewrite.addImport("javax.annotation.Generated");
astRoot.accept(new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
ASTNode copiedNode = null;
// Add Generated annotation
ClassExtension ce;
try {
ce = new ClassExtension(false, false, false, false, false, false, "", "", null, false, false,
"", "", "", graphFile);
NormalAnnotation annotation = ce.getGeneratedClassAnnotation();
if (annotation != null) {
copiedNode = ASTNode.copySubtree(node.getAST(), annotation);
node.modifiers().add(0, copiedNode);
}
} catch (JavaModelException e) {
ResourceManager.logException(e);
}
return super.visit(node);
}
});
TextEdit rewrite = astRoot.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
rewrite.apply(document);
TextEdit rewriteImports = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
rewriteImports.apply(document);
String newSource = document.get();
compilationUnit.getBuffer().setContents(newSource);
compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, new SubProgressMonitor(monitor, 1));
compilationUnit.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
} finally {
compilationUnit.discardWorkingCopy();
monitor.done();
}
// WorkbenchFacade.JDTManager.reorganizeImport(compilationUnit);
}
示例3: createTextEdit
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
public TextEdit createTextEdit(IProgressMonitor monitor)
throws CoreException, OperationCanceledException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
fNumberOfImportsAdded = 0;
fNumberOfImportsRemoved = 0;
monitor.beginTask(
Messages.format(
CodeGenerationMessages.OrganizeImportsOperation_description,
BasicElementLabels.getFileName(fCompilationUnit)),
9);
CompilationUnit astRoot = fASTRoot;
if (astRoot == null) {
astRoot =
SharedASTProvider.getAST(
fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 2));
if (monitor.isCanceled()) throw new OperationCanceledException();
} else {
monitor.worked(2);
}
ImportRewrite importsRewrite = StubUtility.createImportRewrite(astRoot, false);
Set<String> oldSingleImports = new HashSet<>();
Set<String> oldDemandImports = new HashSet<>();
List<SimpleName> typeReferences = new ArrayList<>();
List<SimpleName> staticReferences = new ArrayList<>();
if (!collectReferences(
astRoot, typeReferences, staticReferences, oldSingleImports, oldDemandImports))
return null;
monitor.worked(1);
processor =
new TypeReferenceProcessor(
oldSingleImports, oldDemandImports, astRoot, importsRewrite, fIgnoreLowerCaseNames);
Iterator<SimpleName> refIterator = typeReferences.iterator();
while (refIterator.hasNext()) {
SimpleName typeRef = refIterator.next();
processor.add(typeRef);
}
hasOpenChoices = processor.process(new SubProgressMonitor(monitor, 3));
addStaticImports(staticReferences, importsRewrite);
if (hasOpenChoices) {
choices = processor.getChoices();
ISourceRange[] ranges = processor.getChoicesSourceRanges();
if (fChooseImportQuery != null) {
TypeNameMatch[] chosen = fChooseImportQuery.chooseImports(choices, ranges);
for (int i = 0; i < chosen.length; i++) {
TypeNameMatch typeInfo = chosen[i];
importsRewrite.addImport(typeInfo.getFullyQualifiedName());
}
} else if (chosenFQN != null) {
chosenFQN.forEach(importsRewrite::addImport);
}
}
TextEdit result = importsRewrite.rewriteImports(new SubProgressMonitor(monitor, 3));
determineImportDifferences(importsRewrite, oldSingleImports, oldDemandImports);
return result;
} finally {
monitor.done();
}
}
示例4: run
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Runs the operation.
*
* @param monitor The progress monitor
* @throws CoreException if accessing the CU or rewritting the imports fails
* @throws OperationCanceledException Runtime error thrown when operation is canceled.
*/
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
monitor.beginTask(CodeGenerationMessages.AddImportsOperation_description, 4);
CompilationUnit astRoot =
SharedASTProvider.getAST(
fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 1));
if (astRoot == null) throw new OperationCanceledException();
ImportRewrite importRewrite = StubUtility.createImportRewrite(astRoot, true);
MultiTextEdit res = new MultiTextEdit();
TextEdit edit =
evaluateEdits(
astRoot,
importRewrite,
fSelectionOffset,
fSelectionLength,
new SubProgressMonitor(monitor, 1));
if (edit == null) {
return;
}
res.addChild(edit);
TextEdit importsEdit = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
res.addChild(importsEdit);
fResultingEdit = res;
if (fApply) {
JavaModelUtil.applyEdit(fCompilationUnit, res, fDoSave, new SubProgressMonitor(monitor, 1));
}
} finally {
monitor.done();
}
}
示例5: createType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Creates the new type.
*
* NOTE: If this method throws a {@link JavaModelException}, its {@link JavaModelException#getJavaModelStatus()}
* method can provide more detailed information about the problem.
*/
public IType createType() throws CoreException {
IProgressMonitor monitor = new NullProgressMonitor();
ICompilationUnit cu = null;
try {
String cuName = simpleTypeName + ".java";
// Create empty compilation unit
cu = pckg.createCompilationUnit(cuName, "", false, monitor);
cu.becomeWorkingCopy(monitor);
IBuffer buffer = cu.getBuffer();
// Need to create a minimal type stub here so we can create an import
// rewriter a few lines down. The rewriter has to be in place when we
// create the real type stub, so we can use it to transform the names of
// any interfaces this type extends/implements.
String dummyTypeStub = createDummyTypeStub();
// Generate the content (file comment, package declaration, type stub)
String cuContent = createCuContent(cu, dummyTypeStub);
buffer.setContents(cuContent);
ImportRewrite imports = StubUtility.createImportRewrite(cu, true);
// Create the real type stub and replace the dummy one
int typeDeclOffset = cuContent.lastIndexOf(dummyTypeStub);
if (typeDeclOffset != -1) {
String typeStub = createTypeStub(cu, imports);
buffer.replace(typeDeclOffset, dummyTypeStub.length(), typeStub);
}
// Let our subclasses add members
IType type = cu.getType(simpleTypeName);
createTypeMembers(type, imports);
// Rewrite the imports and apply the edit
TextEdit edit = imports.rewriteImports(monitor);
applyEdit(cu, edit, false, null);
// Format the Java code
String formattedSource = formatJava(type);
buffer.setContents(formattedSource);
// Save the new type
JavaModelUtil.reconcile(cu);
cu.commitWorkingCopy(true, monitor);
return type;
} finally {
if (cu != null) {
cu.discardWorkingCopy();
}
}
}