Search in sources :

Example 1 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement 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 2 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project che by eclipse.

the class JavaSearchScope method toString.

public String toString() {
    //$NON-NLS-1$
    StringBuffer result = new StringBuffer("JavaSearchScope on ");
    if (this.elements != null) {
        //$NON-NLS-1$
        result.append("[");
        for (int i = 0, length = this.elements.size(); i < length; i++) {
            JavaElement element = (JavaElement) this.elements.get(i);
            //$NON-NLS-1$
            result.append("\n\t");
            result.append(element.toStringWithAncestors());
        }
        //$NON-NLS-1$
        result.append("\n]");
    } else {
        if (this.pathsCount == 0) {
            //$NON-NLS-1$
            result.append("[empty scope]");
        } else {
            //$NON-NLS-1$
            result.append("[");
            String[] paths = new String[this.relativePaths.length];
            int index = 0;
            for (int i = 0; i < this.relativePaths.length; i++) {
                String path = this.relativePaths[i];
                if (path == null)
                    continue;
                String containerPath;
                if (ExternalFoldersManager.isInternalPathForExternalFolder(new Path(this.containerPaths[i]))) {
                    Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[i]));
                    containerPath = ((IFolder) target).getLocation().toOSString();
                } else {
                    containerPath = this.containerPaths[i];
                }
                if (path.length() > 0) {
                    paths[index++] = containerPath + '/' + path;
                } else {
                    paths[index++] = containerPath;
                }
            }
            System.arraycopy(paths, 0, paths = new String[index], 0, index);
            Util.sort(paths);
            for (int i = 0; i < index; i++) {
                //$NON-NLS-1$
                result.append("\n\t");
                result.append(paths[i]);
            }
            //$NON-NLS-1$
            result.append("\n]");
        }
    }
    return result.toString();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project che by eclipse.

the class JavaSearchScope method add.

/**
     * Add an element to the java search scope.
     * @param element The element we want to add to current java search scope
     * @throws JavaModelException May happen if some Java Model info are not available
     */
public void add(IJavaElement element) throws JavaModelException {
    IPath containerPath = null;
    String containerPathToString = null;
    PackageFragmentRoot root = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch(element.getElementType()) {
        case IJavaElement.JAVA_MODEL:
            // a workspace sope should be used
            break;
        case IJavaElement.JAVA_PROJECT:
            add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            root = (PackageFragmentRoot) element;
            IPath rootPath = root.internalPath();
            containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            IResource rootResource = root.resource();
            String projectPath = root.getJavaProject().getPath().toString();
            if (rootResource != null && rootResource.isAccessible()) {
                String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
                add(projectPath, relativePath, containerPathToString, false, /*not a package*/
                null);
            } else {
                //$NON-NLS-1$
                add(projectPath, "", containerPathToString, false, /*not a package*/
                null);
            }
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            root = (PackageFragmentRoot) element.getParent();
            projectPath = root.getJavaProject().getPath().toString();
            if (root.isArchive()) {
                String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
                containerPath = root.getPath();
                containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                add(projectPath, relativePath, containerPathToString, true, /*package*/
                null);
            } else {
                IResource resource = ((JavaElement) element).resource();
                if (resource != null) {
                    if (resource.isAccessible()) {
                        containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath();
                    } else {
                        // for working copies, get resource container full path
                        containerPath = resource.getParent().getFullPath();
                    }
                    containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                    String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
                    add(projectPath, relativePath, containerPathToString, true, /*package*/
                    null);
                }
            }
            break;
        default:
            // remember sub-cu (or sub-class file) java elements
            if (element instanceof IMember) {
                if (this.elements == null) {
                    this.elements = new ArrayList();
                }
                this.elements.add(element);
            }
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            projectPath = root.getJavaProject().getPath().toString();
            String relativePath;
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                containerPath = root.getParent().getPath();
                relativePath = Util.relativePath(getPath(element, false), root.getJavaProject().getPath().segmentCount());
            } else {
                containerPath = root.internalPath();
                relativePath = getPath(element, true).toString();
            }
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            add(projectPath, relativePath, containerPathToString, false, /*not a package*/
            null);
    }
    if (root != null)
        addEnclosingProjectOrJar(root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}
Also used : PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 4 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project jbosstools-hibernate by jbosstools.

the class CompilationUnitCollector method processJavaElements.

/**
 * Process object - java element to collect all it's children CompilationUnits
 * @param obj
 * @param bCollect
 */
