Nilzor
Nilzor

Reputation: 18573

Can IntelliJ refactor properties (get/setters) to fields?

Let's say I have a bunch of classes implemented javabean-style and I want to get rid of getters/setters and just have public fields instead. Any way to effectively refactor that using IntelliJ 14 while not breaking compilation?

Example - before:

class Baratheon {
    private String stannis;
    public String getStannis() {
        return stannis;
    }
    public void setStannis(String stannis) {
        this.stannis = stannis;
    }
}

class Lannister {
    public Lannister() {
        Baratheon b = new Baratheon();
        b.setStannis("dead");
    }
}

--after:

class Baratheon {
    public String stannis;
}

class Lannister {
    public Lannister() {
        Baratheon b = new Baratheon();
        b.stannis = "dead";
    }
}

Upvotes: 2

Views: 805

Answers (2)

Andrew Glukhoff
Andrew Glukhoff

Reputation: 916

1.make access to the field the same as that of getter 2.right click on getter then choose refactor / inline... ( in Android Studio )

Upvotes: 1

sisyphus
sisyphus

Reputation: 6392

The obvious steps involved in your refactoring are:

  1. make field public, retain accessors.

  2. change client code to access field directly rather than via accessors.

  3. remove accessors.

You could leverage the 'inline...' refactoring in intellij (Ctrl + Shift + n) to help with step 2.

I don't think there's a single refactoring tool in intellij which does the whole job, bearing in mind that it's only really possible if the entire affected codebase is in a single project. You could look into writing a plugin which does the job.

Upvotes: 5

Related Questions