use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class ReorgCorrectionsSubProcessor method importNotFoundProposals.
public static void importNotFoundProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
ImportDeclaration importDeclaration = (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
if (importDeclaration == null) {
return;
}
if (!importDeclaration.isOnDemand()) {
Name name = importDeclaration.getName();
if (importDeclaration.isStatic() && name.isQualifiedName()) {
name = ((QualifiedName) name).getQualifier();
}
int kind = JavaModelUtil.is50OrHigher(cu.getJavaProject()) ? SimilarElementsRequestor.REF_TYPES : SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
UnresolvedElementsSubProcessor.addNewTypeProposals(cu, name, kind, IProposalRelevance.IMPORT_NOT_FOUND_NEW_TYPE, proposals);
}
String name = ASTNodes.asString(importDeclaration.getName());
if (importDeclaration.isOnDemand()) {
//$NON-NLS-1$
name = JavaModelUtil.concatenateName(name, "*");
}
addProjectSetupFixProposal(context, problem, name, proposals);
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class ImportRemover method divideTypeRefs.
private void divideTypeRefs(List<SimpleName> importNames, List<SimpleName> staticNames, List<SimpleName> removedRefs, List<SimpleName> unremovedRefs) {
final List<int[]> removedStartsEnds = new ArrayList<int[]>();
fRoot.accept(new ASTVisitor(true) {
int fRemovingStart = -1;
@Override
public void preVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == REMOVED) {
if (fRemovingStart == -1) {
fRemovingStart = node.getStartPosition();
} else {
/*
* Bug in client code: REMOVED node should not be nested inside another REMOVED node without
* an intermediate RETAINED node.
* Drop REMOVED property to prevent problems later (premature end of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
} else if (property == RETAINED) {
if (fRemovingStart != -1) {
removedStartsEnds.add(new int[] { fRemovingStart, node.getStartPosition() });
fRemovingStart = -1;
} else {
/*
* Bug in client code: RETAINED node should not be nested inside another RETAINED node without
* an intermediate REMOVED node and must have an enclosing REMOVED node.
* Drop RETAINED property to prevent problems later (premature restart of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
}
super.preVisit(node);
}
@Override
public void postVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == RETAINED) {
int end = node.getStartPosition() + node.getLength();
fRemovingStart = end;
} else if (property == REMOVED) {
if (fRemovingStart != -1) {
int end = node.getStartPosition() + node.getLength();
removedStartsEnds.add(new int[] { fRemovingStart, end });
fRemovingStart = -1;
}
}
super.postVisit(node);
}
});
for (Iterator<SimpleName> iterator = importNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<SimpleName> iterator = staticNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<ImportDeclaration> iterator = fInlinedStaticImports.iterator(); iterator.hasNext(); ) {
ImportDeclaration importDecl = iterator.next();
Name name = importDecl.getName();
if (name instanceof QualifiedName)
name = ((QualifiedName) name).getName();
removedRefs.add((SimpleName) name);
}
}
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 AutoRefactor by JnRouvignac.
the class TypeNameDecider method getImportedTypes.
private static NavigableSet<String> getImportedTypes(CompilationUnit cu) {
final TreeSet<String> results = new TreeSet<String>();
for (ImportDeclaration importDecl : imports(cu)) {
Name importName = importDecl.getName();
results.add(importName.getFullyQualifiedName());
}
return results;
}
Aggregations