/plugins/InspectionGadgets/test/com/siyeh/igtest/style/field_final/FieldMayBeFinal.java

https://bitbucket.org/nbargnesi/idea · Java · 372 lines · 308 code · 58 blank · 6 comment · 18 complexity · 9eb8149a4a47029a4b0faf1a8b974ec3 MD5 · raw file

  1. package com.siyeh.igtest.style.field_final;
  2. import java.awt.*; import java.awt.event.KeyEvent;import java.io.File;import java.io.IOException;
  3. public class FieldMayBeFinal {
  4. private static String string;
  5. private static int i;
  6. static {
  7. string = null;
  8. }
  9. static {
  10. string = null;
  11. }
  12. private String other;
  13. {
  14. other = null;
  15. }
  16. private String ss;
  17. {
  18. ss = "";
  19. }
  20. {
  21. ss = "";
  22. }
  23. private int number;
  24. private String s;
  25. public FieldMayBeFinal() {
  26. s = "";
  27. number = 0;
  28. }
  29. public FieldMayBeFinal(int number) {
  30. new Runnable() {
  31. public void run() {
  32. s = "";
  33. }
  34. };
  35. s = "";
  36. this.number = number;
  37. }
  38. private String utterance = "asdfas";
  39. private class Action {
  40. public void foo() {
  41. utterance = "boo!";
  42. }
  43. }
  44. private String unused;
  45. private static class Boom {
  46. private String notFinal;
  47. private static String two;
  48. static {
  49. if (1 == 2) {
  50. two = "other";
  51. }
  52. }
  53. Boom(boolean b) {
  54. if (b) {
  55. notFinal = "";
  56. }
  57. }
  58. }
  59. private static boolean flag = true;
  60. private static final KeyEventPostProcessor processor = new KeyEventPostProcessor() {
  61. public boolean postProcessKeyEvent(KeyEvent event) {
  62. flag = event.isAltDown();
  63. return false;
  64. }
  65. };
  66. static class Test
  67. {
  68. public static void main(String[] args)
  69. {
  70. Inner inner = new Inner();
  71. inner.val = false;
  72. }
  73. private static class Inner
  74. {
  75. private boolean val = true;
  76. private boolean pleaseTellMeIt = true;
  77. }
  78. }
  79. static class Test3 {
  80. private static String hostName;
  81. static {
  82. try {
  83. hostName = java.net.InetAddress.getLocalHost().getHostName();
  84. } catch (Exception ignored) {
  85. hostName = "localhost";
  86. }
  87. }
  88. }
  89. static class Test4 {
  90. private static String hostName;
  91. static {
  92. try {
  93. hostName = java.net.InetAddress.getLocalHost().getHostName();
  94. } catch (Exception ignored) {
  95. throw new RuntimeException();
  96. }
  97. }
  98. }
  99. static class DoubleAssignment {
  100. private String result;
  101. public DoubleAssignment() {
  102. result = "";
  103. result = "";
  104. }
  105. }
  106. static class IncrementInInitializers {
  107. private int i = 0;
  108. private final int j = i++;
  109. }
  110. static class AssigmentInForeach {
  111. private boolean b;
  112. private boolean c;
  113. /*AssigmentInForeach(int[] is) {
  114. b = false;
  115. for (int i : is) {
  116. b = c = i == 10;
  117. }
  118. }*/
  119. }
  120. static class StaticVariableModifiedInInstanceVariableInitializer {
  121. private static int COUNT = 0; // <<<<<< highlights as "can be made final"
  122. private final int count = COUNT++;
  123. }
  124. static class FalsePositive1 {
  125. private int i;
  126. FalsePositive1() {
  127. System.out.println(i);
  128. i = 1;
  129. }
  130. }
  131. }
  132. class NotFinal {
  133. private static final NotFinal INSTANCE = new NotFinal();
  134. private boolean isInitialized;
  135. private NotFinal() {
  136. isInitialized = false;
  137. }
  138. public static synchronized void initialize() {
  139. INSTANCE.isInitialized = true;
  140. }
  141. }
  142. class AAA {
  143. private String test;
  144. public AAA(int num) {
  145. if(num < 0) {
  146. return;
  147. }
  148. test = "ok";
  149. }
  150. public void feep() {
  151. System.out.println("test = " + test);
  152. }
  153. }
  154. class X {
  155. private int x;
  156. X() {
  157. x += 1;
  158. }
  159. }
  160. class XX {
  161. private int xx;
  162. XX() {
  163. if (true) {
  164. return;
  165. }
  166. xx = 1;
  167. }
  168. }
  169. class Y {
  170. private int x; // can be final
  171. private int y; // can be final
  172. private int z = y = 1; // can be final
  173. Y() {
  174. x = 1;
  175. }
  176. Y(String s) {
  177. this();
  178. }
  179. }
  180. class Z {
  181. Q q = new Q();
  182. class Q {
  183. private int i =1;
  184. }
  185. class R {
  186. {
  187. q.i = 2;
  188. }
  189. }
  190. }
  191. class ZX {
  192. private int i;
  193. ZX() {
  194. if (false && (i = 1) == 1) {
  195. };
  196. }
  197. }
  198. class InspectionTest
  199. {
  200. private Object field;
  201. public InspectionTest(Object field)
  202. {
  203. this.field = field;
  204. }
  205. public InspectionTest()
  206. {
  207. try
  208. {
  209. File file = new File("test");
  210. if (!file.canRead())
  211. {
  212. return;
  213. }
  214. file.createNewFile();
  215. }
  216. catch (IOException e)
  217. {
  218. throw new RuntimeException(e);
  219. }
  220. this.field = new Object();
  221. }
  222. }
  223. class InspectionTest2
  224. {
  225. private Object field;
  226. public InspectionTest2(Object field)
  227. {
  228. this.field = field;
  229. }
  230. public InspectionTest2()
  231. {
  232. try
  233. {
  234. File file = new File("test");
  235. file.createNewFile();
  236. this.field = new Object();
  237. }
  238. catch (IOException e)
  239. {
  240. this.field = null;
  241. }
  242. }
  243. }
  244. class ActionMenu {
  245. private boolean myMnemonicEnabled;
  246. public ActionMenu(final boolean enableMnemonics) {
  247. myMnemonicEnabled = enableMnemonics;
  248. }
  249. private boolean isTopLevel() {
  250. return true;
  251. }
  252. public void setMnemonicEnabled(boolean enable) {
  253. myMnemonicEnabled = enable;
  254. }
  255. }
  256. class H {
  257. private int h = 1;
  258. static void H(H h) {
  259. h.h = 2;
  260. }
  261. }
  262. class I {
  263. private int i = 1;
  264. static class J {
  265. void i(I i) {
  266. i.i = 1;
  267. }
  268. }
  269. }
  270. class J {
  271. private static int j = 1;
  272. class K {
  273. void f() {
  274. J.j = 2;
  275. }
  276. }
  277. }
  278. class K {
  279. private int k = 1;
  280. private static int l = new K().k = 2;
  281. K() {
  282. l = 2;
  283. }
  284. }
  285. class L {
  286. private static int l = 1;
  287. {
  288. l = 3;
  289. }
  290. }
  291. class M {
  292. private int m = 1;
  293. static {
  294. new M().m = 2;
  295. }
  296. }
  297. class N {
  298. private int n;
  299. N() {
  300. if (true && (n = 1) == 1) {}
  301. }
  302. }
  303. class O {
  304. private int o;
  305. O() {
  306. if (false || (o = 1) == 1) {}
  307. }
  308. }
  309. class P {
  310. private int p;
  311. P() {
  312. if (true || (p = 1) == 1) {}
  313. }
  314. }