Example usage for org.eclipse.jdt.core IPackageFragmentRoot getKind

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getKind

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getKind.

Prototype

int getKind() throws JavaModelException;

Source Link

Document

Returns this package fragment root's kind encoded as an integer.

Usage

From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java

License:Open Source License

public static ICompilationUnit findCompilationUnit(IProject project, String fullyQualifiedName) {

    IPackageFragment fragment;/*from  w  w w  .jav a2 s . c  o m*/
    ICompilationUnit compilationUnit;
    IPackageFragmentRoot[] fragmentRoots;
    try {
        fragmentRoots = getPackageFragmentRoots(project);
    } catch (JavaModelException e) {
        log.warn("Could not get packageFragmentRoots of project " + project.getName(), e);
        return null;
    } catch (CoreException e) {
        log.warn("Could not get packageFragmentRoots of project " + project.getName(), e);
        return null;
    }

    // split fullyQualifiedName into package and simple name
    // int lastDotIndex= fullyQualifiedName.lastIndexOf('.');
    String packageName;
    String compilationUnitName;

    Pattern pattern = Pattern.compile("(?:(.*)\\.)?([^.]+\\.[^.]+)");
    Matcher matcher = pattern.matcher(fullyQualifiedName);
    if (matcher.matches()) {
        packageName = matcher.group(1);
        if (packageName == null) {
            packageName = ""; // default package
        }
        compilationUnitName = matcher.group(2);
    } else {
        return null;
    }

    /*
    if (lastDotIndex != -1) {
       packageName= fullyQualifiedName.substring(0, lastDotIndex);
       compilationUnitName= fullyQualifiedName.substring(lastDotIndex + 1,
       fullyQualifiedName.length());
    } else {
       // no dot found ==> try to retrieve class in default package
       packageName= "";
       compilationUnitName= fullyQualifiedName;
    }
    */

    for (IPackageFragmentRoot root : fragmentRoots) {
        try {
            // only take into account source roots
            if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
                continue;
            }
        } catch (JavaModelException e) {
            log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e);
            continue;
        }

        fragment = root.getPackageFragment(packageName);
        if (fragment == null) {
            continue;
        }

        /*
        log.info("Trying package fragment: "
              + fragment.getPath());
        */

        compilationUnit = fragment.getCompilationUnit(compilationUnitName);
        if (compilationUnit != null && compilationUnit.exists()) {
            return compilationUnit;
        }
    }
    return null;
}

From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java

License:Open Source License

public static ICompilationUnit findCompilationUnitInClassPackages(IProject project,
        Iterable<String> classPackages, String packageSuffix, String resourceName) {

    IPackageFragment fragment;//w  w  w  . j a v  a  2  s.c  o  m
    ICompilationUnit compilationUnit;
    IPackageFragmentRoot[] fragmentRoots;
    try {
        fragmentRoots = getPackageFragmentRoots(project);
    } catch (JavaModelException e) {
        log.warn("Could not get packageFragmentRoots of project " + project.getName(), e);
        return null;
    } catch (CoreException e) {
        log.warn("Could not get packageFragmentRoots of project " + project.getName(), e);
        return null;
    }

    if (classPackages == null) {
        // add empty string as classPackage if none supplied
        classPackages = Arrays.asList(new String[] { "" });
    }

    // loop through class package names
    for (String packageName : classPackages) {
        for (IPackageFragmentRoot root : fragmentRoots) {
            try {
                // only take into account source roots
                if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
                    continue;
                }
            } catch (JavaModelException e) {
                log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e);
                continue;
            }

            fragment = root.getPackageFragment(packageName + packageSuffix);
            if (fragment == null) {
                continue;
            }

            /*
            log.info("Trying package fragment: "
                  + fragment.getPath());
            */

            compilationUnit = fragment.getCompilationUnit(resourceName);
            if (compilationUnit != null && compilationUnit.exists()) {
                return compilationUnit;
            }
        }
    }
    return null;
}

From source file:ch.mlutz.plugins.t4e.tools.EclipseTools.java

License:Open Source License

/**
 * @param project/*w w w  . j  a v  a 2  s .  com*/
 * @return
 * @throws CoreException
 * @throws JavaModelException
 */
public static IPackageFragmentRoot[] getPackageFragmentRoots(IProject project)
        throws CoreException, JavaModelException {
    IPackageFragmentRoot[] packageFragmentRoots = null;
    IJavaProject javaProject = null;
    if (project.hasNature(JavaCore.NATURE_ID)) {
        // Cast the IProject to IJavaProject.
        javaProject = JavaCore.create(project);

        // Get the array of IPackageFragmentRoot using getAllPackageFragmentRoots()
        packageFragmentRoots = javaProject.getAllPackageFragmentRoots();

        // Get the one(s) which have getKind() == IPackageFragmentRoot.K_SOURCE
        for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
            if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                System.out.println("Source Folder: " + packageFragmentRoot.getPath());
            }
        }
    }

    return packageFragmentRoots;
}

From source file:cn.ieclipse.adt.ext.jdt.JavaSelection.java

License:Apache License

private void iterate(IJavaElement element) throws JavaModelException {
    if (project == null) {
        project = element.getJavaProject();
    }//from www  .  j a  v a  2  s. c  om
    if (element instanceof IJavaProject) {
        IJavaProject prj = (IJavaProject) element;
        IPackageFragmentRoot[] roots = prj.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            iterate(root);
        }
        return;
    } else if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;
        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
            IJavaElement[] children = root.getChildren();
            for (IJavaElement child : children) {
                iterate(child);
            }
        }
    } else if (element instanceof IPackageFragment) {
        IPackageFragment pkg = (IPackageFragment) element;
        if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) {
            ICompilationUnit[] units = pkg.getCompilationUnits();
            for (ICompilationUnit unit : units) {
                this.units.add(unit);
            }
            IJavaElement[] children = pkg.getChildren();
            for (IJavaElement child : children) {
                iterate(child);
            }
        }
    } else if (element instanceof ICompilationUnit) {
        ICompilationUnit unit = (ICompilationUnit) element;
        this.units.add(unit);
    }
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationService.java

