Example usage for org.eclipse.jdt.core Signature getParameterCount

List of usage examples for org.eclipse.jdt.core Signature getParameterCount

Introduction

In this page you can find the example usage for org.eclipse.jdt.core Signature getParameterCount.

Prototype

public static int getParameterCount(String methodSignature) throws IllegalArgumentException 

Source Link

Document

Returns the number of parameter types in the given method signature.

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 w w  . j av a2  s.  co  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:com.codenvy.ide.ext.java.server.internal.core.BinaryMethod.java

License:Open Source License

public String[] getRawParameterNames() throws JavaModelException {
    IBinaryMethod info = (IBinaryMethod) getElementInfo();
    int paramCount = Signature.getParameterCount(new String(info.getMethodDescriptor()));
    return getRawParameterNames(paramCount);
}

From source file:com.mountainminds.eclemma.internal.core.analysis.MethodLocator.java

License:Open Source License

private String createParamCountKey(final String name, final String fullSignature) {
    return name + "@" + Signature.getParameterCount(fullSignature); //$NON-NLS-1$
}

From source file:de.ovgu.featureide.ui.editors.Completion.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext arg0,
        IProgressMonitor arg1) {/*  ww w  .j  ava  2s.com*/

    final IFile file = ((IFileEditorInput) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .getActiveEditor().getEditorInput()).getFile();
    final IFeatureProject featureProject = CorePlugin.getFeatureProject(file);
    final ArrayList<ICompletionProposal> list = new ArrayList<ICompletionProposal>();

    if (featureProject == null)
        return list;

    String featureName = featureProject.getFeatureName(file);
    JavaContentAssistInvocationContext context = (JavaContentAssistInvocationContext) arg0;

    String prefix = new String(context.getCoreContext().getToken());

    //      projectStructure = MPLPlugin.getDefault().extendedModules_getStruct(featureProject, featureName);
    //      if (projectStructure == null) {
    //         return list;
    //      }
    //      List<CompletionProposal> l = MPLPlugin.getDefault().extendedModules(projectStructure);
    List<CompletionProposal> l = MPLPlugin.getDefault().extendedModules_getCompl(featureProject, featureName);

    for (CompletionProposal curProp : l) {
        curProp.setReplaceRange(context.getInvocationOffset() - context.getCoreContext().getToken().length,
                context.getInvocationOffset());

        if (curProp.getKind() == CompletionProposal.TYPE_REF) {
            LazyJavaCompletionProposal prsss = new LazyJavaCompletionProposal(curProp, context);

            prsss.setStyledDisplayString(new StyledString(new String(curProp.getCompletion())));
            prsss.setReplacementString(new String(curProp.getCompletion()));
            if (prefix.length() >= 0 && new String(curProp.getCompletion()).startsWith(prefix)) {
                list.add(prsss);
            }
        } else if (curProp.getKind() == CompletionProposal.METHOD_REF) {
            LazyJavaCompletionProposal meth = new LazyJavaCompletionProposal(curProp, context);

            String displayString = new String(curProp.getCompletion());
            displayString = displayString.concat("(");
            int paramNr = Signature.getParameterCount(curProp.getSignature());
            for (int i = 0; i < paramNr; i++) {
                displayString = displayString
                        .concat(Signature.getParameterTypes(curProp.getSignature()) + " arg" + i);
                if (i + 1 < paramNr) {
                    displayString = displayString.concat(", ");
                }
            }
            displayString = displayString.concat(") : ");
            // displayString = displayString.concat(new
            // String(Signature.getReturnType(curProp.getSignature())));

            StyledString methString = new StyledString(displayString);
            Styler styler = StyledString.createColorRegistryStyler(JFacePreferences.DECORATIONS_COLOR,
                    JFacePreferences.CONTENT_ASSIST_BACKGROUND_COLOR);
            // TextStyle style = new
            // TextStyle(JFaceResources.getDefaultFont(),JFaceResources.getResources().createColor(new
            // RGB(10, 10,
            // 10)),JFaceResources.getResources().createColor(new
            // RGB(0,0,0)));
            // styler.applyStyles(style);
            StyledString infoString = new StyledString(
                    new String(" - " + new String(curProp.getName()) + " " + featureName), styler);
            methString.append(infoString);
            meth.setStyledDisplayString(methString);

            meth.setReplacementString(new String(curProp.getCompletion()));

            if (prefix.length() >= 0 && new String(curProp.getCompletion()).startsWith(prefix)) {
                list.add(meth);
            }
        } else if (curProp.getKind() == CompletionProposal.FIELD_REF) {
            LazyJavaCompletionProposal field = new LazyJavaCompletionProposal(curProp, context);
            StyledString fieldString = new StyledString(new String(curProp.getCompletion()));
            Styler styler = StyledString.createColorRegistryStyler(JFacePreferences.DECORATIONS_COLOR,
                    JFacePreferences.CONTENT_ASSIST_BACKGROUND_COLOR);
            StyledString infoString = new StyledString(
                    new String(" - " + new String(curProp.getName()) + " " + featureName), styler);
            fieldString.append(infoString);
            field.setStyledDisplayString(fieldString);

            field.setReplacementString(new String(curProp.getCompletion()));
            if (prefix.length() > 0 && new String(curProp.getCompletion()).startsWith(prefix)) {
                list.add(field);
            }
        }
    }
    return list;
}

