Skip to content

Commit 88e3ed7

Browse files
committed
Unify logging and setting the classpath container
Currently some messages about the state is logged in the initializer and some in the computer, also setting an empty classpath required an own inner class while it is actually equal to an empty state. This now unifies the logging and handling of classpath containers.
1 parent 3ed582d commit 88e3ed7

File tree

4 files changed

+87
-76
lines changed

4 files changed

+87
-76
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.pde.internal.core;
1515

16-
import java.io.BufferedInputStream;
1716
import java.io.BufferedOutputStream;
1817
import java.io.File;
1918
import java.io.FileInputStream;
@@ -597,18 +596,17 @@ protected IStatus run(IProgressMonitor jobMonitor) {
597596
return Status.CANCEL_STATUS;
598597
}
599598
if (!updateProjects.isEmpty()) {
599+
int i = 0;
600+
int n = updateProjects.size();
601+
IJavaProject[] javaProjects = new IJavaProject[n];
602+
IClasspathContainer[] container = new IClasspathContainer[n];
603+
for (Entry<IJavaProject, IClasspathContainer> entry : updateProjects.entrySet()) {
604+
javaProjects[i] = entry.getKey();
605+
container[i] = entry.getValue();
606+
i++;
607+
}
600608
try {
601-
int i = 0;
602-
int n = updateProjects.size();
603-
IJavaProject[] javaProjects = new IJavaProject[n];
604-
IClasspathContainer[] container = new IClasspathContainer[n];
605-
for (Entry<IJavaProject, IClasspathContainer> entry : updateProjects.entrySet()) {
606-
javaProjects[i] = entry.getKey();
607-
container[i] = entry.getValue();
608-
i++;
609-
}
610-
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, javaProjects, container,
611-
monitor);
609+
setProjectContainers(javaProjects, container, monitor);
612610
} catch (JavaModelException e) {
613611
return e.getStatus();
614612
}
@@ -681,36 +679,54 @@ private static boolean isUpToDate(IProject project, IClasspathEntry[] currentEnt
681679
}
682680

683681
private static void saveState(IProject project, RequiredPluginsClasspathContainer classpathContainer) {
684-
try {
685-
File stateFile = getStateFile(project);
686-
stateFile.getParentFile().mkdirs();
687-
try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(stateFile))) {
688-
PDEClasspathContainerSaveHelper.writeContainer(classpathContainer, stream);
689-
}
690-
} catch (Exception e) {
691-
// can't write then...
692-
if (PDECore.DEBUG_STATE) {
693-
System.err.println(String.format("Writing project state for %s failed!", project.getName())); //$NON-NLS-1$
694-
e.printStackTrace();
682+
synchronized (project) {
683+
try {
684+
File stateFile = getStateFile(project);
685+
stateFile.getParentFile().mkdirs();
686+
try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(stateFile))) {
687+
PDEClasspathContainerSaveHelper.writeContainer(classpathContainer, stream);
688+
}
689+
} catch (Exception e) {
690+
// can't write then...
691+
if (PDECore.DEBUG_STATE) {
692+
System.err.println(String.format("Writing project state for %s failed!", project.getName())); //$NON-NLS-1$
693+
e.printStackTrace();
694+
}
695695
}
696696
}
697697
}
698698

