Bug 9009 - ClassCastException creating an invalid method
Summary: ClassCastException creating an invalid method
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P1 normal (vote)
Target Milestone: 2.0 M3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-02-03 14:07 EST by Jerome Lanneluc CLA
Modified: 2002-02-06 11:59 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jerome Lanneluc CLA 2002-02-03 14:07:53 EST
Build 20020129

Calling IType.createMethod(...) with the following content (notice the missing 
parenthesis):
"public void foo {\n" +
"  X x = new X();\n" +
"}"

results in a ClassCastException:
  java/lang/ClassCastException.<init>(Ljava/lang/String;)V
   
org/eclipse/jdt/internal/core/CreateMethodOperation.convertDOMMethodTypesToSigna
tures()[Ljava/lang/String;
   org/eclipse/jdt/internal/core/CreateMethodOperation.generateResultHandle()
Lorg/eclipse/jdt/core/IJavaElement;
   
org/eclipse/jdt/internal/core/CreateElementInCUOperation.generateResultHandles()
[Lorg/eclipse/jdt/core/IJavaElement;
   org/eclipse/jdt/internal/core/CreateElementInCUOperation.executeOperation()V
   org/eclipse/jdt/internal/core/JavaModelOperation.execute()V
   org/eclipse/jdt/internal/core/JavaModelOperation.run
(Lorg/eclipse/core/runtime/IProgressMonitor;)V
   org/eclipse/core/internal/resources/Workspace.run
(Lorg/eclipse/core/resources/IWorkspaceRunnable;Lorg/eclipse/core/runtime/IProgr
essMonitor;)V
   org/eclipse/jdt/internal/core/JavaModelManager.runOperation
(Lorg/eclipse/jdt/internal/core/JavaModelOperation;Lorg/eclipse/core/runtime/IPr
ogressMonitor;)V
   org/eclipse/jdt/internal/core/JavaElement.runOperation
(Lorg/eclipse/jdt/internal/core/JavaModelOperation;Lorg/eclipse/core/runtime/IPr
ogressMonitor;)V
   org/eclipse/jdt/internal/core/SourceType.createMethod
(Ljava/lang/String;Lorg/eclipse/jdt/core/IJavaElement;ZLorg/eclipse/core/runtime
/IProgressMonitor;)Lorg/eclipse/jdt/core/IMethod;
Comment 1 Olivier Thomann CLA 2002-02-04 12:26:59 EST
The DOMNode used underneath is a DOMField which cannot be cast to a DOMMethod. I don't see another 
way to fix this problem than adding a instanceof check before doing the casting. Does this sound 
acceptable?
Comment 2 Olivier Thomann CLA 2002-02-04 12:32:34 EST
Do we want the method creation to fail or not?
Comment 3 Jerome Lanneluc CLA 2002-02-04 12:37:07 EST
Yes, I think the instanceof check is acceptable. We should also fail with an 
INVALID_CONTENTS JavaModelException (as specified by IType.createMethod(...)).
Comment 4 Jerome Lanneluc CLA 2002-02-04 12:39:28 EST
I'm supsecting that createField(...) and createType(...) will have the same 
problem.
Comment 5 Olivier Thomann CLA 2002-02-04 12:56:49 EST
I changed protected IDOMNode generateElementDOM() throws JavaModelException {...}
of 
CreateMethodOperation to return null if the fDOMNode is not an instance of IDOMMethod.
This 
fixed the problem. Is this fine for you?
I will check createField(...) and createType(...).
Comment 6 Olivier Thomann CLA 2002-02-04 13:05:05 EST
I added extra checks after the call to generateSyntaxIncorrectDOM(). This call is very 
dangerous. We never checked that what is returned by this method could be used in the specific 
operation.
Comment 7 Olivier Thomann CLA 2002-02-04 13:07:38 EST
Java model test added in the CreateMemberTests. See test testCreateMethodSyntaxError2().
Comment 8 Olivier Thomann CLA 2002-02-04 13:09:00 EST
Fixed and released in HEAD.
Comment 9 Jerome Lanneluc CLA 2002-02-06 11:59:48 EST
Fix is fine. Thanks.