Search in sources :

Example 1 with IMember

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

Example 2 with IMember

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

the class RenameTypeParameterProcessor method checkNewElementName.

public RefactoringStatus checkNewElementName(String name) throws CoreException {
    Assert.isNotNull(name);
    RefactoringStatus result = Checks.checkTypeParameterName(name, fTypeParameter);
    if (Checks.startsWithLowerCase(name))
        result.addWarning(RefactoringCoreMessages.RenameTypeParameterRefactoring_should_start_lowercase);
    if (Checks.isAlreadyNamed(fTypeParameter, name))
        result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_another_name);
    IMember member = fTypeParameter.getDeclaringMember();
    if (member instanceof IType) {
        IType type = (IType) member;
        if (type.getTypeParameter(name).exists())
            result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_class_type_parameter_already_defined);
    } else if (member instanceof IMethod) {
        IMethod method = (IMethod) member;
        if (method.getTypeParameter(name).exists())
            result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_method_type_parameter_already_defined);
    } else {
        //$NON-NLS-1$
        JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
        Assert.isTrue(false);
    }
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 3 with IMember

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

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

the class SourceMapper method getNameRange.

/**
     * Returns the SourceRange for the name of the given element, or
     * {-1, -1} if no source range is known for the name of the element.
     */
public SourceRange getNameRange(IJavaElement element) {
    switch(element.getElementType()) {
        case IJavaElement.METHOD:
            if (((IMember) element).isBinary()) {
                IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
                if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                    element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
                } else {
                    element = el[0];
                }
            }
            break;
        case IJavaElement.TYPE_PARAMETER:
            IJavaElement parent = element.getParent();
            if (parent.getElementType() == IJavaElement.METHOD) {
                IMethod method = (IMethod) parent;
                if (method.isBinary()) {
                    IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
                    if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                        method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
                    } else {
                        method = (IMethod) el[0];
                    }
                    element = method.getTypeParameter(element.getElementName());
                }
            }
            break;
        case IJavaElement.LOCAL_VARIABLE:
            LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(), element.getElementName());
            SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
            if (ranges == null) {
                return UNKNOWN_RANGE;
            } else {
                return ranges[1];
            }
    }
    SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
    if (ranges == null) {
        return UNKNOWN_RANGE;
    } else {
        return ranges[1];
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IMember(org.eclipse.jdt.core.IMember)

Example 5 with IMember

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

the class IntroduceIndirectionRefactoring method adjustVisibility.

private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {
    Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
    if (isRewriteKept(whoToAdjust.getCompilationUnit()))
        adjustments = fIntermediaryAdjustments;
    else
        adjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    int existingAdjustments = adjustments.size();
    addAdjustment(whoToAdjust, neededVisibility, adjustments);
    if (alsoIncreaseEnclosing)
        while (whoToAdjust.getDeclaringType() != null) {
            whoToAdjust = whoToAdjust.getDeclaringType();
            addAdjustment(whoToAdjust, neededVisibility, adjustments);
        }
    boolean hasNewAdjustments = (adjustments.size() - existingAdjustments) > 0;
    if (hasNewAdjustments && ((whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
        return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));
    RefactoringStatus status = new RefactoringStatus();
    // Don't create a rewrite if it is not necessary
    if (!hasNewAdjustments)
        return status;
    try {
        monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
        Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
        if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
            CompilationUnitRewrite rewrite = new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
            rewrite.setResolveBindings(false);
            rewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
            rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
            status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
            rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        }
    } finally {
        monitor.done();
    }
    return status;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashMap(java.util.HashMap) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

IMember (org.eclipse.jdt.core.IMember)33 IType (org.eclipse.jdt.core.IType)15 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 IMethod (org.eclipse.jdt.core.IMethod)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 List (java.util.List)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IField (org.eclipse.jdt.core.IField)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 HashMap (java.util.HashMap)2 IInitializer (org.eclipse.jdt.core.IInitializer)2