Skip to content

Commit 317bf57

Browse files
authored
Fixed ClassCastException when adding module file (#453)
Fixed ClassCastException when adding module file
1 parent b20f75a commit 317bf57

5 files changed

Lines changed: 133 additions & 143 deletions

File tree

plugins/com.gwtplugins.gwt.eclipse.core/src/com/google/gwt/eclipse/core/dialogs/ModuleSelectionDialog.java

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ModuleSelectionDialog extends FilteredItemsSelectionDialog {
5555

5656
/**
5757
* A label provider for details of IModule objects.
58-
*
58+
*
5959
* This code was adapted from the
6060
* FilteredTypesSelectionDialog.TypeItemDetailsLabelProvider class.
6161
*/
@@ -70,7 +70,7 @@ public Image getImage(Object element) {
7070
Image image = null;
7171

7272
IModule module = (IModule) element;
73-
if (!module.isBinary()) {
73+
if (module instanceof ModuleFile) {
7474
ModuleFile moduleFile = (ModuleFile) module;
7575
IFile file = moduleFile.getFile();
7676
IPackageFragment pkFrag;
@@ -101,7 +101,7 @@ public String getText(Object element) {
101101
IModule module = (IModule) element;
102102
String packageName = module.getPackageName();
103103

104-
if (!module.isBinary()) {
104+
if (module instanceof ModuleFile) {
105105
ModuleFile moduleFile = (ModuleFile) module;
106106
IFile file = moduleFile.getFile();
107107
String modulePath = file.getFullPath().makeRelative().toString();
@@ -119,7 +119,7 @@ public String getText(Object element) {
119119

120120
/**
121121
* A label provider for IModule objects.
122-
*
122+
*
123123
* This code was adapted from the
124124
* FilteredTypesSelectionDialog.TypeItemLabelProvider class.
125125
*/
@@ -279,7 +279,7 @@ private boolean matchesPackage(IModule module) {
279279

280280
/**
281281
* Extends functionality of SearchPatterns.
282-
*
282+
*
283283
* This code was adapted from the
284284
* FilteredTypesSelectionDialog.TypeSearchPattern class.
285285
*/
@@ -341,12 +341,12 @@ private String evaluatePackagePattern(String s) {
341341

342342
/**
343343
* Create a new ModuleSelectionDialog and display it.
344-
*
344+
*
345345
* @param shell The current display shell
346346
* @param project The Java project that should be searched for GWT Modules
347347
* @param showModulesInJars true if modules that are located in jars on the
348348
* project's classpath should be displayed
349-
*
349+
*
350350
* @return the IModule corresponding to the module chosen from the list by
351351
* pressing the OK button, null if CANCEL was pressed.
352352
*/
@@ -366,25 +366,25 @@ public static IModule show(Shell shell, IJavaProject project,
366366

367367
/**
368368
* Helper method to return the absolute workspace path of a GWT Module.
369-
*
369+
*
370370
* If the module file is located in a JAR, then the absolute path of the JAR
371371
* on the file system is returned.
372372
*/
373373
private static IPath getPathForModule(IModule module) {
374374

375-
if (module == null) {
376-
return null;
377-
}
378-
379-
if (!module.isBinary()) {
375+
if (module instanceof ModuleFile) {
380376
ModuleFile moduleFile = (ModuleFile) module;
381377
IFile file = moduleFile.getFile();
382378
return file.getFullPath();
383379
}
384-
385-
ModuleJarResource moduleJarResource = (ModuleJarResource) module;
386-
IJarEntryResource jarEntryResource = moduleJarResource.getJarEntryResource();
387-
return jarEntryResource.getPackageFragmentRoot().getPath();
380+
else if(module instanceof ModuleJarResource) {
381+
ModuleJarResource moduleJarResource = (ModuleJarResource) module;
382+
IJarEntryResource jarEntryResource = moduleJarResource.getJarEntryResource();
383+
return jarEntryResource.getPackageFragmentRoot().getPath();
384+
}
385+
else {
386+
return null;
387+
}
388388
}
389389

390390
private IJavaProject javaProject;
@@ -448,40 +448,36 @@ protected IDialogSettings getDialogSettings() {
448448
}
449449

450450
@Override
451-
protected Comparator<?> getItemsComparator() {
452-
return new Comparator<Object>() {
453-
public int compare(Object o1, Object o2) {
454-
Collator collator = Collator.getInstance();
455-
IModule module1 = (IModule) o1;
456-
IModule module2 = (IModule) o2;
457-
458-
// Compare module names
451+
protected Comparator<IModule> getItemsComparator() {
452+
return (IModule module1, IModule module2) -> {
453+
Collator collator = Collator.getInstance();
459454

460-
String s1 = module1.getSimpleName();
461-
String s2 = module2.getSimpleName();
455+
// Compare module names
462456

463-
int comparability = collator.compare(s1, s2);
457+
String s1 = module1.getSimpleName();
458+
String s2 = module2.getSimpleName();
464459

465-
// If module names are identical, then compare
466-
// fully-qualified module names
460+
int comparability = collator.compare(s1, s2);
467461

468-
if (comparability == 0) {
469-
s1 = module1.getQualifiedName();
470-
s2 = module2.getQualifiedName();
471-
comparability = collator.compare(s1, s2);
472-
}
462+
// If module names are identical, then compare
463+
// fully-qualified module names
473464

474-
// If fully-qualified module names are identical, then
475-
// compare file paths.
465+
if (comparability == 0) {
466+
s1 = module1.getQualifiedName();
467+
s2 = module2.getQualifiedName();
468+
comparability = collator.compare(s1, s2);
469+
}
476470

477-
if (comparability == 0) {
478-
s1 = getPathForModule(module1).toString();
479-
s2 = getPathForModule(module2).toString();
480-
comparability = collator.compare(s1, s2);
481-
}
471+
// If fully-qualified module names are identical, then
472+
// compare file paths.
482473

483-
return comparability;
474+
if (comparability == 0) {
475+
s1 = getPathForModule(module1).toString();
476+
s2 = getPathForModule(module2).toString();
477+
comparability = collator.compare(s1, s2);
484478
}
479+
480+
return comparability;
485481
};
486482
}
487483

plugins/com.gwtplugins.gwt.eclipse.core/src/com/google/gwt/eclipse/core/modules/AbstractModule.java

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
*******************************************************************************/
1515
package com.google.gwt.eclipse.core.modules;
1616

17-
import com.google.gdt.eclipse.core.properties.WebAppProjectProperties;
1817
import com.google.gwt.eclipse.core.GWTPluginLog;
19-
import com.google.gwt.eclipse.core.util.Util;
2018

21-
import org.eclipse.core.resources.IFile;
22-
import org.eclipse.core.resources.IFolder;
23-
import org.eclipse.core.resources.IStorage;
2419
import org.eclipse.core.runtime.CoreException;
2520
import org.eclipse.core.runtime.IPath;
2621
import org.eclipse.core.runtime.Path;
@@ -124,15 +119,6 @@ private static List<String> getElementsAttributes(Document doc, String elementNa
124119
return attrValues;
125120
}
126121

127-
protected final IStorage storage;
128-
129-
private String qualifiedName;
130-
131-
protected AbstractModule(IStorage storage) {
132-
assert (storage != null);
133-
this.storage = storage;
134-
}
135-
136122
/**
137123
* Two modules are considered equal iff their qualified names are the same.
138124
*
@@ -262,60 +248,6 @@ public void readModel(IDOMModel model) {
262248
return ret;
263249
}
264250

265-
/**
266-
* Returns the package name for the module.
267-
*/
268-
@Override
269-
public String getQualifiedName() {
270-
// Cache the qualified name
271-
if (qualifiedName == null) {
272-
String shortName = getShortName();
273-
if (shortName != null && !shortName.isEmpty()) {
274-
qualifiedName = getGWtMaven2ModuleName();
275-
return qualifiedName;
276-
}
277-
278-
qualifiedName = Util.removeFileExtension(storage.getName());
279-
280-
String modulePckg = doGetPackageName();
281-
if (modulePckg != null) {
282-
qualifiedName = modulePckg + "." + qualifiedName;
283-
}
284-
}
285-
286-
return qualifiedName;
287-
}
288-
289-
/**
290-
* GWT Maven project will have a short name
291-
* @return the short name for module
292-
*/
293-
private String getShortName() {
294-
IFile file = (IFile) storage;
295-
IFolder moduleFolder = (IFolder) file.getParent();
296-
String shortName = WebAppProjectProperties.getGwtMavenModuleShortName(moduleFolder.getProject());
297-
return shortName;
298-
}
299-
300-
/**
301-
* Get the gwt maven2 module name
302-
* @return module name
303-
*/
304-
private String getModuleNameGwtMaven2() {
305-
IFile file = (IFile) storage;
306-
IFolder moduleFolder = (IFolder) file.getParent();
307-
String moduleName = WebAppProjectProperties.getGwtMavenModuleName(moduleFolder.getProject());
308-
moduleName = moduleName.replaceAll(".*\\.(.*)", "$1");
309-
return moduleName;
310-
}
311-
312-
private String getGWtMaven2ModuleName() {
313-
IFile file = (IFile) storage;
314-
IFolder moduleFolder = (IFolder) file.getParent();
315-
String moduleName = WebAppProjectProperties.getGwtMavenModuleName(moduleFolder.getProject());
316-
return moduleName;
317-
}
318-
319251
/**
320252
* Returns a list of the <set-configuration-property name="propertyName" value="[return value]"/>.
321253
*
@@ -372,7 +304,4 @@ public int hashCode() {
372304
}
373305

374306
protected abstract IDOMModel doGetModelForRead() throws IOException, CoreException;
375-
376-
protected abstract String doGetPackageName();
377-
378307
}

plugins/com.gwtplugins.gwt.eclipse.core/src/com/google/gwt/eclipse/core/modules/ModuleFile.java

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ private IDOMModel getModelForEdit() throws IOException, CoreException {
8585
}
8686
}
8787

88+
private String qualifiedName = null;
89+
private final IFile manifestFile;
90+
8891
protected ModuleFile(IFile file) {
89-
super(file);
92+
this.manifestFile = file;
9093
}
9194

9295
/**
@@ -114,14 +117,57 @@ public void editModel(IDOMModel model) {
114117
}.run();
115118
}
116119

120+
/**
121+
* Returns the package name for the module.
122+
*/
123+
@Override
124+
public String getQualifiedName() {
125+
// Cache the qualified name
126+
if (qualifiedName == null) {
127+
String shortName = getShortName();
128+
if (shortName != null && !shortName.isEmpty()) {
129+
qualifiedName = getGwtMaven2ModuleName();
130+
return qualifiedName;
131+
}
132+
133+
qualifiedName = Util.removeFileExtension(manifestFile.getName());
134+
135+
String modulePckg = doGetPackageName();
136+
if (modulePckg != null) {
137+
qualifiedName = modulePckg + "." + qualifiedName;
138+
}
139+
}
140+
141+
return qualifiedName;
142+
}
143+
144+
/**
145+
* GWT Maven project will have a short name
146+
* @return the short name for module
147+
*/
148+
private String getShortName() {
149+
IFolder moduleFolder = (IFolder) manifestFile.getParent();
150+
String shortName = WebAppProjectProperties.getGwtMavenModuleShortName(moduleFolder.getProject());
151+
return shortName;
152+
}
153+
154+
/**
155+
* Get the gwt maven2 module name
156+
* @return module name
157+
*/
158+
private final String getGwtMaven2ModuleName() {
159+
IFolder moduleFolder = (IFolder) manifestFile.getParent();
160+
String moduleName = WebAppProjectProperties.getGwtMavenModuleName(moduleFolder.getProject());
161+
return moduleName;
162+
}
163+
117164
/**
118165
* Returns the backing IFile for the module XML file.
119166
*
120167
* @return IFile referencing the module XML
121168
*/
122169
public IFile getFile() {
123-
// We received storage as an IFile in our ctor, so we know this cast works
124-
return (IFile) storage;
170+
return manifestFile;
125171
}
126172

127173
/**
@@ -134,7 +180,7 @@ public IFile getFile() {
134180
* @return IFolder corresponding to the path
135181
*/
136182
public IFolder getFolder(IPath moduleRelativePath) {
137-
IPath moduleFolderPath = storage.getFullPath().removeLastSegments(1);
183+
IPath moduleFolderPath = manifestFile.getFullPath().removeLastSegments(1);
138184
IPath folderPath = moduleFolderPath.append(moduleRelativePath);
139185
IResource folder = Util.getWorkspaceRoot().findMember(folderPath);
140186
return (IFolder) folder;
@@ -233,8 +279,7 @@ protected IDOMModel doGetModelForRead() throws IOException, CoreException {
233279
return (IDOMModel) modelManager.getModelForRead(getFile());
234280
}
235281

236-
@Override
237-
protected String doGetPackageName() {
282+
private final String doGetPackageName() {
238283
IFolder moduleFolder = (IFolder) getFile().getParent();
239284
IJavaElement javaElement = JavaCore.create(moduleFolder);
240285

@@ -256,7 +301,6 @@ protected String doGetPackageName() {
256301
} else {
257302
// TODO: handle super-source case here
258303
}
259-
260304
return "";
261305
}
262306

0 commit comments

Comments
 (0)