Example usage for org.eclipse.jdt.core IPackageFragment getCompilationUnits

List of usage examples for org.eclipse.jdt.core IPackageFragment getCompilationUnits

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragment getCompilationUnits.

Prototype

ICompilationUnit[] getCompilationUnits() throws JavaModelException;

Source Link

Document

Returns all of the compilation units in this package fragment.

Usage

From source file:ar.com.fluxit.jqa.utils.JdtUtils.java

License:Open Source License

private static Collection<CommonDescriptor> collectCommonTypes(IPackageFragment packageFragment) {
    try {/*from   w w  w. j  a  va 2  s  . c  o  m*/
        Collection<CommonDescriptor> result = new ArrayList<CommonDescriptor>();
        ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
        for (ICompilationUnit compilationUnit : compilationUnits) {
            for (CommonDescriptor commonType : JdtUtils.collectCommonTypes(compilationUnit)) {
                if (check(commonType, packageFragment.getElementName())) {
                    result.add(commonType);
                }
            }
        }
        return result;
    } catch (JavaModelException e) {
        throw new IllegalStateException("Error while processing common types", e);
    }
}

From source file:ar.com.fluxit.jqa.viewer.JavaElementsColumnLabelProvider.java

License:Open Source License

@Override
public String getToolTipText(Object element) {
    if (element instanceof IPackageFragment) {
        IPackageFragment packageFragment = (IPackageFragment) element;
        try {/*  ww w .  j  a va2 s.  c o m*/
            int length = packageFragment.getCompilationUnits().length;
            StringBuffer result = new StringBuffer(length * 20);
            for (int i = 0; i < length; i++) {
                result.append(packageFragment.getCompilationUnits()[i].getElementName());
                if (i + 1 < length) {
                    result.append("\n");
                }
            }
            return result.toString();
        } catch (JavaModelException e) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:bndtools.internal.pkgselection.JavaSearchScopePackageLister.java

License:Open Source License

@Override
public String[] getPackages(boolean includeNonSource, IPackageFilter filter) throws PackageListException {
    final List<IJavaElement> packageList = new LinkedList<IJavaElement>();
    final SearchRequestor requestor = new SearchRequestor() {
        @Override//from w  w w.j ava2s.co  m
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IJavaElement enclosingElement = (IJavaElement) match.getElement();
            String name = enclosingElement.getElementName();
            if (name.length() > 0) { // Do not include default pkg
                packageList.add(enclosingElement);
            }
        }
    };
    final SearchPattern pattern = SearchPattern.createPattern("*", IJavaSearchConstants.PACKAGE,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);

    IRunnableWithProgress operation = new IRunnableWithProgress() {
        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor,
                        monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };

    try {
        runContext.run(true, true, operation);
    } catch (InvocationTargetException e) {
        throw new PackageListException(e.getCause());
    } catch (InterruptedException e) {
        throw new PackageListException("Operation interrupted");
    }

    // Remove non-source and excludes
    Set<String> packageNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = packageList.iterator(); iter.hasNext();) {
        boolean omit = false;
        IJavaElement element = iter.next();
        if (!includeNonSource) {
            IPackageFragment pkgFragment = (IPackageFragment) element;
            try {
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            } catch (JavaModelException e) {
                throw new PackageListException(e);
            }
        }

        if (filter != null && !filter.select(element.getElementName())) {
            omit = true;
        }
        if (!omit) {
            packageNames.add(element.getElementName());
        }
    }

    return packageNames.toArray(new String[0]);
}

From source file:bndtools.internal.testcaseselection.JavaSearchScopeTestCaseLister.java

License:Open Source License

