Search in sources :

Example 1 with StringLiteral

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

the class SuppressWarningsSubProcessor method addUnknownSuppressWarningProposals.

/**
	 * Adds a proposal to correct the name of the SuppressWarning annotation
	 * @param context the context
	 * @param problem the problem
	 * @param proposals the resulting proposals
	 */
public static void addUnknownSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = context.getCoveringNode();
    if (!(coveringNode instanceof StringLiteral))
        return;
    AST ast = coveringNode.getAST();
    StringLiteral literal = (StringLiteral) coveringNode;
    String literalValue = literal.getLiteralValue();
    String[] allWarningTokens = CorrectionEngine.getAllWarningTokens();
    for (int i = 0; i < allWarningTokens.length; i++) {
        String curr = allWarningTokens[i];
        if (NameMatcher.isSimilarName(literalValue, curr)) {
            StringLiteral newLiteral = ast.newStringLiteral();
            newLiteral.setLiteralValue(curr);
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.replace(literal, newLiteral, null);
            String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_fix_suppress_token_label, new String[] { curr });
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.FIX_SUPPRESS_TOKEN, image);
            proposals.add(proposal);
        }
    }
    addRemoveUnusedSuppressWarningProposals(context, problem, proposals);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 2 with StringLiteral

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

the class ASTNodes method getEscapedStringLiteral.

/**
     * Escapes a string value to a literal that can be used in Java source.
     *
     * @param stringValue
     *         the string value
     * @return the escaped string
     * @see org.eclipse.jdt.core.dom.StringLiteral#getEscapedValue()
     */
public static String getEscapedStringLiteral(String stringValue) {
    StringLiteral stringLiteral = AST.newAST(ASTProvider.SHARED_AST_LEVEL).newStringLiteral();
    stringLiteral.setLiteralValue(stringValue);
    return stringLiteral.getEscapedValue();
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral)

Example 3 with StringLiteral

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

the class ASTFlattenerUtils method canConvertToRichText.

