This question is about byte code level in-lining optimizations that breaks stacktrace and reflection.
I have a library of many static factories to create objects. This class only has static methods such as
public class FruitC {
public static Appple createApple(AppContext ac, AppleParameters...) {
//stuff using AppContext to create apple use parameters
return apple;
}
Code calls
FruitC.createApple(AppContext ac, AppleParameters...)
The original idea was to use such a design to allow easy in-lining so that the optimizers could remove the static class completely because after in-lining the class FruitC would be empty.
But you cannot override static methods and you need to pass the Context in parameters to the factory methods and it looks ugly when coding
So the idea now is to create a non static class for each static factory. The non static class is initialized by the Application context
public class FruitFactory {
private AppContext ac;
public FruitFactory(AppContext ac) {
this.ac = ac;
}
public Apple createApple(AppleParameters...) {
return FruitC.createApple(ac, AppleParameters...)
}
This design allows extension if needed. However the default factory can supposedly be inlined and both FruitFactory and FruitC classes removed completely after optimizations.
Is there an optimization tool that will remove FruitFactory and FruitC ?
The smart optimizer must be able to detect that the AppContext is an object whose reference is written ONCE during the app life and then never changes.
Will ProGuard be able to do that?
This optimization must be done at the java byte code level because it must then be sent to Android or another platform.