License:Apache License

public int getNumBinaryDependencies(final List<IPackageFragmentRoot> packageFragmentRoots) {
    int numBinary = 0;
    for (final IPackageFragmentRoot root : packageFragmentRoots) {
        try {//from w ww  .  j  av  a2  s  .  c o m
            if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                numBinary++;
            }
        } catch (final JavaModelException e) {
            /*
             * Occurs if root does not exist or an exception occurs while accessing
             * resource. If this happens, assume root is not binary and therefore do
             * not increment count
             */
        }
    }
    return numBinary;
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationService.java

License:Apache License

public URL getBinaryDependencyFilepath(final IPackageFragmentRoot packageFragmentRoot) {
    try {//  ww w.j ava2  s . co  m
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
            return packageFragmentRoot.getPath().toFile().toURI().toURL();
        }
    } catch (final JavaModelException e) {
        /*
         * If root does not exist or exception occurs while accessing
         * resource, do not add its filepath to the list of binary
         * dependency filepaths
         */
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.centurylink.mdw.plugin.WizardPage.java

License:Apache License

public void determinePackageFragmentRoot(WorkflowProject workflowProject) {
    IPackageFragmentRoot oldPackageFragmentRoot = getPackageFragmentRoot();

    if (workflowProject != null && workflowProject.isLocalJavaSupported()) {
        try {/*ww  w .j  a  v a  2  s.  c  o m*/
            IPackageFragmentRoot tempRoot = null;
            IPackageFragmentRoot srcRoot = null;
            IJavaProject javaProject = workflowProject == null ? null : workflowProject.getSourceJavaProject();
            if (javaProject != null) {
                for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) {
                    if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        if (pfr.getElementName().equals(MdwPlugin.getSettings().getTempResourceLocation())) {
                            tempRoot = pfr;
                        } else {
                            srcRoot = pfr;
                            break;
                        }
                    }
                }
                if (srcRoot == null && tempRoot == null)
                    srcRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
                setPackageFragmentRoot(srcRoot == null ? tempRoot : srcRoot, true);
            }
        } catch (JavaModelException ex) {
            PluginMessages.log(ex);
        }
    } else {
        setPackageFragmentRoot(getPackageFragmentRoot(), true);
    }
    if (oldPackageFragmentRoot == null || !oldPackageFragmentRoot.equals(getPackageFragmentRoot()))
        setPackageFragment(null, true);
}

From source file:com.centurylink.mdw.plugin.WizardPage.java

License:Apache License

/**
 * Override to prefer non-temp package root.
 *///from  ww w.  j a  v a  2s .com
@SuppressWarnings("restriction")
@Override
protected void initContainerPage(IJavaElement elem) {
    IPackageFragmentRoot tempRoot = null; // only as fallback
    IPackageFragmentRoot initRoot = null;
    if (elem != null) {
        initRoot = org.eclipse.jdt.internal.corext.util.JavaModelUtil.getPackageFragmentRoot(elem);
        try {
            if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
                IJavaProject jproject = elem.getJavaProject();
                if (jproject != null) {
                    initRoot = null;
                    if (jproject.exists()) {
                        IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                                if (roots[i].getElementName()
                                        .equals(MdwPlugin.getSettings().getTempResourceLocation())) {
                                    tempRoot = roots[i];
                                } else {
                                    initRoot = roots[i];
                                    break;
                                }
                            }
                        }
                    }
                    if (initRoot == null && tempRoot == null) {
                        initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
                    }
                }
            }
        } catch (JavaModelException e) {
            org.eclipse.jdt.internal.ui.JavaPlugin.log(e);
        }
    }
    setPackageFragmentRoot(initRoot == null ? tempRoot : initRoot, true);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

private IStatus validateClassFile() {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    try {/*from  www.  j a  va2 s  . c  o m*/
        if (root.getKind() != IPackageFragmentRoot.K_BINARY)
            return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
    } catch (JavaModelException e) {
        return e.getJavaModelStatus();
    }
    IJavaProject project = getJavaProject();
    if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(getElementName())) {
        return Status.OK_STATUS;
    }
    return Status.CANCEL_STATUS;
    //   return JavaConventions.validateClassFileName(getElementName(), project.getOption(JavaCore.COMPILER_SOURCE, true),
    //                                     project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

protected IStatus validateCompilationUnit(File resource) {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    // root never null as validation is not done for working copies
    try {/* ww  w  . j  av a  2  s. co  m*/
        if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
            return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
    } catch (JavaModelException e) {
        return e.getJavaModelStatus();
    }
    if (resource != null) {
        char[][] inclusionPatterns = ((PackageFragmentRoot) root).fullInclusionPatternChars();
        char[][] exclusionPatterns = ((PackageFragmentRoot) root).fullExclusionPatternChars();
        if (Util.isExcluded(new Path(resource.getPath()), inclusionPatterns, exclusionPatterns, false))
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this);
        if (!resource.exists())
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this);
    }
    IJavaProject project = getJavaProject();
    return JavaConventions.validateCompilationUnitName(getElementName(),
            project.getOption(JavaCore.COMPILER_SOURCE, true),
            project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
}