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