Search in sources :

Example 16 with Javadoc

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

the class ExtractConstantRefactoring method createConstantDeclaration.

private void createConstantDeclaration() throws CoreException {
    Type type = getConstantType();
    IExpressionFragment fragment = getSelectedExpression();
    Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
    AST ast = fCuRewrite.getAST();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
    variableDeclarationFragment.setInitializer(initializer);
    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    fieldDeclaration.setType(type);
    Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
    if (accessModifier != null)
        fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
    if (createComments) {
        String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
        if (comment != null && comment.length() > 0) {
            Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
            fieldDeclaration.setJavadoc(doc);
        }
    }
    AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
    ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
    TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
    if (insertFirst()) {
        listRewrite.insertFirst(fieldDeclaration, msg);
    } else {
        listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
    }
    if (fLinkedProposalModel != null) {
        ASTRewrite rewrite = fCuRewrite.getASTRewrite();
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
        String[] nameSuggestions = guessConstantNames();
        if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
            nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
        }
        LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(type), true);
        ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i = 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
            }
        }
        boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 17 with Javadoc

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

the class JavadocContentAccess2 method getHTMLContentFromAttachedSource.

private static String getHTMLContentFromAttachedSource(IPackageFragmentRoot root, IPackageFragment packageFragment, String urlPrefix) throws CoreException {
    String filePath = packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_INFO_JAVA;
    String contents = getFileContentFromAttachedSource(root, filePath);
    if (contents != null) {
        Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, contents);
        if (packageJavadocNode != null)
            return new JavadocContentAccess2(packageFragment, packageJavadocNode, contents, urlPrefix).toHTML();
    }
    filePath = packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_HTML;
    return getFileContentFromAttachedSource(root, filePath);
}
Also used : Javadoc(org.eclipse.jdt.core.dom.Javadoc)

Example 18 with Javadoc

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

the class SelfEncapsulateFieldRefactoring method createSetterMethod.

private MethodDeclaration createSetterMethod(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(fSetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    if (fSetterMustReturnValue) {
        result.setReturnType2((Type) rewriter.createCopyTarget(type));
    }
    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    result.parameters().add(param);
    param.setName(ast.newSimpleName(fArgName));
    param.setType((Type) rewriter.createCopyTarget(type));
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
    param.extraDimensions().addAll(extraDimensions);
    Block block = ast.newBlock();
    result.setBody(block);
    String fieldAccess = createFieldAccess();
    String body = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter);
    if (body != null) {
        ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(setterNode);
    } else {
        Assignment ass = ast.newAssignment();
        ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME));
        ass.setRightHandSide(ast.newSimpleName(fArgName));
        block.statements().add(ass);
    }
    if (fSetterMustReturnValue) {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fArgName));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getSetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) 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) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 19 with Javadoc

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

the class JavadocUtil method shouldAddParamJavadoc.

/**
	 * Decide whether to add a "param" javadoc tag or not.
	 * @param methodDeclaration the method declaration
	 * @return method has javadoc && (method had no parameter before || there is already an @param tag)
	 */
public static boolean shouldAddParamJavadoc(MethodDeclaration methodDeclaration) {
    Javadoc javadoc = methodDeclaration.getJavadoc();
    if (javadoc == null)
        return false;
    if (methodDeclaration.parameters().size() == 0)
        return true;
    List<TagElement> tags = javadoc.tags();
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement element = iter.next();
        if (TagElement.TAG_PARAM.equals(element.getTagName()))
            return true;
    }
    return false;
}
Also used : Javadoc(org.eclipse.jdt.core.dom.Javadoc) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 20 with Javadoc

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

the class StubUtility2 method createDelegationStub.

public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
    Assert.isNotNull(delegate);
    Assert.isNotNull(delegatingField);
    Assert.isNotNull(settings);
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(delegate.getName()));
    decl.setConstructor(false);
    createTypeParameters(imports, context, ast, delegate, decl);
    decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
    List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
    createThrownExceptions(decl, delegate, imports, context, ast);
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    Statement statement = null;
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName(delegate.getName()));
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
    if (settings.useKeywordThis) {
        FieldAccess access = ast.newFieldAccess();
        access.setExpression(ast.newThisExpression());
        access.setName(ast.newSimpleName(delegatingField.getName()));
        invocation.setExpression(access);
    } else
        invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
    if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
        //$NON-NLS-1$
        statement = ast.newExpressionStatement(invocation);
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(invocation);
        statement = returnStatement;
    }
    body.statements().add(statement);
    ITypeBinding declaringType = delegatingField.getDeclaringClass();
    if (declaringType == null) {
        // can be null for
        return decl;
    }
    String qualifiedName = declaringType.getQualifiedName();
    IPackageBinding packageBinding = declaringType.getPackage();
    if (packageBinding != null) {
        if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
            qualifiedName = qualifiedName.substring(packageBinding.getName().length());
    }
    if (settings.createComments) {
        /*
			 * TODO: have API for delegate method comments This is an inlined
			 * version of
			 * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
			 */
        delegate = delegate.getMethodDeclaration();
        String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
        String linkToMethodName = delegate.getName();
        String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
        String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Javadoc(org.eclipse.jdt.core.dom.Javadoc) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

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