use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project flux by eclipse.
the class QuickAssistProcessor method addExceptionToThrows.
private static void addExceptionToThrows(AST ast, MethodDeclaration methodDeclaration, ASTRewrite rewrite, Type type2) {
ITypeBinding binding = type2.resolveBinding();
if (binding == null || isNotYetThrown(binding, methodDeclaration.thrownExceptionTypes())) {
Type newType = (Type) ASTNode.copySubtree(ast, type2);
ListRewrite listRewriter = rewrite.getListRewrite(methodDeclaration, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
listRewriter.insertLast(newType, null);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project flux by eclipse.
the class ModifierRewrite method copyAllModifiers.
public void copyAllModifiers(ASTNode otherDecl, TextEditGroup editGroup, boolean copyIndividually) {
ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
List<IExtendedModifier> originalList = modifierList.getOriginalList();
if (originalList.isEmpty()) {
return;
}
if (copyIndividually) {
for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
ASTNode modifier = (ASTNode) iterator.next();
ASTNode copy = fModifierRewrite.getASTRewrite().createCopyTarget(modifier);
if (copy != null) {
//paranoia check (only left here because we're in RC1)
fModifierRewrite.insertLast(copy, editGroup);
}
}
} else {
ASTNode copy = modifierList.createCopyTarget((ASTNode) originalList.get(0), (ASTNode) originalList.get(originalList.size() - 1));
if (copy != null) {
//paranoia check (only left here because we're in RC1)
fModifierRewrite.insertLast(copy, editGroup);
}
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project generator by mybatis.
the class JavaFileMerger method getMergedSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
public String getMergedSource() throws ShellException, InvalidExistingFileException {
NewJavaFileVisitor newJavaFileVisitor = visitNewJavaFile();
IDocument document = new Document(existingJavaSource);
// delete generated stuff, and collect imports
ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(javaDocTags);
CompilationUnit cu = getCompilationUnitFromSource(existingJavaSource);
AST ast = cu.getAST();
cu.recordModifications();
cu.accept(visitor);
TypeDeclaration typeDeclaration = visitor.getTypeDeclaration();
if (typeDeclaration == null) {
throw new InvalidExistingFileException(ErrorCode.NO_TYPES_DEFINED_IN_FILE);
}
// reconcile the superinterfaces
List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration.superInterfaceTypes(), newJavaFileVisitor);
for (Type newSuperInterface : newSuperInterfaces) {
typeDeclaration.superInterfaceTypes().add(ASTNode.copySubtree(ast, newSuperInterface));
}
// set the superclass
if (newJavaFileVisitor.getSuperclass() != null) {
typeDeclaration.setSuperclassType((Type) ASTNode.copySubtree(ast, newJavaFileVisitor.getSuperclass()));
} else {
typeDeclaration.setSuperclassType(null);
}
// interface or class?
if (newJavaFileVisitor.isInterface()) {
typeDeclaration.setInterface(true);
} else {
typeDeclaration.setInterface(false);
}
// reconcile the imports
List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
for (ImportDeclaration newImport : newImports) {
Name name = ast.newName(newImport.getName().getFullyQualifiedName());
ImportDeclaration newId = ast.newImportDeclaration();
newId.setName(name);
cu.imports().add(newId);
}
TextEdit textEdit = cu.rewrite(document, null);
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException removing prior fields and methods");
}
// regenerate the CompilationUnit to reflect all the deletes and changes
CompilationUnit strippedCu = getCompilationUnitFromSource(document.get());
// find the top level public type declaration
TypeDeclaration topLevelType = null;
Iterator iter = strippedCu.types().iterator();
while (iter.hasNext()) {
TypeDeclaration td = (TypeDeclaration) iter.next();
if (td.getParent().equals(strippedCu) && (td.getModifiers() & Modifier.PUBLIC) > 0) {
topLevelType = td;
break;
}
}
// now add all the new methods and fields to the existing
// CompilationUnit with a ListRewrite
ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
ListRewrite listRewrite = rewrite.getListRewrite(topLevelType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
int i = 0;
while (astIter.hasNext()) {
ASTNode node = astIter.next();
if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
if (visitor.containsInnerClass(name)) {
continue;
}
} else if (node instanceof FieldDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getFieldAnnotations((FieldDeclaration) node));
} else if (node instanceof MethodDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getMethodAnnotations((MethodDeclaration) node));
}
listRewrite.insertAt(node, i++, null);
}
textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException adding new fields and methods");
}
String newSource = document.get();
return newSource;
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project AutoRefactor by JnRouvignac.
the class Refactorings method getListRewrite.
private ListRewrite getListRewrite(ASTNode node, ChildListPropertyDescriptor listProperty) {
final Pair<ASTNode, ChildListPropertyDescriptor> key = Pair.of(node, listProperty);
ListRewrite listRewrite = listRewriteCache.get(key);
if (listRewrite == null) {
listRewrite = rewrite.getListRewrite(node, listProperty);
listRewriteCache.put(key, listRewrite);
}
return listRewrite;
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project Main by SpartanRefactoring.
the class ASTTestClassGenerator method addStaticImports.
public static String addStaticImports(ASTNode root, String importName, String simpleName) {
if (!iz.compilationUnit(root)) {
return root.toString();
}
ListRewrite lr = ASTRewrite.create(root.getAST()).getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
ImportDeclaration id = root.getAST().newImportDeclaration();
id.setStatic(true);
id.setName(root.getAST().newName(importName.split("\\.")));
lr.insertLast(id, null);
//irw.addStaticImport(importName, simpleName, false);
try {
IDocument doc = new Document(root.toString());
TextEdit te = lr.getASTRewrite().rewriteAST(doc, null);
// TextEdit te = irw.rewriteImports(new NullProgressMonitor()); //FIXME: @orenafek: Change null to something else...
te.apply(doc);
return doc.get();
} catch (BadLocationException e) {
note.bug(e);
}
//lr.insertLast(ImportDeclaration.
return root.toString();
}
Aggregations