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

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

Introduction

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

Prototype

boolean isExternal();

Source Link

Document

Returns whether this package fragment root is external to the workbench (that is, a local file), and has no underlying resource.

Usage

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

License:Open Source License

IPackageFragment findPackageFragment(String proj, String pkg) throws BedrockException {
    IProject ip = findProject(proj);//from ww w.jav a2 s .  c o m
    IJavaProject ijp = JavaCore.create(ip);
    if (ijp == null)
        return null;

    try {
        for (IPackageFragmentRoot pfr : ijp.getAllPackageFragmentRoots()) {
            try {
                if (!pfr.isExternal() && !pfr.isArchive() && pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPackageFragment ipf = pfr.getPackageFragment(pkg);
                    if (ipf != null && ipf.isOpen()) {
                        File f = BedrockUtil.getFileForPath(ipf.getPath(), ip);
                        if (f.exists())
                            return ipf;
                        BedrockPlugin.logE("Fragment path doesn't exist: " + f);
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
        throw new BedrockException("Problem finding package roots: " + e, e);
    }

    return null;
}

From source file:fede.workspace.dependencies.eclipse.java.util.TypeUtil.java

License:Apache License

/**
 * Find type.//from w  w  w  . ja  va2s  .co m
 * 
 * @param project
 *            the project
 * @param qualifiedPackage
 *            the qualified package
 * @param typeDef
 *            the type def
 * 
 * @return the i type
 */
public static IType findType(IProject project, String qualifiedPackage, String typeDef) {
    IJavaProject jp = JavaCore.create(project);
    if (!jp.exists())
        return null;

    try {
        IPackageFragmentRoot[] packagesRoot = jp.getPackageFragmentRoots();
        for (IPackageFragmentRoot packageFragmentRoot : packagesRoot) {
            if (packageFragmentRoot.isExternal())
                continue;
            IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(qualifiedPackage);
            if (packageFragment.exists()) {
                IType ret = TypeUtil.findType(packageFragment, typeDef);
                if (ret != null)
                    return ret;
            }
        }
    } catch (JavaModelException e) {
        WSPlugin.logException(e);
    }

    return null;
}

From source file:hydrograph.ui.expression.editor.buttons.ValidateExpressionToolButton.java

License:Apache License

public static Object[] getBuildPathForMethodInvocation() throws JavaModelException, MalformedURLException {
    String transfromJarPath = null;
    Object[] returnObj = new Object[3];
    IJavaProject iJavaProject = JavaCore
            .create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject());
    List<URL> urlList = new ArrayList<>();
    Properties properties = ConfigFileReader.INSTANCE.getCommonConfigurations();
    for (IPackageFragmentRoot iPackageFragmentRoot : iJavaProject.getAllPackageFragmentRoots()) {
        if (!iPackageFragmentRoot.isExternal()
                || StringUtils.contains(iPackageFragmentRoot.getElementName(),
                        properties.getProperty(Constants.KEY_TRANSFORMATION_JAR))
                || StringUtils.contains(iPackageFragmentRoot.getElementName(), Constants.ANTLR_JAR_FILE_NAME)
                || StringUtils.contains(iPackageFragmentRoot.getElementName(),
                        Constants.BEAN_SHELLJAR_FILE_NAME)
                || StringUtils.contains(iPackageFragmentRoot.getElementName(), Constants.SL4JLOG)
                || StringUtils.contains(iPackageFragmentRoot.getElementName(),
                        properties.getProperty(Constants.KEY_EXPRESSION_JAR))) {
            URL url = null;/*from   w  w w .  j  av  a 2  s .  c o  m*/
            if (!iPackageFragmentRoot.isExternal()) {
                url = BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject()
                        .getFile(iPackageFragmentRoot.getPath().removeFirstSegments(1)).getLocation().toFile()
                        .toURI().toURL();
                urlList.add(url);
            } else {
                url = iPackageFragmentRoot.getPath().toFile().toURI().toURL();
                urlList.add(url);
            }

            if (!iPackageFragmentRoot.isExternal()
                    || StringUtils.contains(iPackageFragmentRoot.getElementName(),
                            properties.getProperty(Constants.KEY_TRANSFORMATION_JAR))) {
                if (transfromJarPath == null) {
                    if (OSValidator.isMac() || OSValidator.isUnix())
                        transfromJarPath = url.getPath() + Constants.COLON;
                    else
                        transfromJarPath = url.getPath() + Constants.SEMICOLON;
                } else {
                    if (OSValidator.isMac() || OSValidator.isUnix())
                        transfromJarPath = transfromJarPath + url.getPath() + Constants.COLON;
                    else
                        transfromJarPath = transfromJarPath + url.getPath() + Constants.SEMICOLON;
                }
            }
        }
    }

    returnObj[0] = urlList;
    returnObj[1] = transfromJarPath;
    returnObj[2] = getPropertyFilePath(iJavaProject);
    iJavaProject.close();
    return returnObj;
}

From source file:icy.icy4eclipse.core.Icy4EclipsePlugin.java

License:Open Source License

private void startIcyInternal(boolean debug, boolean bypassJarclassloader) throws CoreException {
    logInfo("Starting Icy - (debug = " + debug + ") (disableJCL = " + bypassJarclassloader + ")");

    // Check parameters
    try {// w  w w .j a v  a 2 s  .  co m
        checkIcyConfiguration();
    } catch (Icy4EclipseException e) {
        logException(e);
        return;
    }

    String hd = getIcyHomeDir();
    File pluginsDirectory = new File(hd, ICY_PLUGINS_DIR);
    List<IcyProject> icyProjects = computeOpenIcyProjectsList();
    List<String> classpath = new ArrayList<String>();

    // Add Icy jars to system class loader if needed
    if (loadIcyJar()) {
        for (String s : ICY_JARS) {
            classpath.add(hd + File.separator + s);
        }
    }

    if (bypassJarclassloader) {
        // Add Eclipse Icy projects to system class loader
        for (IcyProject p : icyProjects) {
            IJavaProject project = p.getJavaProject();
            for (String s : JavaRuntime.computeDefaultRuntimeClassPath(project)) {
                classpath.add(s);
            }
        }

        // Add plugins jars to system class loader
        IPreferenceStore pref = getPreferenceStore();
        String bypassJars = pref.getString(ICY4ECLIPSE_PREF_BYPASS_JARS_KEY);
        List<Pattern> bypassRegexp = null;
        if (bypassJars != null) {
            bypassJars = bypassJars.trim();
            if (bypassJars.length() > 0) {
                String[] bypass = bypassJars.split(":");
                bypassRegexp = new ArrayList<Pattern>();
                for (String r : bypass) {
                    bypassRegexp.add(Pattern.compile("^(.*)" + r + "$", Pattern.CASE_INSENSITIVE));
                }
            }
        }

        if (pluginsDirectory.exists()) {
            List<File> jars = getAllJarFiles(pluginsDirectory);
            for (File f : jars) {
                boolean add = true;
                String path = f.getAbsolutePath();
                if (bypassRegexp != null) {
                    for (Pattern p : bypassRegexp) {
                        Matcher m = p.matcher(path);
                        if (m.matches()) {
                            logInfo(" - bypassing jar : " + p.pattern() + " -> " + path);
                            add = false;
                            break;
                        }
                    }
                }

                if (add) {
                    classpath.add(path);
                }
            }
        }
    }

    // Other launch arguments
    StringBuffer programArguments = new StringBuffer();
    for (String arg : ICY_PRG_ARGS) {
        programArguments.append(" " + arg);
    }
    if (bypassJarclassloader) {
        programArguments.append(" " + ICY_BYPASS_JARCLASSLOADER_ARG);
    }

    StringBuffer jvmArguments = new StringBuffer();
    for (String arg : ICY_VM_MANDATORY_ARGS) {
        jvmArguments.append(" " + arg);
    }
    jvmArguments.append(" -Xmx" + getIcyMemory() + "m");
    jvmArguments.append(" " + getOtherJVMArgs());

    // Launching the JVM
    ILaunchConfigurationType launchType = DebugPlugin.getDefault().getLaunchManager()
            .getLaunchConfigurationType("org.eclipse.jdt.launching.localJavaApplication");
    ILaunchConfigurationWorkingCopy config = launchType.newInstance(null, ICY4ECLIPSE_LABEL);
    config.setAttribute(IDebugUIConstants.ATTR_PRIVATE, false);
    config.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID,
            "org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector");

    if (bypassJarclassloader) {
        ISourceLookupDirector sourceLocator = new JavaSourceLookupDirector();
        ISourcePathComputer computer = DebugPlugin.getDefault().getLaunchManager()
                .getSourcePathComputer("org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer");
        sourceLocator.setSourcePathComputer(computer);

        ArrayList<ISourceContainer> sourceContainers = new ArrayList<ISourceContainer>();

        if (!icyProjects.isEmpty()) {
            for (IcyProject ip : icyProjects) {
                IJavaProject project = ip.getJavaProject();
                sourceContainers.add(new JavaProjectSourceContainer(project));
            }

            Set<IPath> external = new HashSet<IPath>();

            for (IcyProject ip : icyProjects) {
                IJavaProject project = ip.getJavaProject();

                IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
                for (int ri = 0; ri < roots.length; ri++) {
                    IPackageFragmentRoot root = roots[ri];
                    if (root.isExternal()) {
                        IPath location = root.getPath();
                        if (external.contains(location)) {
                            continue;
                        }
                        external.add(location);
                    }
                    sourceContainers.add(new PackageFragmentRootSourceContainer(root));
                }
            }
        }

        sourceContainers.add(new DefaultSourceContainer());

        sourceLocator.setSourceContainers(
                (ISourceContainer[]) sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]));
        sourceLocator.initializeParticipants();
        config.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, sourceLocator.getMemento());
    }

    ArrayList<String> classpathMementos = new ArrayList<String>();
    for (String s : classpath) {
        IRuntimeClasspathEntry cpEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(s));
        cpEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
        classpathMementos.add(cpEntry.getMemento());
    }

    IPath path = new Path(JavaRuntime.JRE_CONTAINER);
    try {
        IClasspathEntry cpEntry = JavaCore.newContainerEntry(path);
        IRuntimeClasspathEntry rcpEntry = JavaRuntime.newRuntimeContainerClasspathEntry(cpEntry.getPath(),
                IRuntimeClasspathEntry.STANDARD_CLASSES);
        classpathMementos.add(rcpEntry.getMemento());
    } catch (CoreException ex) {
        logException(ex);
    }

    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpathMementos);
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArguments.toString());
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, jvmArguments.toString());
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, getIcyMainClass());
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, getIcyHomeDir());

    logInfo(" - home dir   : " + getIcyHomeDir());
    logInfo(" - main class : " + getIcyMainClass());
    logInfo(" - prog args  : " + programArguments);
    logInfo(" - jvm args   : " + jvmArguments);
    logInfo(" - classpath  : " + classpath);

    config.launch(debug ? ILaunchManager.DEBUG_MODE : ILaunchManager.RUN_MODE, null);

}

