Search in sources :

Example 71 with TypeDeclaration

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

the class AndroidViewHolderRefactoring method createViewHolderItemClass.

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

Example 72 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project liferay-ide by liferay.

the class MigrateProjectHandler method _shouldSearch.

@SuppressWarnings("deprecation")
private boolean _shouldSearch(File file) {
    String path = file.getAbsolutePath().replaceAll("\\\\", "/");
    if (path.contains("WEB-INF/classes") || path.contains("WEB-INF/service") || ValidationUtil.isProjectTargetDirFile(file)) {
        return false;
    }
    if (path.endsWith("java")) {
        CompilationUnit ast = CUCache.getCU(file, FileUtil.readContents(file).toCharArray());
        Name superClass = ((TypeDeclaration) ast.types().get(0)).getSuperclass();
        if (superClass != null) {
            return !_checkClassIgnore(ast, superClass.toString());
        }
        List<ASTNode> interfaces = ((TypeDeclaration) ast.types().get(0)).superInterfaces();
        if (interfaces != null) {
            for (ASTNode n : interfaces) {
                String name = n.toString();
                if (_checkClassIgnore(ast, name)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Name(org.eclipse.jdt.core.dom.Name)

Example 73 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project liferay-ide by liferay.

the class JavaFileJDT method findSuperClass.

@Override
public List<SearchResult> findSuperClass(String superClassName) {
    List<SearchResult> searchResults = new ArrayList<>();
    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(TypeDeclaration node) {
            ITypeBinding superClass = null;
            if (node.resolveBinding() != null) {
                superClass = node.resolveBinding().getSuperclass();
                if (superClass != null) {
                    String searchContext = superClass.getName();
                    if (searchContext.equals(superClassName)) {
                        int startLine = _ast.getLineNumber(node.getName().getStartPosition());
                        int startOffset = node.getName().getStartPosition();
                        int endLine = _ast.getLineNumber(node.getName().getStartPosition() + node.getName().getLength());
                        int endOffset = node.getName().getStartPosition() + node.getName().getLength();
                        searchResults.add(createSearchResult(searchContext, startOffset, endOffset, startLine, endLine, true));
                    }
                }
            }
            return true;
        }
    });
    return searchResults;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) SearchResult(com.liferay.blade.api.SearchResult) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 74 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getUniqueMethodName.

private static String getUniqueMethodName(ASTNode astNode, String suggestedName) throws JavaModelException {
    while (astNode != null && !(astNode instanceof TypeDeclaration)) {
        astNode = astNode.getParent();
    }
    if (astNode instanceof TypeDeclaration) {
        ITypeBinding typeBinding = ((TypeDeclaration) astNode).resolveBinding();
        if (typeBinding == null) {
            return suggestedName;
        }
        IType type = (IType) typeBinding.getJavaElement();
        if (type == null) {
            return suggestedName;
        }
        IMethod[] methods = type.getMethods();
        int suggestedPostfix = 2;
        String resultName = suggestedName;
        while (suggestedPostfix < 1000) {
            if (!hasMethod(methods, resultName)) {
                return resultName;
            }
            resultName = suggestedName + suggestedPostfix++;
        }
    }
    return suggestedName;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IMethod(org.eclipse.jdt.core.IMethod) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IType(org.eclipse.jdt.core.IType)

Example 75 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse.jdt.ls by eclipse.

the class UnimplementedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit root, boolean addMissingMethod, boolean makeTypeAbstract, IProblemLocation[] problems) {
    Assert.isLegal(!addMissingMethod || !makeTypeAbstract);
    if (!addMissingMethod && !makeTypeAbstract) {
        return null;
    }
    if (problems.length == 0) {
        return null;
    }
    ArrayList<CompilationUnitRewriteOperation> operations = new ArrayList<>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addMissingMethod) {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode != null && !isTypeBindingNull(typeNode)) {
                operations.add(new AddUnimplementedMethodsOperation(typeNode));
            }
        } else {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode instanceof TypeDeclaration) {
                operations.add(new MakeTypeAbstractOperation((TypeDeclaration) typeNode));
            }
        }
    }
    if (operations.size() == 0) {
        return null;
    }
    String label;
    if (addMissingMethod) {
        label = CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
    } else {
        label = CorrectionMessages.UnimplementedCodeFix_MakeAbstractFix_label;
    }
    return new UnimplementedCodeFix(label, root, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)111 ASTNode (org.eclipse.jdt.core.dom.ASTNode)64 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)51 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)40 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)38 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)24 Type (org.eclipse.jdt.core.dom.Type)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 AST (org.eclipse.jdt.core.dom.AST)22 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)21 ArrayList (java.util.ArrayList)20 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)17 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)15 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)13 Block (org.eclipse.jdt.core.dom.Block)13 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 List (java.util.List)12