Search in sources :

Example 11 with Javadoc

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

the class ModifierCorrectionSubProcessor method addOverridingDeprecatedMethodProposal.

public static void addOverridingDeprecatedMethodProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
    MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
    AST ast = methodDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    if (is50OrHigher) {
        Annotation annot = ast.newMarkerAnnotation();
        //$NON-NLS-1$
        annot.setTypeName(ast.newName("Deprecated"));
        rewrite.getListRewrite(methodDecl, methodDecl.getModifiersProperty()).insertFirst(annot, null);
    }
    Javadoc javadoc = methodDecl.getJavadoc();
    if (javadoc != null || !is50OrHigher) {
        if (!is50OrHigher) {
            javadoc = ast.newJavadoc();
            rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null);
        }
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_DEPRECATED);
        JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
    }
    String label = CorrectionMessages.ModifierCorrectionSubProcessor_overrides_deprecated_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.OVERRIDES_DEPRECATED, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) TagElement(org.eclipse.jdt.core.dom.TagElement) Image(org.eclipse.swt.graphics.Image) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 12 with Javadoc

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

the class SelfEncapsulateFieldRefactoring method createGetterMethod.

private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fGetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
    result.setReturnType2(returnType);
    Block block = ast.newBlock();
    result.setBody(block);
    String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
    if (body != null) {
        ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(getterNode);
    } else {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fField.getElementName()));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 13 with Javadoc

use of org.eclipse.jdt.core.dom.Javadoc 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 14 with Javadoc

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

the class ConvertAnonymousToNestedRefactoring method createNewConstructor.

private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
    ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
        return null;
    IJavaProject project = fCu.getJavaProject();
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = rewrite.getImportRewrite();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
    newConstructor.setName(ast.newSimpleName(fClassName));
    addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
    newConstructor.setBody(ast.newBlock());
    List<Statement> newStatements = newConstructor.getBody().statements();
    List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
    List<String> newParameterNames = new ArrayList<String>();
    // add parameters for elements passed with the instance creation
    if (!instanceCreation.arguments().isEmpty()) {
        IMethodBinding constructorBinding = getSuperConstructorBinding();
        if (constructorBinding != null) {
            SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
            ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
            String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
            for (int i = 0; i < parameterNames.length; i++) {
                String[] nameProposals = parameterNames[i];
                String paramName = nameProposals[0];
                SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
                newParameters.add(param);
                newParameterNames.add(paramName);
                SimpleName newSIArgument = ast.newSimpleName(paramName);
                superConstructorInvocation.arguments().add(newSIArgument);
                if (fLinkedProposalModel != null) {
                    LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
                    positionGroup.addPosition(astRewrite.track(param.getName()), false);
                    positionGroup.addPosition(astRewrite.track(newSIArgument), false);
                    for (int k = 0; k < nameProposals.length; k++) {
                        positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
                    }
                }
            }
            newStatements.add(superConstructorInvocation);
        }
    }
    // add parameters for all outer variables used
    boolean useThisAccess = useThisForFieldAccess();
    for (int i = 0; i < bindings.length; i++) {
        String baseName = StubUtility.getBaseName(bindings[i], project);
        String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
        String paramName = paramNameProposals[0];
        SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
        newParameters.add(param);
        newParameterNames.add(paramName);
        String fieldName = fieldNames[i];
        SimpleName fieldNameNode = ast.newSimpleName(fieldName);
        SimpleName paramNameNode = ast.newSimpleName(paramName);
        newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
            positionGroup.addPosition(astRewrite.track(param.getName()), false);
            positionGroup.addPosition(astRewrite.track(paramNameNode), false);
            for (int k = 0; k < paramNameProposals.length; k++) {
                positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
            }
            fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
        }
    }
    addExceptionsToNewConstructor(newConstructor, importRewrite);
    if (doAddComments()) {
        try {
            String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
            String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                newConstructor.setJavadoc(javadoc);
            }
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    }
    return newConstructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 15 with Javadoc

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

the class ConvertAnonymousToNestedRefactoring method createFieldsForAccessedLocals.

private void createFieldsForAccessedLocals(CompilationUnitRewrite rewrite, IVariableBinding[] varBindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations) throws CoreException {
    final ImportRewrite importRewrite = rewrite.getImportRewrite();
    final ASTRewrite astRewrite = rewrite.getASTRewrite();
    final AST ast = astRewrite.getAST();
    for (int i = 0; i < varBindings.length; i++) {
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setInitializer(null);
        fragment.setName(ast.newSimpleName(fieldNames[i]));
        FieldDeclaration field = ast.newFieldDeclaration(fragment);
        ITypeBinding varType = varBindings[i].getType();
        field.setType(importRewrite.addImport(varType, ast));
        field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
        if (doAddComments()) {
            String string = CodeGeneration.getFieldComment(rewrite.getCu(), varType.getName(), fieldNames[i], StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                field.setJavadoc(javadoc);
            }
        }
        newBodyDeclarations.add(field);
        addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

Javadoc (org.eclipse.jdt.core.dom.Javadoc)32 AST (org.eclipse.jdt.core.dom.AST)20 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)19 Type (org.eclipse.jdt.core.dom.Type)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 TagElement (org.eclipse.jdt.core.dom.TagElement)12 Block (org.eclipse.jdt.core.dom.Block)11 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)11 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)8 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)8 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)8 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)7 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)7 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)7 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)6 Expression (org.eclipse.jdt.core.dom.Expression)6