Search in sources :

Example 1 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.

the class HandleFactory method getJarPkgFragmentRoot.

/**
	 * Returns the package fragment root that corresponds to the given jar path.
	 * See createOpenable(...) for the format of the jar path string.
	 * If not null, uses the given scope as a hint for getting Java project handles.
	 */
private PackageFragmentRoot getJarPkgFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPathString, IJavaSearchScope scope) {
    IPath jarPath = new Path(jarPathString);
    Object target = JavaModel.getTarget(jarPath, false);
    if (target instanceof IFile) {
        // internal jar: is it on the classpath of its project?
        //  e.g. org.eclipse.swt.win32/ws/win32/swt.jar
        //        is NOT on the classpath of org.eclipse.swt.win32
        IFile jarFile = (IFile) target;
        JavaProject javaProject = (JavaProject) this.javaModel.getJavaProject(jarFile);
        try {
            IClasspathEntry entry = javaProject.getClasspathEntryFor(jarPath);
            if (entry != null) {
                return (PackageFragmentRoot) javaProject.getPackageFragmentRoot(jarFile);
            }
        } catch (JavaModelException e) {
        // ignore and try to find another project
        }
    }
    // walk projects in the scope and find the first one that has the given jar path in its classpath
    IJavaProject[] projects;
    if (scope != null) {
        if (scope instanceof AbstractJavaSearchScope) {
            PackageFragmentRoot root = (PackageFragmentRoot) ((AbstractJavaSearchScope) scope).packageFragmentRoot(resourcePathString, jarSeparatorIndex, jarPathString);
            if (root != null)
                return root;
        } else {
            IPath[] enclosingProjectsAndJars = scope.enclosingProjectsAndJars();
            int length = enclosingProjectsAndJars.length;
            projects = new IJavaProject[length];
            int index = 0;
            for (int i = 0; i < length; i++) {
                IPath path = enclosingProjectsAndJars[i];
                if (path.segmentCount() == 1) {
                    projects[index++] = this.javaModel.getJavaProject(path.segment(0));
                }
            }
            if (index < length) {
                System.arraycopy(projects, 0, projects = new IJavaProject[index], 0, index);
            }
            PackageFragmentRoot root = getJarPkgFragmentRoot(jarPath, target, projects);
            if (root != null) {
                return root;
            }
        }
    }
    // not found in the scope, walk all projects
    try {
        projects = this.javaModel.getJavaProjects();
    } catch (JavaModelException e) {
        // java model is not accessible
        return null;
    }
    return getJarPkgFragmentRoot(jarPath, target, projects);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AbstractJavaSearchScope(org.eclipse.jdt.internal.core.search.AbstractJavaSearchScope) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Example 2 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.

the class Util method getUnresolvedJavaElement.

/**
     * Return the java element corresponding to the given compiler binding.
     */
public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
    JavaElement unresolvedJavaElement = getUnresolvedJavaElement(methodBinding.declaringClass, workingCopyOwner, bindingsToNodes);
    if (unresolvedJavaElement == null || unresolvedJavaElement.getElementType() != IJavaElement.TYPE) {
        return null;
    }
    IType declaringType = (IType) unresolvedJavaElement;
    org.eclipse.jdt.internal.compiler.ast.ASTNode node = bindingsToNodes == null ? null : bindingsToNodes.get(methodBinding);
    if (node != null && !declaringType.isBinary()) {
        if (node instanceof AnnotationMethodDeclaration) {
            // node is an AnnotationMethodDeclaration
            AnnotationMethodDeclaration typeMemberDeclaration = (AnnotationMethodDeclaration) node;
            // annotation type members don't have parameters
            return (JavaElement) declaringType.getMethod(String.valueOf(typeMemberDeclaration.selector), CharOperation.NO_STRINGS);
        } else {
            // node is an MethodDeclaration
            MethodDeclaration methodDeclaration = (MethodDeclaration) node;
            Argument[] arguments = methodDeclaration.arguments;
            String[] parameterSignatures;
            if (arguments != null) {
                parameterSignatures = new String[arguments.length];
                for (int i = 0; i < arguments.length; i++) {
                    Argument argument = arguments[i];
                    TypeReference typeReference = argument.type;
                    int arrayDim = typeReference.dimensions();
                    String typeSig = Signature.createTypeSignature(CharOperation.concatWith(typeReference.getTypeName(), '.'), false);
                    if (arrayDim > 0) {
                        typeSig = Signature.createArraySignature(typeSig, arrayDim);
                    }
                    parameterSignatures[i] = typeSig;
                }
            } else {
                parameterSignatures = CharOperation.NO_STRINGS;
            }
            return (JavaElement) declaringType.getMethod(String.valueOf(methodDeclaration.selector), parameterSignatures);
        }
    } else {
        // case of method not in the created AST, or a binary method
        org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = methodBinding.original();
        String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
        boolean isBinary = declaringType.isBinary();
        ReferenceBinding enclosingType = original.declaringClass.enclosingType();
        // Static inner types' constructors don't get receivers (https://bugs.eclipse.org/bugs/show_bug.cgi?id=388137)
        boolean isInnerBinaryTypeConstructor = isBinary && original.isConstructor() && !original.declaringClass.isStatic() && enclosingType != null;
        TypeBinding[] parameters = original.parameters;
        int length = parameters == null ? 0 : parameters.length;
        int declaringIndex = isInnerBinaryTypeConstructor ? 1 : 0;
        String[] parameterSignatures = new String[declaringIndex + length];
        if (isInnerBinaryTypeConstructor)
            parameterSignatures[0] = new String(enclosingType.genericTypeSignature()).replace('/', '.');
        for (int i = 0; i < length; i++) {
            char[] signature = parameters[i].genericTypeSignature();
            if (isBinary) {
                signature = CharOperation.replaceOnCopy(signature, '/', '.');
            } else {
                signature = toUnresolvedTypeSignature(signature);
            }
            parameterSignatures[declaringIndex + i] = new String(signature);
        }
        IMethod result = declaringType.getMethod(selector, parameterSignatures);
        if (isBinary)
            return (JavaElement) result;
        if (// if perfect match (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249567 )
        result.exists())
            return (JavaElement) result;
        IMethod[] methods = null;
        try {
            methods = declaringType.getMethods();
        } catch (JavaModelException e) {
            // declaring type doesn't exist
            return null;
        }
        IMethod[] candidates = Member.findMethods(result, methods);
        if (candidates == null || candidates.length == 0)
            return null;
        return (JavaElement) candidates[0];
    }
}
Also used : MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) IType(org.eclipse.jdt.core.IType) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) IMethod(org.eclipse.jdt.core.IMethod)

