Search in sources :

Example 1 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ContextSensitiveImportRewriteContext method findInContext.

@Override
public int findInContext(String qualifier, String name, int kind) {
    IBinding[] declarationsInScope = getDeclarationsInScope();
    for (int i = 0; i < declarationsInScope.length; i++) {
        if (declarationsInScope[i] instanceof ITypeBinding) {
            ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
            if (isSameType(typeBinding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else if (isConflicting(typeBinding, name)) {
                return RES_NAME_CONFLICT;
            }
        } else if (declarationsInScope[i] != null) {
            if (isConflicting(declarationsInScope[i], name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    Name[] names = getImportedNames();
    for (int i = 0; i < names.length; i++) {
        IBinding binding = names[i].resolveBinding();
        if (binding instanceof ITypeBinding && !binding.isRecovered()) {
            ITypeBinding typeBinding = (ITypeBinding) binding;
            if (isConflictingType(typeBinding, qualifier, name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    List<AbstractTypeDeclaration> list = fCompilationUnit.types();
    for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
        AbstractTypeDeclaration type = iter.next();
        ITypeBinding binding = type.resolveBinding();
        if (binding != null) {
            if (isSameType(binding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else {
                ITypeBinding decl = containingDeclaration(binding, qualifier, name);
                while (decl != null && !decl.equals(binding)) {
                    int modifiers = decl.getModifiers();
                    if (Modifier.isPrivate(modifiers))
                        return RES_NAME_CONFLICT;
                    decl = decl.getDeclaringClass();
                }
            }
        }
    }
    String[] addedImports = fImportRewrite.getAddedImports();
    String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
    for (int i = 0; i < addedImports.length; i++) {
        String addedImport = addedImports[i];
        if (qualifiedName.equals(addedImport)) {
            return RES_NAME_FOUND;
        } else {
            if (isConflicting(name, addedImport))
                return RES_NAME_CONFLICT;
        }
    }
    if (qualifier.equals("java.lang")) {
        //$NON-NLS-1$
        //No explicit import statement required
        ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
        if (typeRoot != null) {
            IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
            try {
                ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
                for (int i = 0; i < compilationUnits.length; i++) {
                    ICompilationUnit cu = compilationUnits[i];
                    IType[] allTypes = cu.getAllTypes();
                    for (int j = 0; j < allTypes.length; j++) {
                        IType type = allTypes[j];
                        String packageTypeName = type.getFullyQualifiedName();
                        if (isConflicting(name, packageTypeName))
                            return RES_NAME_CONFLICT;
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    }
    return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 2 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ASTNodeFactory method newType.

public static Type newType(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPE_HEADER);
    buffer.append(content);
    buffer.append(TYPE_FOOTER);
    CheASTParser p = CheASTParser.newParser(ast.apiLevel());
    p.setSource(buffer.toString().toCharArray());
    CompilationUnit root = (CompilationUnit) p.createAST(null);
    List<AbstractTypeDeclaration> list = root.types();
    TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
    MethodDeclaration methodDecl = typeDecl.getMethods()[0];
    ASTNode type = methodDecl.getReturnType2();
    ASTNode result = ASTNode.copySubtree(ast, type);
    result.accept(new PositionClearer());
    return (Type) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 3 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class Bindings method getBindingOfParentTypeContext.

/**
     * Returns the type binding of the node's type context or null if the node is inside
     * an annotation, type parameter, super type declaration, or Javadoc of a top level type.
     * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body.
     *
     * @param node an AST node
     * @return the type binding of the node's parent type context, or <code>null</code>
     */
public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
    StructuralPropertyDescriptor lastLocation = null;
    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) {
                return decl.resolveBinding();
            } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
                return decl.resolveBinding();
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return ((AnonymousClassDeclaration) node).resolveBinding();
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return null;
}
Also used : AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration)

Example 4 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.

public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ReturnStatementCollector eval = new ReturnStatementCollector();
        decl.accept(eval);
        AST ast = astRoot.getAST();
        ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
        typeBinding = Bindings.normalizeTypeBinding(typeBinding);
        if (typeBinding == null) {
            //$NON-NLS-1$
            typeBinding = ast.resolveWellKnownType("void");
        }
        if (typeBinding.isWildcardType()) {
            typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
        }
        ASTRewrite rewrite = ASTRewrite.create(ast);
        String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
        ImportRewrite imports = proposal.createImportRewrite(astRoot);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        Type type = imports.addImport(typeBinding, ast, importRewriteContext);
        rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
        rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null && typeBinding != null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            TextElement commentStart = ast.newTextElement();
            newTag.fragments().add(commentStart);
            JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
            //$NON-NLS-1$
            proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
        }
        //$NON-NLS-1$
        String key = "return_type";
        proposal.addLinkedPosition(rewrite.track(type), true, key);
        if (typeBinding != null) {
            ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
            for (int i = 0; i < bindings.length; i++) {
                proposal.addLinkedPositionProposal(key, bindings[i]);
            }
        }
        proposals.add(proposal);
        // change to constructor
        ASTNode parentType = ASTResolving.findParentType(decl);
        if (parentType instanceof AbstractTypeDeclaration) {
            boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
            if (!isInterface) {
                String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
                ASTNode nameNode = methodDeclaration.getName();
                label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
                proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) TextElement(org.eclipse.jdt.core.dom.TextElement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 5 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class JavadocContentAccess2 method getJavadocNode.

private static Javadoc getJavadocNode(IJavaElement element, String rawJavadoc) {
    //FIXME: take from SharedASTProvider if available
    //Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
    //$NON-NLS-1$
    String source = rawJavadoc + "class C{}";
    CompilationUnit root = createAST(element, source);
    if (root == null)
        return null;
    List<AbstractTypeDeclaration> types = root.types();
    if (types.size() != 1)
        return null;
    AbstractTypeDeclaration type = types.get(0);
    return type.getJavadoc();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)91 ASTNode (org.eclipse.jdt.core.dom.ASTNode)55 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)36 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)27 AST (org.eclipse.jdt.core.dom.AST)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)18 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)16 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)15 Type (org.eclipse.jdt.core.dom.Type)15 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)15 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 ArrayList (java.util.ArrayList)13 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)13 Javadoc (org.eclipse.jdt.core.dom.Javadoc)12 IType (org.eclipse.jdt.core.IType)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)10