Skip to content

Commit

Permalink
Unify nature checks and reuse existing constants from core.natures.PDE
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Apr 26, 2024
1 parent 692af00 commit 92378a0
Show file tree
Hide file tree
Showing 84 changed files with 500 additions and 730 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2019 IBM Corporation and others.
* Copyright (c) 2008, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1142,7 +1142,7 @@ private static String toString(List<IProject> projects) throws Exception {
contents.append(System.lineSeparator());
contents.append(" is open: " + project.isOpen()); //$NON-NLS-1$
contents.append(System.lineSeparator());
if (project.hasNature(JavaCore.NATURE_ID) && project.isAccessible()) {
if (Util.isJavaProject(project) && project.isAccessible()) {
IJavaProject javaProject = JavaCore.create(project);
boolean ignoreUnresolvedEntry = true;
IClasspathEntry[] projectClassPath = javaProject.getResolvedClasspath(ignoreUnresolvedEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.ITargetPlatformService;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.natures.PDE;
import org.eclipse.pde.internal.core.natures.PluginProject;
import org.eclipse.pde.ui.tests.util.TargetPlatformUtil;

/**
Expand Down Expand Up @@ -145,8 +145,8 @@ protected IJavaProject createProject(String projectName) {
if (project.exists()) {
project.delete(true, new NullProgressMonitor());
}
jproject = ProjectUtils.createPluginProject(projectName, new String[] {
PDE.PLUGIN_NATURE, ApiPlugin.NATURE_ID });
jproject = ProjectUtils.createPluginProject(projectName,
new String[] { PluginProject.NATURE, ApiPlugin.NATURE_ID });
addProject(jproject.getProject());
} catch (CoreException ce) {
ApiPlugin.log(ce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.natures.PDE;
import org.eclipse.pde.internal.core.natures.PluginProject;
import org.eclipse.pde.ui.tests.util.FreezeMonitor;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -155,8 +155,8 @@ protected void createProject(final String name, String[] packages) throws Except
}

// create project and import source
IJavaProject jproject = ProjectUtils.createPluginProject(name, new String[] {
PDE.PLUGIN_NATURE, ApiPlugin.NATURE_ID });
IJavaProject jproject = ProjectUtils.createPluginProject(name,
new String[] { PluginProject.NATURE, ApiPlugin.NATURE_ID });
assertNotNull("The java project must have been created", jproject); //$NON-NLS-1$

IPackageFragmentRoot root = jproject.getPackageFragmentRoot(jproject.getProject().getFolder(ProjectUtils.SRC_FOLDER));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* Copyright (c) 2008, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -34,6 +34,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.api.tools.tests.AbstractApiTest;
import org.eclipse.pde.core.project.IBundleClasspathEntry;
import org.eclipse.pde.core.project.IBundleProjectDescription;
Expand Down Expand Up @@ -201,7 +202,7 @@ public static IJavaProject createPluginProject(String projectName, String[] addi
public static IJavaProject createJavaProject(String projectName, String[] additionalNatures) throws CoreException {
IProgressMonitor monitor = new NullProgressMonitor();
IProject project = createProject(projectName, monitor);
if (!project.hasNature(JavaCore.NATURE_ID)) {
if (!Util.isJavaProject(project)) {
addNatureToProject(project, JavaCore.NATURE_ID, monitor);
}
if (additionalNatures != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.pde.api.tools.tests.util.ProjectUtils;
import org.eclipse.pde.core.project.IPackageExportDescription;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.natures.PDE;
import org.eclipse.pde.internal.core.natures.PluginProject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void testImportClassesTestSource() {
public void testCreatePluginProject() throws CoreException {
IJavaProject jproject = getTestingJavaProject(TESTING_PROJECT_NAME);
IProject project = jproject.getProject();
assertTrue("project must have the PDE nature", project.hasNature(PDE.PLUGIN_NATURE)); //$NON-NLS-1$
assertTrue("project must have the PDE nature", project.hasNature(PluginProject.NATURE)); //$NON-NLS-1$
assertTrue("project must have the java nature", project.hasNature(JavaCore.NATURE_ID)); //$NON-NLS-1$
assertTrue("project must have additional nature for API Tools", project.hasNature(ApiPlugin.NATURE_ID)); //$NON-NLS-1$
IFile file = project.getFile("build.properties"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) Sep 26, 2018 IBM Corporation and others.
* Copyright (c) Sep 26, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -52,6 +52,7 @@
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.util.Signatures;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
import org.eclipse.pde.api.tools.ui.internal.refactoring.CreateFileChange;
Expand Down Expand Up @@ -134,7 +135,7 @@ public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IPro
List<IJavaCompletionProposal> proposals = new ArrayList<>();
ICompilationUnit unit = context.getCompilationUnit();
IProject project = unit.getJavaProject().getProject();
if (!project.hasNature(ApiPlugin.NATURE_ID)) {
if (!Util.isApiProject(project)) {
return new IJavaCompletionProposal[0];
}
IFile build = project.getFile("build.properties"); //$NON-NLS-1$
Expand Down Expand Up @@ -166,8 +167,7 @@ public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IPro
public static Change createChange(ICompilationUnit unit, String qualifiedname) throws CoreException {
IProject project = unit.getJavaProject().getProject();
IFile buildProperties = project.getFile("build.properties"); //$NON-NLS-1$
boolean isBundle = project.hasNature(ApiPlugin.NATURE_ID);
if (!isBundle) {
if (!Util.isApiProject(project)) {
return new NullChange();
}
return new CompositeChange(MarkerMessages.UnknownAnnotationResolution_3, new Change[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2018 IBM Corporation and others.
* Copyright (c) 2007, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -20,13 +20,12 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsHelpContextIds;
Expand Down Expand Up @@ -110,15 +109,10 @@ protected Control createContents(Composite parent) {
Set<IJavaProject> set = new HashSet<>();
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
IProject project = null;
for (IJavaProject p : projects) {
project = p.getProject();
try {
if (project.hasNature(ApiPlugin.NATURE_ID) && block.hasProjectSpecificSettings(project)) {
set.add(p);
}
} catch (CoreException ce) {
// do nothing ignore the project
IProject project = p.getProject();
if (Util.isApiProject(project) && block.hasProjectSpecificSettings(project)) {
set.add(p);
}
}
} catch (JavaModelException jme) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2018 IBM Corporation and others.
* Copyright (c) 2007, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -16,7 +16,6 @@
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -40,7 +39,6 @@
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ViewerComparator;
Expand All @@ -57,6 +55,7 @@
import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsHelpContextIds;
import org.eclipse.pde.api.tools.ui.internal.SWTFactory;
import org.eclipse.pde.internal.core.natures.PluginProject;

Check warning on line 58 in apitools/org.eclipse.pde.api.tools.ui/src/org/eclipse/pde/api/tools/ui/internal/wizards/ApiToolingSetupWizardPage.java

View check run for this annotation

Jenkins - eclipse-pde / Compiler and API Tools

Restriction

NORMAL: Discouraged access: The type 'PluginProject' is not API (restriction on classpath entry '/home/jenkins/agent/workspace/eclipse.pde_PR-941/ui/org.eclipse.pde.core/target/classes')
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionListener;
Expand Down Expand Up @@ -248,24 +247,16 @@ private void updateCheckStatus(int count) {
*/
private IProject[] getInputProjects() {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
ArrayList<IProject> pjs = new ArrayList<>();
for (IProject project : projects) {
try {
if (acceptProject(project)) {
pjs.add(project);
}
} catch (CoreException ce) {
}
}
return pjs.toArray(new IProject[pjs.size()]);
return Arrays.stream(projects).filter(this::acceptProject).toArray(IProject[]::new);
}

private boolean acceptProject(IProject project) throws CoreException {
@SuppressWarnings("restriction")
private boolean acceptProject(IProject project) {
if (project == null) {
return false;
}
return (project.hasNature(JavaCore.NATURE_ID) && project.hasNature("org.eclipse.pde.PluginNature")) //$NON-NLS-1$
&& !project.hasNature(ApiPlugin.NATURE_ID) && !Util.isBinaryProject(project);
return Util.isJavaProject(project) && PluginProject.isPluginProject(project)
&& !Util.isApiProject(project) && !Util.isBinaryProject(project);
}

/**
Expand All @@ -281,25 +272,10 @@ protected Object[] getWorkbenchSelection() {
IWorkbenchSite site = part.getSite();
if (site != null) {
ISelectionProvider provider = site.getSelectionProvider();
if (provider != null) {
ISelection selection = provider.getSelection();
if (selection instanceof IStructuredSelection) {
Object[] jps = ((IStructuredSelection) provider.getSelection()).toArray();
ArrayList<IProject> pjs = new ArrayList<>();
for (Object jp : jps) {
if (jp instanceof IAdaptable) {
IAdaptable adapt = (IAdaptable) jp;
IProject pj = adapt.getAdapter(IProject.class);
try {
if (acceptProject(pj)) {
pjs.add(pj);
}
} catch (CoreException ce) {
}
}
}
return pjs.toArray();
}
if (provider != null && provider.getSelection() instanceof IStructuredSelection selection) {
return Arrays.stream(selection.toArray()).filter(IAdaptable.class::isInstance)
.map(IAdaptable.class::cast).map(a -> a.getAdapter(IProject.class))
.filter(this::acceptProject).toArray();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2021 Red Hat Inc. and others.
* Copyright (c) 2019, 2024 Red Hat Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

import org.eclipse.core.resources.ICommand;
Expand Down Expand Up @@ -376,11 +377,15 @@ private IProject importProject(File projectPath) throws CoreException, IOExcepti
return project;
}

@SuppressWarnings("restriction")
private static final Set<String> REMOVED_BUILDERS = Set.of(
org.eclipse.pde.internal.core.natures.PluginProject.MANIFEST_BUILDER_ID,
org.eclipse.pde.internal.core.natures.PluginProject.SCHEMA_BUILDER_ID);

private static ICommand[] removeManifestAndSchemaBuilders(ICommand[] buildSpec) {
// remove manifest and schema builders
return Arrays.stream(buildSpec).filter(x -> !("org.eclipse.pde.ManifestBuilder".equals(x.getBuilderName()) //$NON-NLS-1$
|| "org.eclipse.pde.SchemaBuilder".equals(x.getBuilderName())) //$NON-NLS-1$
).toArray(ICommand[]::new);
return Arrays.stream(buildSpec).filter(x -> !REMOVED_BUILDERS.contains(x.getBuilderName()))
.toArray(ICommand[]::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* Copyright (c) 2023, 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
Expand All @@ -22,13 +22,12 @@
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.BundleDelta;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.State;
import org.eclipse.osgi.service.resolver.StateDelta;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.core.IClasspathContributor;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
Expand Down Expand Up @@ -77,11 +76,7 @@ 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 Util.isApiProject(resource.getProject());
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2019 IBM Corporation and others.
* Copyright (c) 2007, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -799,7 +799,7 @@ boolean compareBuildProperties(BuildState state) {
* @since 1.1
*/
boolean shouldAbort(IProject project) throws CoreException {
return !project.isAccessible() || !project.hasNature(ApiPlugin.NATURE_ID) || hasBeenBuilt(project) || hasFatalProblems(project);
return !Util.isApiProject(project) || hasBeenBuilt(project) || hasFatalProblems(project);
}

/**
Expand Down Expand Up @@ -1297,7 +1297,7 @@ protected synchronized IApiAnalyzer getAnalyzer() {
*
* @return the list of projects required
*/
IProject[] getRequiredProjects(boolean includebinaries) throws CoreException {
IProject[] getRequiredProjects(boolean includebinaries) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
if (this.currentproject == null || workspaceRoot == null) {
return new IProject[0];
Expand All @@ -1323,16 +1323,8 @@ IProject[] getRequiredProjects(boolean includebinaries) throws CoreException {
IProject p = null;
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_PROJECT: {
p = workspaceRoot.getProject(path.lastSegment()); // missing
// projects
// are
// considered
// too
if (isOptional(entry) && !p.hasNature(ApiPlugin.NATURE_ID)) {// except
// if
// entry
// is
// optional
p = workspaceRoot.getProject(path.lastSegment()); // missing projects are considered too
if (isOptional(entry) && !Util.isApiProject(p)) {// except if entry is optional
p = null;
}
break;
Expand Down
Loading

0 comments on commit 92378a0

Please sign in to comment.