Search in sources :

Example 96 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class NewVariableCorrectionProposal method doAddParam.

private ASTRewrite doAddParam(CompilationUnit cu) {
    AST ast = cu.getAST();
    SimpleName node = fOriginalNode;
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(node);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewrite imports = createImportRewrite((CompilationUnit) decl.getRoot());
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        SingleVariableDeclaration newDecl = ast.newSingleVariableDeclaration();
        newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding(), TypeLocation.PARAMETER));
        newDecl.setName(ast.newSimpleName(node.getIdentifier()));
        ListRewrite listRewriter = rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
        listRewriter.insertLast(newDecl, null);
        // add javadoc tag
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null) {
            HashSet<String> leadingNames = new HashSet<>();
            for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
                SingleVariableDeclaration curr = iter.next();
                leadingNames.add(curr.getName().getIdentifier());
            }
            SimpleName newTagRef = ast.newSimpleName(node.getIdentifier());
            TagElement newTagElement = ast.newTagElement();
            newTagElement.setTagName(TagElement.TAG_PARAM);
            newTagElement.fragments().add(newTagRef);
            TextElement commentStart = ast.newTextElement();
            newTagElement.fragments().add(commentStart);
            ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
            JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
        }
        return rewrite;
    }
    return null;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) HashSet(java.util.HashSet)

Example 97 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class ImportRemover method divideTypeRefs.

