Search in sources :

Example 31 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project xtext-xtend by eclipse.

the class ASTFlattenerUtils method collectCompatibleNodes.

private Iterable<StringLiteral> collectCompatibleNodes(final InfixExpression node) {
    final ArrayList<StringLiteral> strings = CollectionLiterals.<StringLiteral>newArrayList();
    InfixExpression.Operator _operator = node.getOperator();
    boolean _notEquals = (!Objects.equal(_operator, InfixExpression.Operator.PLUS));
    if (_notEquals) {
        return strings;
    }
    final Expression left = node.getLeftOperand();
    if ((left instanceof StringLiteral)) {
        strings.add(((StringLiteral) left));
    } else {
        if ((left instanceof InfixExpression)) {
            Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression) left)));
        }
    }
    final Expression right = node.getRightOperand();
    if ((right instanceof StringLiteral)) {
        strings.add(((StringLiteral) right));
    } else {
        if ((right instanceof InfixExpression)) {
            Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression) right)));
        }
    }
    Iterables.<StringLiteral>addAll(strings, Iterables.<StringLiteral>filter(node.extendedOperands(), StringLiteral.class));
    return strings;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 32 with StringLiteral

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

the class StubUtility method getBaseNameFromExpression.

private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
    String name = null;
    if (assignedExpression instanceof CastExpression) {
        assignedExpression = ((CastExpression) assignedExpression).getExpression();
    }
    if (assignedExpression instanceof Name) {
        Name simpleNode = (Name) assignedExpression;
        IBinding binding = simpleNode.resolveBinding();
        if (binding instanceof IVariableBinding)
            return getBaseName((IVariableBinding) binding, project);
        return ASTNodes.getSimpleNameIdentifier(simpleNode);
    } else if (assignedExpression instanceof MethodInvocation) {
        name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof FieldAccess) {
        return ((FieldAccess) assignedExpression).getName().getIdentifier();
    } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
        String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
        StringBuffer res = new StringBuffer();
        boolean needsUnderscore = false;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
                    res.append('_');
                }
                res.append(ch);
                needsUnderscore = false;
            } else {
                needsUnderscore = res.length() > 0;
            }
        }
        if (res.length() > 0) {
            return res.toString();
        }
    }
    if (name != null) {
        for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
            String curr = KNOWN_METHOD_NAME_PREFIXES[i];
            if (name.startsWith(curr)) {
                if (name.equals(curr)) {
                    // don't suggest 'get' as variable name
                    return null;
                } else if (Character.isUpperCase(name.charAt(curr.length()))) {
                    return name.substring(curr.length());
                }
            }
        }
    }
    return name;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 33 with StringLiteral

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

the class SuppressWarningsSubProcessor method addRemoveUnusedSuppressWarningProposals.

public static void addRemoveUnusedSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
    if (!(coveringNode instanceof StringLiteral))
        return;
    StringLiteral literal = (StringLiteral) coveringNode;
    if (coveringNode.getParent() instanceof MemberValuePair) {
        coveringNode = coveringNode.getParent();
    }
    ASTNode parent = coveringNode.getParent();
    ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
    if (parent instanceof SingleMemberAnnotation) {
        rewrite.remove(parent, null);
    } else if (parent instanceof NormalAnnotation) {
        NormalAnnotation annot = (NormalAnnotation) parent;
        if (annot.values().size() == 1) {
            rewrite.remove(annot, null);
        } else {
            rewrite.remove(coveringNode, null);
        }
    } else if (parent instanceof ArrayInitializer) {
        rewrite.remove(coveringNode, null);
    } else {
        return;
    }
    String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_remove_annotation_label, literal.getLiteralValue());
    //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) Image(org.eclipse.swt.graphics.Image) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 34 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class StringValueOfRatherThanConcatRefactoring method maybeReplaceStringConcatenation.

private boolean maybeReplaceStringConcatenation(final InfixExpression node, final Expression expr, final Expression variable) {
    if (expr instanceof StringLiteral && ((StringLiteral) expr).getLiteralValue().matches("") && !hasType(variable, "java.lang.String", "char[]")) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.invoke("String", "valueOf", b.copy(variable)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 35 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class StringBuilderRefactoring method replaceWithNewStringAppends.

/**
 * Rewrite the successive calls to append()
 *
 * @param node The node to replace.
 * @param allAppendedStrings All appended strings.
 * @param lastExpr The expression on which the methods are called.
 * @param isInstanceCreationToRewrite
 */
private void replaceWithNewStringAppends(final Expression node, final LinkedList<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpr, final boolean isInstanceCreationToRewrite) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    Expression result = null;
    final List<Expression> tempStringLiterals = new ArrayList<Expression>();
    final List<Expression> finalStrings = new ArrayList<Expression>();
    final AtomicBoolean isFirst = new AtomicBoolean(true);
    for (final Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
        if (appendedString.getSecond() instanceof StringLiteral) {
            tempStringLiterals.add(b.copy(appendedString.getSecond()));
        } else {
            result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
            if (isFirst.get()) {
                isFirst.set(false);
                if (!isInstanceCreationToRewrite) {
                    result = b.copy(lastExpr);
                    finalStrings.add(getTypedExpression(b, appendedString));
                } else if ((appendedString.getFirst() != null) ? hasType(appendedString.getFirst(), "java.lang.String") : hasType(appendedString.getSecond(), "java.lang.String")) {
                    result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()), getTypedExpression(b, appendedString));
                } else {
                    result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()));
                    finalStrings.add(getTypedExpression(b, appendedString));
                }
            } else {
                finalStrings.add(getTypedExpression(b, appendedString));
            }
        }
    }
    result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
    for (final Expression finalString : finalStrings) {
        if (result == null) {
            result = finalString;
        } else {
            result = b.invoke(result, "append", finalString);
        }
    }
    ctx.getRefactorings().replace(node, result);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5