699699
static IClasspathContainer readState(IProject project) {
700-
try {
701-
File stateFile = getStateFile(project);
702-
try (InputStream stream = new BufferedInputStream(new FileInputStream(stateFile))) {
703-
return PDEClasspathContainerSaveHelper.readContainer(stream);
704-
}
705-
} catch (Exception e) {
706-
if (PDECore.DEBUG_STATE && !(e instanceof FileNotFoundException)) {
707-
System.err.println(String.format("Restoring project state for %s failed!", project.getName())); //$NON-NLS-1$
708-
e.printStackTrace();
700+
synchronized (project) {
701+
try {
702+
File stateFile = getStateFile(project);
703+
try (InputStream stream = new FileInputStream(stateFile)) {
704+
IClasspathContainer container = Objects
705+
.requireNonNull(PDEClasspathContainerSaveHelper.readContainer(stream));
706+
if (PDECore.DEBUG_STATE) {
707+
System.out.println(String.format("%s is restored from previous state.", project.getName())); //$NON-NLS-1$
708+
}
709+
return container;
710+
}
711+
} catch (Exception e) {
712+
if (PDECore.DEBUG_STATE) {
713+
if (e instanceof FileNotFoundException) {
714+
System.out.println(String.format("%s has no saved state!", project.getName())); //$NON-NLS-1$
715+
} else {
716+
System.err.println(String.format("Restoring project state for %s failed!", project.getName())); //$NON-NLS-1$
717+
e.printStackTrace();
718+
}
719+
}
720+
return PDEClasspathContainerSaveHelper.emptyContainer();
709721
}
710-
return null;
711722
}
712723
}
713724

725+
static void setProjectContainers(IJavaProject[] javaProjects, IClasspathContainer[] container,
726+
IProgressMonitor monitor) throws JavaModelException {
727+
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, javaProjects, container, monitor);
728+
}
729+
714730
private static File getStateFile(IProject project) {
715731
return PDECore.getDefault().getStateLocation().append("cpc").append(project.getName()) //$NON-NLS-1$
716732
.toFile();

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/RequiredPluginsInitializer.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
1010
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
13+
* Christoph Läubrich - refactor to restore from saved state
1314
*******************************************************************************/
1415
package org.eclipse.pde.internal.core;
1516

@@ -20,65 +21,27 @@
2021
import org.eclipse.core.runtime.Status;
2122
import org.eclipse.jdt.core.ClasspathContainerInitializer;
2223
import org.eclipse.jdt.core.IClasspathContainer;
23-
import org.eclipse.jdt.core.IClasspathEntry;
2424
import org.eclipse.jdt.core.IJavaProject;
2525
import org.eclipse.jdt.core.JavaCore;
2626

2727
public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
2828

29-
private static final IClasspathContainer EMPTY_CLASSPATH_CONTAINER = new IClasspathContainer() {
30-
31-
@Override
32-
public IPath getPath() {
33-
return PDECore.REQUIRED_PLUGINS_CONTAINER_PATH;
34-
}
35-
36-
@Override
37-
public int getKind() {
38-
return K_APPLICATION;
39-
}
40-
41-
@Override
42-
public String getDescription() {
43-
return PDECoreMessages.RequiredPluginsClasspathContainer_description;
44-
}
45-
46-
@Override
47-
public IClasspathEntry[] getClasspathEntries() {
48-
// nothing yet, will be updated soon
49-
return new IClasspathEntry[0];
50-
}
51-
};
52-
5329
@Override
5430
public void initialize(IPath containerPath, IJavaProject javaProject) throws CoreException {
5531
IProject project = javaProject.getProject();
5632
IClasspathContainer savedState = ClasspathComputer.readState(project);
57-
if (savedState == null) {
58-
if (PDECore.DEBUG_STATE) {
59-
System.out.println(String.format("%s has no saved state!", javaProject.getProject().getName())); //$NON-NLS-1$
60-
}
61-
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, new IJavaProject[] { javaProject },
62-
new IClasspathContainer[] { EMPTY_CLASSPATH_CONTAINER }, null);
63-
} else {
64-
if (PDECore.DEBUG_STATE) {
65-
System.out.println(
66-
String.format("%s is restored from previous state.", javaProject.getProject().getName())); //$NON-NLS-1$
67-
}
68-
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, new IJavaProject[] { javaProject },
69-
new IClasspathContainer[] { savedState }, null);
70-
}
33+
ClasspathComputer.setProjectContainers(new IJavaProject[] { javaProject },
34+
new IClasspathContainer[] { savedState }, null);
7135
// The saved state might be stale, request a classpath update here, this
7236
// will run in a background job and update the classpath if needed.
73-
ClasspathComputer.requestClasspathUpdate(project, savedState);
37+
ClasspathComputer.requestClasspathUpdate(project);
7438
}
7539

7640
@Override
7741
public Object getComparisonID(IPath containerPath, IJavaProject project) {
7842
if (containerPath == null || project == null) {
7943
return null;
8044
}
81-
8245
return containerPath.segment(0) + "/" + project.getPath().segment(0); //$NON-NLS-1$
8346
}
8447

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEClasspathContainerSaveHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ protected Object resolveObject(Object o) throws IOException {
6262
return (IClasspathContainer) is.readObject();
6363
}
6464

65+
public static IClasspathContainer emptyContainer() {
66+
return new SerializableClasspathContainer();
67+
}
68+
6569
public static void writeContainer(IClasspathContainer container, OutputStream output) throws IOException {
6670
ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(output)) {
6771
{

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/SerializableClasspathContainer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.pde.internal.core.util;
1515

1616
import java.io.Serializable;
17+
import java.util.Arrays;
1718

1819
import org.eclipse.core.runtime.IPath;
1920
import org.eclipse.jdt.core.IClasspathContainer;
@@ -26,13 +27,17 @@ class SerializableClasspathContainer implements IClasspathContainer, Serializabl
2627
private static final long serialVersionUID = 1L;
2728
private final IClasspathEntry[] entries;
2829

30+
public SerializableClasspathContainer() {
31+
this(new IClasspathEntry[0]);
32+
}
33+
2934
public SerializableClasspathContainer(IClasspathEntry[] entries) {
3035
this.entries = entries;
3136
}
3237

3338
@Override
3439
public IClasspathEntry[] getClasspathEntries() {
35-
return entries;
40+
return entries.clone();
3641
}
3742

3843
@Override
@@ -50,4 +55,27 @@ public String getDescription() {
5055
return PDECoreMessages.RequiredPluginsClasspathContainer_description;
5156
}
5257

58+
@Override
59+
public int hashCode() {
60+
final int prime = 31;
61+
int result = 1;
62+
result = prime * result + Arrays.hashCode(entries);
63+
return result;
64+
}
65+
66+
@Override
67+
public boolean equals(Object obj) {
68+
if (this == obj) {
69+
return true;
70+
}
71+
if (obj == null) {
72+
return false;
73+
}
74+
if (getClass() != obj.getClass()) {
75+
return false;
76+
}
77+
SerializableClasspathContainer other = (SerializableClasspathContainer) obj;
78+
return Arrays.equals(entries, other.entries);
79+
}
80+
5381
}

0 commit comments

Comments
 (0)