Community
Participate
Working Groups
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;
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?
Do we want the method creation to fail or not?
Yes, I think the instanceof check is acceptable. We should also fail with an INVALID_CONTENTS JavaModelException (as specified by IType.createMethod(...)).
I'm supsecting that createField(...) and createType(...) will have the same problem.
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(...).
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.
Java model test added in the CreateMemberTests. See test testCreateMethodSyntaxError2().
Fixed and released in HEAD.
Fix is fine. Thanks.