Search in sources :

Example 26 with TypeDeclaration

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

the class NewVariableCorrectionProposal method evaluateFieldModifiers.

private int evaluateFieldModifiers(ASTNode newTypeDecl) {
    if (fSenderBinding.isAnnotation()) {
        return 0;
    }
    if (fSenderBinding.isInterface()) {
        // for interface members copy the modifiers from an existing field
        FieldDeclaration[] fieldDecls = ((TypeDeclaration) newTypeDecl).getFields();
        if (fieldDecls.length > 0) {
            return fieldDecls[0].getModifiers();
        }
        return 0;
    }
    int modifiers = 0;
    if (fVariableKind == CONST_FIELD) {
        modifiers |= Modifier.FINAL | Modifier.STATIC;
    } else {
        ASTNode parent = fOriginalNode.getParent();
        if (parent instanceof QualifiedName) {
            IBinding qualifierBinding = ((QualifiedName) parent).getQualifier().resolveBinding();
            if (qualifierBinding instanceof ITypeBinding) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(fOriginalNode)) {
            modifiers |= Modifier.STATIC;
        }
    }
    ASTNode node = ASTResolving.findParentType(fOriginalNode, true);
    if (newTypeDecl.equals(node)) {
        modifiers |= Modifier.PRIVATE;
    } else if (node instanceof AnonymousClassDeclaration) {
        modifiers |= Modifier.PROTECTED;
    } else {
        modifiers |= Modifier.PUBLIC;
    }
    return modifiers;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 27 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che 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);
    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];
    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) 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 28 with TypeDeclaration

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

the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.

public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
    if (declaration == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
    if (binding == null) {
        return;
    }
    if (declaration instanceof MethodDeclaration) {
        MethodDeclaration methodDecl = (MethodDeclaration) declaration;
        IMethodBinding methodBinding = methodDecl.resolveBinding();
        IMethodBinding overridden = null;
        if (methodBinding != null) {
            overridden = Bindings.findOverriddenMethod(methodBinding, true);
        }
        String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof AbstractTypeDeclaration) {
        String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
        String[] typeParamNames;
        if (declaration instanceof TypeDeclaration) {
            List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
            typeParamNames = new String[typeParams.size()];
            for (int i = 0; i < typeParamNames.length; i++) {
                typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
            }
        } else {
            typeParamNames = new String[0];
        }
        String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof FieldDeclaration) {
        //$NON-NLS-1$
        String comment = "/**\n *\n */\n";
        List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
        if (fragments != null && fragments.size() > 0) {
            VariableDeclaration decl = fragments.get(0);
            String fieldName = decl.getName().getIdentifier();
            String typeName = binding.getName();
            comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
        }
        if (comment != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
        }
    } else if (declaration instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
        String id = enumDecl.getName().getIdentifier();
        String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
        proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 29 with TypeDeclaration

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

the class UnimplementedCodeFix method createMakeTypeAbstractFix.

public static UnimplementedCodeFix createMakeTypeAbstractFix(CompilationUnit root, IProblemLocation problem) {
    ASTNode typeNode = getSelectedTypeNode(root, problem);
    if (!(typeNode instanceof TypeDeclaration))
        return null;
    TypeDeclaration typeDeclaration = (TypeDeclaration) typeNode;
    MakeTypeAbstractOperation operation = new MakeTypeAbstractOperation(typeDeclaration);
    String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_addabstract_description, BasicElementLabels.getJavaElementName(typeDeclaration.getName().getIdentifier()));
    return new UnimplementedCodeFix(label, root, new CompilationUnitRewriteOperation[] { operation });
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 30 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che 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<CompilationUnitRewriteOperation>();
    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.ui.text.java.IProblemLocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) 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