mercredi 23 novembre 2016

¿How to change, inside a class, the value of an attribute in runtime?

I have the following structure, where there is an abstract class with a baseDirectory attribute by default.

public abstract class PerformanceTest {
/**
* Base directory.
*/
private String baseDirectory = "C:/msg-inbox/";

...

On the other hand there is a class that extends from the abstract class.

public class MessagePerformanceTest extends PerformanceTest {
/**
* Constructor.
*/
public MessagePerformanceTest() {
}

...

Finally I have a main class like this:

public class MessagesMain {

    public static void main(String[] args) {

    Field field = PerformanceTest.class.getDeclaredField("baseDirectory");
          field.setAccessible(true);
          field.set(null,args[0]);

    MessagePerformanceTest messagePerformanceTest = new MessagePerformanceTest();
    messagePerformanceTest.createMessages();

...

The problem that i am facing is that i dont know the best way to pass de baseDirectory attribute as an argument because I need to change this value in runtime. I´m trying to use reflection but its not working by the moment. ¿Have anyone idea about a possible workaround?

To sum up I need to change de default value C:/msg-inbox/ to another thing similar to \folder1\test

Regards





Aucun commentaire:

Enregistrer un commentaire