Example usage for org.eclipse.jdt.core JavaCore getGeneratedResources

List of usage examples for org.eclipse.jdt.core JavaCore getGeneratedResources

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore getGeneratedResources.

Prototype

public static IResource[] getGeneratedResources(IRegion region, boolean includesNonJavaResources) 

Source Link

Document

Returns an array that contains the resources generated by the Java builder when building the compilation units contained in the given 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  ww w  .j a va  2  s.  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:org.eclipse.xtext.builder.smap.DebugSourceInstallingCompilationParticipant.java

License:Open Source License

protected List<IFile> findGeneratedJavaClassFiles(IJavaElement element) {
    Region region = new Region();
    region.add(element);/*from   ww  w .  j  a  va  2s.  c  o  m*/
    List<IFile> result = Lists.newLinkedList();
    for (IResource res : JavaCore.getGeneratedResources(region, false))
        if (res instanceof IFile)
            result.add((IFile) res);
    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);//www.  j a  v a2 s.  co  m
    IResource[] resources = JavaCore.getGeneratedResources(region, false);
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setProject(cu.getJavaProject());
    parser.setIgnoreMethodBodies(false);
    IType[] allTypes = getAllTypes(cu);
    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);
                }
            }
        }
    }
}