Search in sources :

Example 26 with SimpleName

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

the class JavadocContentAccess2 method toHTML.

private String toHTML() {
    fBuf = new StringBuffer();
    fLiteralContent = 0;
    // After first loop, non-null entries in the following two lists are missing and need to be inherited:
    List<String> parameterNames = initParameterNames();
    List<String> exceptionNames = initExceptionNames();
    TagElement deprecatedTag = null;
    TagElement start = null;
    List<TagElement> parameters = new ArrayList<TagElement>();
    TagElement returnTag = null;
    List<TagElement> exceptions = new ArrayList<TagElement>();
    List<TagElement> versions = new ArrayList<TagElement>();
    List<TagElement> authors = new ArrayList<TagElement>();
    List<TagElement> sees = new ArrayList<TagElement>();
    List<TagElement> since = new ArrayList<TagElement>();
    List<TagElement> rest = new ArrayList<TagElement>();
    List<TagElement> tags = fJavadoc.tags();
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement tag = iter.next();
        String tagName = tag.getTagName();
        if (tagName == null) {
            start = tag;
        } else if (TagElement.TAG_PARAM.equals(tagName)) {
            parameters.add(tag);
            List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof SimpleName) {
                    String name = ((SimpleName) first).getIdentifier();
                    int paramIndex = parameterNames.indexOf(name);
                    if (paramIndex != -1) {
                        parameterNames.set(paramIndex, null);
                    }
                }
            }
        } else if (TagElement.TAG_RETURN.equals(tagName)) {
            if (returnTag == null)
                // the Javadoc tool only shows the first return tag
                returnTag = tag;
        } else if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
            exceptions.add(tag);
            List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof Name) {
                    String name = ASTNodes.getSimpleNameIdentifier((Name) first);
                    int exceptionIndex = exceptionNames.indexOf(name);
                    if (exceptionIndex != -1) {
                        exceptionNames.set(exceptionIndex, null);
                    }
                }
            }
        } else if (TagElement.TAG_SINCE.equals(tagName)) {
            since.add(tag);
        } else if (TagElement.TAG_VERSION.equals(tagName)) {
            versions.add(tag);
        } else if (TagElement.TAG_AUTHOR.equals(tagName)) {
            authors.add(tag);
        } else if (TagElement.TAG_SEE.equals(tagName)) {
            sees.add(tag);
        } else if (TagElement.TAG_DEPRECATED.equals(tagName)) {
            if (deprecatedTag == null)
                // the Javadoc tool only shows the first deprecated tag
                deprecatedTag = tag;
        } else {
            rest.add(tag);
        }
    }
    //TODO: @Documented annotations before header
    if (deprecatedTag != null)
        handleDeprecatedTag(deprecatedTag);
    if (start != null)
        handleContentElements(start.fragments());
    else if (fMethod != null) {
        CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
        // The Javadoc tool adds "Description copied from class: ..." (only for the main description).
        // We don't bother doing that.
        handleInherited(inherited);
    }
    CharSequence[] parameterDescriptions = new CharSequence[parameterNames.size()];
    boolean hasInheritedParameters = inheritParameterDescriptions(parameterNames, parameterDescriptions);
    boolean hasParameters = parameters.size() > 0 || hasInheritedParameters;
    CharSequence returnDescription = null;
    if (returnTag == null && needsReturnTag())
        returnDescription = fJavadocLookup.getInheritedReturnDescription(fMethod);
    boolean hasReturnTag = returnTag != null || returnDescription != null;
    CharSequence[] exceptionDescriptions = new CharSequence[exceptionNames.size()];
    boolean hasInheritedExceptions = inheritExceptionDescriptions(exceptionNames, exceptionDescriptions);
    boolean hasExceptions = exceptions.size() > 0 || hasInheritedExceptions;
    if (hasParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || (fBuf.length() > 0 && (parameterDescriptions.length > 0 || exceptionDescriptions.length > 0))) {
        handleSuperMethodReferences();
        fBuf.append(BLOCK_TAG_START);
        handleParameterTags(parameters, parameterNames, parameterDescriptions);
        handleReturnTag(returnTag, returnDescription);
        handleExceptionTags(exceptions, exceptionNames, exceptionDescriptions);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_since_section, since);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_version_section, versions);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_author_section, authors);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_see_section, sees);
        handleBlockTags(rest);
        fBuf.append(BLOCK_TAG_END);
    } else if (fBuf.length() > 0) {
        handleSuperMethodReferences();
    }
    String result = fBuf.toString();
    fBuf = null;
    return result;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList)

Example 27 with SimpleName

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

the class JavadocContentAccess2 method handleLink.

