Example usage for org.eclipse.jdt.core IRegion add

List of usage examples for org.eclipse.jdt.core IRegion add

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IRegion add.

Prototype

void add(IJavaElement element);

Source Link

Document

Adds the given element and all of its descendents to this region.

Usage

From source file:com.ebmwebsourcing.petals.common.internal.provisional.ui.JarExportWizardPage.java

License:Open Source License

/**
 * Return the class file resources associated with the given elements.
 * <p>It is assumed the java projects have already been compiled and the resources are all in a saved state.</p>
 *
 * @param elements//from   w  w  w  .j  a v a  2s.  com
 * @return
 */
public static Collection<IFile> getCorrespondingSources(Collection<IJavaElement> elements) {

    IRegion region = JavaCore.newRegion();
    for (IJavaElement element : elements)
        region.add(element);

    IResource[] resources = JavaCore.getGeneratedResources(region, false);
    Set<IFile> classes = new HashSet<IFile>(resources.length);
    for (IResource resource : resources) {
        if (resource instanceof IFile)
            classes.add((IFile) resource);
    }

    return classes;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void handleFindHierarchy(String proj, String pkg, String cls, boolean all, IvyXmlWriter xw)
        throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);
    IRegion rgn = JavaCore.newRegion();
    IType fortype = null;//from ww  w.j a va2s  . com

    boolean havejp = (ijp != null);

    if (ijp == null && (pkg != null || cls != null)) {
        IJavaElement[] aps = getAllProjects();
        if (aps.length == 0)
            return;
        if (cls != null) {
            for (IJavaElement ije : aps) {
                IJavaProject xjp = ije.getJavaProject();
                try {
                    if (xjp.findType(cls) != null) {
                        ijp = xjp;
                        break;
                    }
                } catch (JavaModelException e) {
                }
            }
        }
        if (ijp == null)
            ijp = aps[0].getJavaProject();
    }

    int addct = 0;

    if (cls != null && ijp != null) {
        try {
            IType typ = ijp.findType(cls);
            fortype = typ;
            // rgn.add(typ);
            // ++addct;
        } catch (JavaModelException e) {
            BedrockPlugin.logE("Problem getting type by name: " + e);
        }
    }

    if (pkg != null && ijp != null) {
        String ppth = "/" + pkg.replace(".", "/");
        try {
            for (IPackageFragmentRoot ipr : ijp.getPackageFragmentRoots()) {
                IPath rpath = ipr.getPath();
                Path npath = new Path(rpath.toString() + ppth);
                IPackageFragment ipf = ijp.findPackageFragment(npath);
                if (ipf != null) {
                    rgn.add(ipf);
                    ++addct;
                }
            }
        } catch (Exception e) {
            BedrockPlugin.logE("Problem getting package fragments for " + ppth + ": " + e);
        }
    } else if (havejp && ijp != null) {
        if (all) {
            rgn.add(ijp);
            ++addct;
        } else {
            try {
                for (IPackageFragment ipf : ijp.getPackageFragments()) {
                    for (ICompilationUnit icu : ipf.getCompilationUnits()) {
                        IType ity = ((ITypeRoot) icu).findPrimaryType();
                        if (ity != null) {
                            rgn.add(ity);
                            ++addct;
                        }
                    }
                }
            } catch (Throwable e) {
                BedrockPlugin.logE("Problem getting package fragments: " + e);
            }
        }
    } else {
        for (IJavaElement pi : getAllProjects()) {
            IJavaProject xjp = pi.getJavaProject();
            if (xjp != null && !rgn.contains(xjp)) {
                rgn.add(xjp);
                ++addct;
            }
            // String pnm = pi.getJavaProject().getProject().getName();
            // handleFindHierarchy(pnm,null,null,all,xw);
        }
    }

    if (addct > 0 && ijp != null) {
        try {
            BedrockPlugin.logD("FIND TYPE HIERARCHY FOR " + fortype + " " + addct + " " + rgn);

            ITypeHierarchy ith;
            if (fortype != null)
                ith = ijp.newTypeHierarchy(fortype, rgn, null);
            else
                ith = ijp.newTypeHierarchy(rgn, null);
            BedrockUtil.outputTypeHierarchy(ith, xw);
        } catch (JavaModelException e) {
            BedrockPlugin.logE("Problem outputing type hierarchy: " + e);
        } catch (NullPointerException e) {
            // this is a bug in Eclipse that should be fixed
        }
    }
}

From source file:org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore.java

License:Open Source License

protected IRegion regionWithBDM(final IJavaProject javaProject) throws JavaModelException {
    final IRegion newRegion = JavaCore.newRegion();
    final IClasspathEntry repositoryDependenciesClasspathEntry = find(asIterable(javaProject.getRawClasspath()),
            repositoryDependenciesEntry(), null);
    final IPackageFragmentRoot[] fragmentRoots = javaProject
            .findPackageFragmentRoots(repositoryDependenciesClasspathEntry);
    final IPackageFragmentRoot packageFragmentRoot = find(asIterable(fragmentRoots),
            withElementName(BDM_CLIENT_POJO_JAR_NAME), null);
    if (packageFragmentRoot != null) {
        newRegion.add(packageFragmentRoot);
    }/*from w  w  w  .j  a v a 2 s.  c o  m*/
    return newRegion;
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.RippleMethodFinder2.java

License:Open Source License

private void createHierarchyOfDeclarations(IProgressMonitor pm, WorkingCopyOwner owner)
        throws JavaModelException {
    IRegion region = JavaCore.newRegion();
    for (Iterator iter = fDeclarations.iterator(); iter.hasNext();) {
        // AspectJ Change
        // original
        //          IType declaringType= ((IMethod) iter.next()).getDeclaringType();
        // new/*from   ww  w  . ja  v a2  s .c om*/
        IType declaringType = getType((IMethod) iter.next());
        // AspectJ end
        region.add(declaringType);
    }
    fHierarchy = JavaCore.newTypeHierarchy(region, owner, pm);
}

From source file:org.eclipse.birt.report.designer.internal.ui.ide.util.ClassFinder.java

License:Open Source License

private IRegion getRegion(IJavaElement element) throws JavaModelException {
    IRegion result = JavaCore.newRegion();
    if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
        // for projects only add the contained source folders
        IPackageFragmentRoot[] roots = ((IJavaProject) element).getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (!roots[i].isArchive()) {
                result.add(roots[i]);
            }/*from   w ww. ja  v  a 2 s  . c  om*/
        }
    } else {
        result.add(element);
    }
    return result;
}

From source file:org.eclipse.che.plugin.java.testing.JavaTestFinder.java

License:Open Source License

private IRegion getRegion(IJavaElement element) {
    IRegion result = JavaCore.newRegion();
    if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
        //for projects only add the contained source folders
        try {//from  w  w w.j a va  2  s . c  om
            IPackageFragmentRoot[] packageFragmentRoots = ((IJavaProject) element).getPackageFragmentRoots();
            for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
                if (!packageFragmentRoot.isArchive()) {
                    result.add(packageFragmentRoot);
                }
            }
        } catch (JavaModelException e) {
            LOG.info("Can't read source folders.", e);
        }
    } else {
        result.add(element);
    }
    return result;
}

From source file:org.eclipselabs.nullness.NullnessCompiler.java

License:Open Source License

private void addRuntimeChecks(IFile javaFile, NullAnnotationFinder finder, FieldCache fieldCache)
        throws JavaModelException {
    IRegion region = JavaCore.newRegion();
    ICompilationUnit cu = (ICompilationUnit) JavaCore.create(javaFile);
    region.add(cu);
    IResource[] resources = JavaCore.getGeneratedResources(region, false);
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setProject(cu.getJavaProject());
    parser.setIgnoreMethodBodies(false);
    IType[] allTypes = getAllTypes(cu);//  w  w  w.j av a2  s.c o m
    IBinding[] bindings = parser.createBindings(allTypes, null);
    for (IResource resource : resources) {
        if (resource instanceof IFile) {
            IFile file = (IFile) resource;
            if ("class".equals(file.getFileExtension())) {
                try {
                    final InputStream inputStream = file.getContents();
                    try {
                        ClassReader reader = new ClassReader(inputStream);
                        int version = getClassFileVersion(reader);
                        if (version >= Opcodes.V1_5) {
                            ClassWriter writer = new ClassWriter(getClassWriterFlags(version));
                            String binaryName = file.getFullPath().removeFileExtension().lastSegment();
                            ITypeBinding typeBinding = findTypeBinding(binaryName, bindings);
                            if (typeBinding != null) {
                                final NullnessAssertionInserter nullChecker = new NullnessAssertionInserter(
                                        writer, typeBinding, finder, fieldCache);
                                reader.accept(nullChecker, 0);
                                if (nullChecker.isCheckInserted()) {
                                    ByteArrayInputStream newContent = new ByteArrayInputStream(
                                            writer.toByteArray());
                                    file.setContents(newContent, IResource.NONE, null);
                                }
                            }
                        }
                    } finally {
                        inputStream.close();
                    }
                } catch (CoreException e) {
                    // TODO reasonable exception handling
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    // TODO reasonable exception handling
                    throw new RuntimeException(e);
                }
            }
        }
    }
}