Search in sources :

Example 26 with IMember

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

the class RenameMethodProcessor method searchForOuterTypesOfReferences.

private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
    final Set<IType> outerTypesOfReferences = new HashSet<IType>();
    SearchPattern pattern = RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = createRefactoringScope(getMethod());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (!(element instanceof IMember))
                // e.g. an IImportDeclaration for a static method import
                return;
            IMember member = (IMember) element;
            IType declaring = member.getDeclaringType();
            if (declaring == null)
                return;
            IType outer = declaring.getDeclaringType();
            if (outer != null)
                outerTypesOfReferences.add(declaring);
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMember(org.eclipse.jdt.core.IMember) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 27 with IMember

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

the class RenameTypeParameterProcessor method createRenameChanges.

/**
	 * Creates the necessary changes for the renaming of the type parameter.
	 *
	 * @param monitor
	 *            the progress monitor to display progress
	 * @return the status of the operation
	 * @throws CoreException
	 *             if the change could not be generated
	 */
private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException {
    Assert.isNotNull(monitor);
    RefactoringStatus status = new RefactoringStatus();
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2);
        ICompilationUnit cu = fTypeParameter.getDeclaringMember().getCompilationUnit();
        CompilationUnit root = RefactoringASTParser.parseWithASTProvider(cu, true, null);
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu, root);
        IMember member = fTypeParameter.getDeclaringMember();
        ASTNode declaration = null;
        if (member instanceof IMethod) {
            declaration = ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root);
        } else if (member instanceof IType) {
            declaration = ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root);
        } else {
            //$NON-NLS-1$
            JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
            Assert.isTrue(false);
        }
        monitor.worked(1);
        RenameTypeParameterVisitor visitor = new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status);
        if (declaration != null)
            declaration.accept(visitor);
        fChange = visitor.getResult();
    } finally {
        monitor.done();
    }
    return status;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) 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 28 with IMember

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

the class RefactoringAvailabilityTester method isPullUpAvailable.

public static boolean isPullUpAvailable(final IStructuredSelection selection) throws JavaModelException {
    if (!selection.isEmpty()) {
        if (selection.size() == 1) {
            if (selection.getFirstElement() instanceof ICompilationUnit)
                // Do not force opening
                return true;
            final IType type = getSingleSelectedType(selection);
            if (type != null)
                return Checks.isAvailable(type) && isPullUpAvailable(new IType[] { type });
        }
        for (final Iterator<?> iterator = selection.iterator(); iterator.hasNext(); ) {
            if (!(iterator.next() instanceof IMember))
                return false;
        }
        final Set<IMember> members = new HashSet<IMember>();
        @SuppressWarnings("unchecked") List<IMember> selectionList = (List<IMember>) (List<?>) Arrays.asList(selection.toArray());
        members.addAll(selectionList);
        return isPullUpAvailable(members.toArray(new IMember[members.size()]));
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) List(java.util.List) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 29 with IMember

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

the class RefactoringAvailabilityTester method isExtractSupertypeAvailable.

public static boolean isExtractSupertypeAvailable(final IStructuredSelection selection) throws JavaModelException {
    if (!selection.isEmpty()) {
        if (selection.size() == 1) {
            if (selection.getFirstElement() instanceof ICompilationUnit)
                // Do not force opening
                return true;
            final IType type = getSingleSelectedType(selection);
            if (type != null)
                return Checks.isAvailable(type) && isExtractSupertypeAvailable(new IType[] { type });
        }
        for (final Iterator<?> iterator = selection.iterator(); iterator.hasNext(); ) {
            if (!(iterator.next() instanceof IMember))
                return false;
        }
        final Set<IMember> members = new HashSet<IMember>();
        @SuppressWarnings("unchecked") List<IMember> selectionList = (List<IMember>) (List<?>) Arrays.asList(selection.toArray());
        members.addAll(selectionList);
        return isExtractSupertypeAvailable(members.toArray(new IMember[members.size()]));
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) List(java.util.List) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 30 with IMember

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

the class RefactoringAvailabilityTester method isInlineMethodAvailable.

public static boolean isInlineMethodAvailable(final JavaTextSelection selection) throws JavaModelException {
    final IJavaElement[] elements = selection.resolveElementAtOffset();
    if (elements.length != 1) {
        IJavaElement enclosingElement = selection.resolveEnclosingElement();
        if (!(enclosingElement instanceof IMember))
            return false;
        ITypeRoot typeRoot = ((IMember) enclosingElement).getTypeRoot();
        CompilationUnit compilationUnit = selection.resolvePartialAstAtOffset();
        if (compilationUnit == null)
            return false;
        return getInlineableMethodNode(typeRoot, compilationUnit, selection.getOffset(), selection.getLength()) != null;
    }
    IJavaElement element = elements[0];
    if (!(element instanceof IMethod))
        return false;
    IMethod method = (IMethod) element;
    if (!isInlineMethodAvailable((method)))
        return false;
    // in binary class, only activate for method declarations
    IJavaElement enclosingElement = selection.resolveEnclosingElement();
    if (enclosingElement == null || enclosingElement.getAncestor(IJavaElement.CLASS_FILE) == null)
        return true;
    if (!(enclosingElement instanceof IMethod))
        return false;
    IMethod enclosingMethod = (IMethod) enclosingElement;
    if (enclosingMethod.isConstructor())
        return false;
    int nameOffset = enclosingMethod.getNameRange().getOffset();
    int nameLength = enclosingMethod.getNameRange().getLength();
    return (nameOffset <= selection.getOffset()) && (selection.getOffset() + selection.getLength() <= nameOffset + nameLength);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember)

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