Wrong default visibility marker for classes
Eclipse Plugin to find unused Java code
Brought to you by:
spj
with version 0.12.0, and the following 3 java files, ucdetector detect that the Field class could be set to default. But if i switch it to default, there is a compilation error in Main.foo :
// ===
package tests;
import tests.sub.Container;
public class Main {
void foo() {
Container container = new Container();
String s = container.field.getBar();
}
}
// ===
package tests.sub;
public class Container {
public Field field;
}
// ===
package tests.sub;
public class Field {
public String getBar() { return "Bar"; }
}
Anonymous
This is a bug!
Solution: A class can only have default visibility, when all it members and methods can be default!
That solution won't work in the following situation:
// two/Consumer.java, main class:
package two;
import one.Foo;
public class Consumer { // NO_UCD -- main method
}
// one/Foo.java
package one;
public class Foo {
public Bar getBar() {
return new Bar();
}
}
// one/Bar.java
package one;
// UC detector warning: Change visibility of "Bar" to default
public class Bar {
// Does nothing at all
}
Hi fauxfaux,
you are right! That means: UCDetector can't suggest to make a class default, without compile errors.
I think I will realize my first idea, which resolves the initial problem:
"A class can only have default visibility, when all it members and methods can be default!"
and add a warning: "May cause compile errors"
What do you think?
Joerg
Added a warning: "May cause compile errors"
I think the only solution is to run no visbitility detection for classes!
I didnt find a solution for this bug. Changed resolution to Wont Fix.
The only solution is to parse all local variable/field/parameter references and see if they can "see" their own type if the type is private/protected or public.
I did not understand the last post!
To understand this bug, please check the 3 java examples (same as post from fauxfaux):
http://ucdetector.svn.sourceforge.net/viewvc/ucdetector/trunk/org.ucdetector.example/example/org/ucdetector/example/bugs/Bug2539795Consumer.java?view=markup
http://ucdetector.svn.sourceforge.net/viewvc/ucdetector/trunk/org.ucdetector.example/example/org/ucdetector/example/bugs/impl/Bug2539795Bar.java?view=markup
http://ucdetector.svn.sourceforge.net/viewvc/ucdetector/trunk/org.ucdetector.example/example/org/ucdetector/example/bugs/impl/Bug2539795Foo.java?view=markup
If you parse the AST, you will get some kind of MemberAccess for the ".field" in the Main class, in method foo().
Since you can't access the "tests.sub.Field" class from the "tests.Main.foo()" method, it means the class cannot be default or protected (not same package and no inheritance).
Can you make a code example for the AST stuff? Maybe a eclipse class, which does something similar.
I don't understand your posting. It would be great, if your posting can fix this bug!
If this rule cannot be fixed, it should be disabled or removed, it just creates noise in the results which seriously devalue this tools usefulness.