vendredi 12 juin 2015

Get parameter name of class to pass it to other method in java

I would like to pass parameter name as a parameter to other method, f.e:

I have class:

public class Foo() {
    public Bar bar;
    public Bar anotherBar;
    public Bar yetAnotherBar;

    public void doSomethingWithBar() {
        common.doingSomething(
            getMostImportantBarParameterName()
        );
    }
}

And in this class I would to have method:

public String getMostImportantBarParameterName() {
    return Foo.bar;
}

but instead of returning value of bar, I would like to get a name of parameter bar, so it should just return "bar".

For now I have to do this that way:

public String getMostImportantBarParameterName() {
    return "bar";
}

Why I wanna achieve something like that? I am trying as much I can to avoid using strings in my code, cause in refactorization process I will bypass (skip) it accidentally.

But if I will have "hard coded" parameters that way, when I will later rename this parameter it will be automatically replaced in all instances by Eclipse IDE (Using LALT+LSHIFT+R)

Also my method: common.doingSomething() use parameter in runtime, So I won't get compilation error, which it makes hard to maintain this method.

I don't write unit test, cause I can't yet.

Please give me some help on this. Thanks





Aucun commentaire:

Enregistrer un commentaire