Is it possible to change a String of a method at runtime?

Asked

Viewed 99 times

4

If I have one String fixed within a method is it possible to change it using reflection or something like that? Example:

public static void main(String[] args) {
    String nome = getName();
    boolean valid = isValidName(nome);
    if (valid) {
        System.out.print("Seu nome é valido!");
    } else {
        System.out.println("Seu nome é inválido!");
    }
}

I would need to change the message "Your name is invalid!" to "Invalid name." that at runtime, that is, after compiling and running the program I would need to edit this message.

I am working with an API that is in English, the client asked me to translate it 100%. The API has configurable messages, where you can simply go in the lang.txt file and exchange the messages, but there are some messages that are not available in that lang.txt file so I had to edit-were using reflection, but I came across some messages that are "fixed" (I don’t know if that’s the correct term) and I would need to edit these messages... these messages follow the example above.

  • 1

    Change how? It is not very clear what you want to do

  • I want to change the message "Your name is invalid!" to "Not valid name" for example.

  • 1

    When? Click [Dit] and explain what Oce is doing, what she is trying to develop and is not succeeding. Your doubt is too superficial. Explaining in these details that I told you, it is easier to understand.

  • @Articuno did an issue of the post now explaining the case better, I hope you understand... I honestly think that what I’m trying to do is impossible... I’ve worked a few times with Reflection but I think not everything in this life is possible ><

  • the message calls are always with System.out? Or was it a demonstrative example

  • It was a demonstrative example... I just studied and did MANY tests with refleciton and methods, and I think that what I’m trying to do is impossible :(

  • I think the only "way" to do this is to try to interpret the method that sends the message to the user, as the API is a game API, I think it shouldn’t be too difficult to interpret the packages that are sent in the player’s chat.

Show 2 more comments

1 answer

1

You don’t necessarily need to use Reflection for this, you can create your own API.

I have had to do this, with several old and unsupported jars whose source code nobody possessed, usually internal, but also happened to need to change a third party jar, that not even the site existed more.

Use the Jd-Gui program (or any decompiler you like) to see all the source code of your jar.

Create a new project in your IDE and copy the decompile codes there, make your modifications and export to a new file.

I believe that this way is even easier, can even change in order to make all strings to be loaded from a properties.

  • Unfortunately the JAR I have weighs 18MB and it is not opensource, working with reverse engineering in this It would be virtually impossible... it would be labor messes spent to edit 4 or 5 sentences.

  • I don’t recommend using Jd-Gui... it is by far one of the worst decompilers... I recommend luytem or if you prefer use an online http://www.javadecompilers.com/ (I recommend)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.