public void processJavaElements(Object obj, boolean bCollect) {
    if (obj instanceof ICompilationUnit) {
        ICompilationUnit cu = (ICompilationUnit) obj;
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof File) {
        File file = (File) obj;
        if (file.getProject() != null) {
            IJavaProject javaProject = JavaCore.create(file.getProject());
            ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject, file.getFullPath());
            if (cus != null) {
                for (int i = 0; i < cus.length; i++) {
                    addCompilationUnit(cus[i], bCollect);
                }
            }
        }
    } else if (obj instanceof JavaProject) {
        JavaProject javaProject = (JavaProject) obj;
        IPackageFragmentRoot[] pfr = null;
        try {
            pfr = javaProject.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (pfr != null) {
            for (int i = 0; i < pfr.length; i++) {
                processJavaElements(pfr[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragment) {
        PackageFragment packageFragment = (PackageFragment) obj;
        ICompilationUnit[] cus = null;
        try {
            cus = packageFragment.getCompilationUnits();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (cus != null && cus.length > 0) {
            if (bCollect) {
                selection2UpdateList.add(obj);
                bCollect = false;
            }
            for (int i = 0; i < cus.length; i++) {
                addCompilationUnit(cus[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragmentRoot) {
        JavaElement javaElement = (JavaElement) obj;
        JavaElementInfo javaElementInfo = null;
        try {
            javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (javaElementInfo != null) {
            IJavaElement[] je = javaElementInfo.getChildren();
            for (int i = 0; i < je.length; i++) {
                processJavaElements(je[i], true);
            }
        }
    } else if (obj instanceof JavaElement) {
        JavaElement javaElement = (JavaElement) obj;
        ICompilationUnit cu = javaElement.getCompilationUnit();
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof SourceType) {
        if (bCollect) {
            selection2UpdateList.add(obj);
            bCollect = false;
        }
        SourceType sourceType = (SourceType) obj;
        processJavaElements(sourceType.getJavaModel(), bCollect);
    } else {
    // ignore
    // System.out.println("1 Blah! " + selection); //$NON-NLS-1$
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) SourceType(org.eclipse.jdt.internal.core.SourceType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaElement(org.eclipse.jdt.internal.core.JavaElement) JavaElementInfo(org.eclipse.jdt.internal.core.JavaElementInfo) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(org.eclipse.core.internal.resources.File)

Example 5 with JavaElement

use of org.eclipse.jdt.internal.core.JavaElement in project che by eclipse.

the class Util method getUnresolvedJavaElement.

/**
     * Return the java element corresponding to the given compiler binding.
     */
public static JavaElement getUnresolvedJavaElement(TypeBinding typeBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
    if (typeBinding == null)
        return null;
    switch(typeBinding.kind()) {
        case Binding.ARRAY_TYPE:
            typeBinding = ((org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) typeBinding).leafComponentType();
            return getUnresolvedJavaElement(typeBinding, workingCopyOwner, bindingsToNodes);
        case Binding.BASE_TYPE:
        case Binding.WILDCARD_TYPE:
        case Binding.INTERSECTION_TYPE:
            return null;
        default:
            if (typeBinding.isCapture())
                return null;
    }
    ReferenceBinding referenceBinding;
    if (typeBinding.isParameterizedType() || typeBinding.isRawType())
        referenceBinding = (ReferenceBinding) typeBinding.erasure();
    else
        referenceBinding = (ReferenceBinding) typeBinding;
    char[] fileName = referenceBinding.getFileName();
    if (referenceBinding.isLocalType() || referenceBinding.isAnonymousType()) {
        // local or anonymous type
        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
            int jarSeparator = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR, fileName);
            // pkgEnd is exclusive
            int pkgEnd = CharOperation.lastIndexOf('/', fileName);
            if (pkgEnd == -1)
                pkgEnd = CharOperation.lastIndexOf(File.separatorChar, fileName);
            if (// if in a jar and no slash, it is a default package -> pkgEnd should be equal to jarSeparator
            jarSeparator != -1 && pkgEnd < jarSeparator)
                pkgEnd = jarSeparator;
            if (pkgEnd == -1)
                return null;
            IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator);
            char[] constantPoolName = referenceBinding.constantPoolName();
            if (constantPoolName == null) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                return classFile == null ? null : (JavaElement) classFile.getType();
            }
            pkgEnd = CharOperation.lastIndexOf('/', constantPoolName);
            char[] classFileName = CharOperation.subarray(constantPoolName, pkgEnd + 1, constantPoolName.length);
            ClassFile classFile = (ClassFile) pkg.getClassFile(new String(classFileName) + SuffixConstants.SUFFIX_STRING_class);
            return (JavaElement) classFile.getType();
        }
        ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
        if (cu == null)
            return null;
        // must use getElementAt(...) as there is no back pointer to the defining method (scope is null after resolution has ended)
        try {
            int sourceStart = ((org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) referenceBinding).sourceStart;
            return (JavaElement) cu.getElementAt(sourceStart);
        } catch (JavaModelException e) {
            // does not exist
            return null;
        }
    } else if (referenceBinding.isTypeVariable()) {
        // type parameter
        final String typeVariableName = new String(referenceBinding.sourceName());
        org.eclipse.jdt.internal.compiler.lookup.Binding declaringElement = ((org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) referenceBinding).declaringElement;
        if (declaringElement instanceof MethodBinding) {
            IMethod declaringMethod = (IMethod) getUnresolvedJavaElement((MethodBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringMethod.getTypeParameter(typeVariableName);
        } else {
            IType declaringType = (IType) getUnresolvedJavaElement((TypeBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringType.getTypeParameter(typeVariableName);
        }
    } else {
        // case of a WilCardBinding that doesn't have a corresponding Java element
        if (fileName == null)
            return null;
        // member or top level type
        TypeBinding declaringTypeBinding = typeBinding.enclosingType();
        if (declaringTypeBinding == null) {
            // top level type
            if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                if (classFile == null)
                    return null;
                return (JavaElement) classFile.getType();
            }
            ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
            if (cu == null)
                return null;
            return (JavaElement) cu.getType(new String(referenceBinding.sourceName()));
        } else {
            // member type
            IType declaringType = (IType) getUnresolvedJavaElement(declaringTypeBinding, workingCopyOwner, bindingsToNodes);
            if (declaringType == null)
                return null;
            return (JavaElement) declaringType.getType(new String(referenceBinding.sourceName()));
        }
    }
}
Also used : FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) 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) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)8 JavaElement (org.eclipse.jdt.internal.core.JavaElement)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IType (org.eclipse.jdt.core.IType)3 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)3 PackageFragmentRoot (org.eclipse.jdt.internal.core.PackageFragmentRoot)3 IPath (org.eclipse.core.runtime.IPath)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IMethod (org.eclipse.jdt.core.IMethod)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)2 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)2 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)2 JavaElementInfo (org.eclipse.jdt.internal.core.JavaElementInfo)2 JavaProject (org.eclipse.jdt.internal.core.JavaProject)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1