Search in sources :

Example 21 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project flux 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 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 22 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project flux 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 23 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse-pmd by acanda.

the class UseIndexOfCharQuickFix method apply.

/**
 * Replaces the string literal <code>"a"</code> in <code>s.indexOf("a")</code> with the character literal
 * <code>'a'</code>.
 */
@Override
protected boolean apply(final MethodInvocation node) {
    @SuppressWarnings("unchecked") final List<Expression> arguments = node.arguments();
    if (arguments.size() == 1 && arguments.get(0) instanceof StringLiteral) {
        final CharacterLiteral character = node.getAST().newCharacterLiteral();
        final StringLiteral string = (StringLiteral) arguments.get(0);
        character.setEscapedValue(toCharValue(string.getEscapedValue()));
        replace(string, character);
        return true;
    }
    return false;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 24 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse-pmd by acanda.

the class SuppressWarningsQuickFix method createPMDLiteralValue.

/**
 * Create the "PMD.<i>RuleName</i>" string literal for the {@code @SuppressWarnings} annotation.
 */
private StringLiteral createPMDLiteralValue(final AST ast) {
    final StringLiteral newValue = (StringLiteral) ast.createInstance(StringLiteral.class);
    newValue.setLiteralValue("PMD." + marker.getRuleName());
    return newValue;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral)

Example 25 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse-pmd by acanda.

the class AddEmptyStringQuickFix method apply.

@Override
@SuppressWarnings("unchecked")
protected boolean apply(final InfixExpression node) {
    final Expression rightOperand = node.getRightOperand();
    // "" + x.toString() -> x.toString()
    if (isString(rightOperand)) {
        return replace(node, copy(rightOperand));
    }
    // "" + 'a' -> "a"
    if (isCharacterLiteral(rightOperand)) {
        final AST ast = node.getAST();
        final StringLiteral stringLiteral = ast.newStringLiteral();
        final String escapedCharacter = ((CharacterLiteral) rightOperand).getEscapedValue();
        stringLiteral.setEscapedValue(convertToEscapedString(escapedCharacter));
        return replace(node, stringLiteral);
    }
    // "" + x -> String.valueOf(x)
    final AST ast = node.getAST();
    final MethodInvocation toString = ast.newMethodInvocation();
    toString.setExpression(ast.newSimpleName("String"));
    toString.setName(ast.newSimpleName("valueOf"));
    toString.arguments().add(copy(rightOperand));
    return replace(node, toString);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

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