Search in sources :

Example 51 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class NewBndProjectWizardPageOne method getSourceClasspathEntries.

@Override
public IClasspathEntry[] getSourceClasspathEntries() {
    IPath projectPath = new Path(getProjectName()).makeAbsolute();
    ProjectPaths projectPaths = ProjectPaths.DEFAULT;
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(2);
    newEntries.add(JavaCore.newSourceEntry(projectPath.append(projectPaths.getSrc()), null, projectPath.append(projectPaths.getBin())));
    boolean enableTestSrcDir;
    try {
        if (template == null)
            enableTestSrcDir = true;
        else {
            ObjectClassDefinition templateMeta = template.getMetadata();
            enableTestSrcDir = findAttribute(templateMeta, ProjectTemplateParam.TEST_SRC_DIR.getString()) != null;
        }
    } catch (Exception e) {
        Plugin.getDefault().getLog().log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error accessing template parameters", e));
        enableTestSrcDir = true;
    }
    if (enableTestSrcDir)
        newEntries.add(JavaCore.newSourceEntry(projectPath.append(projectPaths.getTestSrc()), null, projectPath.append(projectPaths.getTestBin())));
    return newEntries.toArray(new IClasspathEntry[0]);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) ProjectPaths(org.bndtools.api.ProjectPaths) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 52 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class NewBndProjectWizardPageTwo method isPageComplete.

@Override
public boolean isPageComplete() {
    boolean resultFromSuperClass = super.isPageComplete();
    int nr = 0;
    try {
        IClasspathEntry[] entries = getJavaProject().getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                nr++;
            // here we could do more validation on the paths if we want to
            // for now we just count pages
            }
        }
    } catch (Exception e) {
        // if for some reason we cannot access the resolved classpath
        // we simply set an error message
        setErrorMessage("Could not access resolved classpaths: " + e);
    }
    // have the test source set
    return resultFromSuperClass && (1 <= nr) && (nr <= 2);
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 53 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class BndContainerSourceManager method loadAttachedSources.

/**
     * Return (a potentially modified) list of {@link IClasspathEntry} instances that will have any previously persisted
     * attached sources added.
     */
public static List<IClasspathEntry> loadAttachedSources(final IProject project, final List<IClasspathEntry> classPathEntries) throws CoreException {
    if (classPathEntries.isEmpty()) {
        return classPathEntries;
    }
    final Properties props = loadSourceAttachmentProperties(project);
    final List<IClasspathEntry> configuredClassPathEntries = new ArrayList<IClasspathEntry>(classPathEntries.size());
    for (final IClasspathEntry entry : classPathEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY || entry.getSourceAttachmentPath() != null) {
            configuredClassPathEntries.add(entry);
            continue;
        }
        final String key = entry.getPath().toPortableString();
        IPath srcPath = null;
        IPath srcRoot = null;
        // Retrieve the saved source attachment information
        if (props != null && props.containsKey(key + PROPERTY_SRC_PATH)) {
            srcPath = Path.fromPortableString((String) props.get(key + PROPERTY_SRC_PATH));
            if (props.containsKey(key + PROPERTY_SRC_ROOT)) {
                srcRoot = Path.fromPortableString((String) props.get(key + PROPERTY_SRC_ROOT));
            }
        } else {
            // If there is no saved source attachment, then try and find a source bundle
            Map<String, String> extraProps = new HashMap<String, String>();
            for (IClasspathAttribute attr : entry.getExtraAttributes()) {
                extraProps.put(attr.getName(), attr.getValue());
            }
            File sourceBundle = getSourceBundle(entry.getPath(), extraProps);
            if (sourceBundle != null) {
                srcPath = new Path(sourceBundle.getAbsolutePath());
            }
        }
        if (srcPath != null || srcRoot != null) {
            configuredClassPathEntries.add(JavaCore.newLibraryEntry(entry.getPath(), srcPath, srcRoot, entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported()));
        } else {
            configuredClassPathEntries.add(entry);
        }
    }
    return configuredClassPathEntries;
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) File(java.io.File)

Example 54 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class BndProjectNature method removeBndClasspath.

private void removeBndClasspath() throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(classpath.length);
    boolean changed = false;
    for (IClasspathEntry entry : classpath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && BndtoolsConstants.BND_CLASSPATH_ID.equals(entry.getPath())) {
            changed = true;
        } else {
            newEntries.add(entry);
        }
    }
    if (changed)
        javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), null);
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Example 55 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class BndProjectNature method installBndClasspath.

private void installBndClasspath() throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : classpath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && BndtoolsConstants.BND_CLASSPATH_ID.equals(entry.getPath()))
            // already installed
            return;
    }
    IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + 1];
    System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
    newEntries[classpath.length] = JavaCore.newContainerEntry(BndtoolsConstants.BND_CLASSPATH_ID);
    javaProject.setRawClasspath(newEntries, null);
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)55 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ArrayList (java.util.ArrayList)35 Path (org.eclipse.core.runtime.Path)27 JavaModelException (org.eclipse.jdt.core.JavaModelException)23 IProject (org.eclipse.core.resources.IProject)20 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 CoreException (org.eclipse.core.runtime.CoreException)12 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)10 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)7