Example usage for org.eclipse.jdt.core JavaCore TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC

List of usage examples for org.eclipse.jdt.core JavaCore TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC.

Prototype

String TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC

To view the source code for org.eclipse.jdt.core JavaCore TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC.

Click Source Link

Document

Core option ID: Set the timeout value for retrieving the method's parameter names from javadoc.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryMethod.java

License:Open Source License

public String[] getParameterNames() throws JavaModelException {
    if (this.parameterNames != null)
        return this.parameterNames;

    // force source mapping if not already done
    IType type = (IType) getParent();/*from w ww.ja  v  a  2  s .c  o m*/
    SourceMapper mapper = getSourceMapper();
    if (mapper != null) {
        char[][] paramNames = mapper.getMethodParameterNames(this);

        // map source and try to find parameter names
        if (paramNames == null) {
            IBinaryType info = (IBinaryType) ((BinaryType) getDeclaringType()).getElementInfo();
            char[] source = mapper.findSource(type, info);
            if (source != null) {
                mapper.mapSource(type, source, info);
            }
            paramNames = mapper.getMethodParameterNames(this);
        }

        // if parameter names exist, convert parameter names to String array
        if (paramNames != null) {
            String[] names = new String[paramNames.length];
            for (int i = 0; i < paramNames.length; i++) {
                names[i] = new String(paramNames[i]);
            }
            return this.parameterNames = names;
        }
    }

    // try to see if we can retrieve the names from the attached javadoc
    IBinaryMethod info = (IBinaryMethod) getElementInfo();
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=316937
    // Use Signature#getParameterCount() only if the argument names are not already available.
    int paramCount = Signature.getParameterCount(new String(info.getMethodDescriptor()));
    if (this.isConstructor()) {
        final IType declaringType = this.getDeclaringType();
        if (declaringType.isMember() && !Flags.isStatic(declaringType.getFlags())) {
            paramCount--; // remove synthetic argument from constructor param count
        } else if (declaringType.isEnum()) {
            if (paramCount >= 2) // https://bugs.eclipse.org/bugs/show_bug.cgi?id=436347
                paramCount -= 2;
        }
    }

    if (paramCount != 0) {
        // don't try to look for javadoc for synthetic methods
        int modifiers = getFlags();
        if ((modifiers & ClassFileConstants.AccSynthetic) != 0) {
            return this.parameterNames = getRawParameterNames(paramCount);
        }
        JavadocContents javadocContents = null;
        IType declaringType = getDeclaringType();
        JavaModelManager.PerProjectInfo projectInfo = manager.getPerProjectInfoCheckExistence();
        synchronized (projectInfo.javadocCache) {
            javadocContents = (JavadocContents) projectInfo.javadocCache.get(declaringType);
            if (javadocContents == null) {
                projectInfo.javadocCache.put(declaringType, BinaryType.EMPTY_JAVADOC);
            }
        }

        String methodDoc = null;
        if (javadocContents == null) {
            long timeOut = 50; // default value
            try {
                String option = getJavaProject()
                        .getOption(JavaCore.TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC, true);
                if (option != null) {
                    timeOut = Long.parseLong(option);
                }
            } catch (NumberFormatException e) {
                // ignore
            }
            if (timeOut == 0) {
                // don't try to fetch the values and don't cache either (https://bugs.eclipse.org/bugs/show_bug.cgi?id=329671)
                return getRawParameterNames(paramCount);
            }
            final class ParametersNameCollector {
                String javadoc;

                public void setJavadoc(String s) {
                    this.javadoc = s;
                }

                public String getJavadoc() {
                    return this.javadoc;
                }
            }
            /*
             * The declaring type is not in the cache yet. The thread wil retrieve the javadoc contents
             */
            final ParametersNameCollector nameCollector = new ParametersNameCollector();
            Thread collect = new Thread() {
                public void run() {
                    try {
                        // this call has a side-effect on the per project info cache
                        nameCollector.setJavadoc(BinaryMethod.this.getAttachedJavadoc(null));
                    } catch (JavaModelException e) {
                        // ignore
                    }
                    synchronized (nameCollector) {
                        nameCollector.notify();
                    }
                }
            };
            collect.start();
            synchronized (nameCollector) {
                try {
                    nameCollector.wait(timeOut);
                } catch (InterruptedException e) {
                    // ignore
                }
            }
            methodDoc = nameCollector.getJavadoc();
        } else if (javadocContents != BinaryType.EMPTY_JAVADOC) {
            // need to extract the part relative to the binary method since javadoc contains the javadoc for the declaring type
            try {
                methodDoc = javadocContents.getMethodDoc(this);
            } catch (JavaModelException e) {
                javadocContents = null;
            }
        }
        if (methodDoc != null) {
            int indexOfOpenParen = methodDoc.indexOf('(');
            // Annotations may have parameters, so make sure we are parsing the actual method parameters.
            if (info.getAnnotations() != null) {
                while (indexOfOpenParen != -1
                        && !isOpenParenForMethod(methodDoc, getElementName(), indexOfOpenParen)) {
                    indexOfOpenParen = methodDoc.indexOf('(', indexOfOpenParen + 1);
                }
            }
            if (indexOfOpenParen != -1) {
                final int indexOfClosingParen = methodDoc.indexOf(')', indexOfOpenParen);
                if (indexOfClosingParen != -1) {
                    final char[] paramsSource = CharOperation.replace(
                            methodDoc.substring(indexOfOpenParen + 1, indexOfClosingParen).toCharArray(),
                            "&nbsp;".toCharArray(), //$NON-NLS-1$
                            new char[] { ' ' });
                    final char[][] params = splitParameters(paramsSource, paramCount);
                    final int paramsLength = params.length;
                    String[] names = new String[paramsLength];
                    for (int i = 0; i < paramsLength; i++) {
                        final char[] param = params[i];
                        int indexOfSpace = CharOperation.lastIndexOf(' ', param);
                        if (indexOfSpace != -1) {
                            names[i] = String.valueOf(param, indexOfSpace + 1, param.length - indexOfSpace - 1);
                        } else {
                            names[i] = "arg" + i; //$NON-NLS-1$
                        }
                    }
                    return this.parameterNames = names;
                }
            }
        }
        // let's see if we can retrieve them from the debug infos
        char[][] argumentNames = info.getArgumentNames();
        if (argumentNames != null && argumentNames.length == paramCount) {
            String[] names = new String[paramCount];
            for (int i = 0; i < paramCount; i++) {
                names[i] = new String(argumentNames[i]);
            }
            return this.parameterNames = names;
        }
    }
    // If still no parameter names, produce fake ones, but don't cache them (https://bugs.eclipse.org/bugs/show_bug.cgi?id=329671)
    return getRawParameterNames(paramCount);
    //   throw new UnsupportedOperationException();
}

