Example usage for org.eclipse.jdt.core ITypeHierarchy getSubtypes

List of usage examples for org.eclipse.jdt.core ITypeHierarchy getSubtypes

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeHierarchy getSubtypes.

Prototype

IType[] getSubtypes(IType type);

Source Link

Document

Returns the direct resolved subtypes of the given type, in no particular order, limited to the types in this type hierarchy's graph.

Usage

From source file:ca.mt.wb.devtools.tx.InheritanceView.java

License:unlicense.org

protected void addSubTypes(final IType type) {
    Job job = new Job("Add Subtypes") {
        @Override//  w w w  .j a va  2 s . co m
        protected IStatus run(IProgressMonitor monitor) {
            monitor.beginTask("Adding subtypes of " + type.getElementName(), 100);
            try {
                monitor.subTask("Computing type hierarchy");
                ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(monitor, 90));
                monitor.subTask("Adding elements");
                final Collection<IType> subtypes = Arrays.asList(hierarchy.getSubtypes(type));
                monitor.worked(10);
                getDisplay().asyncExec(new Runnable() {
                    public void run() {
                        addTypes(subtypes);
                    }
                });
            } catch (JavaModelException e) {
                return new Status(IStatus.ERROR, "ca.mt.wb.devtools.tx",
                        "Error occurred computing type hierarchy for " + type.getElementName(), e);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}

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

License:Open Source License

/**
 * @see net.sourceforge.metrics.calculators.Calculator#calculate(net.sourceforge.metrics.core.sources.AbstractMetricSource)
 *//*from   ww w  .jav a 2s. c  o m*/
@Override
public void calculate(AbstractMetricSource source) throws InvalidSourceException {
    if (source.getLevel() != TYPE) {
        throw new InvalidSourceException("InheritanceDepth only applicable to types");
    }
    TypeMetrics tm = (TypeMetrics) source;
    IType iType = (IType) source.getJavaElement();
    ITypeHierarchy hierarchy = tm.getHierarchy();
    IType[] supers = hierarchy.getAllSuperclasses(iType);
    IType[] subs = hierarchy.getSubtypes(iType); // BUG #933209
    source.setValue(new Metric(INHERITANCE_DEPTH, supers.length));
    source.setValue(new Metric(SUBCLASSES, subs.length));
}

From source file:net.sourceforge.metrics.calculators.qmood.NumberOfHierarchies.java

License:Open Source License

/**
 * @see net.sourceforge.metrics.calculators.Calculator#calculate(net.sourceforge.metrics.core.sources.AbstractMetricSource)
 *///w ww.j  ava 2 s  .  co m
@Override
public void calculate(AbstractMetricSource source) throws InvalidSourceException {
    if (source.getLevel() != TYPE) {
        throw new InvalidSourceException("NumberOfHierarchies only applicable to types");
    }
    TypeMetrics tm = (TypeMetrics) source;
    IType iType = (IType) source.getJavaElement();
    ITypeHierarchy hierarchy = tm.getHierarchy();
    IType[] supers = hierarchy.getAllSuperclasses(iType);
    int numSourceSupers = 0;
    for (IType type : supers) {
        if (!type.isBinary())
            numSourceSupers++;
    }
    int numSubSources = 0;
    IType[] subs = hierarchy.getSubtypes(iType); // BUG #933209
    for (IType type : subs) {
        if (!type.isBinary())
            numSubSources++;
    }
    //If the type has no source supers and any subclasses, it's considered a root.
    if (numSourceSupers == 0 && numSubSources > 0)
        source.setValue(new Metric(NOH, 1));
    else
        source.setValue(new Metric(NOH, 0));
}

From source file:org.eclipse.flux.jdt.services.TypeProposalUtils.java

License:Open Source License

static IType[] computeInheritancePath(IType subType, IType superType) throws JavaModelException {
    if (superType == null)
        return null;

    // optimization: avoid building the type hierarchy for the identity case
    if (superType.equals(subType))
        return new IType[] { subType };

    ITypeHierarchy hierarchy = subType.newSupertypeHierarchy(new NullProgressMonitor());
    if (!hierarchy.contains(superType))
        return null; // no path

    List<IType> path = new LinkedList<IType>();
    path.add(superType);//from   ww w .  j  a  v  a2 s .com
    do {
        // any sub type must be on a hierarchy chain from superType to subType
        superType = hierarchy.getSubtypes(superType)[0];
        path.add(superType);
    } while (!superType.equals(subType)); // since the equality case is handled above, we can spare one check

    return path.toArray(new IType[path.size()]);
}

From source file:org.eclipse.jst.ws.internal.axis.consumption.core.locator.AxisWebServiceLocator.java

License:Open Source License

/**
 * Tries to find Axis "Proxy" classes related the given Stub.
 * @param axisStub The Axis client "Stub" from which to search.
 * @param list A list to which we'll add and WebServiceClientInfo
 * objects for "Proxy" classes we find in the search. 
 *//*from   w  ww.  j a va2 s .c  om*/
private void addAxisProxies(IJavaProject javaProject, IType axisStub, List list, IProgressMonitor monitor) {
    try {
        // Compute a hierarchy to help us find all the
        // generated Stub subclasses of the Axis "Stub"
        // class across the entire workspace:
        ITypeHierarchy axisStubHierarchy = axisStub.newTypeHierarchy(javaProject, monitor);
        IType[] stubs = axisStubHierarchy.getSubtypes(axisStub);
        for (int s = 0; s < stubs.length; s++) {
            // For each stub, find all its super-interfaces of which
            // there should be one, namely the generated SEI:
            IType seis[] = axisStubHierarchy.getSuperInterfaces(stubs[s]);
            for (int i = 0; i < seis.length; i++) {
                try {
                    // Compute a hierarchy to help us find all the
                    // generated classes that implement the SEI
                    // confined to the current project:
                    ITypeHierarchy seiHierarchy = seis[i].newTypeHierarchy(javaProject, monitor);
                    IType[] proxies = seiHierarchy.getSubtypes(seis[i]);
                    for (int p = 0; p < proxies.length; p++) {
                        // Under normal circumstances we should find two subclasses:
                        // 1. The generated stub that got us here (two loops above).
                        // 2. The generated "Proxy" that we're looking for.
                        // Skip #1 and capture #2:
                        if (!proxies[p].getFullyQualifiedName().equals(stubs[s].getFullyQualifiedName())) {
                            WebServiceClientInfo wscInfo = newWebServiceClientInfo(proxies[p]);
                            if (wscInfo != null) {
                                list.add(wscInfo);
                            }
                        }
                    }
                } catch (Exception e) {
                    // We'll land here if the JDT was unable to compute
                    // a type hierarchy for the current SEI.
                    // This should never happen, but if it does,
                    // there's not much we can do except bypass the SEI
                    // and loop on to the next one.
                }
            }
        }
    } catch (Exception e) {
        // We'll land here if JDT was unable to compute
        // a type hierarchy for the Axis client "Stub" class.
        // This should never happen, but if it does,
        // there's not much we can do except bail out, having
        // added nothing to the list.
    }
}

From source file:org.eclipse.xtend.ide.refactoring.DispatchRenameSupport.java

License:Open Source License

protected Iterable<JvmGenericType> getSubTypes(JvmGenericType type, ResourceSet tempResourceSet) {
    IType javaType = (IType) javaElementFinder.findExactElementFor(type);
    List<JvmGenericType> allSubTypes = newArrayList();
    try {/*from   w  ww .ja v a2s . c  o  m*/
        ITypeHierarchy typeHierarchy = javaType.newTypeHierarchy(javaType.getJavaProject(),
                new NullProgressMonitor());
        for (IType subType : typeHierarchy.getSubtypes(javaType)) {
            EObject jvmSubType = jvmElementFinder.getCorrespondingJvmElement(subType, tempResourceSet);
            if (jvmSubType instanceof JvmGenericType) {
                EObject indexJvmSubType = jvmElementFinder.findJvmElementDeclarationInIndex(jvmSubType,
                        subType.getJavaProject().getProject(), type.eResource().getResourceSet());
                if (indexJvmSubType instanceof JvmGenericType) {
                    allSubTypes.add((JvmGenericType) indexJvmSubType);
                } else {
                    EObject jvmSubTypeInOtherResourceSet = type.eResource().getResourceSet()
                            .getEObject(EcoreUtil2.getPlatformResourceOrNormalizedURI(jvmSubType), true);
                    if (jvmSubTypeInOtherResourceSet instanceof JvmGenericType)
                        allSubTypes.add((JvmGenericType) jvmSubTypeInOtherResourceSet);
                }
            }
        }
    } catch (JavaModelException e) {
        LOG.error("Error calculating subtypes", e);
    }
    return allSubTypes;

}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtilsTestCase.java

License:Open Source License

@Test
public void shouldResolveTypeHierarchyOnLibrariesWithSubclasses() throws CoreException {
    // preconditions
    IType type = projectMonitor.resolveType("javax.ws.rs.core.Application");
    Assert.assertNotNull("SourceType not found", type);
    // operation//from   w w  w.  ja v a 2  s. c  o m
    final ITypeHierarchy hierarchy = JdtUtils.resolveTypeHierarchy(type, type.getJavaProject(), true,
            new NullProgressMonitor());
    // verifications
    Assert.assertNotNull("SourceType hierarchy not found", hierarchy);
    Assert.assertEquals("SourceType hierarchy incomplete", 1, hierarchy.getSubtypes(type).length);
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtilsTestCase.java

License:Open Source License

@Test
public void shouldResolveTypeHierarchyOnLibrariesWithNoSubclass() throws CoreException {
    // preconditions
    IType type = projectMonitor.resolveType("javax.ws.rs.core.Application");
    Assert.assertNotNull("SourceType not found", type);
    final IPackageFragmentRoot lib = projectMonitor.resolvePackageFragmentRoot("lib/jaxrs-api-2.0.1.GA.jar");
    Assert.assertNotNull("Lib not found", lib);
    // operation/*from ww w. j a  v  a 2s  . c o  m*/
    final ITypeHierarchy hierarchy = JdtUtils.resolveTypeHierarchy(type, lib, true, new NullProgressMonitor());
    // verifications
    Assert.assertNotNull("SourceType hierarchy not found", hierarchy);
    Assert.assertEquals("SourceType hierarchy incomplete", 0, hierarchy.getSubtypes(type).length);
}