@Override
public String[] getTestCases(boolean includeNonSource, ITestCaseFilter filter) throws TestCaseListException {
    final List<IJavaElement> testCaseList = new LinkedList<IJavaElement>();

    search(Arrays.asList("junit.framework.TestCase", "junit.framework.TestSuite"), testCaseList); //$NON-NLS-1$ //$NON-NLS-2$

    // Remove non-source and excludes
    Set<String> testCaseNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = testCaseList.iterator(); iter.hasNext();) {
        boolean omit = false;
        IJavaElement element = iter.next();
        try {/*from   www.ja  v  a2s.c o m*/

            IType type = (IType) element.getAncestor(IJavaElement.TYPE);
            if (Flags.isAbstract(type.getFlags())) {
                omit = true;
            }

            if (!includeNonSource) {
                IPackageFragment pkgFragment = (IPackageFragment) element
                        .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            }

        } catch (JavaModelException e) {
            throw new TestCaseListException(e);
        }
        String className = getClassName(element);
        if (filter != null && !filter.select(className)) {
            omit = true;
        }
        if (!omit) {
            testCaseNames.add(className);
        }
    }

    return testCaseNames.toArray(new String[0]);
}

From source file:br.ufal.cideei.handlers.DoAnalysisOnClassPath.java

License:Open Source License

/**
 * Configures the classpath, sets up the transformers, load (jimplify) classes and run the packs.
 * //from  w  w  w  . ja v a2  s  .co  m
 * @param javaProject
 * @param entry
 * @param libs
 */
private void addPacks(IJavaProject javaProject, IClasspathEntry entry, String libs) {
    /*
     * if the classpath entry is "", then JDT will complain about it.
     */
    String classPath;
    if (entry.getPath().toOSString().equals(File.separator + javaProject.getElementName())) {
        classPath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    } else {
        classPath = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath()).getLocation()
                .toOSString();
    }

    SootManager.configure(classPath + File.pathSeparator + libs);

    System.out.println(classPath);

    IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().getExtracter();
    IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(entry);
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        IJavaElement[] children = null;
        try {
            children = packageFragmentRoot.getChildren();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (IJavaElement child : children) {
            IPackageFragment packageFragment = (IPackageFragment) child;
            ICompilationUnit[] compilationUnits = null;
            try {
                compilationUnits = packageFragment.getCompilationUnits();
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            for (ICompilationUnit compilationUnit : compilationUnits) {
                String fragmentName = packageFragment.getElementName();
                String compilationName = compilationUnit.getElementName();
                StringBuilder qualifiedNameStrBuilder = new StringBuilder(fragmentName);
                // If it's the default package:
                if (qualifiedNameStrBuilder.length() == 0) {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(compilationName.substring(0, compilationName.length() - 5));
                } else {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(".")
                            .append(compilationName.substring(0, compilationName.length() - 5));
                }

                // This goes into Soot loadAndSupport
                SootManager.loadAndSupport(qualifiedNameStrBuilder.toString());
            }
        }
    }

    Scene.v().loadNecessaryClasses();

    AlloyConfigurationCheck alloyConfigurationCheck = null;
    // #ifdef FEATUREMODEL
    //@      try {
    //@         String absolutePath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    //@         alloyConfigurationCheck = new AlloyConfigurationCheck(absolutePath + File.separator  + "fm.als");
    //@      } catch (CannotReadAlloyFileException e) {
    //@         e.printStackTrace();
    //@         return;
    //@      }
    // #endif

    addPacks(classPath, extracter, alloyConfigurationCheck);

    SootManager.runPacks(extracter);
}

From source file:br.ufal.cideei.handlers.DoFeatureObliviousAnalysisOnClassPath.java

License:Open Source License

