/** * Adds the necessary imports for an AST node to the specified compilation unit. * * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated * @param node the AST node specifying the element for which imports should be added * @param typeImports the map of name nodes to strings (element type: Map <Name, String>). * @param staticImports the map of name nodes to strings (element type: Map <Name, String>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void addImports(final CompilationUnitRewrite rewrite, final ASTNode node, final Map typeImports, final Map staticImports, final boolean declarations) { addImports(rewrite, node, typeImports, staticImports, null, declarations); }
/** * Collects the necessary imports for an element represented by the specified AST node. * * @param project the java project containing the element * @param node the AST node specifying the element for which imports should be collected * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>). * @param staticBindings the set of bindings (element type: Set <IBinding>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void collectImports(final IJavaProject project, final ASTNode node, final Collection<ITypeBinding> typeBindings, final Collection<IBinding> staticBindings, final boolean declarations) { collectImports(project, node, typeBindings, staticBindings, null, declarations); }
/** * Adds the necessary imports for an AST node to the specified compilation unit. * * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated * @param context the import rewrite context, or <code>null</code> if none available * @param node the AST node specifying the element for which imports should be added * @param typeImports the map of name nodes to strings (element type: Map <Name, String>). * @param staticImports the map of name nodes to strings (element type: Map <Name, String>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void addImports(final CompilationUnitRewrite rewrite, ImportRewriteContext context, final ASTNode node, final Map<Name, String> typeImports, final Map<Name, String> staticImports, final boolean declarations) { addImports(rewrite, context, node, typeImports, staticImports, null, declarations); }
/** * Collects the necessary imports for an element represented by the specified AST node. * * @param project the java project containing the element * @param node the AST node specifying the element for which imports should be collected * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>). * @param staticBindings the set of bindings (element type: Set <IBinding>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void collectImports(final IJavaProject project, final ASTNode node, final Collection typeBindings, final Collection staticBindings, final boolean declarations) { collectImports(project, node, typeBindings, staticBindings, null, declarations); }
/** * Adds the necessary imports for an AST node to the specified compilation unit. * * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated * @param context the import rewrite context, or <code>null</code> if none available * @param node the AST node specifying the element for which imports should be added * @param typeImports the map of name nodes to strings (element type: Map <Name, String>). * @param staticImports the map of name nodes to strings (element type: Map <Name, String>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void addImports(final CompilationUnitRewrite rewrite, ImportRewriteContext context, final ASTNode node, final Map<Name, String> typeImports, final Map<Name, String> staticImports, final boolean declarations) { addImports(rewrite, context, node, typeImports, staticImports, null, declarations); }
/** * Collects the necessary imports for an element represented by the specified AST node. * * @param project the java project containing the element * @param node the AST node specifying the element for which imports should be collected * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>). * @param staticBindings the set of bindings (element type: Set <IBinding>). * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise */ public static void collectImports(final IJavaProject project, final ASTNode node, final Collection<ITypeBinding> typeBindings, final Collection<IBinding> staticBindings, final boolean declarations) { collectImports(project, node, typeBindings, staticBindings, null, declarations); }
/** * Creates the necessary imports for the copied method in the target * compilation unit. * * @param rewrite * the target compilation unit rewrite * @param declaration * the source method declaration * @param monitor * the progress monitor to use * @param status * the refactoring status to use * @throws CoreException * if an error occurs */ protected void createMethodImports(final CompilationUnitRewrite rewrite, final MethodDeclaration declaration, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { Assert.isNotNull(rewrite); Assert.isNotNull(declaration); Assert.isNotNull(monitor); Assert.isNotNull(status); monitor.beginTask("", 1); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.MoveInstanceMethodProcessor_creating); try { ImportRewriteUtil.addImports(rewrite, null, declaration, new HashMap<Name, String>(), new HashMap<Name, String>(), false); } finally { monitor.done(); } }
/** * Creates a new method body rewriter. * * @param targetRewrite * the target compilation unit rewrite to use * @param rewrite * the source ast rewrite to use * @param sourceDeclaration * the source method declaration */ public MethodBodyRewriter(final CompilationUnitRewrite targetRewrite, final ASTRewrite rewrite, final MethodDeclaration sourceDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(rewrite); Assert.isNotNull(sourceDeclaration); fTargetRewrite= targetRewrite; fRewrite= rewrite; fDeclaration= sourceDeclaration; fStaticImports.clear(); ImportRewriteUtil.collectImports(fMethod.getJavaProject(), sourceDeclaration, new HashSet<ITypeBinding>(), fStaticImports, false); }
/** * Creates the necessary imports for the copied method in the target * compilation unit. * * @param rewrite * the target compilation unit rewrite * @param declaration * the source method declaration * @param monitor * the progress monitor to use * @param status * the refactoring status to use * @throws CoreException * if an error occurs */ protected void createMethodImports(final CompilationUnitRewrite rewrite, final MethodDeclaration declaration, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { Assert.isNotNull(rewrite); Assert.isNotNull(declaration); Assert.isNotNull(monitor); Assert.isNotNull(status); monitor.beginTask("", 1); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.MoveInstanceMethodProcessor_creating); try { ImportRewriteUtil.addImports(rewrite, declaration, new HashMap(), new HashMap(), false); } finally { monitor.done(); } }
/** * Creates a new method body rewriter. * * @param targetRewrite * the target compilation unit rewrite to use * @param rewrite * the source ast rewrite to use * @param sourceDeclaration * the source method declaration */ public MethodBodyRewriter(final CompilationUnitRewrite targetRewrite, final ASTRewrite rewrite, final MethodDeclaration sourceDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(rewrite); Assert.isNotNull(sourceDeclaration); fTargetRewrite= targetRewrite; fRewrite= rewrite; fDeclaration= sourceDeclaration; fStaticImports.clear(); ImportRewriteUtil.collectImports(fMethod.getJavaProject(), sourceDeclaration, new HashSet(), fStaticImports, false); }
/** * Creates the necessary imports for the copied method in the target * compilation unit. * * @param rewrite * the target compilation unit rewrite * @param declaration * the source method declaration * @param monitor * the progress monitor to use * @param status * the refactoring status to use * @throws CoreException * if an error occurs */ protected void createMethodImports(final CompilationUnitRewrite rewrite, final MethodDeclaration declaration, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { Assert.isNotNull(rewrite); Assert.isNotNull(declaration); Assert.isNotNull(monitor); Assert.isNotNull(status); monitor.beginTask("", 1); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.MoveInstanceMethodProcessor_creating); try { ImportRewriteUtil.addImports(rewrite, null, declaration, new HashMap<Name, String>(), new HashMap<Name, String>(), false); } finally { monitor.done(); } }
/** * Creates a new method body rewriter. * * @param targetRewrite * the target compilation unit rewrite to use * @param rewrite * the source ast rewrite to use * @param sourceDeclaration * the source method declaration */ public MethodBodyRewriter(final CompilationUnitRewrite targetRewrite, final ASTRewrite rewrite, final MethodDeclaration sourceDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(rewrite); Assert.isNotNull(sourceDeclaration); fTargetRewrite= targetRewrite; fRewrite= rewrite; fDeclaration= sourceDeclaration; fStaticImports.clear(); ImportRewriteUtil.collectImports(fMethod.getJavaProject(), sourceDeclaration, new HashSet<ITypeBinding>(), fStaticImports, false); }
MovedMemberAnalyzer analyzer= new MovedMemberAnalyzer(fSource, fMemberBindings, fSourceBinding, target); declaration.accept(analyzer); ImportRewriteUtil.addImports(fTarget, declaration, new HashMap(), new HashMap(), exclude, false); if (getDeclaringType().isInterface() && !fDestinationType.isInterface()) { if (declaration instanceof FieldDeclaration) {
/** * Creates the type parameters of the new supertype. * * @param targetRewrite * the target compilation unit rewrite * @param subType * the subtype * @param sourceDeclaration * the type declaration of the source type * @param targetDeclaration * the type declaration of the target type */ protected final void createTypeParameters(final ASTRewrite targetRewrite, final IType subType, final AbstractTypeDeclaration sourceDeclaration, final AbstractTypeDeclaration targetDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(sourceDeclaration); Assert.isNotNull(targetDeclaration); if (sourceDeclaration instanceof TypeDeclaration) { TypeParameter parameter= null; final ListRewrite rewrite= targetRewrite.getListRewrite(targetDeclaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY); for (final Iterator iterator= ((TypeDeclaration) sourceDeclaration).typeParameters().iterator(); iterator.hasNext();) { parameter= (TypeParameter) iterator.next(); rewrite.insertLast(ASTNode.copySubtree(targetRewrite.getAST(), parameter), null); ImportRewriteUtil.collectImports(subType.getJavaProject(), sourceDeclaration, fTypeBindings, fStaticBindings, false); } } }
final FieldDeclaration newField= createNewFieldDeclarationNode(rewriter, root, (IField) member, oldField, mapping, new SubProgressMonitor(subsub, 1), status, flags); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newField, ASTNodes.getInsertionIndex(newField, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, oldField.getParent(), new HashMap(), new HashMap(), false); final MethodDeclaration newMethod= createNewMethodDeclarationNode(sourceRewriter, rewrite, ((IMethod) member), oldMethod, root, mapping, adjustments, new SubProgressMonitor(subsub, 1), status); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, oldMethod, new HashMap(), new HashMap(), false); final BodyDeclaration newType= createNewTypeDeclarationNode(((IType) member), oldType, root, mapping, rewriter); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newType, ASTNodes.getInsertionIndex(newType, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, oldType, new HashMap(), new HashMap(), false);
/** * Creates the type parameters of the new supertype. * * @param targetRewrite * the target compilation unit rewrite * @param subType * the subtype * @param sourceDeclaration * the type declaration of the source type * @param targetDeclaration * the type declaration of the target type */ protected final void createTypeParameters(final ASTRewrite targetRewrite, final IType subType, final AbstractTypeDeclaration sourceDeclaration, final AbstractTypeDeclaration targetDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(sourceDeclaration); Assert.isNotNull(targetDeclaration); if (sourceDeclaration instanceof TypeDeclaration) { TypeParameter parameter= null; final ListRewrite rewrite= targetRewrite.getListRewrite(targetDeclaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY); for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) sourceDeclaration).typeParameters().iterator(); iterator.hasNext();) { parameter= iterator.next(); rewrite.insertLast(ASTNode.copySubtree(targetRewrite.getAST(), parameter), null); ImportRewriteUtil.collectImports(subType.getJavaProject(), sourceDeclaration, fTypeBindings, fStaticBindings, false); } } }
final FieldDeclaration newField= createNewFieldDeclarationNode(rewriter, root, (IField) member, oldField, mapping, new SubProgressMonitor(subsub, 1), status, flags); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newField, ASTNodes.getInsertionIndex(newField, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldField.getParent(), new HashMap<Name, String>(), new HashMap<Name, String>(), false); final MethodDeclaration newMethod= createNewMethodDeclarationNode(sourceRewriter, rewrite, ((IMethod) member), oldMethod, mapping, adjustments, new SubProgressMonitor(subsub, 1), status); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldMethod, new HashMap<Name, String>(), new HashMap<Name, String>(), false); final BodyDeclaration newType= createNewTypeDeclarationNode(((IType) member), oldType, root, mapping, rewriter); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newType, ASTNodes.getInsertionIndex(newType, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldType, new HashMap<Name, String>(), new HashMap<Name, String>(), false);
/** * Creates the type parameters of the new supertype. * * @param targetRewrite * the target compilation unit rewrite * @param subType * the subtype * @param sourceDeclaration * the type declaration of the source type * @param targetDeclaration * the type declaration of the target type */ protected final void createTypeParameters(final ASTRewrite targetRewrite, final IType subType, final AbstractTypeDeclaration sourceDeclaration, final AbstractTypeDeclaration targetDeclaration) { Assert.isNotNull(targetRewrite); Assert.isNotNull(sourceDeclaration); Assert.isNotNull(targetDeclaration); if (sourceDeclaration instanceof TypeDeclaration) { TypeParameter parameter= null; final ListRewrite rewrite= targetRewrite.getListRewrite(targetDeclaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY); for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) sourceDeclaration).typeParameters().iterator(); iterator.hasNext();) { parameter= iterator.next(); rewrite.insertLast(ASTNode.copySubtree(targetRewrite.getAST(), parameter), null); ImportRewriteUtil.collectImports(subType.getJavaProject(), sourceDeclaration, fTypeBindings, fStaticBindings, false); } } }
final FieldDeclaration newField= createNewFieldDeclarationNode(rewriter, root, (IField) member, oldField, mapping, new SubProgressMonitor(subsub, 1), status, flags); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newField, org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite.getInsertionIndex(newField, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldField.getParent(), new HashMap<Name, String>(), new HashMap<Name, String>(), false); final MethodDeclaration newMethod= createNewMethodDeclarationNode(sourceRewriter, rewrite, ((IMethod) member), oldMethod, mapping, adjustments, new SubProgressMonitor(subsub, 1), status); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newMethod, org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite.getInsertionIndex(newMethod, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldMethod, new HashMap<Name, String>(), new HashMap<Name, String>(), newMethod.getBody() == null); final BodyDeclaration newType= createNewTypeDeclarationNode(((IType) member), oldType, root, mapping, rewriter); rewriter.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newType, org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite.getInsertionIndex(newType, declaration.bodyDeclarations()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PULL_UP)); ImportRewriteUtil.addImports(rewrite, context, oldType, new HashMap<Name, String>(), new HashMap<Name, String>(), false);
Assert.isNotNull(fragment); final FieldDeclaration field= (FieldDeclaration) fragment.getParent(); ImportRewriteUtil.collectImports(fSubType.getJavaProject(), field, fTypeBindings, fStaticBindings, false); final ASTRewrite rewrite= ASTRewrite.create(field.getAST()); final ITrackedNodePosition position= rewrite.track(field);