Search in sources :

Example 6 with IMember

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

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 7 with IMember

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

the class JavaDebuggerUtils method findFqnByPosition.

/**
     * Return nested class fqn if line with number {@code lineNumber} contains such element, otherwise return outer class fqn.
     *
     * @param projectPath
     *         project path which contains class with {@code outerClassFqn}
     * @param outerClassFqn
     *         fqn outer class
     * @param lineNumber
     *         line position to search
     * @throws DebuggerException
     */
public String findFqnByPosition(String projectPath, String outerClassFqn, int lineNumber) throws DebuggerException {
    if (projectPath == null) {
        return outerClassFqn;
    }
    IJavaProject project = MODEL.getJavaProject(projectPath);
    IType outerClass;
    IMember iMember;
    try {
        outerClass = project.findType(outerClassFqn);
        if (outerClass == null) {
            return outerClassFqn;
        }
        String source;
        if (outerClass.isBinary()) {
            IClassFile classFile = outerClass.getClassFile();
            source = classFile.getSource();
        } else {
            ICompilationUnit unit = outerClass.getCompilationUnit();
            source = unit.getSource();
        }
        Document document = new Document(source);
        IRegion region = document.getLineInformation(lineNumber);
        int start = region.getOffset();
        int end = start + region.getLength();
        iMember = binSearch(outerClass, start, end);
    } catch (JavaModelException e) {
        throw new DebuggerException(format("Unable to find source for class with fqn '%s' in the project '%s'", outerClassFqn, project), e);
    } catch (BadLocationException e) {
        throw new DebuggerException("Unable to calculate breakpoint location", e);
    }
    if (iMember instanceof IType) {
        return ((IType) iMember).getFullyQualifiedName();
    }
    if (iMember != null) {
        return iMember.getDeclaringType().getFullyQualifiedName();
    }
    return outerClassFqn;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClassFile(org.eclipse.jdt.core.IClassFile) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Document(org.eclipse.jface.text.Document) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

Example 8 with IMember

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

the class RippleMethodFinder2 method uniteWithSupertypes.

private void uniteWithSupertypes(IType anchor, IType type) throws JavaModelException {
    IType[] supertypes = fHierarchy.getSupertypes(type);
    for (int i = 0; i < supertypes.length; i++) {
        IType supertype = supertypes[i];
        IType superRep = fUnionFind.find(supertype);
        if (superRep == null) {
            //Type doesn't declare method, but maybe supertypes?
            uniteWithSupertypes(anchor, supertype);
        } else {
            //check whether method in supertype is really overridden:
            IMember superMethod = fTypeToMethod.get(supertype);
            if (JavaModelUtil.isVisibleInHierarchy(superMethod, anchor.getPackageFragment())) {
                IType rep = fUnionFind.find(anchor);
                fUnionFind.union(rep, superRep);
                // current type is no root anymore
                fRootTypes.remove(anchor);
                uniteWithSupertypes(supertype, supertype);
            } else {
            //Not overridden -> overriding chain ends here.
            }
        }
    }
}
Also used : IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 9 with IMember

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

the class SearchManager method performFindUsageSearch.

private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException {
    JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
    boolean isInsideJRE = factory.isInsideJRE(element);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
    Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
    JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
    for (Object o : javaResult.getElements()) {
        IJavaElement javaElement = (IJavaElement) o;
        IDocument document = null;
        if (javaElement instanceof IMember) {
            IMember member = ((IMember) javaElement);
            if (member.isBinary()) {
                if (member.getClassFile().getSource() != null) {
                    document = new Document(member.getClassFile().getSource());
                }
            } else {
                document = getDocument(member.getCompilationUnit());
            }
        } else if (javaElement instanceof IPackageDeclaration) {
            ICompilationUnit ancestor = (ICompilationUnit) (javaElement).getAncestor(IJavaElement.COMPILATION_UNIT);
            document = getDocument(ancestor);
        }
        converter.addElementToProjectHierarchy(javaElement);
        Match[] matches = javaResult.getMatches(o);
        List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
        for (Match match : matches) {
            org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory.newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
            if (document != null) {
                IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());
                int offsetInLine = match.getOffset() - lineInformation.getOffset();
                Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine).withLength(match.getLength());
                dtoMatch.setMatchInLine(matchInLine);
                dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
                dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
            }
            dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()).withLength(match.getLength()));
            matchList.add(dtoMatch);
        }
        mapMaches.put(javaElement.getHandleIdentifier(), matchList);
    }
    List<JavaProject> projects = converter.getProjects();
    response.setProjects(projects);
    response.setMatches(mapMaches);
    response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
    return response;
}
Also used : ISearchResult(org.eclipse.search.ui.ISearchResult) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) Match(org.eclipse.search.ui.text.Match) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult) ArrayList(java.util.ArrayList) List(java.util.List) JavaSearchScopeFactory(org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProject(org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) Region(org.eclipse.che.ide.ext.java.shared.dto.Region) IRegion(org.eclipse.jface.text.IRegion) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) IDocument(org.eclipse.jface.text.IDocument)

Example 10 with IMember

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

the class StubCreator method appendMembers.

protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
    try {
        monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
        final IJavaElement[] children = type.getChildren();
        for (int index = 0; index < children.length; index++) {
            final IMember child = (IMember) children[index];
            final int flags = child.getFlags();
            final boolean isPrivate = Flags.isPrivate(flags);
            final boolean isDefault = !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
            final boolean stub = fStubInvisible || (!isPrivate && !isDefault);
            if (child instanceof IType) {
                if (stub)
                    appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
            } else if (child instanceof IField) {
                if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
                    appendFieldDeclaration((IField) child);
            } else if (child instanceof IMethod) {
                final IMethod method = (IMethod) child;
                final String name = method.getElementName();
                if (method.getDeclaringType().isEnum()) {
                    final int count = method.getNumberOfParameters();
                    if (//$NON-NLS-1$
                    count == 0 && "values".equals(name))
                        continue;
                    if (//$NON-NLS-1$ //$NON-NLS-2$
                    count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0]))
                        continue;
                    if (method.isConstructor())
                        continue;
                }
                //$NON-NLS-1$
                boolean skip = !stub || name.equals("<clinit>");
                if (method.isConstructor())
                    skip = false;
                skip = skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
                if (!skip)
                    appendMethodDeclaration(method);
            }
            //$NON-NLS-1$
            fBuffer.append("\n");
        }
    } finally {
        monitor.done();
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IMember(org.eclipse.jdt.core.IMember) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

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