Blade icon indicating copy to clipboard operation
Blade copied to clipboard

Generate assertions from standard Android annotations

Open yuraj11 opened this issue 6 years ago • 2 comments

When method parameter is annotated with @NonNull -> generate runtime assertions in code like this:

void foo(@NonNull final String param) {
 /* Code generated by Blade */
 if (param == null) {
  throw new AssertionError("text == null");
 }
 /* ~ */

 // Actual method code here
}

Consider adding support for other Android support annotations e.g.: @IntRange :

void foo(@IntRange(from=0) final int index) {
 /* Code generated by Blade */
 if (index < 0) {
  throw new AssertionError("index < 0");
 }
 /* ~ */

 // Actual method code here
}

Other possible assertions for: @FloatRange, @Size, @UiThread, @WorkerThread, @RequiresApi

[idea] And what about when method has annotated return value like this:

@NonNull
public String foo() {
 // Actual method code here
 String returnVal = null;

 /* Code modified by Blade */
 if (returnVal == null) {
  throw new AssertionError("return value of method foo is null");
 }
 return returnVal;
 /* ~ */
}

yuraj11 avatar Oct 26 '17 13:10 yuraj11

@yuraj11 do you think there will be a need for an ignore mechanism or should the Blade process every method with supported annotation?

FrantisekGazo avatar Nov 10 '17 19:11 FrantisekGazo

I think It's not needed right now or at least in the first implementation (will see in future).

yuraj11 avatar Nov 13 '17 11:11 yuraj11