Example usage for org.eclipse.jdt.internal.core JavaProject getPackageFragments

List of usage examples for org.eclipse.jdt.internal.core JavaProject getPackageFragments

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaProject getPackageFragments.

Prototype

@Override
public IPackageFragment[] getPackageFragments() throws JavaModelException 

Source Link

Usage

From source file:org.eclipse.acceleo.internal.ide.ui.builders.AcceleoMarkerUtils.java

License:Open Source License

/**
 * Computes if the java service described in the message is accessible from the given file.
 * /* w  w  w .  j a v  a 2 s. c o  m*/
 * @param file
 *            The file
 * @param message
 *            The message describing the service
 * @param marker
 *            The marker to use if the service is not accessible
 * @throws JavaModelException
 *             In case of problem during the search of the service class.
 * @throws CoreException
 *             In case of problem during the search of the service class.
 */
private static void computeAccessibleService(IFile file, String message, IMarker marker)
        throws JavaModelException, CoreException {
    boolean exported = false;
    boolean found = false;

    String projectName = ""; //$NON-NLS-1$

    IProject project = file.getProject();
    AcceleoProject acceleoProject = new AcceleoProject(project);
    List<IProject> recursivelyAccessibleProjects = acceleoProject.getRecursivelyAccessibleProjects();
    for (IProject iProject : recursivelyAccessibleProjects) {
        if (iProject.isAccessible() && iProject.hasNature(JavaCore.NATURE_ID)) {
            JavaProject javaProject = new JavaProject();
            javaProject.setProject(iProject);

            IType type = null;

            List<IType> types = new ArrayList<IType>();
            IPackageFragment[] packageFragments = javaProject.getPackageFragments();
            for (IPackageFragment iPackageFragment : packageFragments) {
                if (iPackageFragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    ICompilationUnit[] compilationUnits = iPackageFragment.getCompilationUnits();
                    for (ICompilationUnit iCompilationUnit : compilationUnits) {
                        types.addAll(Arrays.asList(iCompilationUnit.getTypes()));
                    }
                }
            }
            for (IType iType : types) {
                if (iType.getFullyQualifiedName()
                        .equals(message.substring(AcceleoParserInfo.SERVICE_INVOCATION.length()))) {
                    type = iType;
                }
            }

            BundleDescription bundleDescription = null;
            if (type != null && PluginRegistry.findModel(iProject) != null) {
                found = true;
                projectName = iProject.getName();
                IPluginModelBase plugin = PluginRegistry.findModel(iProject);
                bundleDescription = plugin.getBundleDescription();
            }
            if (type != null && PluginRegistry.findModel(iProject) != null && bundleDescription != null) {
                ExportPackageDescription[] exportPackages = bundleDescription.getExportPackages();
                for (ExportPackageDescription exportPackageDescription : exportPackages) {
                    if (exportPackageDescription.getName().equals(type.getPackageFragment().getElementName())) {
                        exported = true;
                    }
                }
            }
        }
    }
    if (found && !exported) {
        marker.setAttribute(IMarker.MESSAGE,
                AcceleoUIMessages.getString("AcceleoMarkerUtils.JavaServiceClassNotExported", message //$NON-NLS-1$
                        .substring(AcceleoParserInfo.SERVICE_INVOCATION.length()), projectName));
    } else {
        marker.delete();
    }
}