Example usage for org.eclipse.jdt.core IType newTypeHierarchy

List of usage examples for org.eclipse.jdt.core IType newTypeHierarchy

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType newTypeHierarchy.

Prototype

ITypeHierarchy newTypeHierarchy(WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Creates and returns a type hierarchy for this type containing this type, all of its supertypes, and all its subtypes in the workspace, considering types in the working copies with the given owner.

Usage

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.java

License:Open Source License

/**
 * Returns all activities found in the given project (including those in libraries,
 * except for android.jar itself)//from ww w.  j av a2 s .c  o  m
 *
 * @param project the project
 * @return a list of activity classes as fully qualified class names
 */
@SuppressWarnings("restriction") // BinaryType
@NonNull
public static List<String> getProjectActivities(IProject project) {
    final List<String> activities = new ArrayList<String>();
    try {
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType[] activityTypes = new IType[0];
            IType activityType = javaProject.findType(CLASS_ACTIVITY);
            if (activityType != null) {
                ITypeHierarchy hierarchy = activityType.newTypeHierarchy(javaProject,
                        new NullProgressMonitor());
                activityTypes = hierarchy.getAllSubtypes(activityType);
                for (IType type : activityTypes) {
                    if (type instanceof BinaryType
                            && (type.getClassFile() == null || type.getClassFile().getResource() == null)) {
                        continue;
                    }
                    activities.add(type.getFullyQualifiedName());
                }
            }
        }
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }

    return activities;
}

From source file:com.android.ide.eclipse.auidt.internal.editors.manifest.ManifestInfo.java

License:Open Source License

/**
 * Returns all activities found in the given project (including those in libraries,
 * except for android.jar itself)/*  www.j  a  va 2 s. co  m*/
 *
 * @param project the project
 * @return a list of activity classes as fully qualified class names
 */
@SuppressWarnings("restriction") // BinaryType
@NonNull
public static List<String> getProjectActivities(IProject project) {
    final List<String> activities = new ArrayList<String>();
    try {
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType[] activityTypes = new IType[0];
            IType activityType = javaProject.findType(SdkConstants.CLASS_ACTIVITY);
            if (activityType != null) {
                ITypeHierarchy hierarchy = activityType.newTypeHierarchy(javaProject,
                        new NullProgressMonitor());
                activityTypes = hierarchy.getAllSubtypes(activityType);
                for (IType type : activityTypes) {
                    if (type instanceof BinaryType
                            && (type.getClassFile() == null || type.getClassFile().getResource() == null)) {
                        continue;
                    }
                    activities.add(type.getFullyQualifiedName());
                }
            }
        }
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }

    return activities;
}

From source file:com.google.gdt.eclipse.designer.util.type.YesNoFilter.java

License:Open Source License

/**
 * Expects {"pkg1","class1", "pkg2", "class2"}
 *///from www. j  a v a 2  s.co m
private List<String> getSubTypes(String[] types) {
    if (types == null) {
        return Collections.emptyList();
    }
    //
    IJavaProject javaProject = m_package.getJavaProject();
    List<String> subTypesList = new ArrayList<String>();
    for (int baseIndex = 0; baseIndex < types.length; baseIndex += 2) {
        try {
            IType baseType = javaProject.findType(types[baseIndex], types[baseIndex + 1]);
            if (baseType != null) {
                subTypesList.add(baseType.getFullyQualifiedName());
                // prepare sub-types
                IType[] subTypes;
                {
                    m_monitor.subTask(baseType.getFullyQualifiedName());
                    ITypeHierarchy th = baseType.newTypeHierarchy(javaProject, new NullProgressMonitor());
                    subTypes = th.getAllSubtypes(baseType);
                    m_monitor.worked(1);
                }
                //
                for (int subTypeIndex = 0; subTypeIndex < subTypes.length; subTypeIndex++) {
                    subTypesList.add(subTypes[subTypeIndex].getFullyQualifiedName());
                }
            }
        } catch (JavaModelException e) {
            DesignerPlugin.log(e);
        }
    }
    return subTypesList;
}

From source file:com.liferay.ide.core.util.PropertiesUtil.java

License:Open Source License