public boolean canConvertToRichText(final InfixExpression node) {
    final FieldDeclaration parentFieldDecl = this.<FieldDeclaration>findParentOfType(node, FieldDeclaration.class);
    if ((parentFieldDecl != null)) {
        final TypeDeclaration typeDeclr = this.<TypeDeclaration>findParentOfType(parentFieldDecl, TypeDeclaration.class);
        if ((typeDeclr.isInterface() || (this.isFinal(parentFieldDecl.modifiers()) && this.isStatic(parentFieldDecl.modifiers())))) {
            return false;
        }
    }
    final SingleMemberAnnotation parentSingleMemberAnnotation = this.<SingleMemberAnnotation>findParentOfType(node, SingleMemberAnnotation.class);
    if ((parentSingleMemberAnnotation != null)) {
        return false;
    }
    final Iterable<StringLiteral> nodes = this.collectCompatibleNodes(node);
    return ((!IterableExtensions.isEmpty(nodes)) && IterableExtensions.<StringLiteral>forall(nodes, ((Function1<StringLiteral, Boolean>) (StringLiteral it) -> {
        return Boolean.valueOf(this.canTranslate(it));
    })));
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 4 with StringLiteral

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

the class StringRatherThanNewStringRefactoring method visit.

@Override
public boolean visit(ClassInstanceCreation node) {
    if (hasType(node, "java.lang.String") && arguments(node).size() == 1) {
        final Expression arg0 = arguments(node).get(0);
        if (hasType(arg0, "java.lang.String") && ((arg0 instanceof StringLiteral) || (arg0 instanceof InfixExpression))) {
            final ASTBuilder b = ctx.getASTBuilder();
            ctx.getRefactorings().replace(node, b.parenthesizeIfNeeded(b.copy(arg0)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 5 with StringLiteral

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

the class StringRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    final Expression expr = node.getExpression();
    final ASTNode parent = node.getParent();
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    final boolean isStringValueOf = isStringValueOf(node);
    if (isMethod(node, "java.lang.Object", "toString")) {
        if (hasType(expr, "java.lang.String")) {
            // if node is already a String, no need to call toString()
            r.replace(node, b.move(expr));
            return DO_NOT_VISIT_SUBTREE;
        } else if (parent.getNodeType() == INFIX_EXPRESSION) {
            // if node is in a String context, no need to call toString()
            final InfixExpression ie = (InfixExpression) node.getParent();
            final Expression leftOp = ie.getLeftOperand();
            final Expression rightOp = ie.getRightOperand();
            final boolean leftOpIsString = hasType(leftOp, "java.lang.String");
            final boolean rightOpIsString = hasType(rightOp, "java.lang.String");
            final MethodInvocation lmi = as(leftOp, MethodInvocation.class);
            final MethodInvocation rmi = as(rightOp, MethodInvocation.class);
            if (!node.equals(lmi) && !node.equals(rmi) && (leftOpIsString || rightOpIsString)) {
                // node is in the extended operands
                r.replace(node, replaceToString(node.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            } else if (leftOpIsString && isMethod(rmi, "java.lang.Object", "toString")) {
                r.replace(rmi, replaceToString(rmi.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            } else if (rightOpIsString && node.equals(lmi)) {
                r.replace(lmi, replaceToString(lmi.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    } else if (isStringValueOf && hasType(arg0(node), "java.lang.String")) {
        if ((arg0(node) instanceof StringLiteral) || (arg0(node) instanceof InfixExpression)) {
            r.replace(node, b.parenthesizeIfNeeded(b.move(arg0(node))));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if ((isToStringForPrimitive(node) || isStringValueOf) && parent.getNodeType() == INFIX_EXPRESSION) {
        // if node is in a String context, no need to call toString()
        final InfixExpression ie = (InfixExpression) node.getParent();
        final Expression lo = ie.getLeftOperand();
        final Expression ro = ie.getRightOperand();
        if (node.equals(lo)) {
            if (hasType(ro, "java.lang.String")) {
                replaceStringValueOfByArg0(lo, node);
                return DO_NOT_VISIT_SUBTREE;
            }
        } else if (node.equals(ro)) {
            if (hasType(lo, "java.lang.String") && // to avoid compilation errors post refactoring
            !r.hasBeenRefactored(lo)) {
                replaceStringValueOfByArg0(ro, node);
                return DO_NOT_VISIT_SUBTREE;
            }
        } else {
            // left or right operation is necessarily a string, so just replace
            replaceStringValueOfByArg0(node, node);
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "java.lang.String", "equals", "java.lang.Object")) {
        final MethodInvocation leftInvocation = as(node.getExpression(), MethodInvocation.class);
        final MethodInvocation rightInvocation = as(arg0(node), MethodInvocation.class);
        if (leftInvocation != null && rightInvocation != null && ((isMethod(leftInvocation, "java.lang.String", "toLowerCase") && isMethod(rightInvocation, "java.lang.String", "toLowerCase")) || (isMethod(leftInvocation, "java.lang.String", "toUpperCase") && isMethod(rightInvocation, "java.lang.String", "toUpperCase")))) {
            final Expression leftExpr = leftInvocation.getExpression();
            final Expression rightExpr = rightInvocation.getExpression();
            r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "java.lang.String", "equalsIgnoreCase", "java.lang.String")) {
        final AtomicBoolean isRefactoringNeeded = new AtomicBoolean(false);
        final Expression leftExpr = getReducedStringExpression(node.getExpression(), isRefactoringNeeded);
        final Expression rightExpr = getReducedStringExpression(arg0(node), isRefactoringNeeded);
        if (isRefactoringNeeded.get()) {
            r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Refactorings(org.autorefactor.refactoring.Refactorings) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)51 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)14 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 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 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)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 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)5