Example usage for org.eclipse.jdt.core ITypeRoot getPath

List of usage examples for org.eclipse.jdt.core ITypeRoot getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.CompilationUnit node) {
    CompilationUnit element = (CompilationUnit) this.binding.get(node);

    // if any type of this cu already exists in the model, we don't
    // visit this cu
    if (this.isAlreadyVisited) {
        return;//from  w ww. java2s. co m
    }

    this.jdtModel.getCompilationUnits().add(element);
    ITypeRoot rootType = node.getTypeRoot();

    if (rootType != null) {
        if (rootType instanceof IClassFile) {
            // type comes from a .class file
            ClassFile classFile = this.factory.createClassFile();
            classFile.setName(rootType.getElementName());
            classFile.setAttachedSource(element);
            classFile.setOriginalFilePath(this.currentFilePath);

            Archive ar = LibraryReader.getArchive((IClassFile) rootType, this.factory, this.jdtModel);
            if (ar == null) {
                this.jdtModel.getClassFiles().add(classFile);
            } else {
                ar.getClassFiles().add(classFile);
            }
        } else if (rootType instanceof ICompilationUnit) {
            // type comes a .java file
            IPath absolutePath = null;
            try {
                absolutePath = rootType.getCorrespondingResource().getLocation();
            } catch (JavaModelException e) {
                absolutePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation()
                        .append(rootType.getPath());
            }
            element.setOriginalFilePath(absolutePath.toOSString());
        } else {
            element.setOriginalFilePath(""); //$NON-NLS-1$
        }
        element.setName(rootType.getElementName());
    } else {
        element.setProxy(true);
    }

    Package packageDeclaration = (Package) this.binding.get(node.getPackage());
    element.setPackage(packageDeclaration);

    for (Object importNode : node.imports()) {
        ImportDeclaration importDeclaration = (ImportDeclaration) this.binding.get(importNode);
        element.getImports().add(importDeclaration);
    }

    for (Object typeNode : node.types()) {
        AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) this.binding.get(typeNode);
        element.getTypes().add(typeDeclaration);
    }

    try {
        // accessing BlockComment and LineComment
        // (https://bugs.eclipse.org/bugs/show_bug.cgi?format=multiple&id=84528)
        List<?> comments = node.getCommentList();
        for (Object name : comments) {
            org.eclipse.jdt.core.dom.Comment aComment = (org.eclipse.jdt.core.dom.Comment) name;

            Comment commentElement = null;
            if (aComment.isLineComment()) {
                commentElement = this.factory.createLineComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isBlockComment()) {
                commentElement = this.factory.createBlockComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isDocComment()) {
                // one javadoc node (and its tag elements) should have been
                // already visited

                commentElement = (Javadoc) this.binding.get(aComment);
                if (commentElement == null) { // happen if more than one javadoc
                    // for a node
                    commentElement = this.factory.createJavadoc();
                    initializeNode(commentElement, aComment);
                }
                commentElement.setContent(aComment.toString());
            }

            getCommentsBinding().put(aComment, commentElement);
            // initialisation of element CompilationUnit
            element.getCommentList().add(commentElement);
        }

        CommentsManager.resolveCommentPositions(this);
    } catch (StringIndexOutOfBoundsException e) {
        // IGNORE hub, sam, markus
    }
}

From source file:org.evosuite.eclipse.popup.actions.ExtendSuiteEditorAction.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);

    String SUT = "";
    IResource target = null;/*from  w  w w  .j  a  v  a2 s.  c om*/

    System.out.println("Current selection of type " + selection.getClass().getName() + ": " + selection);
    if (selection instanceof TreeSelection) {
        TreeSelection treeSelection = (TreeSelection) selection;
        IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

        // Relies on an internal API, bad juju
        if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
            try {
                org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
                String packageName = "";
                if (compilationUnit.getPackageDeclarations().length > 0) {
                    System.out.println(
                            "Package: " + compilationUnit.getPackageDeclarations()[0].getElementName());
                    packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = compilationUnit.getElementName().replace(".java", "");
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
                target = compilationUnit.getResource();
            } catch (JavaModelException e) {

            }
        }
    } else if (activeEditor instanceof JavaEditor) {
        ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeEditor, false);
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor).getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element;

        try {
            element = root.getElementAt(offset);
            if (element.getElementType() == IJavaElement.METHOD) {
                IJavaElement pDeclaration = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                IPackageFragment pFragment = (IPackageFragment) pDeclaration;
                String packageName = "";
                if (pFragment.getCompilationUnits()[0].getPackageDeclarations().length > 0) {
                    System.out.println("Package: "
                            + pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName());
                    packageName = pFragment.getCompilationUnits()[0].getPackageDeclarations()[0]
                            .getElementName();
                }
                String targetSuite = element.getParent().getElementName();
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
            } else if (element.getElementType() == IJavaElement.TYPE) {
                IType type = ((IType) element);
                System.out.println("Selected class: " + type.getFullyQualifiedName());
                SUT = type.getFullyQualifiedName();
            }

            IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
            target = wroot.findMember(root.getPath());
        } catch (JavaModelException e) {

        }
    }
    if (!SUT.isEmpty() && target != null) {
        IProject proj = target.getProject();
        fixJUnitClassPath(JavaCore.create(proj));
        generateTests(target);
    }

    return null;
}

