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);
}
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();
}
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));
})));
}
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;
}
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;
}
Aggregations