Skip to content

Commit

Permalink
[eclipse] Avoid NPE when creating a new empty project.
Browse files Browse the repository at this point in the history
see #991

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 30, 2020
1 parent c720308 commit e68f45a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
Expand Up @@ -279,9 +279,12 @@ public static void configureSARLProject(IProject project, boolean addNatures,
outputFolder, testOutputFolder);

// SARL specific configuration
final IFolder testGenerationFolderFolder = testGenerationFolder.get();
final IPath testGenerationFolderPath = testGenerationFolderFolder == null ? null
: testGenerationFolderFolder.getProjectRelativePath();
SARLPreferences.setSpecificSARLConfigurationFor(project,
generationFolder.get().getProjectRelativePath(),
testGenerationFolder.get().getProjectRelativePath());
testGenerationFolderPath);
subMonitor.worked(1);

// Create the Java project
Expand Down Expand Up @@ -338,27 +341,50 @@ private static void ensureSourceFolders(IProject project, boolean createFolders,
final IFolder testOutputFolder = ensureOutputFolder(project,
SARLConfig.FOLDER_TEST_BIN, true, createFolders, monitor.newChild(1));
if (sourcePaths != null) {
sourcePaths.set(new IFolder[] {sourceSarlFolder, sourceJavaFolder, resourcesFolder});
assert sourceSarlFolder != null : "sourceSarlFolder must not be null";
if (sourceJavaFolder != null) {
if (resourcesFolder != null) {
sourcePaths.set(new IFolder[] {sourceSarlFolder, sourceJavaFolder, resourcesFolder});
} else {
sourcePaths.set(new IFolder[] {sourceSarlFolder, sourceJavaFolder});
}
} else if (resourcesFolder != null) {
sourcePaths.set(new IFolder[] {sourceSarlFolder, resourcesFolder});
} else {
sourcePaths.set(new IFolder[] {sourceSarlFolder});
}
}
if (testSourcePaths != null) {
testSourcePaths.set(new IFolder[] {testSourceSarlFolder});
if (testSourceSarlFolder != null) {
testSourcePaths.set(new IFolder[] {testSourceSarlFolder});
} else {
testSourcePaths.set(new IFolder[] {});
}
}
if (generationPaths != null) {
assert generationFolder != null : "generationFolder must not be null";
generationPaths.set(new IFolder[] {generationFolder});
}
if (testGenerationPaths != null) {
testGenerationPaths.set(new IFolder[] {testGenerationFolder});
if (testGenerationFolder != null) {
testGenerationPaths.set(new IFolder[] {testGenerationFolder});
} else {
testGenerationPaths.set(new IFolder[] {});
}
}
if (standardGenerationFolder != null) {
assert generationFolder != null : "generationFolder must not be null";
standardGenerationFolder.set(generationFolder);
}
if (testingGenerationFolder != null) {
if (testingGenerationFolder != null && testGenerationFolder != null) {
testingGenerationFolder.set(testGenerationFolder);
}
if (classOutput != null) {
assert outputFolder != null : "outputFolder must not be null";
classOutput.set(outputFolder);
}
if (testClassOutput != null) {
assert testOutputFolder != null : "testOutputFolder must not be null";
testClassOutput.set(testOutputFolder);
}
}
Expand Down
Expand Up @@ -290,37 +290,40 @@ public void run() {
* @param compilerCompliance the Java version that is supported by the project.
*/
protected void createDefaultMavenPom(IJavaProject project, String compilerCompliance) {
// Get the template resource.
final URL templateUrl = getPomTemplateLocation();
if (templateUrl != null) {
final String compliance = Strings.isNullOrEmpty(compilerCompliance)
? SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH : compilerCompliance;
final String groupId = getDefaultMavenGroupId();
// Read the template and do string replacement.
final StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(templateUrl.openStream()))) {
String line = reader.readLine();
while (line != null) {
line = line.replaceAll(Pattern.quote("@GROUP_ID@"), groupId); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@PROJECT_NAME@"), project.getElementName()); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@PROJECT_VERSION@"), DEFAULT_MAVEN_PROJECT_VERSION); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@SARL_VERSION@"), SARLVersion.SARL_RELEASE_VERSION_MAVEN); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@JAVA_VERSION@"), compliance); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@FILE_ENCODING@"), Charset.defaultCharset().displayName()); //$NON-NLS-1$
content.append(line).append("\n"); //$NON-NLS-1$
line = reader.readLine();
final IFile pomFile = project.getProject().getFile("pom.xml"); //$NON-NLS-1$
// Do not create the pom if already present.
if (!pomFile.exists()) {
// Get the template resource.
final URL templateUrl = getPomTemplateLocation();
if (templateUrl != null) {
final String compliance = Strings.isNullOrEmpty(compilerCompliance)
? SARLVersion.MINIMAL_JDK_VERSION_IN_SARL_PROJECT_CLASSPATH : compilerCompliance;
final String groupId = getDefaultMavenGroupId();
// Read the template and do string replacement.
final StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(templateUrl.openStream()))) {
String line = reader.readLine();
while (line != null) {
line = line.replaceAll(Pattern.quote("@GROUP_ID@"), groupId); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@PROJECT_NAME@"), project.getElementName()); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@PROJECT_VERSION@"), DEFAULT_MAVEN_PROJECT_VERSION); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@SARL_VERSION@"), SARLVersion.SARL_RELEASE_VERSION_MAVEN); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@JAVA_VERSION@"), compliance); //$NON-NLS-1$
line = line.replaceAll(Pattern.quote("@FILE_ENCODING@"), Charset.defaultCharset().displayName()); //$NON-NLS-1$
content.append(line).append("\n"); //$NON-NLS-1$
line = reader.readLine();
}
} catch (IOException exception) {
throw new RuntimeIOException(exception);
}
// Write the pom
try (StringInputStream is = new StringInputStream(content.toString())) {
pomFile.create(is, true, new NullProgressMonitor());
} catch (CoreException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeIOException(exception);
}
} catch (IOException exception) {
throw new RuntimeIOException(exception);
}
// Write the pom
final IFile pomFile = project.getProject().getFile("pom.xml"); //$NON-NLS-1$
try (StringInputStream is = new StringInputStream(content.toString())) {
pomFile.create(is, true, new NullProgressMonitor());
} catch (CoreException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeIOException(exception);
}
}
}
Expand Down

0 comments on commit e68f45a

Please sign in to comment.