Search in sources :

Example 91 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux by eclipse.

the class ScopeAnalyzer method addLocalDeclarations.

private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
    if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
        if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
            ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
            declaration.accept(visitor);
            return visitor.fBreak;
        }
    }
    return false;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 92 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration 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 93 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.

the class AndroidViewHolderCleanUp method createViewHolderItemClass.

private TypeDeclaration createViewHolderItemClass(final FindViewByIdVisitor findViewByIdVisitor, final SimpleName typeName, final TypeNameDecider typeNameDecider) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TypeDeclaration result = ast.getAST().newTypeDeclaration();
    Collections.addAll(result.modifiers(), ast.private0(), ast.static0());
    result.setName(typeName);
    List<BodyDeclaration> viewItemsFieldDecls = result.bodyDeclarations();
    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
        viewItemsFieldDecls.add(item.toFieldDecl(ast, typeNameDecider));
    }
    return result;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 94 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.

the class ObsoleteRedundantComparatorCleanUp method isClassToRemove.

private boolean isClassToRemove(final ClassInstanceCreation classInstanceCreation, final boolean isForward) {
    AnonymousClassDeclaration anonymousClassDecl = classInstanceCreation.getAnonymousClassDeclaration();
    Type type = classInstanceCreation.getType();
    if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && classInstanceCreation.arguments().isEmpty() && anonymousClassDecl != null) {
        List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
        ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
        if (bodies != null && bodies.size() == 1 && typeArgument != null) {
            BodyDeclaration body = bodies.get(0);
            if (body instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) body;
                ReturnStatement returnStatement = ASTNodes.as(methodDecl.getBody(), ReturnStatement.class);
                if (returnStatement != null && returnStatement.getExpression() != null && // $NON-NLS-1$
                ASTNodes.usesGivenSignature(// $NON-NLS-1$
                methodDecl, // $NON-NLS-1$
                Comparator.class.getCanonicalName(), // $NON-NLS-1$
                "compare", typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
                    VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
                    VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
                    return isReturnedExpressionToRemove(object1.getName(), object2.getName(), returnStatement.getExpression(), isForward);
                }
            }
        }
    }
    return false;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 95 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method maybeRefactorClassInstanceCreation.

private boolean maybeRefactorClassInstanceCreation(final ClassInstanceCreation visited, final Set<String> classesToUseWithImport) {
    AnonymousClassDeclaration anonymousClassDecl = visited.getAnonymousClassDeclaration();
    Type type = visited.getType();
    if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && visited.arguments().isEmpty() && anonymousClassDecl != null && anonymousClassDecl.bodyDeclarations() != null && anonymousClassDecl.bodyDeclarations().size() == 1) {
        List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
        ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
        if (bodies != null && bodies.size() == 1 && typeArgument != null) {
            BodyDeclaration body = bodies.get(0);
            if (body instanceof MethodDeclaration) {
                return maybeRefactorMethod(visited, typeArgument, (MethodDeclaration) body, classesToUseWithImport);
            }
        }
    }
    return true;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Aggregations

BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)98 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)53 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)26 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 Type (org.eclipse.jdt.core.dom.Type)25 AST (org.eclipse.jdt.core.dom.AST)23 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)17 Expression (org.eclipse.jdt.core.dom.Expression)16 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)15 Initializer (org.eclipse.jdt.core.dom.Initializer)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 ArrayList (java.util.ArrayList)12