-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an OSGi property tester to check if action should apply
Currently the UnresolvedImportFixProcessor can only work on a project if it has the org.eclipse.pde.PluginNature but actually we can work on many different kind of projects independent of the nature. This now adds a new property tester that evaluates the project if it matches a certain layout than a nature and allows to enable the classpath container on them on demand.
- Loading branch information
Showing
5 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
ui/org.eclipse.pde.bnd.ui/src/org/eclipse/pde/bnd/ui/OSGiPropertyTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 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.bnd.ui; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.jar.JarFile; | ||
import java.util.jar.Manifest; | ||
|
||
import org.eclipse.core.expressions.PropertyTester; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.osgi.framework.Constants; | ||
|
||
import aQute.bnd.build.Project; | ||
import aQute.bnd.build.Workspace; | ||
|
||
public class OSGiPropertyTester extends PropertyTester { | ||
|
||
@Override | ||
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { | ||
IProject project = getProject(receiver); | ||
if (project != null) { | ||
if ("isOSGiClasspathProject".equals(property)) { | ||
return isOSGiClasspathProject(project); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean isOSGiClasspathProject(IProject project) { | ||
// only java projects can have a classpath... | ||
if (isJavaNature(project)) { | ||
// if it already has some well known nature we can assume its true | ||
if (isPdeNature(project) || isBndToolsNature(project)) { | ||
return true; | ||
} | ||
// otherwise we need to dig a bit deeper | ||
if (isAutomaticManifest(project) || isManifestFirst(project)) { | ||
return true; | ||
} | ||
if (isBndWithClasspath(project)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isAutomaticManifest(IProject project) { | ||
return project.getFile("pde.bnd").exists(); | ||
} | ||
|
||
protected static boolean isBndWithClasspath(IProject project) { | ||
IFile bndFile = project.getFile(Project.BNDFILE); | ||
if (bndFile.exists()) { | ||
// it might be a BND project with classpath ... | ||
IPath location = project.getLocation(); | ||
if (location != null) { | ||
File projectDir = location.toFile(); | ||
if (projectDir != null) { | ||
try { | ||
Project plainBnd = Workspace.getProject(projectDir); | ||
if (!plainBnd.getBuildpath().isEmpty() || !plainBnd.getTestpath().isEmpty()) { | ||
return true; | ||
} | ||
} catch (Exception e) { | ||
// no way to tell... | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected static boolean isManifestFirst(IProject project) { | ||
IFile manifestFile = project.getFile(JarFile.MANIFEST_NAME); | ||
if (manifestFile.exists()) { | ||
// This seems to be a "manifest-first" project... | ||
try (InputStream contents = manifestFile.getContents()) { | ||
Manifest manifest = new Manifest(contents); | ||
return manifest.getMainAttributes().getValue(Constants.BUNDLE_MANIFESTVERSION) != null; | ||
} catch (IOException | CoreException e) { | ||
// we can't make further assumptions here... | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isBndToolsNature(IProject project) { | ||
try { | ||
return project.hasNature("bndtools.core.bndnature"); | ||
} catch (CoreException e) { | ||
// we can't know then... | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isPdeNature(IProject project) { | ||
try { | ||
return project.hasNature("org.eclipse.pde.PluginNature"); | ||
} catch (CoreException e) { | ||
// we can't know then... | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isJavaNature(IProject project) { | ||
try { | ||
return project.hasNature("org.eclipse.jdt.core.javanature"); | ||
} catch (CoreException e) { | ||
// we can't know then... | ||
} | ||
return false; | ||
} | ||
|
||
private IProject getProject(Object receiver) { | ||
if (receiver instanceof IProject project) { | ||
return project; | ||
} | ||
if (receiver instanceof IJavaProject java) { | ||
return java.getProject(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
ui/org.eclipse.pde.bnd.ui/src/org/eclipse/pde/bnd/ui/Resources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters