Example usage for org.eclipse.jdt.core IJavaElement getAncestor

List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getAncestor.

Prototype

IJavaElement getAncestor(int ancestorType);

Source Link

Document

Returns this Java element or the first ancestor of this element that has the given type.

Usage

From source file:net.sourceforge.metrics.calculators.RMartinCouplings.java

License:Open Source License

/**
 * Create a search scope consisting of all source packages in the element's project and all referencing projects. Exclude the element itself. This results in a DRAMATIC performance increase over SearchEngine.createWorkspaceScope().,...
 * /*www  .  jav  a  2 s  . com*/
 * @param element
 * @return IJavaSearchScope
 */
private IJavaSearchScope createProjectSearchScope(IJavaElement element) throws JavaModelException {
    IJavaProject p = (IJavaProject) element.getAncestor(IJavaElement.JAVA_PROJECT);
    List<IPackageFragment> scopeElements = new ArrayList<IPackageFragment>();
    addPackagesInScope(p, scopeElements);
    // find referencing projects and add their packages if any
    IProject[] refProjects = p.getProject().getReferencingProjects();
    if ((refProjects != null) && (refProjects.length > 0)) {
        for (IProject refProject : refProjects) {
            IJavaProject next = JavaCore.create(refProject);
            if (next != null) {
                addPackagesInScope(next, scopeElements);
            }
        }
    }
    // don't include the package under investigation!
    scopeElements.remove(element);
    return SearchEngine.createJavaSearchScope(scopeElements.toArray(new IJavaElement[] {}));
}

From source file:net.sourceforge.metrics.core.sources.AbstractMetricSource.java

License:Open Source License

/**
 * get the ICompilationUnit for the source
 * //from w w w  .j ava2 s . com
 * @return ICompilationUnit
 */
public ICompilationUnit getCompilationUnit() {
    IJavaElement input = getJavaElement();
    if (input.getElementType() == IJavaElement.COMPILATION_UNIT) {
        return (ICompilationUnit) input;
    } /* else { */
    return (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT);
    /* } */
}

From source file:net.sourceforge.metrics.core.sources.AbstractMetricSource.java

License:Open Source License

/**
 * get the original compilation unit for the given IJavaElement. If the compilation unit turns out to be a WorkingCopy this method returns its original source compilation unit
 * /*  w  w  w. ja  va 2 s  . c o  m*/
 * @param input
 * @return ICompilationUnit
 */
public static ICompilationUnit getOriginalCompilationUnit(IJavaElement input) {

    return (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT);
}

From source file:net.sourceforge.metrics.core.sources.Cache.java

License:Open Source License

/**
 * @param element/*from w  ww  . java2s.c om*/
 * @return
 */
private String getProjectName(IJavaElement element) {
    if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
        return element.getElementName();
    } /* else { */
    IJavaElement p = element.getAncestor(IJavaElement.JAVA_PROJECT);
    return p.getElementName();
    /* } */
}

From source file:net.sourceforge.metrics.core.sources.Dispatcher.java

License:Open Source License

/**
 * Get the AbstractMetricSource for the given IJavaElement from cache or create a new one and have it calculate the metrics.
 * //from  w ww .j  av  a 2  s. c o  m
 * @param input
 * @return AbstractMetricSource
 */
