Skip to content

Commit

Permalink
Fixed things from review
Browse files Browse the repository at this point in the history
- javaProjects as List instead of Array
- Copyright information updated / inserted
- Fixed multithreading bug
- removed xsiframe from gwt.xml file
- replaced mkdirs() with Files.createDirectories()
- Use UTF-8 instead of defaultCharset
-Use Files.writeString() instead of BufferedWriter
  • Loading branch information
keinhaar committed Oct 27, 2024
1 parent ddb8276 commit e946628
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.List;

/**
* Creates web app projects populated with files relevant to the selected natures.
Expand Down Expand Up @@ -96,11 +97,10 @@ void create(IProgressMonitor monitor) throws CoreException, MalformedURLExceptio
void setBuildMaven(boolean buildMaven);

/**
* Returns the created Java project. This is available half way through the creation process.
*
* @return the java projeect.
* Returns the created Java projects. This is available half way through the creation process.
* @return the java project.
*/
IJavaProject[] getCreatedJavaProjects();
List<IJavaProject> getCreatedJavaProjects();

/**
* Returns build a Maven Project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;

import java.util.List;

public class MavenEnablingWebAppCreatorParicipant implements IWebAppProjectCreator.Participant {

private IJavaProject[] javaProjects;
private List<IJavaProject> javaProjects;

@Override
public void updateWebAppProjectCreator(IWebAppProjectCreator webAppProjectCreator) {
Expand All @@ -42,9 +44,9 @@ private void runJob() {
protected IStatus run(IProgressMonitor monitor) {
// Turn on the Maven nature
try {
for(int i=0;i<javaProjects.length;i++)
for(int i=0;i<javaProjects.size();i++)
{
NatureUtils.addNature(javaProjects[i].getProject(), MavenUtils.MAVEN2_NATURE_ID);
NatureUtils.addNature(javaProjects.get(i).getProject(), MavenUtils.MAVEN2_NATURE_ID);
}
} catch (CoreException e1) {
e1.printStackTrace();
Expand All @@ -56,9 +58,9 @@ protected IStatus run(IProgressMonitor monitor) {
// Maven update project will add the Maven dependencies to the classpath
IProjectConfigurationManager projectConfig = MavenPlugin.getProjectConfigurationManager();
try {
for(int i=0;i<javaProjects.length;i++)
for(int i=0;i<javaProjects.size();i++)
{
projectConfig.updateProjectConfiguration(javaProjects[i].getProject(), monitor);
projectConfig.updateProjectConfiguration(javaProjects.get(i).getProject(), monitor);
}
} catch (CoreException e) {
// TODO(${user}): Auto-generated catch block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
eclipse.preferences.version=1
encoding//project_templates/client_server_shared/client/.settings/org.eclipse.jdt.core.prefs=UTF-8
encoding//project_templates/client_server_shared/server/.settings/org.eclipse.jdt.core.prefs=UTF-8
encoding//project_templates/client_server_shared/shared/.settings/org.eclipse.jdt.core.prefs=UTF-8
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@

<!-- Specify the paths for translatable code -->
<source path='ui'/>

<!-- allow Super Dev Mode -->
<add-linker name="xsiframe"/>
</module>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2011 Google Inc. All Rights Reserved.
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -45,6 +45,12 @@
public class NewWebAppTemplateProjectWizard extends NewElementWizard implements INewWizard {

private NewWebAppTemplateProjectWizardPage newProjectWizardPage;
private String projectName;
private boolean useGWT;
private IPath gwtSdkContainerPath;
private String packageName;
private URI locationURI;
private ProjectTemplate template;

public NewWebAppTemplateProjectWizard() {
}
Expand Down Expand Up @@ -74,6 +80,12 @@ public void init(IWorkbench workbench, IStructuredSelection selection) {
*/
@Override
public boolean performFinish() {
projectName = newProjectWizardPage.getProjectName();
useGWT = newProjectWizardPage.useGWT();
gwtSdkContainerPath = newProjectWizardPage.getGWTSdkContainerPath();
packageName = newProjectWizardPage.getPackage();
locationURI = newProjectWizardPage.getCreationLocationURI();
template = newProjectWizardPage.getTemplate();

/**
* HACK: We need to make sure that the DebugUITools plugin (and the DebugUIPlugin plugin) is loaded via the main
Expand All @@ -96,13 +108,7 @@ public boolean performFinish() {
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
try {
String projectName = newProjectWizardPage.getProjectName();
boolean useGWT = newProjectWizardPage.useGWT();
IPath gwtSdkContainerPath = newProjectWizardPage.getGWTSdkContainerPath();
String packageName = newProjectWizardPage.getPackage();
URI locationURI = newProjectWizardPage.getCreationLocationURI();

WebAppTemplateProjectCreator wapc = new WebAppTemplateProjectCreator(newProjectWizardPage.getTemplate());
WebAppTemplateProjectCreator wapc = new WebAppTemplateProjectCreator(template);
wapc.setProjectName(projectName);
wapc.setPackageName(packageName);
wapc.setLocationURI(locationURI);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2011 Google Inc. All Rights Reserved.
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
/**
/*******************************************************************************
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
*/
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.google.gdt.eclipse.suite.wizards;

import org.apache.commons.io.FileUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2011 Google Inc. All Rights Reserved.
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -77,6 +77,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -654,8 +655,10 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
* Return the Java project created. This will only work half way through the process.
*/
@Override
public IJavaProject[] getCreatedJavaProjects() {
return new IJavaProject[] {createdJavaProject};
public List<IJavaProject> getCreatedJavaProjects() {
List<IJavaProject> list = new ArrayList<>();
list.add(createdJavaProject);
return Collections.unmodifiableList(list);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2011 Google Inc. All Rights Reserved.
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -51,16 +51,18 @@
import org.eclipse.jdt.ui.PreferenceConstants;
import org.osgi.service.prefs.BackingStoreException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

Expand Down Expand Up @@ -112,7 +114,7 @@ private static void reformatJavaFiles(File outDir) throws CoreException {
private URI locationURI;
private String packageBaseName;
private String projectBaseName;
private IJavaProject[] createdJavaProjects;
private List<IJavaProject> createdJavaProjects;
private IProgressMonitor monitor;
private Sdk gwtSdk;
private ProjectTemplate projectTemplate;
Expand Down Expand Up @@ -149,7 +151,7 @@ public void create(IProgressMonitor monitor)

List<String> projectNames = projectTemplate.getProjectNames(projectBaseName);
IProject[] project = new IProject[projectNames.size()];
createdJavaProjects = new IJavaProject[projectNames.size()];
createdJavaProjects = new ArrayList<>();
for(int i=0;i<projectNames.size();i++)
{
project[i] = createProject(monitor, projectNames.get(i));
Expand All @@ -168,7 +170,7 @@ public void create(IProgressMonitor monitor)
NatureUtils.addNatures(project[i], projectTemplate.getNatureIds(i));

// Create the java project
createdJavaProjects[i] = JavaCore.create(project[i]);
createdJavaProjects.add( JavaCore.create(project[i]) );

// Create a source folder and add it to the raw classpath
IResource warFolder = project[i].findMember(WebAppUtilities.DEFAULT_WAR_DIR_NAME);
Expand All @@ -179,15 +181,15 @@ public void create(IProgressMonitor monitor)
WebAppUtilities.setDefaultWarSettings(project[i]);

// Set the default output directory
WebAppUtilities.setOutputLocationToWebInfClasses(createdJavaProjects[i], monitor);
WebAppUtilities.setOutputLocationToWebInfClasses(createdJavaProjects.get(i), monitor);
/**
* Copy files into the web-inf lib folder. This code assumes that it is running in a context that has a workspace
* lock.
*/
Sdk gwtSdk = getGWTSdk();
setGwtSdk(gwtSdk);
if (gwtSdk != null) {
new GWTUpdateWebInfFolderCommand(createdJavaProjects[i], gwtSdk).execute();
new GWTUpdateWebInfFolderCommand(createdJavaProjects.get(i), gwtSdk).execute();
}
}
reformatJavaFiles(dir);
Expand All @@ -214,17 +216,25 @@ private void replacePackageNames(IProject iProject, File dir) throws IOException
String name = file.getName();
name = name.replace("_PACKAGENAME_", packageBaseName.replace('.', File.separatorChar));
File newFile = new File(file.getParentFile(), name);
newFile.getParentFile().mkdirs();
file.renameTo(newFile);
Files.createDirectories(newFile.getParentFile().toPath());
boolean ret = file.renameTo(newFile);
if(ret == false)
{
throw new IOException("Could not rename");
}
file = newFile;
}
if(file.getName().contains("_.._"))
{
String name = file.getName();
name = name.replace("_.._", "../");
File newFile = new File(file.getParentFile(), name);
newFile.getParentFile().mkdirs();
file.renameTo(newFile);
Files.createDirectories(newFile.getParentFile().toPath());
boolean ret = file.renameTo(newFile);
if(ret == false)
{
throw new IOException("Could not rename");
}
file = newFile;
}
if(file.isDirectory())
Expand All @@ -248,7 +258,7 @@ private void replacePackageNames(IProject iProject, File dir) throws IOException
* @throws IOException
*/
public static void replaceInFile(String file, String pattern, String replacement) throws IOException {
Charset cset = Charset.defaultCharset();
Charset cset = StandardCharsets.UTF_8;
java.nio.file.Path path = Paths.get(file);
if (Files.exists(path)) {
if (Files.size(path) > 1000000) {
Expand All @@ -257,9 +267,7 @@ public static void replaceInFile(String file, String pattern, String replacement
byte[] data = Files.readAllBytes(path);
String str = new String(data, cset);
String newStr = str.replaceAll(pattern, replacement);
BufferedWriter writer = Files.newBufferedWriter(path, cset);
writer.write(newStr);
writer.close();
Files.writeString(path, newStr, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
}
}

Expand Down Expand Up @@ -407,8 +415,8 @@ private Sdk getSdk(String containerId, SdkManager<? extends Sdk> sdkManager) {
* Return the Java project created. This will only work half way through the process.
*/
@Override
public IJavaProject[] getCreatedJavaProjects() {
return createdJavaProjects;
public List<IJavaProject> getCreatedJavaProjects() {
return Collections.unmodifiableList(createdJavaProjects);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.gwt.eclipse.wtp.projects;

import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -19,7 +21,7 @@

public class WtpFacetCreatorParicipant implements IWebAppProjectCreator.Participant {

private IJavaProject[] javaProjects;
private List<IJavaProject> javaProjects;
private IFacetedProjectListener projectFacetListener;

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<properties>
<tycho.version>3.0.1</tycho.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<eclipse.target>2023-09</eclipse.target> <!-- TODO change to neon -->
<eclipse.target>2023-09</eclipse.target>

<!-- Repositories that Tycho will use to resolve compilation dependencies. -->
<eclipse-repo.url>https://download.eclipse.org/releases/${eclipse.target}</eclipse-repo.url>
Expand Down

0 comments on commit e946628

Please sign in to comment.