0

I'm storing a full method in a String (actually reading it from a file as a String). Now I need to execute the method which is in the String. I mean, in the MainActivity I have to execute this method inside the String. Is there any possible way to do it?

Thanks in Advance.

The method which is stored in the String is:

public void showTheText()
{
    System.out.println("Hi There...");
}
La Corazón
  • 125
  • 1
  • 1
  • 9
  • what do you want? logging the method in txt file? – Randyka Yudhistira Feb 06 '15 at 06:45
  • I have a `text file` which has the above method. I'm reading it to a `String` in the `MainActivity`. So I'm having a `String object` which holds the method completely. Now I need to execute this String(method inside String) in the `MainActivity`. – La Corazón Feb 06 '15 at 06:55
  • impossible to do that – Randyka Yudhistira Feb 06 '15 at 06:56
  • In JavaScript we can refer a String like this and execute it whenever needed. But in Android, I doono what to do. Is there any Workaround? – La Corazón Feb 06 '15 at 07:07
  • i dont know what do you want to achive, but in android you can run shell command from java code. Try read this [link](http://stackoverflow.com/questions/6882248/running-shell-commands-though-java-code-on-android) – Randyka Yudhistira Feb 06 '15 at 07:14
  • @ Randyka All I need is to create, input and output to a text file at runtime. The content of the file is the method in the question. The next thing is that I'm reading this file in the Activity as a String object. Then I must execute the method contained in the String at runtime. The method I mentioned in the Question is an example. It can be any method. But I've to execute it in the MainActivity. – La Corazón Feb 06 '15 at 07:29
  • what about using Reflection? – La Corazón Feb 06 '15 at 09:50

1 Answers1

0
  • Before doing this, if you are trying to execute code dynamically, keep in mind that guys on Google Play might not like it, as it can be use for remote execution of code, or code that wasn't analyzed by them.

  • That being said, in java there is no proper way of evaluating expressions in strings. But you can always embed some interpreter and make it execute the code (i.e. https://code.google.com/p/android-scripting/wiki/FAQ).

  • If your methods are present on some class of your application, you would only need the name of the method you want to execute and then use reflection to invoke it.

Logain
  • 4,259
  • 1
  • 23
  • 32
  • @ Logain That article suggests me to use an interpreter. Do you have any idea about Beanshell for Android? – La Corazón Feb 06 '15 at 09:37
  • can we use Reflection here? – La Corazón Feb 06 '15 at 09:50
  • Yes, reflection can be used as in any other java <=1.7 application. I haven't tried Beanshell, only made some experiements with Clojure – Logain Feb 06 '15 at 09:58
  • what actually can I do with Reflection? I mean for my purpose here. Can I execute a method stored as a String object with Reflection? – La Corazón Feb 06 '15 at 10:04
  • No, what you can do is very limited. For example, given the name of a class and the name of a method (in strings) you can execute that method, but it needs to already be on a class in order to do so. Can I ask why yo need to do all this? Maybe there is a better way, and the core issue is another one. – Logain Feb 06 '15 at 10:09
  • What I'm doing is that I'm reading a file at runtime. This file contains a method like I mentioned above. Im reading this file to a String. So the String have this method. Now I've to execute this. Its like getting a Method holding String at runtime and executing it. – La Corazón Feb 06 '15 at 10:14
  • Yeah, I got that, but I mean... why? why do you have such a requirement on the first place? What are you using those files for? Maybe there is a more straightforward way of achieving your main goal without having to evaluate code from a string. – Logain Feb 06 '15 at 10:17