private void divideTypeRefs(List<SimpleName> importNames, List<SimpleName> staticNames, List<SimpleName> removedRefs, List<SimpleName> unremovedRefs) {
    final List<int[]> removedStartsEnds = new ArrayList<>();
    fRoot.accept(new ASTVisitor(true) {

        int fRemovingStart = -1;

        @Override
        public void preVisit(ASTNode node) {
            Object property = node.getProperty(PROPERTY_KEY);
            if (property == REMOVED) {
                if (fRemovingStart == -1) {
                    fRemovingStart = node.getStartPosition();
                } else {
                    /*
						 * Bug in client code: REMOVED node should not be nested inside another REMOVED node without
						 * an intermediate RETAINED node.
						 * Drop REMOVED property to prevent problems later (premature end of REMOVED section).
						 */
                    node.setProperty(PROPERTY_KEY, null);
                }
            } else if (property == RETAINED) {
                if (fRemovingStart != -1) {
                    removedStartsEnds.add(new int[] { fRemovingStart, node.getStartPosition() });
                    fRemovingStart = -1;
                } else {
                    /*
						 * Bug in client code: RETAINED node should not be nested inside another RETAINED node without
						 * an intermediate REMOVED node and must have an enclosing REMOVED node.
						 * Drop RETAINED property to prevent problems later (premature restart of REMOVED section).
						 */
                    node.setProperty(PROPERTY_KEY, null);
                }
            }
            super.preVisit(node);
        }

        @Override
        public void postVisit(ASTNode node) {
            Object property = node.getProperty(PROPERTY_KEY);
            if (property == RETAINED) {
                int end = node.getStartPosition() + node.getLength();
                fRemovingStart = end;
            } else if (property == REMOVED) {
                if (fRemovingStart != -1) {
                    int end = node.getStartPosition() + node.getLength();
                    removedStartsEnds.add(new int[] { fRemovingStart, end });
                    fRemovingStart = -1;
                }
            }
            super.postVisit(node);
        }
    });
    for (Iterator<SimpleName> iterator = importNames.iterator(); iterator.hasNext(); ) {
        SimpleName name = iterator.next();
        if (isInRemoved(name, removedStartsEnds)) {
            removedRefs.add(name);
        } else {
            unremovedRefs.add(name);
        }
    }
    for (Iterator<SimpleName> iterator = staticNames.iterator(); iterator.hasNext(); ) {
        SimpleName name = iterator.next();
        if (isInRemoved(name, removedStartsEnds)) {
            removedRefs.add(name);
        } else {
            unremovedRefs.add(name);
        }
    }
    for (Iterator<ImportDeclaration> iterator = fInlinedStaticImports.iterator(); iterator.hasNext(); ) {
        ImportDeclaration importDecl = iterator.next();
        Name name = importDecl.getName();
        if (name instanceof QualifiedName) {
            name = ((QualifiedName) name).getName();
        }
        removedRefs.add((SimpleName) name);
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 98 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class ChangeMethodSignatureProposal method modifyParameters.

private void modifyParameters(ASTRewrite rewrite, MethodDeclaration methodDecl) {
    AST ast = methodDecl.getAST();
    ArrayList<String> usedNames = new ArrayList<>();
    boolean hasCreatedVariables = false;
    IVariableBinding[] declaredFields = fSenderBinding.getDeclaringClass().getDeclaredFields();
    for (int i = 0; i < declaredFields.length; i++) {
        // avoid to take parameter names that are equal to field names
        usedNames.add(declaredFields[i].getName());
    }
    ImportRewrite imports = getImportRewrite();
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
    ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY);
    // old parameters
    List<SingleVariableDeclaration> parameters = methodDecl.parameters();
    // index over the oldParameters
    int k = 0;
    for (int i = 0; i < fParameterChanges.length; i++) {
        ChangeDescription curr = fParameterChanges[i];
        if (curr == null) {
            SingleVariableDeclaration oldParam = parameters.get(k);
            usedNames.add(oldParam.getName().getIdentifier());
            k++;
        } else if (curr instanceof InsertDescription) {
            InsertDescription desc = (InsertDescription) curr;
            SingleVariableDeclaration newNode = ast.newSingleVariableDeclaration();
            newNode.setType(imports.addImport(desc.type, ast, context, TypeLocation.PARAMETER));
            // $NON-NLS-1$
            newNode.setName(ast.newSimpleName("x"));
            // remember to set name later
            desc.resultingParamName = new SimpleName[] { newNode.getName() };
            desc.resultingParamType = newNode.getType();
            hasCreatedVariables = true;
            listRewrite.insertAt(newNode, i, null);
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null) {
                TagElement newTagElement = ast.newTagElement();
                newTagElement.setTagName(TagElement.TAG_PARAM);
                // $NON-NLS-1$
                SimpleName arg = ast.newSimpleName("x");
                newTagElement.fragments().add(arg);
                insertParamTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), parameters, k, newTagElement);
                // set the name later
                desc.resultingTagArg = arg;
            } else {
                desc.resultingTagArg = null;
            }
        } else if (curr instanceof RemoveDescription) {
            SingleVariableDeclaration decl = parameters.get(k);
            listRewrite.remove(decl, null);
            k++;
            TagElement tagNode = findParamTag(methodDecl, decl);
            if (tagNode != null) {
                rewrite.remove(tagNode, null);
            }
        } else if (curr instanceof EditDescription) {
            EditDescription desc = (EditDescription) curr;
            ITypeBinding newTypeBinding = desc.type;
            SingleVariableDeclaration decl = parameters.get(k);
            if (k == parameters.size() - 1 && i == fParameterChanges.length - 1 && decl.isVarargs() && newTypeBinding.isArray()) {
                // stick with varargs if it was before
                newTypeBinding = newTypeBinding.getElementType();
            } else {
                rewrite.set(decl, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
            }
            Type newType = imports.addImport(newTypeBinding, ast, context, TypeLocation.PARAMETER);
            rewrite.replace(decl.getType(), newType, null);
            DimensionRewrite.removeAllChildren(decl, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            TypeAnnotationRewrite.removePureTypeAnnotations(decl, SingleVariableDeclaration.MODIFIERS2_PROPERTY, rewrite, null);
            IBinding binding = decl.getName().resolveBinding();
            if (binding != null) {
                SimpleName[] names = LinkedNodeFinder.findByBinding(decl.getRoot(), binding);
                SimpleName[] newNames = new SimpleName[names.length];
                for (int j = 0; j < names.length; j++) {
                    // $NON-NLS-1$  // name will be set later
                    SimpleName newName = ast.newSimpleName("x");
                    newNames[j] = newName;
                    rewrite.replace(names[j], newName, null);
                }
                desc.resultingParamName = newNames;
            } else {
                // $NON-NLS-1$  // name will be set later
                SimpleName newName = ast.newSimpleName("x");
                rewrite.replace(decl.getName(), newName, null);
                // remember to set name later
                desc.resultingParamName = new SimpleName[] { newName };
            }
            desc.resultingParamType = newType;
            desc.orginalName = decl.getName().getIdentifier();
            hasCreatedVariables = true;
            k++;
            TagElement tagNode = findParamTag(methodDecl, decl);
            if (tagNode != null) {
                List<? extends ASTNode> fragments = tagNode.fragments();
                if (!fragments.isEmpty()) {
                    // $NON-NLS-1$
                    SimpleName arg = ast.newSimpleName("x");
                    rewrite.replace(fragments.get(0), arg, null);
                    desc.resultingTagArg = arg;
                }
            }
        } else if (curr instanceof SwapDescription) {
            SingleVariableDeclaration decl1 = parameters.get(k);
            SingleVariableDeclaration decl2 = parameters.get(((SwapDescription) curr).index);
            rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
            rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
            usedNames.add(decl1.getName().getIdentifier());
            k++;
            TagElement tagNode1 = findParamTag(methodDecl, decl1);
            TagElement tagNode2 = findParamTag(methodDecl, decl2);
            if (tagNode1 != null && tagNode2 != null) {
                rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
                rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
            }
        }
    }
    if (!hasCreatedVariables) {
        return;
    }
    if (methodDecl.getBody() != null) {
        // avoid take a name of a local variable inside
        CompilationUnit root = (CompilationUnit) methodDecl.getRoot();
        IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsAfter(methodDecl.getBody().getStartPosition(), ScopeAnalyzer.VARIABLES);
        for (int i = 0; i < bindings.length; i++) {
            usedNames.add(bindings[i].getName());
        }
    }
    fixupNames(rewrite, usedNames);
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) ArrayList(java.util.ArrayList) List(java.util.List) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Example 99 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class ChangeMethodSignatureProposal method fixupNames.

