|
| 1 | +# Java Clean Ups |
| 2 | + |
| 3 | +Java clean ups are run on the current document whenever it's saved. |
| 4 | +They can fix a wide variety of issues, from code style to programming mistakes, |
| 5 | +and can even modernize the sources based on new Java language features. |
| 6 | +Here is some information on the supported cleanups and the details of what they do. |
| 7 | + |
| 8 | +### `qualifyMembers` |
| 9 | + |
| 10 | +Whenever a member (field or method) of a class is accessed from within the class, |
| 11 | +prefix the access with `this`. |
| 12 | +This is similar to how Python requires the programmer to access members using `self`. |
| 13 | + |
| 14 | +For instance: |
| 15 | + |
| 16 | +```java |
| 17 | +private int value; |
| 18 | + |
| 19 | +public void getValue() { |
| 20 | + return value; |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +becomes: |
| 25 | + |
| 26 | +```java |
| 27 | +private int value; |
| 28 | + |
| 29 | +public void getValue() { |
| 30 | + return this.value; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### `qualifyStaticMembers` |
| 35 | + |
| 36 | +Whenever there is a static variable or function, prefix the access with the name of the class that the static variable or function belongs to. |
| 37 | + |
| 38 | +For instance: |
| 39 | + |
| 40 | +```java |
| 41 | +import static java.lang.System.out; |
| 42 | + |
| 43 | +public class MyClass { |
| 44 | + public static final double FACTOR = 0.5; |
| 45 | + |
| 46 | + public double getNumber(double value) { |
| 47 | + out.println("moo"); |
| 48 | + return value * FACTOR; |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +becomes: |
| 54 | + |
| 55 | +```java |
| 56 | +import static java.lang.System.out; |
| 57 | + |
| 58 | +public class MyClass { |
| 59 | + public static final double FACTOR = 0.5; |
| 60 | + |
| 61 | + public double getNumber(double value) { |
| 62 | + System.out.println("moo"); |
| 63 | + return value * MyClass.FACTOR; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### `addOverride` |
| 69 | + |
| 70 | +When a method of a class that overrides a method from a parent class or provides an implementation for a method from an interface, add the `@Override` annotation. |
| 71 | + |
| 72 | +For example: |
| 73 | + |
| 74 | +```java |
| 75 | +public class MyRunner implements Runnable { |
| 76 | + public void run() { |
| 77 | + System.out.println("Hello, World!"); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +becomes: |
| 83 | + |
| 84 | +```java |
| 85 | +public class MyRunner implements Runnable { |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + System.out.println("Hello, World!"); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### `addDeprecated` |
| 94 | + |
| 95 | +When a method is marked `@deprecated` in the Javadoc, but doesn't have the `@Deprecated` annotation, add the `@Deprecated` annotation. |
| 96 | +This only works if the compiler has been configured to mark deprecated methods without the deprecated annotation |
| 97 | +as an info/warning/error in the JDT settings. |
| 98 | + |
| 99 | +For example: |
| 100 | + |
| 101 | +```java |
| 102 | +/** |
| 103 | + * Not used anymore, please stop using. |
| 104 | + * |
| 105 | + * @deprecated |
| 106 | + */ |
| 107 | +public boolean isAGoat() { |
| 108 | + return false; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +becomes: |
| 113 | + |
| 114 | +```java |
| 115 | +/** |
| 116 | + * Not used anymore, please stop using. |
| 117 | + * |
| 118 | + * @deprecated |
| 119 | + */ |
| 120 | +@Deprecated |
| 121 | +public boolean isAGoat() { |
| 122 | + return false; |
| 123 | +} |
| 124 | +``` |
0 commit comments