From source file:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.NamingAdvisor.java

License:Open Source License

private void addMethod(String klass, String name, char[] desc) {
    String lookup = name + Signature.getParameterCount(desc);
    String fqn = klass.replace('/', '.') + "." + name + convertDescriptor(desc);
    String other = methodMap.get(lookup);
    if (other == null) {
        methodMap.put(lookup, fqn);/*from ww w.ja v a 2s .c  o  m*/
    } else if (!other.equals(fqn)) {
        methodMap.put(lookup, null);
    }
}

From source file:org.drools.eclipse.editors.completion.CompletionUtil.java

License:Apache License

public static String getPropertyName(String methodName, char[] signature) {
    if (signature == null || methodName == null) {
        return methodName;
    }/*from  w  w w  .  j ava2 s .c  o m*/

    int parameterCount = Signature.getParameterCount(signature);
    String returnType = new String(Signature.getReturnType(signature));

    return getPropertyName(methodName, parameterCount, returnType);
}

From source file:org.drools.eclipse.editors.completion.CompletionUtil.java

License:Apache License

public static String getWritablePropertyName(String methodName, char[] signature) {
    if (signature == null || methodName == null) {
        return methodName;
    }//from w  ww.  java 2s.c  om

    int parameterCount = Signature.getParameterCount(signature);
    String returnType = new String(Signature.getReturnType(signature));

    return getWritablePropertyName(methodName, parameterCount, returnType);
}

From source file:org.eclipse.che.jdt.BinaryTypeConvector.java

License:Open Source License

private static JsonElement toJsonParameterAnnotations(IBinaryMethod method) {
    if (method.getAnnotatedParametersCount() != 0) {
        JsonArray array = new JsonArray();
        int parameterCount = Signature.getParameterCount(method.getMethodDescriptor());
        for (int i = 0; i < parameterCount; i++) {
            array.add(toJsonAnnotations(method.getParameterAnnotations(i)));
        }//from   w  w  w. ja v  a  2  s .c  om
        return array;
    } else
        return JsonNull.INSTANCE;
}

From source file:org.eclipse.flux.jdt.services.CompletionProposalReplacementProvider.java

License:Open Source License

private boolean hasParameters(CompletionProposal proposal) throws IllegalArgumentException {
    return Signature.getParameterCount(proposal.getSignature()) > 0;
}

From source file:org.eclipse.jpt.jpa.core.jpql.spi.ClassConstructor.java

License:Open Source License

protected ITypeDeclaration[] buildParameterTypes() {

    BindingKey bindingKey = new BindingKey(method.getKey());
    String signature = bindingKey.toSignature();

    int count = Signature.getParameterCount(signature);
    ITypeDeclaration[] typeDeclarations = new ITypeDeclaration[count];
    int index = 0;

    for (String parameterType : Signature.getParameterTypes(signature)) {

        // 1. Retrieve the parameter type (without the type parameters)
        String parameterTypeName = Signature.getTypeErasure(parameterType);

        // 3. Convert the type signature to a dot-based name
        parameterTypeName = Signature.toString(parameterTypeName);

        // 4. Create the ITypeDeclaration
        typeDeclarations[index++] = new JpaTypeDeclaration(getTypeRepository().getType(parameterTypeName),
                buildTypeParameters(parameterType), Signature.getArrayCount(parameterType));
    }/*from   ww w  . j a  v  a2  s  . c  om*/

    return typeDeclarations;
}