Search in sources :

Example 1 with IPackageDeclaration

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

the class CorrectPackageDeclarationProposal method getName.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeCorrectionProposal#getName()
	 */
@Override
public String getName() {
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    try {
        IPackageDeclaration[] decls = cu.getPackageDeclarations();
        if (parentPack.isDefaultPackage() && decls.length > 0) {
            return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
        }
        if (!parentPack.isDefaultPackage() && decls.length == 0) {
            return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
        }
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration)

Example 2 with IPackageDeclaration

use of org.eclipse.jdt.core.IPackageDeclaration 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 3 with IPackageDeclaration

use of org.eclipse.jdt.core.IPackageDeclaration in project bndtools by bndtools.

the class ComponentDecorator method decorate.

@Override
public void decorate(Object element, IDecoration decoration) {
    try {
        if (element instanceof CompilationUnit) {
            CompilationUnit unit = (CompilationUnit) element;
            if (!unit.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
                return;
            }
            if (!isComponentInImports(unit)) {
                return;
            }
            IPackageDeclaration[] decs = unit.getPackageDeclarations();
            if (decs != null && decs.length > 0) {
                IPackageDeclaration dec = decs[0];
                if (dec != null) {
                    boolean found = false;
                    String customText = null;
                    for (IMarker marker : unit.getResource().findMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE)) {
                        found = true;
                        customText = marker.getAttribute(IMarker.MESSAGE).toString();
                    }
                    if (found) {
                        decoration.addOverlay(componentIcon);
                        if (customText != null) {
                            if (customText.equals("OSGi Component")) {
                                decoration.addSuffix(" [Component]");
                            } else {
                                decoration.addSuffix(" [" + customText + "]");
                            }
                        }
                    }
                }
            }
        } else if (element instanceof SourceType) {
            SourceType type = (SourceType) element;
            if (!type.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
                return;
            }
            if (!isComponentInImports(type.getCompilationUnit())) {
                return;
            }
            boolean found = false;
            String customText = null;
            for (IMarker marker : type.getCompilationUnit().getResource().findMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE)) {
                found = true;
                customText = marker.getAttribute(IMarker.MESSAGE).toString();
            }
            if (found) {
                decoration.addOverlay(componentIcon);
                if (customText != null) {
                    if (customText.equals("OSGi Component")) {
                        decoration.addSuffix(" [Component]");
                    } else {
                        decoration.addSuffix(" [" + customText + "]");
                    }
                }
            }
        } else if (element instanceof IPackageFragment) {
            IPackageFragment frag = (IPackageFragment) element;
            if (!frag.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
                return;
            }
            IResource resource = (IResource) frag.getAdapter(IResource.class);
            if (resource != null && countComponents(resource)) {
                decoration.addOverlay(componentIcon);
            }
        }
    } catch (CoreException e) {
        logger.logError("Component Decorator error", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.internal.core.CompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CoreException(org.eclipse.core.runtime.CoreException) SourceType(org.eclipse.jdt.internal.core.SourceType) IMarker(org.eclipse.core.resources.IMarker) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) IResource(org.eclipse.core.resources.IResource)

Example 4 with IPackageDeclaration

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

the class ReorgCorrectionsSubProcessor method getWrongPackageDeclNameProposals.

public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    boolean isLinked = cu.getResource().isLinked();
    // correct package declaration
    // bug 38357
    int relevance = cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION;
    proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));
    // move to package
    IPackageDeclaration[] packDecls = cu.getPackageDeclarations();
    //$NON-NLS-1$
    String newPackName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
    IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(cu);
    IPackageFragment newPack = root.getPackageFragment(newPackName);
    ICompilationUnit newCU = newPack.getCompilationUnit(cu.getElementName());
    if (!newCU.exists() && !isLinked) {
        String label;
        if (newPack.isDefaultPackage()) {
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
        } else {
            String packageLabel = JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
        }
        CompositeChange composite = new CompositeChange(label);
        composite.add(new CreatePackageChange(newPack));
        composite.add(new MoveCompilationUnitChange(cu, newPack));
        proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CreatePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration)

Example 5 with IPackageDeclaration

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

the class CorrectPackageDeclarationProposal method addEdits.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
	 * .TextBuffer)
	 */
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    IPackageDeclaration[] decls = cu.getPackageDeclarations();
    if (parentPack.isDefaultPackage() && decls.length > 0) {
        for (int i = 0; i < decls.length; i++) {
            ISourceRange range = decls[i].getSourceRange();
            root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
        }
        return;
    }
    if (!parentPack.isDefaultPackage() && decls.length == 0) {
        String lineDelim = StubUtility.getLineDelimiterUsed(cu);
        //$NON-NLS-1$
        String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
        root.addChild(new InsertEdit(0, str));
        return;
    }
    root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 IPackageDeclaration (org.eclipse.jdt.core.IPackageDeclaration)6 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MemberValuePairLocationRetriever (org.bndtools.builder.utils.MemberValuePairLocationRetriever)1 Region (org.eclipse.che.ide.ext.java.shared.dto.Region)1 JavaProject (org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject)1 FindUsagesResponse (org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse)1 IMarker (org.eclipse.core.resources.IMarker)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 IAnnotation (org.eclipse.jdt.core.IAnnotation)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IMember (org.eclipse.jdt.core.IMember)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1