From source file:org.eclipse.jdt.internal.core.JavaModelManager.java

License:Open Source License

private Hashtable getDefaultOptionsNoInitialization() {
    Map defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults

    // Override some compiler defaults
    defaultOptionsMap.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
    defaultOptionsMap.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
    defaultOptionsMap.put(JavaCore.COMPILER_TASK_TAGS, JavaCore.DEFAULT_TASK_TAGS);
    defaultOptionsMap.put(JavaCore.COMPILER_TASK_PRIORITIES, JavaCore.DEFAULT_TASK_PRIORITIES);
    defaultOptionsMap.put(JavaCore.COMPILER_TASK_CASE_SENSITIVE, JavaCore.ENABLED);
    defaultOptionsMap.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    defaultOptionsMap.put(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, JavaCore.ERROR);

    // Builder settings
    defaultOptionsMap.put(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, JavaCore.ABORT);
    defaultOptionsMap.put(JavaCore.CORE_JAVA_BUILD_DUPLICATE_RESOURCE, JavaCore.WARNING);
    defaultOptionsMap.put(JavaCore.CORE_JAVA_BUILD_CLEAN_OUTPUT_FOLDER, JavaCore.CLEAN);

    // JavaCore settings
    defaultOptionsMap.put(JavaCore.CORE_JAVA_BUILD_ORDER, JavaCore.IGNORE);
    defaultOptionsMap.put(JavaCore.CORE_INCOMPLETE_CLASSPATH, JavaCore.ERROR);
    defaultOptionsMap.put(JavaCore.CORE_CIRCULAR_CLASSPATH, JavaCore.ERROR);
    defaultOptionsMap.put(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, JavaCore.IGNORE);
    defaultOptionsMap.put(JavaCore.CORE_OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE, JavaCore.WARNING);
    defaultOptionsMap.put(JavaCore.CORE_ENABLE_CLASSPATH_EXCLUSION_PATTERNS, JavaCore.ENABLED);
    defaultOptionsMap.put(JavaCore.CORE_ENABLE_CLASSPATH_MULTIPLE_OUTPUT_LOCATIONS, JavaCore.ENABLED);

    // Formatter settings
    defaultOptionsMap.putAll(DefaultCodeFormatterConstants.getEclipseDefaultSettings());

    // CodeAssist settings
    defaultOptionsMap.put(JavaCore.CODEASSIST_VISIBILITY_CHECK, JavaCore.DISABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_DEPRECATION_CHECK, JavaCore.DISABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_IMPLICIT_QUALIFICATION, JavaCore.DISABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_PREFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_PREFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_PREFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_PREFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_SUFFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_SUFFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_SUFFIXES, ""); //$NON-NLS-1$
    defaultOptionsMap.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, JavaCore.ENABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK, JavaCore.DISABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_CAMEL_CASE_MATCH, JavaCore.ENABLED);
    defaultOptionsMap.put(JavaCore.CODEASSIST_SUGGEST_STATIC_IMPORTS, JavaCore.ENABLED);

    // Time out for parameter names
    defaultOptionsMap.put(JavaCore.TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC, "50"); //$NON-NLS-1$

    return new Hashtable(defaultOptionsMap);
}