List of usage examples for org.eclipse.jdt.core.dom StringLiteral getLiteralValue
public String getLiteralValue()
From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
BodyDeclaration declaration) throws CoreException {
List modifiers = declaration.modifiers();
SingleMemberAnnotation existing = null;
for (Object o : modifiers) {
if (o instanceof SingleMemberAnnotation) {
SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
String type = annotation.getTypeName().getFullyQualifiedName();
if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
existing = annotation;/*www .j ava 2s . c o m*/
break;
}
}
}
ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
AST ast = declaration.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
if (existing == null) {
SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(local));
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
newAnnotation.setValue(value);
ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
listRewrite.insertFirst(newAnnotation, null);
} else {
Expression existingValue = existing.getValue();
if (existingValue instanceof StringLiteral) {
StringLiteral stringLiteral = (StringLiteral) existingValue;
if (mId.equals(stringLiteral.getLiteralValue())) {
// Already contains the id
return null;
}
// Create a new array initializer holding the old string plus the new id
ArrayInitializer array = ast.newArrayInitializer();
StringLiteral old = ast.newStringLiteral();
old.setLiteralValue(stringLiteral.getLiteralValue());
array.expressions().add(old);
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
array.expressions().add(value);
rewriter.set(existing, VALUE_PROPERTY, array, null);
} else if (existingValue instanceof ArrayInitializer) {
// Existing array: just append the new string
ArrayInitializer array = (ArrayInitializer) existingValue;
List expressions = array.expressions();
if (expressions != null) {
for (Object o : expressions) {
if (o instanceof StringLiteral) {
if (mId.equals(((StringLiteral) o).getLiteralValue())) {
// Already contains the id
return null;
}
}
}
}
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
listRewrite.insertLast(value, null);
} else {
assert false : existingValue;
return null;
}
}
TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
TextEdit annotationEdits = rewriter.rewriteAST(document, null);
// Apply to the document
MultiTextEdit edit = new MultiTextEdit();
// Create the edit to change the imports, only if
// anything changed
if (importEdits.hasChildren()) {
edit.addChild(importEdits);
}
edit.addChild(annotationEdits);
return edit;
}
From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w w w. j av a 2 s . c o m*/ public boolean visit(StringLiteral node) { if (node.getLiteralValue().equals(mOldString)) { // We want to analyze the calling context to understand whether we can // just replace the string literal by the named int constant (R.id.foo) // or if we should generate a Context.getString() call. boolean useGetResource = false; useGetResource = examineVariableDeclaration(node) || examineMethodInvocation(node) || examineAssignment(node); Name qualifierName = mAst.newName(mRQualifier + ".string"); //$NON-NLS-1$ SimpleName idName = mAst.newSimpleName(mXmlId); ASTNode newNode = mAst.newQualifiedName(qualifierName, idName); boolean disabledChange = false; String title = "Replace string by ID"; if (useGetResource) { Expression context = methodHasContextArgument(node); if (context == null && !isClassDerivedFromContext(node)) { // if we don't have a class that derives from Context and // we don't have a Context method argument, then try a bit harder: // can we find a method or a field that will give us a context? context = findContextFieldOrMethod(node); if (context == null) { // If not, let's write Context.getString(), which is technically // invalid but makes it a good clue on how to fix it. Since these // will not compile, we create a disabled change by default. context = mAst.newSimpleName("Context"); //$NON-NLS-1$ disabledChange = true; } } MethodInvocation mi2 = mAst.newMethodInvocation(); mi2.setName(mAst.newSimpleName("getString")); //$NON-NLS-1$ mi2.setExpression(context); mi2.arguments().add(newNode); newNode = mi2; title = "Replace string by Context.getString(R.string...)"; } TextEditGroup editGroup = new EnabledTextEditGroup(title, !disabledChange); mEditGroups.add(editGroup); mRewriter.replace(node, newNode, editGroup); } return super.visit(node); }
From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
BodyDeclaration declaration) throws CoreException {
List modifiers = declaration.modifiers();
SingleMemberAnnotation existing = null;
for (Object o : modifiers) {
if (o instanceof SingleMemberAnnotation) {
SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
String type = annotation.getTypeName().getFullyQualifiedName();
if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
existing = annotation;//from w w w. j av a 2s. c om
break;
}
}
}
ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
AST ast = declaration.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
if (existing == null) {
SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(local));
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
newAnnotation.setValue(value);
ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
listRewrite.insertFirst(newAnnotation, null);
} else {
Expression existingValue = existing.getValue();
if (existingValue instanceof StringLiteral) {
// Create a new array initializer holding the old string plus the new id
ArrayInitializer array = ast.newArrayInitializer();
StringLiteral old = ast.newStringLiteral();
StringLiteral stringLiteral = (StringLiteral) existingValue;
old.setLiteralValue(stringLiteral.getLiteralValue());
array.expressions().add(old);
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
array.expressions().add(value);
rewriter.set(existing, VALUE_PROPERTY, array, null);
} else if (existingValue instanceof ArrayInitializer) {
// Existing array: just append the new string
ArrayInitializer array = (ArrayInitializer) existingValue;
StringLiteral value = ast.newStringLiteral();
value.setLiteralValue(mId);
ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
listRewrite.insertLast(value, null);
} else {
assert false : existingValue;
return null;
}
}
TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
TextEdit annotationEdits = rewriter.rewriteAST(document, null);
// Apply to the document
MultiTextEdit edit = new MultiTextEdit();
// Create the edit to change the imports, only if
// anything changed
if (importEdits.hasChildren()) {
edit.addChild(importEdits);
}
edit.addChild(annotationEdits);
return edit;
}
From source file:com.google.currysrc.processors.ModifyStringLiterals.java
License:Apache License
@Override public void process(Context context, CompilationUnit cu) { final ASTRewrite rewrite = context.rewrite(); ASTVisitor visitor = new ASTVisitor(false /* visitDocTags */) { @Override/*from w w w .j a va 2s .co m*/ public boolean visit(StringLiteral node) { String literalValue = node.getLiteralValue(); // TODO Replace with Pattern if (literalValue.contains(oldString)) { String newLiteralValue = literalValue.replace(oldString, newString); StringLiteral newLiteral = node.getAST().newStringLiteral(); newLiteral.setLiteralValue(newLiteralValue); rewrite.replace(node, newLiteral, null /* editGorup */); } return false; } }; cu.accept(visitor); }
From source file:com.google.dart.java2dart.SyntaxTranslator.java
License:Open Source License
@Override public boolean visit(org.eclipse.jdt.core.dom.StringLiteral node) { String tokenValue = node.getEscapedValue(); tokenValue = StringUtils.replace(tokenValue, "$", "\\$"); SimpleStringLiteral literal = new SimpleStringLiteral(token(TokenType.STRING, tokenValue), node.getLiteralValue()); context.putNodeTypeBinding(literal, node.resolveTypeBinding()); return done(literal); }
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
@Override public boolean visit(StringLiteral node) { if (isValidCppString(node)) { buffer.append('@'); buffer.append(UnicodeUtils.escapeUnicodeSequences(node.getEscapedValue())); } else {//from w ww .j av a 2 s. co m buffer.append(buildStringFromChars(node.getLiteralValue())); } return false; }
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
static boolean isValidCppString(StringLiteral node) { return UnicodeUtils.hasValidCppCharacters(node.getLiteralValue()) && !node.getEscapedValue().matches("\".*\\\\[2-3][0-9][0-9].*\""); }
From source file:com.google.devtools.j2objc.ast.DebugASTPrinter.java
License:Apache License
@Override public boolean visit(StringLiteral node) { sb.print(UnicodeUtils.escapeStringLiteral(node.getLiteralValue())); return false; }
From source file:com.google.gdt.eclipse.designer.refactoring.RemoteServiceRenameParticipant.java
License:Open Source License
/** * @return the {@link Change} for modifying service name in AST. *///w ww . j a va2 s . c o m private Change replaceServiceNameInAST(IType serviceType, final String oldServletName, final String newServletName) throws Exception { ICompilationUnit serviceCompilationUnit = serviceType.getCompilationUnit(); // parse service type into AST and change servlet path CompilationUnit serviceUnit = CodeUtils.parseCompilationUnit(serviceCompilationUnit); serviceUnit.recordModifications(); serviceUnit.accept(new ASTVisitor() { @Override public void endVisit(StringLiteral node) { if (oldServletName.equals(node.getLiteralValue())) { node.setLiteralValue(newServletName); } } }); // create text edits corresponding to that changes in AST TextEdit astTextEdit; { String source = serviceCompilationUnit.getBuffer().getContents(); Document document = new Document(source); astTextEdit = serviceUnit.rewrite(document, serviceType.getJavaProject().getOptions(true)); } // merge AST edit with existing edit for service type file { IFile serviceTypeFile = (IFile) serviceType.getUnderlyingResource(); TextChange existingTextChange = getTextChange(serviceTypeFile); if (existingTextChange != null) { existingTextChange.addEdit(astTextEdit); return null; } else { CompilationUnitChange serviceUnitChange = new CompilationUnitChange( serviceType.getFullyQualifiedName(), serviceCompilationUnit); serviceUnitChange.setEdit(astTextEdit); return serviceUnitChange; } } }
From source file:com.google.gdt.eclipse.designer.uibinder.model.util.EventHandlerProperty.java
License:Open Source License
/** * @return <code>true</code> if {@link MethodDeclaration} has "@UiHandler" annotation with * required name./*from w w w . j a va 2s.c o m*/ */ static boolean isObjectHandler(MethodDeclaration methodDeclaration, String name) { SingleMemberAnnotation handlerAnnotation = getHandlerAnnotation(methodDeclaration); if (handlerAnnotation != null && handlerAnnotation.getValue() instanceof StringLiteral) { StringLiteral handlerLiteral = (StringLiteral) handlerAnnotation.getValue(); String handlerName = handlerLiteral.getLiteralValue(); return name.equals(handlerName); } return false; }