Example 3 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project che 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 4 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project che 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
            e.printStackTrace();
        }
    }
    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 5 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.

the class StubUtility method getLineDelimiterUsed.

/**
	 * @param elem a Java element (doesn't have to exist)
	 * @return the existing or default line delimiter for the element
	 */
public static String getLineDelimiterUsed(IJavaElement elem) {
    IOpenable openable = elem.getOpenable();
    if (openable instanceof ITypeRoot) {
        try {
            return openable.findRecommendedLineSeparator();
        } catch (JavaModelException exception) {
        // Use project setting
        }
    }
    IJavaProject project = elem.getJavaProject();
    return getProjectLineDelimiter(project.exists() ? project : null);
}
Also used : IOpenable(org.eclipse.jdt.core.IOpenable) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeRoot(org.eclipse.jdt.core.ITypeRoot)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)162 IJavaProject (org.eclipse.jdt.core.IJavaProject)41 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)40 IType (org.eclipse.jdt.core.IType)39 IJavaElement (org.eclipse.jdt.core.IJavaElement)34 CoreException (org.eclipse.core.runtime.CoreException)28 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)23 ArrayList (java.util.ArrayList)21 IPath (org.eclipse.core.runtime.IPath)20 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)15 HashMap (java.util.HashMap)14 IResource (org.eclipse.core.resources.IResource)13 IMethod (org.eclipse.jdt.core.IMethod)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 IFile (org.eclipse.core.resources.IFile)11 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 Iterator (java.util.Iterator)8 ISourceRange (org.eclipse.jdt.core.ISourceRange)8