Community
Participate
Working Groups
I2003-02-14 Situation: I have a class Assert that has a static method notNull which I want to inline in an attempt of switching to JDK 1.4's assert facility. The notNull method looks like: public static final void notNull(Object ref, String message) { assert ref != null : message; } A method that uses the notNull method looks like: void method(String param1, String param2, String param3) { Assert.notNull(param1, "param1 != null"); Assert.notNull(param2, "param2 != null"); Assert.notNull(param3, "param3 != null"); } Unfortunately, after inlinig, it looks like: void method(String param1, String param2) { assert param1 != null : "param1 != null"; Object ref = param2; assert ref != null : "param2 != null"; Object ref1 = param3; assert ref1 != null : "param3 != null"; } What I had expected was something like: void method(String param1, String param2) { assert param1 != null : "param1 != null"; assert param2 != null : "param2 != null"; assert param3 != null : "param3 != null"; }
Fixed for RC2.
With I20030306, the following has the same result as inlining Strings before: void method(String[] param) { Assert.notNull(param[0], "param[0] != null"); Assert.notNull(param[1], "param[1] != null"); Assert.notNull(param[2], "param[2] != null"); }
Another non-working one: void method(String param1, String param2, String param3) { Assert.notNull(param1, "param1 != null"); Assert.notNull(param2, "param2 != null"); Assert.notNull(param3, "param3 != null"); param1 = param1; param2 = param2; param3 = param3; } It seems as if inlinig doesn't produce the correct result once there is read access to a specific variable.
Ok, these are different case. The problem here is that the flow analysis figures out that param[x] might be changed after reading it so it evalutes it instead of passing it. But since the method to be inline uses the arg only ones this is not necessary. The last example it even more complicated and will not be addressed for 2.1 since it is unusual anyways.
I don't think the second example is that unusual, I just tried to make it as simple as possible. The original code was someting like: void compress(File in, File out9 { Assert.notNull(in, "in != null"); Assert.notNull(out, "out != null"); FileInputStream in = new FileInputStream(in); FileOutputStream os = new FileOutputStream(out); ...
I misinterpreted the second example (took it as the method to be inlined). You are correct. What will not work is the following: public void foo(int x) { int y= x; x= x; } public void use() { foo(value()); } inlining foo will produce public void use() { int x = value(); int y= x; x= x; } also the flow analysis could figure out that x= x is a noop.
Fixed the cases provided in comment #2 and #3. The problem was: - forgot to handle boolean literals. - no optimazation of a single read access in the method to be inlined. Boris, thanks for providing the test cases.