Skip to content

Commit

Permalink
Add a ClasspathContributor for API tools annotations
Browse files Browse the repository at this point in the history
Currently it is quite cumbersome to use and discover the PDE own API
tools annotations and requires some kind of workarounds.

This adds a classpath contributor that makes the annotations available
like already done for ds and bundle annotations.

Fix eclipse-pde#447
  • Loading branch information
laeubi committed Nov 29, 2023
1 parent 62e8a76 commit 2264a6d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
6 changes: 6 additions & 0 deletions apitools/org.eclipse.pde.api.tools/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,10 @@
</run>
</application>
</extension>
<extension
point="org.eclipse.pde.core.pluginClasspathContributors">
<contributor
class="org.eclipse.pde.api.tools.internal.ApiAnnotationsClasspathContributor">
</contributor>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.api.tools.internal;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.core.IClasspathContributor;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.ClasspathUtilCore;

public class ApiAnnotationsClasspathContributor implements IClasspathContributor {

private static final Collection<String> API_TOOLS_ANNOTATIONS = List.of("org.eclipse.pde.api.tools.annotations"); //$NON-NLS-1$

@Override
public List<IClasspathEntry> getInitialEntries(BundleDescription project) {
IPluginModelBase model = PluginRegistry.findModel(project);
if (hasApiNature(model)) {
return ClasspathUtilCore.classpathEntries(annotations()).collect(Collectors.toList());
}
return Collections.emptyList();
}



private boolean hasApiNature(IPluginModelBase model) {
if (model != null) {
IResource resource = model.getUnderlyingResource();
if (resource != null) {
try {
return resource.getProject().hasNature(ApiPlugin.NATURE_ID);
} catch (CoreException e) {
// assume not compatible project then...
}
}
}
return false;
}



/**
* @return s stream of all current available annotations in the current plugin
* registry
*/
public static Stream<IPluginModelBase> annotations() {
return API_TOOLS_ANNOTATIONS.stream().map(PluginRegistry::findModel).filter(Objects::nonNull)
.filter(IPluginModelBase::isEnabled);
}

@Override
public List<IClasspathEntry> getEntriesForDependency(BundleDescription project, BundleDescription addedDependency) {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.osgi.service.resolver.BundleDescription;
Expand Down Expand Up @@ -76,6 +81,28 @@ private static Collection<ClasspathLibrary> collectLibraryEntries(IPluginModelBa
return entries;
}

public static Stream<IClasspathEntry> classpathEntries(Stream<IPluginModelBase> models) {
return models.flatMap(model -> {
String location = model.getInstallLocation();
if (location == null) {
return Stream.empty();
}
boolean isJarShape = new File(location).isFile();
IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
if (isJarShape || libraries.length == 0) {
return Stream.of(IPath.fromOSString(location));
}
return Arrays.stream(libraries).filter(library -> !IPluginLibrary.RESOURCE.equals(library.getType()))
.map(library -> {
String name = library.getName();
String expandedName = ClasspathUtilCore.expandLibraryName(name);
IPath path = ClasspathUtilCore.getPath(model, expandedName, isJarShape);
return path;
}).filter(Objects::nonNull);
}).map(path -> JavaCore.newLibraryEntry(path, path, IPath.ROOT, new IAccessRule[0], new IClasspathAttribute[0],
false));
}

private static void addLibraryEntry(IPluginLibrary library, Collection<ClasspathLibrary> entries) {

String name = library.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.IClasspathContributor;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.ClasspathUtilCore;

/**
* This makes the <a href=
Expand All @@ -48,20 +45,14 @@ public class OSGiAnnotationsClasspathContributor implements IClasspathContributo
public List<IClasspathEntry> getInitialEntries(BundleDescription project) {
IPluginModelBase model = PluginRegistry.findModel(project);
if (model != null) {
return entries().collect(Collectors.toList());
return ClasspathUtilCore.classpathEntries(annotations()).collect(Collectors.toList());
}
return Collections.emptyList();
}

static Stream<IClasspathEntry> entries() {
return annotations().map(IPluginModelBase::getInstallLocation).filter(Objects::nonNull).map(IPath::fromOSString)
.map(path -> JavaCore.newLibraryEntry(path, path, IPath.ROOT, new IAccessRule[0],
new IClasspathAttribute[0], false));
}

/**
* @return s stream of all current aviable annotations in the current plugin
* registry
* @return s stream of all current available annotations in the current
* plugin registry
*/
public static Stream<IPluginModelBase> annotations() {
return OSGI_ANNOTATIONS.stream().map(PluginRegistry::findModel).filter(Objects::nonNull)
Expand Down

0 comments on commit 2264a6d

Please sign in to comment.