public static List<IFile> getDefaultLanguagePropertiesFromModuleProject(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);

    IType portletType = null;

    List<IFile> retvals = new ArrayList<IFile>();

    try {//from w ww.  ja  va  2 s .c  om
        portletType = javaProject.findType("javax.portlet.Portlet");

        final ITypeHierarchy typeHierarchy = portletType.newTypeHierarchy(javaProject,
                new NullProgressMonitor());

        final IPackageFragmentRoot[] packageRoots = javaProject.getPackageFragmentRoots();

        List<String> packages = new ArrayList<String>();

        List<IType> srcJavaTypes = new ArrayList<IType>();

        for (IPackageFragmentRoot packageRoot : packageRoots) {
            if (packageRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                IJavaElement[] javaElements = packageRoot.getChildren();

                for (IJavaElement javaElement : javaElements) {
                    IPackageFragment packageFragment = (IPackageFragment) javaElement;

                    packages.add(packageFragment.getElementName());
                }
            }
        }

        IType[] subTypes = typeHierarchy.getAllSubtypes(portletType);

        for (IType type : subTypes) {
            if (isInPackage(packages, type.getFullyQualifiedName())) {
                srcJavaTypes.add(type);
            }
        }

        String resourceBundleValue = null;

        for (IType type : srcJavaTypes) {
            File file = type.getResource().getLocation().toFile();
            String content = FileUtil.readContents(file);

            String key = "javax.portlet.resource-bundle=";

            int i = content.indexOf(key);

            if (i == -1) {
                continue;
            } else {
                i += key.length();

                StringBuilder strBuilder = new StringBuilder();

                for (; i < content.length(); i++) {
                    char ch = content.charAt(i);
                    if (ch != '"') {
                        strBuilder.append(ch);
                    } else {
                        break;
                    }
                }

                resourceBundleValue = strBuilder.toString();
                // find the first language config
                break;
            }
        }

        String resourceBundle = resourceBundleValue.replaceAll("(^\\s*)|(\\s*$)", StringPool.BLANK);

        if (!resourceBundle.endsWith(PROPERTIES_FILE_SUFFIX) && !resourceBundle.contains(IPath.SEPARATOR + "")
                && !(CoreUtil.isWindows() && resourceBundle.contains("\\"))) {
            resourceBundle = new Path(resourceBundle.replace(".", IPath.SEPARATOR + "")).toString();
        }

        final ILiferayProject lrproject = LiferayCore.create(project);
        final IFolder[] srcFolders = lrproject.getSourceFolders();

        for (IFolder srcFolder : srcFolders) {
            final IFile languageFile = CoreUtil.getWorkspaceRoot()
                    .getFile(srcFolder.getFullPath().append(resourceBundle + PROPERTIES_FILE_SUFFIX));

            if ((languageFile != null) && languageFile.exists()) {
                retvals.add(languageFile);
            }
        }
    } catch (Exception e) {
    }

    return retvals;
}

From source file:com.windowtester.eclipse.ui.jdt.analysis.util.TypeHierarchyCache.java

License:Open Source License

/**
 * Return a type hierarchy containing the given type, all supertypes, and
 * all subtypes of the given type.//  w ww. ja  v  a  2  s  .co m
 *
 * @param type the type around which the type hierarchy is centered
 * @param project the project used to identify which classes should be
 *        included in the hierarchy
 *
 * @return a type hierarchy for the given type
 */
public ITypeHierarchy getTypeHierarchy(IType type, IJavaProject project) {
    ITypeHierarchy typeHierarchy;
    IType[] subtypes;

    typeHierarchy = getHierarchy(type);
    if (typeHierarchy == null) {
        try {
            //long startTime = System.currentTimeMillis();
            //System.out.println("   " + startTime + " start creating type hierarchy for " + type.getElementName());
            typeHierarchy = type.newTypeHierarchy(project, null);
            //long endTime = System.currentTimeMillis();
            //System.out.println("   " + endTime + " finished (" + (endTime - startTime) + "ms)");
            addHierarchy(type, typeHierarchy);
            subtypes = typeHierarchy.getAllSubtypes(type);
            for (int i = 0; i < subtypes.length; i++) {
                cache.put(subtypes[i], typeHierarchy);
            }
        } catch (JavaModelException exception) {
            Logger.log("Could not create type hierarchy for the type " + type.getElementName(), exception);
        }
    }
    return typeHierarchy;
}

From source file:edu.clarkson.serl.critic.CriticPlugin.java

License:Open Source License

/**
 * Constructs a new type hierarchy rooted at the supplied type in the context of the project being analyzed.
 * /*from w  w w. j a va2  s  . c o m*/
 * @param name The fully qualified name of a type.
 * @return {@link ITypeHierarchy} representing the type hierarchy rooted at the supplied type.
 */