From source file:io.sarl.lang.ui.contentassist.SARLProposalProvider.java

License:Apache License

private static String extractProjectPath(IPath fileWorkspacePath, IJavaProject javaProject) {
    if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
        try {/* w  w  w .j  a  va2  s .c  o m*/
            for (final IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                if (!root.isArchive() && !root.isExternal()) {
                    final IResource resource = root.getResource();
                    if (resource != null) {
                        final IPath sourceFolderPath = resource.getFullPath();
                        if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
                            final IPath claspathRelativePath = fileWorkspacePath
                                    .makeRelativeTo(sourceFolderPath);
                            return claspathRelativePath.removeLastSegments(1).toString().replace("/", "."); //$NON-NLS-1$//$NON-NLS-2$
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            Logger.getLogger(SARLProposalProvider.class).error(e.getLocalizedMessage(), e);
        }
    }
    return null;
}

From source file:io.sarl.lang.ui.validation.SARLUIValidator.java

License:Apache License

private static String getRelativeSourceFolder(IJavaProject javaProject, IPath rootPath)
        throws JavaModelException {
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (!root.isArchive() && !root.isExternal()) {
            IResource resource = root.getResource();
            if (resource != null) {
                IPath sourceFolderPath = resource.getFullPath();
                if (sourceFolderPath.isPrefixOf(rootPath)) {
                    IPath classpathRelativePath = rootPath.makeRelativeTo(sourceFolderPath);
                    return classpathRelativePath.removeLastSegments(1).toString().replace("/", "."); //$NON-NLS-1$//$NON-NLS-2$
                }//from  w w  w. j  a v  a2s  . c o  m
            }
        }
    }
    return null;
}

From source file:net.sf.eclipse.tomcat.VMLauncherUtility.java

License:Open Source License

private static ISourceLocator getSourceLocator(boolean trace) throws CoreException {
    ArrayList tempList = new ArrayList();
    StringBuffer traceBuffer = new StringBuffer();

    traceBuffer.append("Projects in source path :\n");
    List projects = TomcatLauncherPlugin.getDefault().getProjectsInSourcePath();
    for (Iterator iter = projects.iterator(); iter.hasNext();) {
        IProject project = ((ProjectListElement) iter.next()).getProject();
        traceBuffer.append("Project " + project.getName());
        if ((project.isOpen()) && project.hasNature(JavaCore.NATURE_ID)) {
            tempList.add(project.getNature(JavaCore.NATURE_ID));
            traceBuffer.append(" added to tempList\n");
        }//from  w w  w.j a  va  2s  .  c o  m
    }

    ISourceLookupDirector sourceLocator = null;

    sourceLocator = new JavaSourceLookupDirector();
    ISourcePathComputer computer = DebugPlugin.getDefault().getLaunchManager()
            .getSourcePathComputer("org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer");
    sourceLocator.setSourcePathComputer(computer); //$NON-NLS-1$

    ArrayList sourceContainers = new ArrayList();

    if (!tempList.isEmpty()) {
        IJavaProject[] javaProjects = (IJavaProject[]) tempList.toArray(new IJavaProject[1]);

        // Eclipse stops looking for source if it finds a jar containing the source code
        // despite this jar as no attached source (the user will have to use 'Attach source' button).
        // So we have to enforce that sources in project are searched before jar files,
        // To do so we add source containers in this orders :
        // - First project source containers.
        // - second packageFragmentRoot container (jar files in projects build path will be added to source path)
        // - third DefaultSourceContainer (jar files added to classpath will be added to source path)

        // First add all projects source containers
        for (int i = 0; i < javaProjects.length; i++) {
            IJavaProject project = javaProjects[i];
            traceBuffer
                    .append("  -> Add JavaProjectSourceContainer for " + project.getProject().getName() + "\n");
            sourceContainers.add(new JavaProjectSourceContainer(project));
        }

        // Adding packageFragmentRoot source containers, so classes in jar files associated to a project will be seen 
        HashSet external = new HashSet();

        for (int i = 0; i < javaProjects.length; i++) {
            IJavaProject project = javaProjects[i];
            traceBuffer.append("  -> Compute SourceContainers for " + project.getProject().getName() + " :\n");

            IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
            for (int ri = 0; ri < roots.length; ri++) {
                IPackageFragmentRoot root = roots[ri];
                if (root.isExternal()) {
                    IPath location = root.getPath();
                    if (external.contains(location)) {
                        continue;
                    }
                    external.add(location);
                }
                sourceContainers.add(new PackageFragmentRootSourceContainer(root));
                traceBuffer.append(
                        "     RootSourceContainer created for : " + root.getPath().toPortableString() + "\n");
            }
        }
    }

    // Last add DefaultSourceContainer, classes in jar files added to classpath will be visible
    sourceContainers.add(new DefaultSourceContainer());

    sourceLocator.setSourceContainers(
            (ISourceContainer[]) sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]));
    sourceLocator.initializeParticipants();

    if (trace)
        TomcatLauncherPlugin.log(traceBuffer.toString());
    return sourceLocator;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.JavaCloudFoundryArchiver.java

