Get return statements in java method

How can I get all possible method return values ​​in java?

Example:

Object onEvent() { if (condition) { return "a"; } if (condition2) { return "b"; } if (condition3) { return "c"; } } 

I need something like this:

 String[] returns = Utils.getReturnStatements("object.onEvent()"); returns = ["a", "b", "c"] 
+4
source share
7 answers

If you only need to analyze simple methods that return constant values, such as the one in your example, then you can do this relatively easily through static analysis using ASM or similar bytecode tools.

For methods that match the structure of your example (i.e., only direct return constants), you just need to look for a pattern

 LDC ??? ARETURN 

And collect the constants loaded using LDC. That would be very simple.

If the methods can be more complex, for example, if they return the values ​​assigned to the variables, then you will need to perform a flow analysis. This is much more, but ASM provides support.

If the methods you analyze return values ​​other than simple constants, then it will be incredibly difficult / impossible to do with static analysis.

+4
source

You can only get the signature of the method, which in this case will be Object as a return type.

To get more detailed information, you need to either statically analyze the source code or return a type like enum .

+5
source

You cannot do such things in Java, but you can do something like this:

 Object onEvent() { List<String> list = new ArrayList<String>(); if (condition) { list.add("a"); } if (condition2) { list.add("b"); } if (condition3) { list.add("c"); } return list.toArray(); } 

And then:

 String[] returns = (String[])MyObj.onEvent(); 
+1
source

As @Johan said this is not possible. The only way if you really need it is to store these possible results in Map by matching the method name with a List or array, or, better, use an enumerate if possible.

Edit:

After reading your comment, I think you should use a HashMap with Node as the key and value of List. When you parse Node, you create a list of output nodes in the list, and then map this list to node.

+1
source

First remove some returns:

Also, to get all return types, just pass the list of objects to your method and change the onEvent code as follows:

 Object onEvent(List<Object> rets) { String ret = ""; rets.add("a"); rets.add("b"); rets.add("c"); if (condition) { ret = "a"; } if (condition2) { ret = "b"; } if (condition3) { ret = "c"; } return ret; } 

Make an onEvent call like this:

 List<Object> returns = new ArrayList<Object>(); Object retVal = obj.onEvent(returns); 
+1
source

Using the command template and enum there is a workaround.

 public class OnEvent implements Command<EventInfo> { @Override public EventInfo execute() { // do some checking return EventInfo.A; } @Override public EventInfo[] getValues() { return EventInfo.values(); } public static void main(String[] args) { OnEvent e = new OnEvent(); EventInfo retVal = e.execute(); EventInfo[] values = Utils.getReturnStatements(e); } } enum EventInfo { A, B, C; } interface Command<TYPE extends Enum<?>> extends KnownReturnValues<TYPE> { public TYPE execute(); } interface KnownReturnValues<TYPE> { public TYPE[] getValues(); } class Utils { private Utils() {} public static <TYPE extends Enum<?>> TYPE[] getReturnStatements(Command<TYPE> c) { return c.getValues(); } } 
0
source

This is not a good programming practice, but you can create a class with three public data elements, and your code looks something like this. (I call the class "myclass")

 public a, b, c = null; //And then your main class would look something like this if (condition){ myclass.a=whatever; } else if (condition){ myclass.b=whatever; } else if (condition){ myclass.c=whatever } 

Then you need another control structure that says something like if lines (myclass.datamember! = Null) to make sure you have values ​​in the data elements of the class. Again, this is not a good programming practice, but it will work for what you want.

-one
source

Source: https://habr.com/ru/post/1497321/


All Articles