Search in sources :

Example 21 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.

the class MoveCuUpdateCreator method addImportRewriteUpdates.

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));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite)

Example 22 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.

the class MoveCuUpdateCreator method removeImportsToDestinationPackageTypes.

private void removeImportsToDestinationPackageTypes(ICompilationUnit movedUnit) throws CoreException {
    ImportRewrite importEdit = getImportRewrite(movedUnit);
    IType[] destinationTypes = getDestinationPackageTypes();
    for (int i = 0; i < destinationTypes.length; i++) {
        importEdit.removeImport(destinationTypes[i].getFullyQualifiedName('.'));
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) IType(org.eclipse.jdt.core.IType)

Example 23 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.

the class MoveCuUpdateCreator method addImportToSourcePackageTypes.

private void addImportToSourcePackageTypes(ICompilationUnit movedUnit, IProgressMonitor pm) throws CoreException {
    List<ICompilationUnit> cuList = Arrays.asList(fCus);
    IType[] allCuTypes = movedUnit.getAllTypes();
    IType[] referencedTypes = ReferenceFinderUtil.getTypesReferencedIn(allCuTypes, pm);
    ImportRewrite importEdit = getImportRewrite(movedUnit);
    importEdit.setFilterImplicitImports(false);
    IPackageFragment srcPack = (IPackageFragment) movedUnit.getParent();
    for (int i = 0; i < referencedTypes.length; i++) {
        IType iType = referencedTypes[i];
        if (!iType.exists())
            continue;
        if (!JavaModelUtil.isSamePackage(iType.getPackageFragment(), srcPack))
            continue;
        if (cuList.contains(iType.getCompilationUnit()))
            continue;
        importEdit.addImport(iType.getFullyQualifiedName('.'));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) IType(org.eclipse.jdt.core.IType)

Example 24 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.

the class CoreTests method newImportsRewrite.

protected ImportRewrite newImportsRewrite(CompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports) {
    ImportRewrite rewrite = ImportRewrite.create(cu, restoreExistingImports);
    rewrite.setImportOrder(order);
    rewrite.setOnDemandImportThreshold(normalThreshold);
    rewrite.setStaticOnDemandImportThreshold(staticThreshold);
    return rewrite;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite)

Example 25 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method createNewConstructor.

private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
    ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
        return null;
    IJavaProject project = fCu.getJavaProject();
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = rewrite.getImportRewrite();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
    newConstructor.setName(ast.newSimpleName(fClassName));
    addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
    newConstructor.setBody(ast.newBlock());
    List<Statement> newStatements = newConstructor.getBody().statements();
    List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
    List<String> newParameterNames = new ArrayList<String>();
    // add parameters for elements passed with the instance creation
    if (!instanceCreation.arguments().isEmpty()) {
        IMethodBinding constructorBinding = getSuperConstructorBinding();
        if (constructorBinding != null) {
            SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
            ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
            String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
            for (int i = 0; i < parameterNames.length; i++) {
                String[] nameProposals = parameterNames[i];
                String paramName = nameProposals[0];
                SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
                newParameters.add(param);
                newParameterNames.add(paramName);
                SimpleName newSIArgument = ast.newSimpleName(paramName);
                superConstructorInvocation.arguments().add(newSIArgument);
                if (fLinkedProposalModel != null) {
                    LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
                    positionGroup.addPosition(astRewrite.track(param.getName()), false);
                    positionGroup.addPosition(astRewrite.track(newSIArgument), false);
                    for (int k = 0; k < nameProposals.length; k++) {
                        positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
                    }
                }
            }
            newStatements.add(superConstructorInvocation);
        }
    }
    // add parameters for all outer variables used
    boolean useThisAccess = useThisForFieldAccess();
    for (int i = 0; i < bindings.length; i++) {
        String baseName = StubUtility.getBaseName(bindings[i], project);
        String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
        String paramName = paramNameProposals[0];
        SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
        newParameters.add(param);
        newParameterNames.add(paramName);
        String fieldName = fieldNames[i];
        SimpleName fieldNameNode = ast.newSimpleName(fieldName);
        SimpleName paramNameNode = ast.newSimpleName(paramName);
        newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
            positionGroup.addPosition(astRewrite.track(param.getName()), false);
            positionGroup.addPosition(astRewrite.track(paramNameNode), false);
            for (int k = 0; k < paramNameProposals.length; k++) {
                positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
            }
            fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
        }
    }
    addExceptionsToNewConstructor(newConstructor, importRewrite);
    if (doAddComments()) {
        try {
            String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
            String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                newConstructor.setJavadoc(javadoc);
            }
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    }
    return newConstructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Aggregations

ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)62 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)34 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)32 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 AST (org.eclipse.jdt.core.dom.AST)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 SimpleName (org.eclipse.jdt.core.dom.SimpleName)18 Image (org.eclipse.swt.graphics.Image)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 Type (org.eclipse.jdt.core.dom.Type)15 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)12 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 ArrayList (java.util.ArrayList)9 IType (org.eclipse.jdt.core.IType)9 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)9 Expression (org.eclipse.jdt.core.dom.Expression)9 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9