importRewrite.setFilterImplicitImports(false); for (Iterator iterator= importChange.fStaticToRemove.iterator(); iterator.hasNext();) { importRewrite.removeStaticImport((String) iterator.next()); importRewrite.removeImport((String) iterator.next()); importRewrite.addStaticImport(toAdd[0], toAdd[1], true); importRewrite.addImport((String) iterator.next()); if (importRewrite.hasRecordedChanges()) { TextEdit importEdit= importRewrite.rewriteImports(pm); String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports; try {
/** * Adds a new import to the rewriter's record and returns a {@link Type} node that can be used * in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard. * If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type * arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard * of wildcards are ignored. * <p> * No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead. * </p> * <p> * The content of the compilation unit itself is actually not modified * in any way by this method; rather, the rewriter just records that a new import has been added. * </p> * @param typeSig the signature of the type to be added. * @param ast the AST to create the returned type for. * @return a type node for the given type signature. Type names are simple names if an import could be used, * or else qualified names if an import conflict prevented an import. */ public Type addImportFromSignature(String typeSig, AST ast) { return addImportFromSignature(typeSig, ast, this.defaultContext); }
static ImportRewrite createImportRewrite(ICompilationUnit compilationUnit) { try { ImportRewrite rewrite = ImportRewrite.create(compilationUnit, true); String[] importOrder = JavaLanguageServerPlugin.getPreferencesManager() == null ? new String[0] : JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getImportOrder(); rewrite.setImportOrder(importOrder); rewrite.setOnDemandImportThreshold(IMPORTS_THRESHOLD); rewrite.setStaticOnDemandImportThreshold(IMPORTS_THRESHOLD); return rewrite; } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); return null; } }
private void addImportsToAccessorCu(ICompilationUnit newCu, IProgressMonitor pm) throws CoreException { ImportRewrite is= StubUtility.createImportRewrite(newCu, true); if (fIsEclipseNLS) { is.addImport("org.eclipse.osgi.util.NLS"); //$NON-NLS-1$ } else { is.addImport("java.util.MissingResourceException"); //$NON-NLS-1$ is.addImport("java.util.ResourceBundle"); //$NON-NLS-1$ } TextEdit edit= is.rewriteImports(pm); JavaModelUtil.applyEdit(newCu, edit, false, null); }
private void addImportRewriteUpdates(TextChangeManager changeManager) throws CoreException { for (Iterator<ICompilationUnit> iter= fImportRewrites.keySet().iterator(); iter.hasNext();) { ICompilationUnit cu= iter.next(); ImportRewrite importRewrite= fImportRewrites.get(cu); if (importRewrite != null && importRewrite.hasRecordedChanges()) { TextChangeCompatibility.addTextEdit(changeManager.get(cu), RefactoringCoreMessages.MoveCuUpdateCreator_update_imports, importRewrite.rewriteImports(null)); } } }
@Override public void endVisit(CompilationUnit node) { // If we don't had a static import to the field we don't // have to add any, even if we generated a setter or // getter access. if (!fRemoveStaticImport) { return; } ITypeBinding type = fFieldBinding.getDeclaringClass(); String fieldName = fFieldBinding.getName(); String typeName = type.getQualifiedName(); if (fRemoveStaticImport) { fImportRewriter.removeStaticImport(typeName + "." + fieldName); //$NON-NLS-1$ } if (fReferencingGetter) { fImportRewriter.addStaticImport(typeName, fGetter, false); } if (fReferencingSetter) { fImportRewriter.addStaticImport(typeName, fSetter, false); } }
protected void insertImport(CompilationUnit compilationUnit, String qualifiedTypeToImport, IProgressMonitor pm) throws CoreException { if (compilationUnit == null || qualifiedTypeToImport == null) { return; } ImportRewrite rewrite = ImportRewrite.create(compilationUnit, true); if (rewrite == null) { return; } if (!isUndo()) { rewrite.addImport(qualifiedTypeToImport); } else { rewrite.removeImport(qualifiedTypeToImport); } TextEdit rewriteImports = rewrite.rewriteImports(pm); ICompilationUnit iCompilationUnit = (ICompilationUnit) compilationUnit.getJavaElement() .getAdapter(IOpenable.class); performTextEdit(rewriteImports, (IFile) iCompilationUnit.getResource(), pm); }
/** * Create a Type suitable as the creationType in a ClassInstanceCreation expression. * @param ast The AST to create the nodes for. * @param typeBinding binding representing the given class type * @param importRewrite the import rewrite to use * @param importContext the import context used to determine which (null) annotations to consider * @return a Type suitable as the creationType in a ClassInstanceCreation expression. */ public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) { if (typeBinding.isParameterizedType()) { Type baseType= newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext); IAnnotationBinding[] typeAnnotations= importContext.removeRedundantTypeAnnotations(typeBinding.getTypeAnnotations(), TypeLocation.NEW, typeBinding); for (IAnnotationBinding typeAnnotation : typeAnnotations) { ((AnnotatableType)baseType).annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext)); } ParameterizedType parameterizedType= ast.newParameterizedType(baseType); for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) { typeArgument= StubUtility2Core.replaceWildcardsAndCaptures(typeArgument); parameterizedType.typeArguments().add(importRewrite.addImport(typeArgument, ast, importContext, TypeLocation.TYPE_ARGUMENT)); } return parameterizedType; } else { return importRewrite.addImport(typeBinding, ast, importContext, TypeLocation.NEW); } } }
private void addExceptionsToNewConstructor(MethodDeclaration newConstructor, ImportRewrite importRewrite) { IMethodBinding constructorBinding= getSuperConstructorBinding(); if (constructorBinding == null) return; ITypeBinding[] exceptions= constructorBinding.getExceptionTypes(); for (int i= 0; i < exceptions.length; i++) { Type exceptionType= importRewrite.addImport(exceptions[i], fAnonymousInnerClassNode.getAST()); newConstructor.thrownExceptionTypes().add(exceptionType); } }
private void addStaticImports(List/*<SimpleName>*/ staticReferences, ImportRewrite importsStructure) { for (int i= 0; i < staticReferences.size(); i++) { Name name= (Name) staticReferences.get(i); IBinding binding= name.resolveBinding(); if (binding != null) { // paranoia check importsStructure.addStaticImport(binding); } } }
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 StringBuilder buffer= new StringBuilder(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()); } }
chosenType= chosenType.getTypeDeclaration(); BindingKey bindingKey= new BindingKey(chosenType.getBindingKey()); typeArgument= rewrite.getImportRewrite().addImportFromSignature(bindingKey.toSignature(), rewrite.getAST()); ArrayList<CollectionElementVariable2> nestedTypeArgumentCvs= getTypeArgumentCvs(elementCv, tCModel); typeArgument= rewrite.getAST().newWildcardType(); } else { String object= rewrite.getImportRewrite().addImport("java.lang.Object"); //$NON-NLS-1$ typeArgument= (Type) rewrite.getASTRewrite().createStringPlaceholder(object, ASTNode.SIMPLE_TYPE);
try importRewrite = ImportRewrite.create(sourceUnit, true); boolean removed = importRewrite.removeImport(unusedImport); if (!removed) importRewrite.removeImport(unusedImport + ".*"); TextEdit textEdits = importRewrite.rewriteImports(null); Document document = new Document(result[0]); textEdits.apply(document);
/* package */ void removeImport(String qualifiedName) { fImportsRewrite.removeImport(qualifiedName); }
public void applyRemoves(ImportRewrite importRewrite) { IBinding[] bindings= getImportsToRemove(); for (int i= 0; i < bindings.length; i++) { if (bindings[i] instanceof ITypeBinding) { ITypeBinding typeBinding= (ITypeBinding) bindings[i]; importRewrite.removeImport(typeBinding.getTypeDeclaration().getQualifiedName()); } else if (bindings[i] instanceof IMethodBinding) { IMethodBinding binding= (IMethodBinding) bindings[i]; importRewrite.removeStaticImport(binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName()); } else if (bindings[i] instanceof IVariableBinding) { IVariableBinding binding= (IVariableBinding) bindings[i]; importRewrite.removeStaticImport(binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName()); } } }
void create(ICompilationUnit cu, boolean toRestore) throws CoreException { ImportRewrite.create(cu, toRestore); TextEdit edit = fImportsStructure.rewriteImports(new NullProgressMonitor()); IDocument document = new Document(cu.getSource()); try { edit.apply(document); } catch (Exception e) { } String imports = document.get(); cu.getBuffer().setContents(imports); cu.commitWorkingCopy(false, new NullProgressMonitor()); }
/** * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(CompilationUnit, boolean)} and * configures the rewriter with the settings as specified in the JDT UI preferences. * * @param astRoot the AST root to create the rewriter on * @param restoreExistingImports specifies if the existing imports should be kept or removed. * @return the new rewriter configured with the settings as specified in the JDT UI preferences. * * @see ImportRewrite#create(CompilationUnit, boolean) */ public static ImportRewrite createImportRewrite(CompilationUnit astRoot, boolean restoreExistingImports) { return configureImportRewrite(ImportRewrite.create(astRoot, restoreExistingImports)); }
return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig))); case Signature.ARRAY_TYPE_SIGNATURE: Type elementType= addImportFromSignature(Signature.getElementType(typeSig), ast, context); return ast.newArrayType(elementType, Signature.getArrayCount(typeSig)); case Signature.CLASS_TYPE_SIGNATURE: erasureName= internalAddImport(erasureName, context); for (int i= 0; i < typeArguments.length; i++) { String curr= typeArguments[i]; if (containsNestedCapture(curr)) { // see bug 103044 argNodes.add(ast.newWildcardType()); } else { argNodes.add(addImportFromSignature(curr, ast, context)); char ch= typeSig.charAt(0); if (ch != Signature.C_STAR) { Type bound= addImportFromSignature(typeSig.substring(1), ast, context); wildcardType.setBound(bound, ch == Signature.C_EXTENDS); return addImportFromSignature(typeSig.substring(1), ast, context); default: throw new IllegalArgumentException("Unknown type signature kind: " + typeSig); //$NON-NLS-1$
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException { TextChange change = fChangeManager.get(unit); MultiTextEdit root = new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator<TextEditGroup> iter = groups.iterator(); iter.hasNext();) { change.addTextEditGroup(iter.next()); } }
private void annotateList(List annotations, IAnnotationBinding [] annotationBindings, AST ast, ImportRewriteContext context) { for (int i = 0; i< annotationBindings.length; i++) { Annotation annotation = addAnnotation(annotationBindings[i], ast, context); if (annotation != null) annotations.add(annotation); } }