Search in sources :

Example 6 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 7 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 8 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 9 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)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)9 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 AST (org.eclipse.jdt.core.dom.AST)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 Name (org.eclipse.jdt.core.dom.Name)2 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)2 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)2 LinkedCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal)2 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)2 Image (org.eclipse.swt.graphics.Image)2 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)1 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1