createMethodFromText question

what exactly comprises the necessary text? the method signature? signature plus open bracket + body plus close bracket? What is this method looking for?


Thanks!

3 comments

Hello softwarevisualization,

what exactly comprises the necessary text? the method signature?
signature plus open bracket + body plus close bracket? What is this
method looking for?


You need to provide the complete text of a method - signature plus brackets
plus body, if you need one.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I'm having trouble understanding the mapping between the various createXXXFromText methods and what they generate and what they require.

For instance, I want to generate a simple statement in a method let's say

System.out.println("hello, world" + counter);

So, a method that did not have this statement, will come to have it after my plugin's action has been invoked.

At first I thought this might be a method, and I should use createMethodFromText but then I thought that's probably for creating method definitions.

So then I thought to use createExpressionFromText. I take this to mean -"create a valid Java statement equivalent to the String I pass in.

But this I get this

com.intellij.util.IncorrectOperationException: Incorrect expression "System.out.println( \"hello world \" );"

I am trying to write System.out.println("hello world");
in a method.

What am I doing wrong? The String I am using is
]]>
String expression = "System.out.println(
\"hello world
\" );";
</snip>

I have tried every combo of escape character, + and " that I think might have somer reasonable interpretation.Now I am thinking there's something more than just Reason and Logic that's at issue here.

0

Hello softwarevisualization,

I'm having trouble understanding the mapping between the various
createXXXFromText methods and what they generate and what they
require.

For instance, I want to generate a simple statement in a method let's
say

System.out.println("hello, world" + counter);

So, a method that did not have this statement, will come to have it
after my plugin's action has been invoked.

At first I thought this might be a method, and I should use
createMethodFromText but then I thought that's probably for creating
method definitions.

So then I thought to use createExpressionFromText. I take this to mean
-"create a valid Java statement equivalent to the String I pass in.

But this I get this

com.intellij.util.IncorrectOperationException: Incorrect expression
"System.out.println( \"hello world \" );"

I am trying to write System.out.println("hello world"); in a method.

What am I doing wrong? The String I am using is
<snip>
String expression = "System.out.println(
\"hello
world
\" );";
</snip>
I have tried every combo of escape character, + and " that I think
might have somer reasonable interpretation.Now I am thinking there's
something more than just Reason and Logic that's at issue here.


No escaping is necessary, besides the one normally required by Java literals.
Your code fails because the language construct you're trying to create is
a statement rather than an expression. Thus, you need to use createStatementFromText()
rather than createExpressionFromText().

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.