private void addPacks(IJavaProject javaProject, IClasspathEntry entry, String libs) {
    /*/*from  w  ww .  j  av a 2 s.c  om*/
     * if the classpath entry is "", then JDT will complain about it.
     */
    String classPath;
    if (entry.getPath().toOSString().equals(File.separator + javaProject.getElementName())) {
        classPath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    } else {
        classPath = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath()).getLocation()
                .toOSString();
    }

    SootManager.configure(classPath + File.pathSeparator + libs);

    IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().getExtracter();
    IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(entry);
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        IJavaElement[] children = null;
        try {
            children = packageFragmentRoot.getChildren();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (IJavaElement child : children) {
            IPackageFragment packageFragment = (IPackageFragment) child;
            ICompilationUnit[] compilationUnits = null;
            try {
                compilationUnits = packageFragment.getCompilationUnits();
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            for (ICompilationUnit compilationUnit : compilationUnits) {
                String fragmentName = packageFragment.getElementName();
                String compilationName = compilationUnit.getElementName();
                StringBuilder qualifiedNameStrBuilder = new StringBuilder(fragmentName);
                // If it's the default package:
                if (qualifiedNameStrBuilder.length() == 0) {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(compilationName.substring(0, compilationName.length() - 5));
                } else {
                    // Remove ".java" suffix
                    qualifiedNameStrBuilder.append(".")
                            .append(compilationName.substring(0, compilationName.length() - 5));
                }

                // This goes into Soot loadAndSupport
                SootManager.loadAndSupport(qualifiedNameStrBuilder.toString());
            }
        }
    }
    Scene.v().loadNecessaryClasses();

    AlloyConfigurationCheck alloyConfigurationCheck = null;
    // #ifdef FEATUREMODEL
    //@      try {
    //@         String absolutePath = javaProject.getResource().getLocation().toFile().getAbsolutePath();
    //@         alloyConfigurationCheck = new AlloyConfigurationCheck(absolutePath + File.separator  + "fm.als");
    //@      } catch (CannotReadAlloyFileException e) {
    //@         e.printStackTrace();
    //@         return;
    //@      }
    // #endif

    addPacks(classPath, extracter, alloyConfigurationCheck);

    SootManager.runPacks(extracter);
}

From source file:byke.DependencyAnalysis.java

License:Open Source License

private List<ICompilationUnit> compilationUnits() throws JavaModelException {
    if (!(_subject instanceof IPackageFragment))
        return asList(((ICompilationUnit) _subject.getAncestor(IJavaElement.COMPILATION_UNIT)));

    List<ICompilationUnit> ret = new ArrayList<ICompilationUnit>();
    for (IPackageFragment packge : withSubpackages((IPackageFragment) _subject))
        ret.addAll(asList(packge.getCompilationUnits()));

    return ret;//from w w w  .java2  s  .  c  om
}

From source file:byke.tests.workspaceutils.JavaModelUtility.java

License:Open Source License

public static void collectCompilationUnits(List<ICompilationUnit> result, IPackageFragmentRoot root)
        throws JavaModelException {
    IJavaElement[] elements = root.getChildren();
    for (int j = 0; j < elements.length; ++j) {
        IPackageFragment p = (IPackageFragment) elements[j];
        result.addAll(Arrays.asList(p.getCompilationUnits()));
    }//from  w w  w .jav  a  2s.c o m
}

From source file:ca.mcgill.cs.swevo.jayfx.JayFX.java

License:Open Source License

/**
 * Returns all the compilation units in this projects
 * //from   w w w  .  j av  a2  s .c o  m
 * @param pProject
 *            The project to analyze. Should never be null.
 * @return The compilation units to generate. Never null.
 * @throws JayFXException
 *             If the method cannot complete correctly
 */
private static List<ICompilationUnit> getCompilationUnits(final IJavaProject pProject) throws JayFXException {
    // assert pProject != null;

    final List<ICompilationUnit> lReturn = new ArrayList<ICompilationUnit>();

    try {
        final IPackageFragment[] lFragments = pProject.getPackageFragments();
        for (final IPackageFragment element : lFragments) {
            final ICompilationUnit[] lCUs = element.getCompilationUnits();
            for (final ICompilationUnit element2 : lCUs) {
                if (!element2.getResource().getFileExtension().equals("aj"))
                    lReturn.add(element2);
            }
        }
    } catch (final JavaModelException pException) {
        throw new JayFXException("Could not extract compilation units from project", pException);
    }
    return lReturn;
}

From source file:ch.qos.logback.beagle.util.EditorUtil.java

License:Open Source License

private static IType findType(IPackageFragment pack, String fullyQualifiedName) throws JavaModelException {
    ICompilationUnit[] cus = pack.getCompilationUnits();
    for (int i = 0; i < cus.length; i++) {
        ICompilationUnit unit = cus[i];/* ww  w  . j  a va2 s .  c  o m*/
        IType type = findType(unit, fullyQualifiedName);
        if (type != null && type.exists())
            return type;
    }
    return null;
}