License:Open Source License

protected IFile getManifest(IPackageFragmentRoot[] roots, IJavaProject javaProject) throws CoreException {

    IFolder metaFolder = null;//from w ww  .  j  av  a  2  s .c  om
    for (IPackageFragmentRoot root : roots) {
        if (!root.isArchive() && !root.isExternal()) {
            IResource resource = root.getResource();
            metaFolder = getMetaFolder(resource);
            if (metaFolder != null) {
                break;
            }
        }
    }

    // Otherwise look for manifest file in the java project:
    if (metaFolder == null) {
        metaFolder = getMetaFolder(javaProject.getProject());
    }

    if (metaFolder != null) {
        IResource[] members = metaFolder.members();
        if (members != null) {
            for (IResource mem : members) {
                if (MANIFEST_FILE.equals(mem.getName().toUpperCase()) && mem instanceof IFile) {
                    return (IFile) mem;
                }
            }
        }
    }

    return null;

}

From source file:org.eclipse.che.jdt.javadoc.JavadocContentAccess2.java

License:Open Source License

/**
 * Returns the Javadoc for a package which could be present in package.html, package-info.java
 * or from an attached Javadoc./*from w w  w  . ja v a  2  s  . c om*/
 *
 * @param packageFragment the package which is requesting for the document
 * @param urlPrefix
 * @return the document content in HTML format or <code>null</code> if there is no associated
 *         Javadoc
 * @throws CoreException if the Java element does not exists or an exception occurs while
 *             accessing the file containing the package Javadoc
 * @since 3.9
 */