public static ITypeHierarchy getTypeHierarchy(String name) {
    try {
        IType type = javaProject.findType(name);
        return type.newTypeHierarchy(javaProject, null);
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.clarkson.serl.critic.interpreter.model.ExprInstanceOf.java

License:Open Source License

public ISymbol<? extends Value> execute() {
    Value op = this.getSootValue().getOp();
    if (!(op instanceof Local)) // Sanity check
        throw new UnsupportedOperationException(
                "The operand of instanceof expression must be a local variable");

    ISymbol<? extends Value> operand = Interpreter.instance().peek().lookup(op);
    Type opType = operand.getType();
    Type checkedType = this.getSootValue().getCheckType();

    // Note: Incomplete implementation using Soot's fast hierarchy for future consideration.
    // The FastHierarchy API does not provide required functionalities that ITypeHierarchy provides.      
    //      if(operand.isKnown()) {
    //         FastHierarchy fastHierarchy = Scene.v().getOrMakeFastHierarchy();
    //         if(fastHierarchy.canStoreType(opType, checkedType)) {
    //            this.result = Interpreter.TRUE;
    //            return Interpreter.TRUE;
    //         }/*from  w w w  .  ja va2  s. c  om*/
    //         this.result = Interpreter.FALSE;
    //         return Interpreter.FALSE;
    //      }

    IJavaProject project;
    IType opIType;
    IType checkedIType;
    ITypeHierarchy opHierarchy;
    try {
        project = CriticPlugin.getIJavaProject();
        opIType = project.findType(opType.toString());
        checkedIType = project.findType(checkedType.toString());
        opHierarchy = opIType.newSupertypeHierarchy(null);
        if (opHierarchy.contains(checkedIType)) { // For both open and closed this holds
            this.result = Interpreter.TRUE;
            return Interpreter.TRUE;
        } else if (!this.isOpen()) { // For only closed object this holds
            this.result = Interpreter.FALSE;
            return Interpreter.FALSE;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // At this point we are dealing with open object only and we know that 
    // checkedIType is not a supertype of opIType. So, instead it may hold 
    // that the checkedIType is a subtype of the supplied opType.
    try {
        ITypeHierarchy checkedHierarchy = checkedIType.newSupertypeHierarchy(null);

        if (!checkedHierarchy.contains(opIType)) {
            // At this point neither checkedIType is a parent of opIType, nor opIType is a parent of checkedIType.  
            // This mean that opType and checkedType are in two different sub-hierarchies from a common parent
            this.result = Interpreter.FALSE;
            return Interpreter.FALSE;
        }

        // At this point opIType is a subtype of checkedIType. This may be true because the real type of op is open.
        // Lets check whether any type related information is available.

        // First the inclusive types
        ClosedSet<? extends Value> types = (ClosedSet<? extends Value>) operand.get(SymbolicKey.TYPES_IN);
        if (types != null && !types.isEmpty()) {
            for (ISymbol<? extends Value> t : types) {
                IType tIType = project.findType(t.getType().toString());
                ITypeHierarchy tHierarchy = tIType.newSupertypeHierarchy(null);
                if (tHierarchy.contains(checkedIType)) { // The checked type is a super type of one of the inclusive type
                    this.result = Interpreter.TRUE;
                    return Interpreter.TRUE;
                }
            }
        }

        // Inclusive types could not resolve the instanceof test
        // Now lets try exclusive types
        types = (ClosedSet<? extends Value>) operand.get(SymbolicKey.TYPES_OUT);
        if (types != null && !types.isEmpty()) {
            for (ISymbol<? extends Value> t : types) {
                IType tIType = project.findType(t.getType().toString());
                if (checkedIType.equals(tIType)) { // instaceof test of the excluded type
                    this.result = Interpreter.FALSE;
                    return Interpreter.FALSE;
                }
                ITypeHierarchy tHierarchy = tIType.newTypeHierarchy(project, null);
                IType[] subtypes = tHierarchy.getAllSubtypes(tIType);
                for (IType sub : subtypes) {
                    if (checkedIType.equals(sub)) { // instaceof test of a subtype of the excluded type
                        this.result = Interpreter.FALSE;
                        return Interpreter.FALSE;
                    }
                }

            }
        }

        // Neither inclusive nor exclusive set resolved the instanceof test.
        // Hence, result is set to be open.
        this.result = this;
        return this;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.illinois.compositerefactorings.steps.CopyMemberToSubtype.java

License:Open Source License

/**
 * The computation of {@code flags} is based on
 * {@link CopyMemberToSubtypeRefactoringProcessor#createChange(IProgressMonitor)}
 *///from  ww  w .j a  v  a2 s  . c  o  m
@Override
protected Collection<RefactoringDescriptor> getDescriptors(Object input) throws CoreException {
    IMember member = (IMember) input;
    IType supertype = member.getDeclaringType();
    IType[] subtypes = supertype.newTypeHierarchy(getJavaProject(), new NullProgressMonitor())
            .getSubtypes(supertype);

    Collection<RefactoringDescriptor> descriptors = new ArrayList<RefactoringDescriptor>();
    for (int i = 0; i < MAXIMUM_NUMBER_OF_PROPOSALS && i < subtypes.length; ++i) {
        descriptors.add(createDescriptor(supertype, member, subtypes[i]));
    }
    return descriptors;
}

From source file:edu.illinois.compositerefactorings.steps.MoveToImmediateSuperclass.java

License:Open Source License

@Override
protected Collection<RefactoringDescriptor> getDescriptors(Object input) throws CoreException {
    Collection<RefactoringDescriptor> descriptors = new ArrayList<RefactoringDescriptor>();
    IMember member = (IMember) input;/*from   ww w  . j  av a 2 s.c o  m*/
    IType declaringType = member.getDeclaringType();
    IType immediateSuperclass = declaringType.newTypeHierarchy(getJavaProject(), new NullProgressMonitor())
            .getSuperclass(declaringType);

    Map<String, String> arguments = new HashMap<String, String>();
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil
            .elementToHandle(getJavaProject().getElementName(), immediateSuperclass));
    arguments.put("stubs", String.valueOf(false));
    arguments.put("instanceof", String.valueOf(false));
    arguments.put("replace", String.valueOf(false));
    arguments.put("abstract", String.valueOf(0));
    int numberOfMethodsToRemoveFromSubclasses = 0;
    if (member instanceof IField) {
        numberOfMethodsToRemoveFromSubclasses = 0;
    } else if (member instanceof IMethod) {
        numberOfMethodsToRemoveFromSubclasses = 1;
    }
    arguments.put("delete", Integer.toString(numberOfMethodsToRemoveFromSubclasses));
    arguments.put("pull", String.valueOf(1));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1,
            JavaRefactoringDescriptorUtil.elementToHandle(getJavaProject().getElementName(), member));
    if (numberOfMethodsToRemoveFromSubclasses > 0) {
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 2,
                JavaRefactoringDescriptorUtil.elementToHandle(getJavaProject().getElementName(), member));
    }
    String description = MessageFormat.format(
            CompositeRefactoringsMessages.MoveToImmediateSuperclass_description, member.getElementName(),
            immediateSuperclass.getElementName());
    PullUpDescriptor descriptor = new PullUpDescriptor(getJavaProject().getElementName(), description, null,
            arguments, RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE);
    descriptors.add(descriptor);
    return descriptors;
}

From source file:edu.illinois.compositerefactorings.steps.UseSuperTypeWherePossible.java

License:Open Source License

protected static List<IType> getClosestSupertypes(IJavaProject project, IType type) throws CoreException {
    final int MAX_NUMBER_OF_SUPERTYPES = 2;
    List<IType> closestsSuperTypes = new LinkedList<IType>();
    LinkedList<IType> remainingTypes = new LinkedList<IType>();
    remainingTypes.add(type);/*w  w w  .  jav a2 s.co  m*/
    ITypeHierarchy typeHierarchy = type.newTypeHierarchy(project, new NullProgressMonitor());
    while (!remainingTypes.isEmpty() && closestsSuperTypes.size() < MAX_NUMBER_OF_SUPERTYPES) {
        IType currentType = remainingTypes.removeFirst();
        if (currentType != type) {
            closestsSuperTypes.add(currentType);
        }
        IType currentSupertype = typeHierarchy.getSuperclass(currentType);
        if (currentSupertype != null) {
            remainingTypes.addLast(currentSupertype);
        }
        remainingTypes.addAll(Arrays.asList(typeHierarchy.getSuperInterfaces(currentType)));
    }
    return closestsSuperTypes;
}