Search in sources :

Example 36 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 37 with ImportDeclaration

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

Example 38 with ImportDeclaration

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

Example 39 with ImportDeclaration

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);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 40 with ImportDeclaration

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

Aggregations

ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)55 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 List (java.util.List)20 ArrayList (java.util.ArrayList)17 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)16 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 Block (org.eclipse.jdt.core.dom.Block)11 Name (org.eclipse.jdt.core.dom.Name)11 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)10 TIntArrayList (gnu.trove.list.array.TIntArrayList)9 HashSet (java.util.HashSet)9 PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)7 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)7