Search in sources :

Example 11 with ImportDeclaration

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 12 with ImportDeclaration

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);
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 13 with ImportDeclaration

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;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Name(org.eclipse.jdt.core.dom.Name) Iterator(java.util.Iterator) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ShellException(org.mybatis.generator.exception.ShellException) Type(org.eclipse.jdt.core.dom.Type) TextEdit(org.eclipse.text.edits.TextEdit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 14 with ImportDeclaration

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;
}
Also used : TreeSet(java.util.TreeSet) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)14 Name (org.eclipse.jdt.core.dom.Name)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 HashSet (java.util.HashSet)3 List (java.util.List)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 Type (org.eclipse.jdt.core.dom.Type)3 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 Iterator (java.util.Iterator)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)2