public static AbstractMetricSource calculateAbstractMetricSource(IJavaElement input) {
    AbstractMetricSource m = getAbstractMetricSource(input);
    if (m == null) {
        IJavaElement calculate = input;
        // calculate from COMPILATION_UNIT down if type or method
        if (input.getElementType() > IJavaElement.COMPILATION_UNIT) {
            calculate = input.getAncestor(IJavaElement.COMPILATION_UNIT);
        }
        m = singleton.createNewSource(calculate);
        m.setJavaElement(calculate);
        m.recurse(null);
        // should be in cache now
        m = Cache.singleton.get(input);
    }
    return m;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

/**
 * @param values/*from  ww  w .j  a va  2  s  .c o  m*/
 * @param id
 * @param pOut
 */
private void printValues(List<AbstractMetricSource> values, final String id, XMLPrintStream pOut,
        MetricDescriptor md, NumberFormat nf) {
    // sort values first
    Collections.sort(values, new Comparator<AbstractMetricSource>() {

        public int compare(AbstractMetricSource left, AbstractMetricSource right) {
            Metric lm = left.getValue(id);
            Metric rm = right.getValue(id);
            int result;
            if (lm == null) { // BUG #826997
                result = (rm == null) ? 0 : -1;
            } else {
                result = -lm.compareTo(rm); // BUG 746394
            }
            if (result != 0) {
                return result;
            }
            return left.getHandle().compareTo(right.getHandle());
        }

    });
    for (AbstractMetricSource next : values) {
        IJavaElement element = next.getJavaElement();
        Metric val = next.getValue(id);
        if (val != null) {
            pOut.indent(3);
            pOut.print("<Value name=\"");
            pOut.print(getName(element));
            pOut.print("\" ");
            IJavaElement source = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (source != null) {
                pOut.print("source =\"");
                pOut.print(getName(source));
                pOut.print("\" ");
            }
            IJavaElement packageF = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
            if (packageF != null) {
                pOut.print("package =\"");
                pOut.print(getName(packageF));
                pOut.print("\" ");
            }
            pOut.print("value =\"");
            pOut.print(nf.format(val.doubleValue()));
            pOut.print("\"");
            if (!md.isValueInRange(val.doubleValue())) {
                pOut.print(" inrange=\"false\"");
            }
            pOut.println("/>");
        }
    }
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String buildName(IJavaElement element) {
    String l_return = element.getElementName();
    if (element instanceof IType) {
        IJavaElement container = element.getParent();
        if (container != null && container.getAncestor(IJavaElement.TYPE) != null) {
            l_return = buildParentTypeNamePart(element);
        }/*from w  ww.jav  a 2  s . c  o  m*/
    } else if (element instanceof IMethod) {
        IJavaElement container = element.getAncestor(IJavaElement.TYPE);
        if (container != null && container.getParent() != null
                && container.getParent().getAncestor(IJavaElement.TYPE) != null) {
            l_return = buildParentTypeNamePart(container) + "#" + element.getElementName();
        }
    }
    return l_return;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String buildParentTypeNamePart(IJavaElement element) {
    StringBuffer l_strBuffer = new StringBuffer(getNotBlankName(element.getElementName(), element));
    IJavaElement l_current = element.getParent().getAncestor(IJavaElement.TYPE);
    while (l_current != null) {
        l_strBuffer.insert(0, '.');
        l_strBuffer.insert(0, getNotBlankName(l_current.getElementName(), l_current));
        l_current = l_current.getParent();
        if (l_current != null) {
            l_current = l_current.getAncestor(IJavaElement.TYPE);
        }// ww  w .j  a  va  2s . c o m
    }
    return l_strBuffer.toString();
}

From source file:net.sourceforge.metrics.ui.dependencies.EclipseNode.java

License:Open Source License

private void openInEditor() {
    Display d = Display.getDefault();//w  w w . j  a v  a  2 s.  co  m
    d.asyncExec(new Runnable() {

        public void run() {
            try {
                IJavaElement element = JavaCore.create(getID());
                ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                IEditorPart javaEditor = JavaUI.openInEditor(cu);
                JavaUI.revealInEditor(javaEditor, element);
            } catch (PartInitException e) {
                Log.logError("Node.openInEditor", e);
            } catch (JavaModelException e) {
                Log.logError("Node.openInEditor", e);
            }
        }
    });
}

From source file:org.codecover.eclipse.utils.EclipseMASTLinkage.java

License:Open Source License

/**
 * Find the corresponding CodeCover MAST HierarchyLevel to the given
 * Eclipse Java Element./*from  w  ww.j  a  va2s  .c  om*/
 * 
 * @param code
 *   root of code (MAST) to search
 * @param element
 *   search key
 * @return
 *   the HierarchyLevel of <i>element</i>, null if not found
 */
public static HierarchyLevel findSource(HierarchyLevel code, IJavaElement element) {
    HierarchyLevel result = null; //null until element is found

    /* check input */
    if (code == null) {
        throw new IllegalArgumentException("code is null"); //$NON-NLS-1$
    }
    if (element == null) {
        throw new IllegalArgumentException("element is null"); //$NON-NLS-1$
    }

    /* get corresponding ICompilationUnit */
    ICompilationUnit compilationUnit;
    if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
        compilationUnit = (ICompilationUnit) element;
    } else {
        compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
    }

    if (compilationUnit != null) {

        /* Extract fully qualified class name with its package */
        String fileName = compilationUnit.getElementName();
        String className = fileName.split("\\.")[0]; //$NON-NLS-1$
        IPackageFragment pkgF = (IPackageFragment) compilationUnit.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        if (pkgF != null) {
            String path[];
            if (pkgF.getElementName().equals("")) { //$NON-NLS-1$
                path = new String[] { className };
            } else {
                className = pkgF.getElementName() + "." + className; //$NON-NLS-1$
                path = className.split("\\."); //$NON-NLS-1$
            }

            /* find HierarchyLevel for the class by name */
            HierarchyLevel current = code;
            boolean found = true;

            for (int i = 0; i < path.length && found; ++i) {
                found = false;

                /* find next HierarchyLevel in path */
                for (HierarchyLevel l : current.getChildren()) {
                    if (l.getName().equals(path[i])) {
                        current = l;
                        found = true;
                        break;
                    }
                }
            }
            if (found) {

                /* the whole path was successfully traversed */
                result = current;
            }
        }
    }

    return result;
}