I'm working on an annotation processor library and I'm using JavaPoet to generate som Java code. I need to generate a class with a field declared with an anonymous class like the following code:
public class Dummy {
private final OnSharedPreferenceChangeListener valueFloatListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case "value_float":
valueFloatSubject.onNext(sharedPreferences.getFloat(key, defaultBean.valueFloat));
break;
}
}
};
}
How can I define valueFloatListener
field with JavaPoet? Tnx in advance.
You have to use
TypeSpec.anonymousClassBuilder("")
.Something like this should work:
Not sure if the code is compile clean, but I think it shows, how to solve the problem.