List of usage examples for org.eclipse.jdt.core.dom BodyDeclaration getAST
public final AST getAST()
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;/* 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) {
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.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addTargetApiAnnotation(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_TARGET_API) || type.endsWith(TARGET_API)) {
existing = annotation;//from w w w . ja v a2s. c om
break;
}
}
}
ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
importRewrite.addImport("android.os.Build"); //$NON-NLS-1$
String local = importRewrite.addImport(FQCN_TARGET_API);
AST ast = declaration.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
if (existing == null) {
SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(local));
Expression value = createLiteral(ast);
newAnnotation.setValue(value);
ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
listRewrite.insertFirst(newAnnotation, null);
} else {
Expression value = createLiteral(ast);
rewriter.set(existing, VALUE_PROPERTY, value, null);
}
TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
TextEdit annotationEdits = rewriter.rewriteAST(document, null);
MultiTextEdit edit = new MultiTextEdit();
if (importEdits.hasChildren()) {
edit.addChild(importEdits);
}
edit.addChild(annotationEdits);
return edit;
}
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 ww.j a v a 2 s .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.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addTargetApiAnnotation(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_TARGET_API) || type.endsWith(TARGET_API)) {
existing = annotation;/*from w w w . java 2 s. co m*/
break;
}
}
}
ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
String local = importRewrite.addImport(FQCN_TARGET_API);
AST ast = declaration.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
if (existing == null) {
SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(local));
NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi));
//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 NumberLiteral) {
// Change the value to the new value
NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi));
rewriter.set(existing, VALUE_PROPERTY, value, null);
} else {
assert false : existingValue;
return null;
}
}
TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
TextEdit annotationEdits = rewriter.rewriteAST(document, null);
MultiTextEdit edit = new MultiTextEdit();
if (importEdits.hasChildren()) {
edit.addChild(importEdits);
}
edit.addChild(annotationEdits);
return edit;
}
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java
License:Open Source License
@Override protected void updateUID(Object element, Object correspondingElement) { if (element instanceof BodyDeclaration) { BodyDeclaration node = (BodyDeclaration) element; Javadoc javadoc = node.getJavadoc(); // if it doesn't have any doc, create it if (javadoc == null) { javadoc = node.getAST().newJavadoc(); node.setJavadoc(javadoc);// w w w . jav a 2 s .c om } // first remove the existing flower tag, this way we also make sure that it's the last tag // note: if we only change the id, the rewriter won't format it correctly for (Object obj : javadoc.tags()) { if (FLOWER_UID.equals(((TagElement) obj).getTagName())) { javadoc.tags().remove(obj); break; } } // create new tag element for UID TagElement tag = javadoc.getAST().newTagElement(); tag.setTagName(FLOWER_UID); javadoc.tags().add(tag); TextElement text = javadoc.getAST().newTextElement(); tag.fragments().add(text); EObject eObject = (EObject) correspondingElement; text.setText(eObject.eResource().getURIFragment(eObject)); System.out.println(javadoc); } }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java
License:Open Source License
protected void setJavaDoc(Object element, Object docComment) { if (element instanceof BodyDeclaration) { BodyDeclaration node = (BodyDeclaration) element; ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS); parser.setSource(("/** " + docComment + "*/ int x;").toCharArray()); TypeDeclaration type = (TypeDeclaration) parser.createAST(null); BodyDeclaration x = (BodyDeclaration) type.bodyDeclarations().get(0); Javadoc javadoc = x.getJavadoc(); node.setJavadoc((Javadoc) ASTNode.copySubtree(node.getAST(), javadoc)); }/*from w w w .j av a 2s. c o m*/ }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java
License:Open Source License
/** * @flowerModelElementId _zVs8XZiOEd6aNMdNFvR5WQ *//* w w w . j a v a2 s . com*/ @SuppressWarnings("unchecked") public static void updateVisibilityFromModelToJavaClass(BodyDeclaration bd, VisibilityKind visibilityKind) {// NamedElement // modelElement) { ModifierKeyword javaVisibFlag = getJavaVisibilityFlagFromJavaClass(bd); ModifierKeyword modelVisibFlag = convertModelToJavaVisibilityFlag(visibilityKind); if (modelVisibFlag != javaVisibFlag) { if (javaVisibFlag != null) { // /meaning the package visibility // Which to remove? Modifier toRemove = null; for (Iterator<Modifier> it = bd.modifiers().iterator(); it.hasNext();) { Modifier modif = it.next(); if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) { toRemove = modif; break; } } bd.modifiers().remove(toRemove); } // Adding if (modelVisibFlag != null) { Modifier newModif = bd.getAST().newModifier(modelVisibFlag); bd.modifiers().add(newModif); } } }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java
License:Open Source License
/** * Updates java code according to the model value for abstract or static modifiers * /*w w w . j a va 2 s . com*/ * @author Luiza * @param bd <code>{@link BodyDeclaration}</code> for a class, method or field. * @param isTrue value for the modifier * @param modifier flag indicating the modifier to update. * This should be one of the predefined flags: * <ul> * <li>{@link #MODIFIER_ABSTRACT}</li> * <li>{@link #MODIFIER_STATIC}</li> * </ul> * @flowerModelElementId _zVs8cJiOEd6aNMdNFvR5WQ */ @SuppressWarnings("unchecked") public static void updateModifierFromModelToJavaClass(BodyDeclaration bd, boolean isTrue, String modifier) { ModifierKeyword javaModifierFlag = null; ModifierKeyword modelModifierFlag = null; boolean isForAbstractModif = modifier.equals(MODIFIER_ABSTRACT); boolean isForStaticModif = modifier.equals(MODIFIER_STATIC); boolean isForFinalModif = modifier.equals(MODIFIER_FINAL); if (isForAbstractModif) { javaModifierFlag = Modifier.isAbstract(bd.getModifiers()) ? ModifierKeyword.ABSTRACT_KEYWORD : null; modelModifierFlag = isTrue ? ModifierKeyword.ABSTRACT_KEYWORD : null; } else if (isForStaticModif) { javaModifierFlag = Modifier.isStatic(bd.getModifiers()) ? ModifierKeyword.STATIC_KEYWORD : null; modelModifierFlag = isTrue ? ModifierKeyword.STATIC_KEYWORD : null; } else if (isForFinalModif) { javaModifierFlag = Modifier.isFinal(bd.getModifiers()) ? ModifierKeyword.FINAL_KEYWORD : null; modelModifierFlag = isTrue ? ModifierKeyword.FINAL_KEYWORD : null; } else throw new IllegalArgumentException( "updateModifierFromModelToJavaClass - can't update java code for modifier " + modifier); //if there are differences between the model and the java file if (modelModifierFlag != javaModifierFlag) { //if this modifier has been set before find it and remove it if (javaModifierFlag != null) { //Modifier toRemove = null; /* * we use bd.modifiers() instead of bd.getModifiers(), which computes a bit-wise integer consisting in all the modifier values * because it also iterates over the modifiers and method bd.setModifiers(modifiers) is deprecated */ for (Iterator<Modifier> it = bd.modifiers().iterator(); it.hasNext();) { Modifier modif = it.next(); if (isForAbstractModif && modif.isAbstract()) { it.remove(); break; } else if (isForStaticModif && modif.isStatic()) { it.remove(); break; } else if (isForFinalModif && modif.isFinal()) { it.remove(); break; } } //bd.modifiers().remove(toRemove); } // add the new modifier value if changes have been made in the model if (modelModifierFlag != null) { Modifier newModif = bd.getAST().newModifier(modelModifierFlag); bd.modifiers().add(newModif); } } }
From source file:com.google.currysrc.api.process.JavadocUtils.java
License:Apache License
public static void addJavadocTag(ASTRewrite rewrite, BodyDeclaration node, String tagText) { Javadoc javadoc = node.getJavadoc(); if (javadoc == null) { AST ast = node.getAST(); javadoc = (Javadoc) ast.createInstance(Javadoc.class); rewrite.set(node, node.getJavadocProperty(), javadoc, null /* editGroup */); }/*ww w. j av a 2 s.c o m*/ addJavadocTag(rewrite, javadoc, tagText); }
From source file:com.halware.nakedide.eclipse.ext.annot.ast.AbstractAnnotationEvaluatorOrModifier.java
License:Open Source License
private void addMarkerAnnotation(ICompilationUnit compilationUnit, BodyDeclaration declaration) throws MalformedTreeException, BadLocationException, CoreException { Annotation annotation = ASTTools.annotation(declaration, getAnnotationUnqualifiedName()); if (annotation != null) { return;/*from ww w . j a v a2 s . co m*/ } AST ast = declaration.getAST(); annotation = AstUtils.createMarkerAnnotation(ast, getAnnotationFullyQualifiedName()); annotation.setSourceRange(AstUtils.calculateOffset(declaration, true).offset, annotation.getLength()); ImportDeclaration importDeclaration = AstUtils.createImportStatement(ast, getAnnotationFullyQualifiedName()); AstUtils.rewriteAddFirst(compilationUnit, declaration, annotation, importDeclaration); }