public static String getHTMLContent(IPackageFragment packageFragment, String urlPrefix) throws CoreException {
    IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    //1==> Handle the case when the documentation is present in package-info.java or package-info.class file
    ITypeRoot packageInfo;
    boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY;
    if (isBinary) {
        packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
    } else {
        packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
    }
    if (packageInfo != null && packageInfo.exists()) {
        String cuSource = packageInfo.getSource();
        //the source can be null for some of the class files
        if (cuSource != null) {
            Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource);
            if (packageJavadocNode != null) {
                IJavaElement element;
                if (isBinary) {
                    element = ((IClassFile) packageInfo).getType();
                } else {
                    element = packageInfo.getParent(); // parent is the IPackageFragment
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource, urlPrefix).toHTML();
            }
        }
    }

    // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or
    // coming from a jar file
    else {
        Object[] nonJavaResources = packageFragment.getNonJavaResources();
        // 2.1 ==>If the package.html file is present in the source or directly in the binary jar
        for (Object nonJavaResource : nonJavaResources) {
            if (nonJavaResource instanceof IFile) {
                IFile iFile = (IFile) nonJavaResource;
                if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) {
                    return getIFileContent(iFile);
                }
            }
        }

        // 2.2==>The file is present in a binary container
        if (isBinary) {
            for (Object nonJavaResource : nonJavaResources) {
                // The content is from an external binary class folder
                if (nonJavaResource instanceof IJarEntryResource) {
                    IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                    String encoding = getSourceAttachmentEncoding(root);
                    if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName())
                            && jarEntryResource.isFile()) {
                        return getHTMLContent(jarEntryResource, encoding);
                    }
                }
            }
            //2.3 ==>The file is present in the source attachment path.
            String contents = getHTMLContentFromAttachedSource(root, packageFragment, urlPrefix);
            if (contents != null)
                return contents;
        }
    }

    //3==> Handle the case when the documentation is coming from the attached Javadoc
    if ((root.isArchive() || root.isExternal())) {
        return packageFragment.getAttachedJavadoc(null);

    }

    return null;
}

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {//from   w  ww  .j a v  a  2 s.  c o m
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = JavaModelUtil.getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a
                              // referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(JavaElementLabels.CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                        if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
                        }
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());

            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}