use of org.eclipse.jdt.core.dom.ImportDeclaration 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.ImportDeclaration in project flux by eclipse.
the class ScopeAnalyzer method getUsedVariableNames.
public Collection<String> getUsedVariableNames(int offset, int length) {
HashSet<String> result = new HashSet<String>();
IBinding[] bindingsBefore = getDeclarationsInScope(offset, VARIABLES);
for (int i = 0; i < bindingsBefore.length; i++) {
result.add(bindingsBefore[i].getName());
}
IBinding[] bindingsAfter = getDeclarationsAfter(offset + length, VARIABLES);
for (int i = 0; i < bindingsAfter.length; i++) {
result.add(bindingsAfter[i].getName());
}
List<ImportDeclaration> imports = fRoot.imports();
for (int i = 0; i < imports.size(); i++) {
ImportDeclaration decl = imports.get(i);
if (decl.isStatic() && !decl.isOnDemand()) {
result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
}
}
return result;
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project AutoRefactor by JnRouvignac.
the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method visit.
@Override
public boolean visit(final TypeDeclaration visited) {
ITypeBinding typeBinding = visited.resolveBinding();
if (typeBinding != null && !typeBinding.isNested() && visited.getParent() instanceof CompilationUnit) {
CompilationUnit compilationUnit = (CompilationUnit) visited.getParent();
for (ImportDeclaration importDecl : (List<ImportDeclaration>) compilationUnit.imports()) {
readImport(importDecl);
}
// $NON-NLS-1$
importTypesFromPackage("java.lang", compilationUnit);
visited.accept(new NamesCollector());
}
return true;
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project AutoRefactor by JnRouvignac.
the class TestNGAssertCleanUp method visit.
@Override
public boolean visit(final CompilationUnit visited) {
canUseAssertNotEquals = false;
for (Object object : visited.imports()) {
ImportDeclaration importDeclaration = (ImportDeclaration) object;
ITypeBinding typeBinding = resolveTypeBinding(importDeclaration);
if (ASTNodes.hasType(typeBinding, TESTNG_CLASS)) {
for (IMethodBinding mb : typeBinding.getDeclaredMethods()) {
if (mb.toString().contains("assertNotEquals")) {
// $NON-NLS-1$
canUseAssertNotEquals = true;
return super.visit(visited);
}
}
}
}
// New file: reset the value
return super.visit(visited);
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method newImportDeclaration.
/**
* Builds a new {@link ImportDeclaration} instance.
*
* @param name the if name
* @return a new if statement
*/
public ImportDeclaration newImportDeclaration(final Name name) {
ImportDeclaration id = ast.newImportDeclaration();
id.setName(name);
id.setStatic(false);
id.setOnDemand(false);
return id;
}
Aggregations