Search in sources :

Example 1 with IJavaElement

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

the class Util method getPackageFragment.

public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, int jarSeparator) {
    if (jarSeparator != -1) {
        String jarMemento = new String(fileName, 0, jarSeparator);
        PackageFragmentRoot root = (PackageFragmentRoot) JavaCore.create(jarMemento);
        if (pkgEnd == jarSeparator)
            return root.getPackageFragment(CharOperation.NO_STRINGS);
        char[] pkgName = CharOperation.subarray(fileName, jarSeparator + 1, pkgEnd);
        char[][] compoundName = CharOperation.splitOn('/', pkgName);
        return root.getPackageFragment(CharOperation.toStrings(compoundName));
    } else {
        Path path = new Path(new String(fileName, 0, pkgEnd));
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IContainer folder = path.segmentCount() == 1 ? workspaceRoot.getProject(path.lastSegment()) : (IContainer) workspaceRoot.getFolder(path);
        IJavaElement element = JavaCore.create(folder);
        if (element == null)
            return null;
        switch(element.getElementType()) {
            case IJavaElement.PACKAGE_FRAGMENT:
                return (IPackageFragment) element;
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                return ((PackageFragmentRoot) element).getPackageFragment(CharOperation.NO_STRINGS);
            case IJavaElement.JAVA_PROJECT:
                PackageFragmentRoot root = (PackageFragmentRoot) ((IJavaProject) element).getPackageFragmentRoot(folder);
                if (root == null)
                    return null;
                return root.getPackageFragment(CharOperation.NO_STRINGS);
        }
        return null;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IContainer(org.eclipse.core.resources.IContainer)

Example 2 with IJavaElement

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

the class Util method isExcluded.

/*
 * Returns whether the given java element is exluded from its root's classpath.
 * It doesn't check whether the root itself is on the classpath or not
 */
public static final boolean isExcluded(IJavaElement element) {
    int elementType = element.getElementType();
    switch(elementType) {
        case IJavaElement.JAVA_MODEL:
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return false;
        case IJavaElement.PACKAGE_FRAGMENT:
            PackageFragmentRoot root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            IResource resource = ((PackageFragment) element).resource();
            return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());
        case IJavaElement.COMPILATION_UNIT:
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            resource = element.getResource();
            if (resource == null)
                return false;
            if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
                return true;
            return isExcluded(element.getParent());
        default:
            IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            return cu != null && isExcluded(cu);
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IResource(org.eclipse.core.resources.IResource)

Example 3 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement 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 IJavaElement

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

the class ContextSensitiveImportRewriteContext method getImportedNames.

private Name[] getImportedNames() {
    if (fImportedNames == null) {
        IJavaProject project = null;
        IJavaElement javaElement = fCompilationUnit.getJavaElement();
        if (javaElement != null)
            project = javaElement.getJavaProject();
        List<SimpleName> imports = new ArrayList<SimpleName>();
        ImportReferencesCollector.collect(fCompilationUnit, project, null, imports, null);
        fImportedNames = imports.toArray(new Name[imports.size()]);
    }
    return fImportedNames;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 5 with IJavaElement

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

the class HandleFactory method createElement.

/**
	 * Create handle by adding child to parent obtained by recursing into parent scopes.
	 */
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;
    switch(scope.kind) {
        case Scope.COMPILATION_UNIT_SCOPE:
            newElement = unit;
            break;
        case Scope.CLASS_SCOPE:
            IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            switch(parentElement.getElementType()) {
                case IJavaElement.COMPILATION_UNIT:
                    newElement = ((ICompilationUnit) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.TYPE:
                    newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.FIELD:
                case IJavaElement.INITIALIZER:
                case IJavaElement.METHOD:
                    IMember member = (IMember) parentElement;
                    if (member.isBinary()) {
                        return null;
                    } else {
                        newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                        // increment occurrence count if collision is detected
                        if (newElement != null) {
                            while (!existingElements.add(newElement)) ((SourceRefElement) newElement).occurrenceCount++;
                        }
                    }
                    break;
            }
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
            break;
        case Scope.METHOD_SCOPE:
            if (scope.isLambdaScope()) {
                parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
                LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
                if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && !(expression.descriptor instanceof ProblemMethodBinding)) {
                    // chain in lambda element only if resolved properly.
                    //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    knownScopes.put(scope, newElement);
                    return newElement;
                }
                return parentElement;
            }
            IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            MethodScope methodScope = (MethodScope) scope;
            if (methodScope.isInsideInitializer()) {
                // inside field or initializer, must find proper one
                TypeDeclaration type = methodScope.referenceType();
                int occurenceCount = 1;
                int length = type.fields == null ? 0 : type.fields.length;
                for (int i = 0; i < length; i++) {
                    FieldDeclaration field = type.fields[i];
                    if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
                        switch(field.getKind()) {
                            case AbstractVariableDeclaration.FIELD:
                            case AbstractVariableDeclaration.ENUM_CONSTANT:
                                newElement = parentType.getField(new String(field.name));
                                break;
                            case AbstractVariableDeclaration.INITIALIZER:
                                newElement = parentType.getInitializer(occurenceCount);
                                break;
                        }
                        break;
                    } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                        occurenceCount++;
                    }
                }
            } else {
                // method element
                AbstractMethodDeclaration method = methodScope.referenceMethod();
                newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
                if (newElement != null) {
                    knownScopes.put(scope, newElement);
                }
            }
            break;
        case Scope.BLOCK_SCOPE:
            // standard block, no element per se
            newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            break;
    }
    return newElement;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodScope(org.eclipse.jdt.internal.compiler.lookup.MethodScope) IMember(org.eclipse.jdt.core.IMember) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) SourceRefElement(org.eclipse.jdt.internal.core.SourceRefElement) IType(org.eclipse.jdt.core.IType)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)208 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)46 IType (org.eclipse.jdt.core.IType)41 ArrayList (java.util.ArrayList)34 IJavaProject (org.eclipse.jdt.core.IJavaProject)32 JavaModelException (org.eclipse.jdt.core.JavaModelException)31 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)30 IResource (org.eclipse.core.resources.IResource)29 IMethod (org.eclipse.jdt.core.IMethod)28 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)28 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)21 IField (org.eclipse.jdt.core.IField)14 IMember (org.eclipse.jdt.core.IMember)14 CoreException (org.eclipse.core.runtime.CoreException)13 StringTokenizer (java.util.StringTokenizer)11 ISourceRange (org.eclipse.jdt.core.ISourceRange)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 IPath (org.eclipse.core.runtime.IPath)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)8