private void handleLink(List<? extends ASTNode> fragments) {
    //TODO: Javadoc shortens type names to minimal length according to context
    int fs = fragments.size();
    if (fs > 0) {
        Object first = fragments.get(0);
        String refTypeName = null;
        String refMemberName = null;
        String[] refMethodParamTypes = null;
        String[] refMethodParamNames = null;
        if (first instanceof Name) {
            Name name = (Name) first;
            refTypeName = name.getFullyQualifiedName();
        } else if (first instanceof MemberRef) {
            MemberRef memberRef = (MemberRef) first;
            Name qualifier = memberRef.getQualifier();
            //$NON-NLS-1$
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName();
            refMemberName = memberRef.getName().getIdentifier();
        } else if (first instanceof MethodRef) {
            MethodRef methodRef = (MethodRef) first;
            Name qualifier = methodRef.getQualifier();
            //$NON-NLS-1$
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName();
            refMemberName = methodRef.getName().getIdentifier();
            List<MethodRefParameter> params = methodRef.parameters();
            int ps = params.size();
            refMethodParamTypes = new String[ps];
            refMethodParamNames = new String[ps];
            for (int i = 0; i < ps; i++) {
                MethodRefParameter param = params.get(i);
                refMethodParamTypes[i] = ASTNodes.asString(param.getType());
                SimpleName paramName = param.getName();
                if (paramName != null)
                    refMethodParamNames[i] = paramName.getIdentifier();
            }
        }
        if (refTypeName != null) {
            //$NON-NLS-1$
            fBuf.append("<a href='");
            try {
                String scheme = urlPrefix;
                String uri = JavaElementLinks.createURI(scheme, fElement, refTypeName, refMemberName, refMethodParamTypes);
                fBuf.append(uri);
            } catch (URISyntaxException e) {
                LOG.error(e.getMessage(), e);
            }
            //$NON-NLS-1$
            fBuf.append("'>");
            if (fs > 1 && !(fs == 2 && isWhitespaceTextElement(fragments.get(1)))) {
                handleContentElements(fragments.subList(1, fs), true);
            } else {
                fBuf.append(refTypeName);
                if (refMemberName != null) {
                    if (refTypeName.length() > 0) {
                        fBuf.append('.');
                    }
                    fBuf.append(refMemberName);
                    if (refMethodParamTypes != null) {
                        fBuf.append('(');
                        for (int i = 0; i < refMethodParamTypes.length; i++) {
                            String pType = refMethodParamTypes[i];
                            fBuf.append(pType);
                            String pName = refMethodParamNames[i];
                            if (pName != null) {
                                fBuf.append(' ').append(pName);
                            }
                            if (i < refMethodParamTypes.length - 1) {
                                //$NON-NLS-1$
                                fBuf.append(", ");
                            }
                        }
                        fBuf.append(')');
                    }
                }
            }
            //$NON-NLS-1$
            fBuf.append("</a>");
        } else {
            handleContentElements(fragments);
        }
    }
}
Also used : MethodRef(org.eclipse.jdt.core.dom.MethodRef) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodRefParameter(org.eclipse.jdt.core.dom.MethodRefParameter) MemberRef(org.eclipse.jdt.core.dom.MemberRef) URISyntaxException(java.net.URISyntaxException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 28 with SimpleName

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

the class SourceProvider method updateStaticReferences.

private void updateStaticReferences(ASTRewrite rewriter, CallContext context) {
    ImportRewrite importer = context.importer;
    for (Iterator<SimpleName> iter = fAnalyzer.getStaticsToImport().iterator(); iter.hasNext(); ) {
        Name element = iter.next();
        IBinding binding = element.resolveBinding();
        if (binding != null) {
            String s = importer.addStaticImport(binding);
            if (!ASTNodes.asString(element).equals(s)) {
                rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null);
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 29 with SimpleName

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

the class SourceProvider method replaceParameterWithExpression.

private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
    Expression[] arguments = context.arguments;
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
        for (int i = 0; i < arguments.length; i++) {
            Expression expression = arguments[i];
            String expressionString = null;
            if (expression instanceof SimpleName) {
                expressionString = ((SimpleName) expression).getIdentifier();
            } else {
                try {
                    expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
                } catch (BadLocationException exception) {
                    JavaPlugin.log(exception);
                    continue;
                }
            }
            ParameterData parameter = getParameterData(i);
            List<SimpleName> references = parameter.references();
            for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
                ASTNode element = iter.next();
                Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
                AST ast = rewriter.getAST();
                ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
                if (explicitCast != null) {
                    CastExpression cast = ast.newCastExpression();
                    if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
                        newExpression = createParenthesizedExpression(newExpression, ast);
                    }
                    cast.setExpression(newExpression);
                    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
                    cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
                    expression = newExpression = cast;
                }
                if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
                    newExpression = createParenthesizedExpression(newExpression, ast);
                }
                rewriter.replace(element, newExpression, null);
            }
        }
    } finally {
        RefactoringFileBuffers.release(context.compilationUnit);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 30 with SimpleName

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

the class SourceProvider method updateImplicitReceivers.

private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
    if (context.receiver == null)
        return;
    List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
    for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
        if (node instanceof MethodInvocation) {
            final MethodInvocation inv = (MethodInvocation) node;
            rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
        } else if (node instanceof ClassInstanceCreation) {
            final ClassInstanceCreation inst = (ClassInstanceCreation) node;
            rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
        } else if (node instanceof ThisExpression) {
            rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
        } else if (node instanceof FieldAccess) {
            final FieldAccess access = (FieldAccess) node;
            rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
        } else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
            IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
            if (vb.isField()) {
                Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
                if (receiver != null) {
                    FieldAccess access = node.getAST().newFieldAccess();
                    ASTNode target = rewriter.createMoveTarget(node);
                    access.setName((SimpleName) target);
                    access.setExpression(receiver);
                    rewriter.replace(node, access, null);
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32