Search in sources :

Example 96 with IMethod

use of org.eclipse.jdt.core.IMethod in project azure-tools-for-java by Microsoft.

the class SparkSubmissionExDialog method getClassesWithMainMethod.

private java.util.Set<String> getClassesWithMainMethod() throws CoreException {
    java.util.Set<String> filterClassSet = new HashSet<String>();
    if (myProject != null && myProject.isNatureEnabled(JAVA_NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(myProject);
        if (javaProject == null) {
            return filterClassSet;
        }
        IPackageFragment[] packages = javaProject.getPackageFragments();
        if (packages == null || packages.length == 0) {
            return filterClassSet;
        }
        for (IPackageFragment mypackage : packages) {
            // get source type
            if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
                for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
                    IType[] allTypes = unit.getAllTypes();
                    final String unitName = unit.getElementName();
                    for (IType type : allTypes) {
                        IMethod[] methods = type.getMethods();
                        for (IMethod method : methods) {
                            if (method.getElementName().equalsIgnoreCase("main")) {
                                String simpleClassName = type.getElementName();
                                if (simpleClassName == null || simpleClassName.isEmpty()) {
                                    // remove .class suffix
                                    simpleClassName = unitName.substring(0, unitName.length() - 6);
                                }
                                // Handle duplicated Scala class
                                if (simpleClassName.endsWith("$") && simpleClassName.length() > 1) {
                                    simpleClassName = simpleClassName.substring(0, simpleClassName.length() - 1);
                                }
                                if (mypackage.isDefaultPackage()) {
                                    filterClassSet.add(simpleClassName);
                                } else {
                                    final String className = String.format("%s.%s", mypackage.getElementName(), simpleClassName);
                                    filterClassSet.add(className);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return filterClassSet;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 97 with IMethod

use of org.eclipse.jdt.core.IMethod in project flux by eclipse.

the class StubUtility method suggestArgumentNames.

public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) {
    int nParams = binding.getParameterTypes().length;
    if (nParams > 0) {
        try {
            IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
            if (method != null) {
                String[] paramNames = method.getParameterNames();
                if (paramNames.length == nParams) {
                    String[] namesArray = EMPTY;
                    ArrayList<String> newNames = new ArrayList<String>(paramNames.length);
                    // Ensure that the code generation preferences are respected
                    for (int i = 0; i < paramNames.length; i++) {
                        String curr = paramNames[i];
                        String baseName = NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, method.getJavaProject());
                        if (!curr.equals(baseName)) {
                            // make the existing name the favorite
                            newNames.add(curr);
                        } else {
                            newNames.add(suggestArgumentName(project, curr, namesArray));
                        }
                        namesArray = newNames.toArray(new String[newNames.size()]);
                    }
                    return namesArray;
                }
            }
        } catch (JavaModelException e) {
        // ignore
        }
    }
    String[] names = new String[nParams];
    for (int i = 0; i < names.length; i++) {
        // $NON-NLS-1$
        names[i] = "arg" + i;
    }
    return names;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) IMethod(org.eclipse.jdt.core.IMethod)

Example 98 with IMethod

use of org.eclipse.jdt.core.IMethod in project flux by eclipse.

the class StubUtility method getBaseNameFromLocationInParent.

private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
    if (binding == null)
        return null;
    ITypeBinding[] parameterTypes = binding.getParameterTypes();
    if (// beware of guessed method bindings
    parameterTypes.length != arguments.size())
        return null;
    int index = arguments.indexOf(assignedExpression);
    if (index == -1)
        return null;
    ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding();
    if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
        return null;
    try {
        IJavaElement javaElement = binding.getJavaElement();
        if (javaElement instanceof IMethod) {
            IMethod method = (IMethod) javaElement;
            if (method.getOpenable().getBuffer() != null) {
                // avoid dummy names and lookup from Javadoc
                String[] parameterNames = method.getParameterNames();
                if (index < parameterNames.length) {
                    return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
                }
            }
        }
    } catch (JavaModelException e) {
    // ignore
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IMethod(org.eclipse.jdt.core.IMethod)

Example 99 with IMethod

use of org.eclipse.jdt.core.IMethod in project flux by eclipse.

the class StubUtility method suggestArgumentNamesWithProposals.

public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
    int nParams = binding.getParameterTypes().length;
    if (nParams > 0) {
        try {
            IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
            if (method != null) {
                String[] parameterNames = method.getParameterNames();
                if (parameterNames.length == nParams) {
                    return suggestArgumentNamesWithProposals(project, parameterNames);
                }
            }
        } catch (JavaModelException e) {
        // ignore
        }
    }
    String[][] names = new String[nParams][];
    for (int i = 0; i < names.length; i++) {
        // $NON-NLS-1$
        names[i] = new String[] { "arg" + i };
    }
    return names;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethod(org.eclipse.jdt.core.IMethod)

Example 100 with IMethod

use of org.eclipse.jdt.core.IMethod in project flux by eclipse.

the class Bindings method findMethod.

/**
 * Finds a method for the given <code>IMethodBinding</code>. Returns
 * <code>null</code> if the type doesn't contain a corresponding method.
 * @param method the method to find
 * @param type the type to look in
 * @return the corresponding IMethod or <code>null</code>
 * @throws JavaModelException if an error occurs in the Java model
 * @deprecated Use {@link #findMethodInHierarchy(ITypeBinding, String, String[])} or {@link JavaModelUtil}
 */
public static IMethod findMethod(IMethodBinding method, IType type) throws JavaModelException {
    method = method.getMethodDeclaration();
    IMethod[] candidates = type.getMethods();
    for (int i = 0; i < candidates.length; i++) {
        IMethod candidate = candidates[i];
        if (candidate.getElementName().equals(method.getName()) && sameParameters(method, candidate)) {
            return candidate;
        }
    }
    return null;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)217 IType (org.eclipse.jdt.core.IType)112 IJavaElement (org.eclipse.jdt.core.IJavaElement)56 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)37 ArrayList (java.util.ArrayList)35 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 IField (org.eclipse.jdt.core.IField)27 Test (org.junit.Test)22 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)19 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)19 IMember (org.eclipse.jdt.core.IMember)15 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)14 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)13 IJavaProject (org.eclipse.jdt.core.IJavaProject)12 HashSet (java.util.HashSet)11 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 ISourceRange (org.eclipse.jdt.core.ISourceRange)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)9