Search in sources :

Example 21 with TypeDeclaration

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

the class PromoteTempToFieldRefactoring method checkClashesInConstructors.

private RefactoringStatus checkClashesInConstructors() {
    Assert.isTrue(fInitializeIn == INITIALIZE_IN_CONSTRUCTOR);
    Assert.isTrue(!isDeclaredInAnonymousClass());
    final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
    if (declaration instanceof TypeDeclaration) {
        MethodDeclaration[] methods = ((TypeDeclaration) declaration).getMethods();
        for (int i = 0; i < methods.length; i++) {
            MethodDeclaration method = methods[i];
            if (!method.isConstructor())
                continue;
            NameCollector nameCollector = new NameCollector(method) {

                @Override
                protected boolean visitNode(ASTNode node) {
                    return true;
                }
            };
            method.accept(nameCollector);
            List<String> names = nameCollector.getNames();
            if (names.contains(fFieldName)) {
                String[] keys = { BasicElementLabels.getJavaElementName(fFieldName), BindingLabelProvider.getBindingLabel(method.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED) };
                String msg = Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict, keys);
                return RefactoringStatus.createFatalErrorStatus(msg);
            }
        }
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 22 with TypeDeclaration

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

the class UnresolvedElementsSubProcessor method addNewTypeProposals.

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
    Name node = refNode;
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
            // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding != null && binding.isRecovered()) {
                    binding = null;
                }
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }
            if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
                // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
                //TODO NewCUUsingWizardProposal
                if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
                }
                if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
                }
                if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
                }
                if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
                    //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
                    addNullityAnnotationTypesProposals(cu, node, proposals);
                }
            }
        }
        node = qualifier;
    } while (node != null);
    // type parameter proposals
    if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 8;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
                if (isLikelyMethodTypeParameterName(name))
                    rel += 2;
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AddTypeParameterProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddTypeParameterProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 23 with TypeDeclaration

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

the class ModifierCorrectionSubProcessor method addAbstractTypeProposals.

public static void addAbstractTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    TypeDeclaration parentTypeDecl = null;
    if (selectedNode instanceof SimpleName) {
        ASTNode parent = selectedNode.getParent();
        if (parent != null) {
            parentTypeDecl = (TypeDeclaration) parent;
        }
    } else if (selectedNode instanceof TypeDeclaration) {
        parentTypeDecl = (TypeDeclaration) selectedNode;
    }
    if (parentTypeDecl == null) {
        return;
    }
    addMakeTypeAbstractProposal(context, parentTypeDecl, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 24 with TypeDeclaration

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

the class ModifierCorrectionSubProcessor method addAbstractMethodProposals.

public static void addAbstractMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    MethodDeclaration decl;
    if (selectedNode instanceof SimpleName) {
        decl = (MethodDeclaration) selectedNode.getParent();
    } else if (selectedNode instanceof MethodDeclaration) {
        decl = (MethodDeclaration) selectedNode;
    } else {
        return;
    }
    ASTNode parentType = ASTResolving.findParentType(decl);
    TypeDeclaration parentTypeDecl = null;
    boolean parentIsAbstractClass = false;
    if (parentType instanceof TypeDeclaration) {
        parentTypeDecl = (TypeDeclaration) parentType;
        parentIsAbstractClass = !parentTypeDecl.isInterface() && Modifier.isAbstract(parentTypeDecl.getModifiers());
    }
    boolean hasNoBody = decl.getBody() == null;
    int id = problem.getProblemId();
    if (id == IProblem.AbstractMethodInAbstractClass || id == IProblem.EnumAbstractMethodMustBeImplemented || id == IProblem.AbstractMethodInEnum || parentIsAbstractClass) {
        AST ast = astRoot.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        removeModifier(decl, rewrite, Modifier.ABSTRACT);
        if (hasNoBody) {
            Block newBody = ast.newBlock();
            rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
            Type returnType = decl.getReturnType2();
            if (returnType != null) {
                Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
                if (expr != null) {
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(expr);
                    newBody.statements().add(returnStatement);
                }
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removeabstract_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ABSTRACT_MODIFIER, image);
        proposals.add(proposal);
    }
    if (!hasNoBody && id == IProblem.BodyForAbstractMethod) {
        AST ast = decl.getAST();
        {
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.remove(decl.getBody(), null);
            String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY, image);
            proposals.add(proposal);
        }
        if (JavaModelUtil.is18OrHigher(cu.getJavaProject()) && parentTypeDecl.isInterface()) {
            {
                // insert proposal to add static modifier
                ASTRewrite rewrite = ASTRewrite.create(ast);
                removeModifier(decl, rewrite, Modifier.ABSTRACT);
                Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD);
                rewrite.getListRewrite(decl, MethodDeclaration.MODIFIERS2_PROPERTY).insertLast(newModifier, null);
                String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, decl.getName());
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                int included = Modifier.STATIC;
                int excluded = Modifier.ABSTRACT | Modifier.DEFAULT;
                proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_STATIC_MODIFIER, image));
            }
            {
                // insert proposal to add default modifier
                ASTRewrite rewrite = ASTRewrite.create(ast);
                removeModifier(decl, rewrite, Modifier.ABSTRACT);
                Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.DEFAULT_KEYWORD);
                rewrite.getListRewrite(decl, MethodDeclaration.MODIFIERS2_PROPERTY).insertLast(newModifier, null);
                String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertodefault_description, decl.getName());
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                int included = Modifier.DEFAULT;
                int excluded = Modifier.ABSTRACT | Modifier.STATIC;
                proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_DEFAULT_MODIFIER, image));
            }
        }
    }
    if (id == IProblem.AbstractMethodInAbstractClass && parentTypeDecl != null) {
        addMakeTypeAbstractProposal(context, parentTypeDecl, proposals);
    }
}
Also used : 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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 25 with TypeDeclaration

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

the class ImplementInterfaceProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    ImportRewrite imports = createImportRewrite(newRoot);
    if (declNode instanceof TypeDeclaration) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        Type newInterface = imports.addImport(fNewInterface, ast, importRewriteContext);
        ListRewrite listRewrite = rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
        listRewrite.insertLast(newInterface, null);
        // set up linked mode
        //$NON-NLS-1$
        final String KEY_TYPE = "type";
        addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE);
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)24 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)17 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 Type (org.eclipse.jdt.core.dom.Type)11 ArrayList (java.util.ArrayList)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 AST (org.eclipse.jdt.core.dom.AST)7 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 List (java.util.List)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)5 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4