Skip to main content

Use explicit this.field and this.method()

org.openrewrite.staticanalysis.ExplicitThis

Add explicit 'this.' prefix to field and method access.

Recipe source

GitHub: ExplicitThis.java, Issue Tracker, Maven Central

This recipe is available under the Moderne Source Available License.

Example

Before
import java.util.function.Consumer;

class Parent {
private String parentField;

Parent(String value) {
parentField = value;
}
}

class Test extends Parent {
private String value;
private String field;
private String field1;
private String field2;
private static String staticField;
private String alreadyPrefixed;

private String fieldInit1 = "initial";
private String fieldInit2 = field1;
private String fieldInit3 = field1 + field2;

private static String staticFieldInit = staticField;

static {
staticField = "static initializer";
staticHelper();
}

{
field = "instance initializer";
field1 = field2;
}

Test(String value) {
super(value);
// Shadowed parameter: looks like a bug but replacing would change semantics
value = value;
field = "constructor";
field1 = field2;
}

Test() {
this("default");
}

void instanceMethod(String parameter) {
field = "value";
field1 = field2;
helper();
staticField = "static context";
staticMethod();

String localVariable = parameter;
String result = parameter + localVariable;

this.alreadyPrefixed = "already has this";
this.alreadyPrefixedMethod();
super.toString();

Consumer<String> lambda = s -> {
field = s;
field1 = field2;
};

Runnable runnable = () -> field = "lambda";
}

static void staticMethod() {
staticField = "static context";
staticHelper();
}

// Shadowed parameter: looks like a bug but replacing would change semantics
void setField(String field) {
field = field;
}

void helper() {}
void alreadyPrefixedMethod() {}
static void staticHelper() {}

class Inner {
private String innerField;

void method() {
innerField = "inner";
field = "outer";
Consumer<String> lambda = s -> {
innerField = s;
field = s;
};
}

class Nested {
private String nestedField;

void method() {
nestedField = "nested";
innerField = "inner";
field = "outer";
}
}
}

static class StaticNested {
private String nestedField;
private static String nestedStaticField;

void method() {
nestedField = "nested";
nestedStaticField = "static";
staticField = "outer static";
}

static void staticMethod() {
nestedStaticField = "static";
staticField = "outer static";
}
}

void anonymousClassExample() {
Runnable r = new Runnable() {
private String anonymousField = "anonymous";

@Override
public void run() {
field = "outer";
String local = anonymousField;
}
};
}
}
After
import java.util.function.Consumer;

class Parent {
private String parentField;

Parent(String value) {
this.parentField = value;
}
}

class Test extends Parent {
private String value;
private String field;
private String field1;
private String field2;
private static String staticField;
private String alreadyPrefixed;

private String fieldInit1 = "initial";
private String fieldInit2 = this.field1;
private String fieldInit3 = this.field1 + this.field2;

private static String staticFieldInit = staticField;

static {
staticField = "static initializer";
staticHelper();
}

{
this.field = "instance initializer";
this.field1 = this.field2;
}

Test(String value) {
super(value);
// Shadowed parameter: looks like a bug but replacing would change semantics
value = value;
this.field = "constructor";
this.field1 = this.field2;
}

Test() {
this("default");
}

void instanceMethod(String parameter) {
this.field = "value";
this.field1 = this.field2;
this.helper();
staticField = "static context";
staticMethod();

String localVariable = parameter;
String result = parameter + localVariable;

this.alreadyPrefixed = "already has this";
this.alreadyPrefixedMethod();
super.toString();

Consumer<String> lambda = s -> {
this.field = s;
this.field1 = this.field2;
};

Runnable runnable = () -> this.field = "lambda";
}

static void staticMethod() {
staticField = "static context";
staticHelper();
}

// Shadowed parameter: looks like a bug but replacing would change semantics
void setField(String field) {
field = field;
}

void helper() {}
void alreadyPrefixedMethod() {}
static void staticHelper() {}

class Inner {
private String innerField;

void method() {
this.innerField = "inner";
Test.this.field = "outer";
Consumer<String> lambda = s -> {
this.innerField = s;
Test.this.field = s;
};
}

class Nested {
private String nestedField;

void method() {
this.nestedField = "nested";
Inner.this.innerField = "inner";
Test.this.field = "outer";
}
}
}

static class StaticNested {
private String nestedField;
private static String nestedStaticField;

void method() {
this.nestedField = "nested";
nestedStaticField = "static";
staticField = "outer static";
}

static void staticMethod() {
nestedStaticField = "static";
staticField = "outer static";
}
}

void anonymousClassExample() {
Runnable r = new Runnable() {
private String anonymousField = "anonymous";

@Override
public void run() {
Test.this.field = "outer";
String local = this.anonymousField;
}
};
}
}

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-static-analysis` in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.staticanalysis.ExplicitThis")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.31.0")
    }
  2. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Source files that had results

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.