From source file:org.jboss.tools.cdi.deltaspike.text.ext.AuthorizerHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    this.region = region;
    this.viewer = textViewer;

    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor))
        return null;

    int offset = region.getOffset();

    ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, false);
    if (input == null)
        return null;

    document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IProject project = null;//from   w  ww  .  j a v a  2s  .co m

    project = input.getJavaProject().getProject();

    if (project == null)
        return null;

    CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project);
    if (cdiNature == null)
        return null;

    IJavaElement[] elements = null;
    IType annotationType = null;

    try {
        elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength());
        if (elements == null)
            return null;
        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        int position = 0;
        if (elements[0] instanceof IType) {
            annotationType = (IType) elements[0];
            if (!annotationType.isAnnotation()) {
                annotationType = null;
            }
            elements[0] = input.getElementAt(wordRegion.getOffset());
            if (elements[0] == null)
                return null;

            if (elements[0] instanceof IMethod) {
                position = offset;
            }
        }

        findAuthorizerMethods(cdiNature, elements[0], annotationType, position, input.getPath(), hyperlinks);

        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        DeltaspikeCorePlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.jboss.tools.cdi.seam.text.ext.hyperlink.GenericInjectedPointHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    this.region = region;
    this.viewer = textViewer;

    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor))
        return null;

    int offset = region.getOffset();

    ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, false);
    if (input == null)
        return null;

    document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IProject project = null;//from w w w  .j a v a 2s  .  co  m

    project = input.getJavaProject().getProject();

    if (project == null)
        return null;

    CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project);
    if (cdiNature == null)
        return null;

    IJavaElement[] elements = null;

    try {
        elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength());
        if (elements == null)
            return null;
        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        int position = 0;
        if (elements[0] instanceof IType) {
            elements[0] = input.getElementAt(wordRegion.getOffset());
            if (elements[0] == null)
                return null;

            if (elements[0] instanceof IMethod) {
                position = offset;
            }
        }

        findInjectedBeans(cdiNature, elements[0], position, input.getPath(), hyperlinks);

        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        CDISeamExtPlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.jboss.tools.cdi.text.ext.hyperlink.EventAndObserverMethodHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !(textEditor instanceof JavaEditor))
        return null;

    if (textEditor.getEditorInput() instanceof IFileEditorInput) {
        file = ((IFileEditorInput) textEditor.getEditorInput()).getFile();
    }/*  w  w  w . ja  va 2s  .  c o  m*/

    int offset = region.getOffset();

    ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true);
    if (input == null)
        return null;

    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IProject project = null;

    project = input.getJavaProject().getProject();

    if (project == null)
        return null;

    CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project);
    if (cdiNature == null)
        return null;

    IJavaElement[] elements = null;

    try {
        elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength());

        if (elements == null)
            return null;

        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        int position = 0;

        if (elements[0] instanceof IType) {
            elements[0] = input.getElementAt(wordRegion.getOffset());

            if (elements[0] == null)
                return null;

            if (elements[0] instanceof IMethod) {
                position = offset;
            }
        }
        ICDIProject cdiProject = CDIUtil.getCDIProject(file, cdiNature, textEditor.isDirty());
        if (cdiProject != null) {
            IInjectionPoint injectionPoint = findInjectedPoint(cdiProject, elements[0], position,
                    input.getPath());
            Set<IParameter> param = findObserverParameter(cdiProject, elements[0], offset, input.getPath());
            if (injectionPoint != null) {
                Collection<IObserverMethod> observerMethods = cdiProject.resolveObserverMethods(injectionPoint);

                if (observerMethods.size() == 1) {
                    hyperlinks.add(
                            new ObserverMethodHyperlink(region, observerMethods.iterator().next(), document));
                } else if (observerMethods.size() > 0) {
                    hyperlinks.add(
                            new ObserverMethodListHyperlink(textViewer, region, observerMethods, document));
                }

            } else if (param != null) {
                Set<IInjectionPoint> events = new HashSet<IInjectionPoint>();
                for (IParameter p : param)
                    events.addAll(cdiProject.findObservedEvents(p));

                if (events.size() == 1) {
                    hyperlinks.add(new EventHyperlink(region, events.iterator().next(), document));
                } else if (events.size() > 0) {
                    hyperlinks.add(new EventListHyperlink(textViewer, region, events, document));
                }
            }
        }

        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        CDIExtensionsPlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.jboss.tools.cdi.text.ext.hyperlink.InjectedPointHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    this.region = region;
    this.viewer = textViewer;

    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !(textEditor instanceof JavaEditor))
        return null;

    if (textEditor.getEditorInput() instanceof IFileEditorInput) {
        file = ((IFileEditorInput) textEditor.getEditorInput()).getFile();
    }/*from   ww  w.j a v  a2  s  .c o m*/

    int offset = region.getOffset();

    ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true);
    if (input == null)
        return null;

    document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IProject project = null;

    project = input.getJavaProject().getProject();

    if (project == null)
        return null;

    CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project);
    if (cdiNature == null)
        return null;

    IJavaElement[] elements = null;

    try {
        elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength());
        if (elements == null)
            return null;

        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        int position = 0;
        if (elements[0] instanceof IType) {
            elements[0] = input.getElementAt(wordRegion.getOffset());

            if (elements[0] == null)
                return null;

            if (elements[0] instanceof IMethod) {
                position = offset;
            }
        }

        findInjectedBeans(cdiNature, elements[0], position, input.getPath(), hyperlinks, textEditor.isDirty());

        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        CDIExtensionsPlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.jboss.tools.cdi.text.ext.hyperlink.ProducerDisposerHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null || !(textEditor instanceof JavaEditor))
        return null;

    if (textEditor.getEditorInput() instanceof IFileEditorInput) {
        file = ((IFileEditorInput) textEditor.getEditorInput()).getFile();
    }/*from   w  ww .java2s  .co m*/

    int offset = region.getOffset();

    ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true);
    if (input == null)
        return null;

    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IProject project = null;

    project = input.getJavaProject().getProject();

    if (project == null)
        return null;

    Collection<IBean> beans = getBeans(project, input.getPath(), textEditor.isDirty());

    if (beans == null)
        return null;

    IJavaElement[] elements = null;

    try {
        elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength());

        if (elements == null)
            return null;
        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        if (elements[0] instanceof IType) {
            if (CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME
                    .equals(((IType) elements[0]).getFullyQualifiedName())) {
                elements[0] = input.getElementAt(wordRegion.getOffset());

                if (elements[0] == null)
                    return null;
            }
        }

        if (elements[0] instanceof IMethod) {
            for (IBean bean : beans) {
                if (bean instanceof IClassBean) {
                    IProducerMethod producer = getProducer((IClassBean) bean, (IMethod) elements[0]);
                    if (producer != null) {
                        List<IMethod> disposers = findDisposerMethods(producer);
                        for (IMethod method : disposers) {
                            hyperlinks.add(new DisposerHyperlink(region, method, document));
                        }
                    } else {
                        IBeanMethod disposer = getDisposer((IClassBean) bean, (IMethod) elements[0]);
                        if (disposer != null) {
                            List<IMethod> producers = findProducerMethods((IClassBean) bean, disposer);
                            for (IMethod method : producers) {
                                hyperlinks.add(new ProducerHyperlink(region, method, document));
                            }
                        }
                    }
                }
            }
        }

        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        CDIExtensionsPlugin.getDefault().logError(jme);
    }
    return null;
}