Search in sources :

Example 6 with TypeDeclaration

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

the class AddTypeParameterProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
        createImportRewrite(fAstRoot);
    } else {
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
        createImportRewrite(newRoot);
    }
    AST ast = declNode.getAST();
    TypeParameter newTypeParam = ast.newTypeParameter();
    newTypeParam.setName(ast.newSimpleName(fTypeParamName));
    if (fBounds != null && fBounds.length > 0) {
        List<Type> typeBounds = newTypeParam.typeBounds();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
        for (int i = 0; i < fBounds.length; i++) {
            Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
            typeBounds.add(newBound);
        }
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite;
    Javadoc javadoc;
    List<TypeParameter> otherTypeParams;
    if (declNode instanceof TypeDeclaration) {
        TypeDeclaration declaration = (TypeDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    } else {
        MethodDeclaration declaration = (MethodDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    }
    listRewrite.insertLast(newTypeParam, null);
    if (javadoc != null && otherTypeParams != null) {
        ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
        Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
        String name = '<' + fTypeParamName + '>';
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_PARAM);
        TextElement text = ast.newTextElement();
        text.setText(name);
        newTag.fragments().add(text);
        JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
    }
    return rewrite;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) 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) TagElement(org.eclipse.jdt.core.dom.TagElement) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 7 with TypeDeclaration

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

the class PromoteTempToFieldRefactoring method getNamesOfFieldsInDeclaringType.

private String[] getNamesOfFieldsInDeclaringType() {
    final AbstractTypeDeclaration type = getEnclosingType();
    if (type instanceof TypeDeclaration) {
        FieldDeclaration[] fields = ((TypeDeclaration) type).getFields();
        List<String> result = new ArrayList<String>(fields.length);
        for (int i = 0; i < fields.length; i++) {
            for (Iterator<VariableDeclarationFragment> iter = fields[i].fragments().iterator(); iter.hasNext(); ) {
                VariableDeclarationFragment field = iter.next();
                result.add(field.getName().getIdentifier());
            }
        }
        return result.toArray(new String[result.size()]);
    }
    return new String[] {};
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 8 with TypeDeclaration

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

the class ConvertAnonymousToNestedRefactoring method createNewNestedClass.

private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
    final AST ast = fAnonymousInnerClassNode.getAST();
    final TypeDeclaration newDeclaration = ast.newTypeDeclaration();
    newDeclaration.setInterface(false);
    newDeclaration.setJavadoc(null);
    newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
    newDeclaration.setName(ast.newSimpleName(fClassName));
    TypeParameter parameter = null;
    for (int index = 0; index < typeParameters.length; index++) {
        parameter = ast.newTypeParameter();
        parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
        newDeclaration.typeParameters().add(parameter);
    }
    setSuperType(newDeclaration);
    IJavaProject project = fCu.getJavaProject();
    IVariableBinding[] bindings = getUsedLocalVariables();
    ArrayList<String> fieldNames = new ArrayList<String>();
    for (int i = 0; i < bindings.length; i++) {
        String name = StubUtility.getBaseName(bindings[i], project);
        String[] fieldNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
        fieldNames.add(fieldNameProposals[0]);
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
            for (int k = 0; k < fieldNameProposals.length; k++) {
                positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
            }
        }
    }
    String[] allFieldNames = fieldNames.toArray(new String[fieldNames.size()]);
    List<BodyDeclaration> newBodyDeclarations = newDeclaration.bodyDeclarations();
    createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);
    MethodDeclaration newConstructorDecl = createNewConstructor(rewrite, bindings, allFieldNames);
    if (newConstructorDecl != null) {
        newBodyDeclarations.add(newConstructorDecl);
    }
    updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);
    if (doAddComments()) {
        String[] parameterNames = new String[typeParameters.length];
        for (int index = 0; index < parameterNames.length; index++) {
            parameterNames[index] = typeParameters[index].getName();
        }
        String string = CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
            newDeclaration.setJavadoc(javadoc);
        }
    }
    if (fLinkedProposalModel != null) {
        addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
    }
    return newDeclaration;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IJavaProject(org.eclipse.jdt.core.IJavaProject) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 9 with TypeDeclaration

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

the class ASTNodeFactory method newTypeParameter.

public static TypeParameter newTypeParameter(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
    buffer.append(content);
    buffer.append(TYPEPARAM_FOOTER);
    ASTParser p = ASTParser.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];
    TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
    ASTNode result = ASTNode.copySubtree(ast, tp);
    result.accept(new PositionClearer());
    return (TypeParameter) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 10 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project flux 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);
        proposals.add(proposal);
    }
    if (!hasNoBody && id == IProblem.BodyForAbstractMethod) {
        AST ast = decl.getAST();
        {
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.remove(decl.getBody(), null);
            int excluded;
            if (parentTypeDecl.isInterface()) {
                excluded = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
            } else {
                excluded = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.ABSTRACT);
            }
            ModifierRewrite.create(rewrite, decl).setModifiers(0, excluded, 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);
            proposals.add(proposal);
        }
        if (JavaModelUtil.is18OrHigher(cu.getJavaProject()) && parentTypeDecl.isInterface()) {
            {
                // insert proposal to add static modifier
                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));
            }
            {
                // insert proposal to add default modifier
                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));
            }
        }
    }
    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) 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)

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