private void fixupNames(ASTRewrite rewrite, ArrayList<String> usedNames) {
    AST ast = rewrite.getAST();
    // set names for new parameters
    for (int i = 0; i < fParameterChanges.length; i++) {
        ChangeDescription curr = fParameterChanges[i];
        if (curr instanceof ModifyDescription) {
            ModifyDescription desc = (ModifyDescription) curr;
            String typeKey = getParamTypeGroupId(i);
            String nameKey = getParamNameGroupId(i);
            // collect name suggestions
            String favourite = null;
            String[] excludedNames = usedNames.toArray(new String[usedNames.size()]);
            String suggestedName = desc.name;
            if (suggestedName != null) {
                favourite = StubUtility.suggestArgumentName(getCompilationUnit().getJavaProject(), suggestedName, excludedNames);
            }
            Type type = desc.resultingParamType;
            String[] suggestedNames = StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), type, excludedNames);
            if (favourite == null) {
                favourite = suggestedNames[0];
            }
            usedNames.add(favourite);
            SimpleName[] names = desc.resultingParamName;
            for (int j = 0; j < names.length; j++) {
                names[j].setIdentifier(favourite);
            }
            SimpleName tagArg = desc.resultingTagArg;
            if (tagArg != null) {
                tagArg.setIdentifier(favourite);
            }
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 100 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class GetterSetterCorrectionSubProcessor method addGetterSetterProposal.

private static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, Collection<CUCorrectionProposal> proposals) {
    if (!(coveringNode instanceof SimpleName)) {
        return false;
    }
    SimpleName sn = (SimpleName) coveringNode;
    IBinding binding = sn.resolveBinding();
    if (!(binding instanceof IVariableBinding)) {
        return false;
    }
    IVariableBinding variableBinding = (IVariableBinding) binding;
    if (!variableBinding.isField()) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    CUCorrectionProposal proposal = getProposal(context.getCompilationUnit(), sn, variableBinding);
    if (proposal != null) {
        proposals.add(proposal);
    }
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32