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