Skip to content

UseCases

Joseph Hoare edited this page Mar 15, 2021 · 2 revisions

The main API for Astra is AstraCore.run, which takes a directory path for Astra to run over, and a UseCase, which is a way of bundling a set of operations and related information together.

The examples given in the Type reference refactor and Method invocation refactor pages of this Wiki show UseCases with just one operation, but it's likely that when we want to perform bigger refactors, we'd want to bundle many of these together.

An example would be performing a library upgrade, in which case we might want to refactor all the uses of a given library to an alternative. For example, if we upgraded from an earlier version of Apache Commons Lang to Apache Commons Lang 3, we might bundle a number of operations like this with one another:

TypeReferenceRefactor.builder().fromType("org.apache.commons.lang.time.DateUtils").toType("org.apache.commons.lang3.time.DateUtils").build(),
TypeReferenceRefactor.builder().fromType("org.apache.commons.lang.RandomStringUtils").toType("org.apache.commons.lang3.RandomStringUtils").build(),
MethodInvocationRefactor
        .from(MethodMatcher.builder().withMethodName("escapeHtml").withFullyQualifiedDeclaringType("org.apache.commons.lang.StringEscapeUtils").build())
        .to(new Changes().toNewMethodName("escapeHtml4").toNewType("org.apache.commons.lang3.StringEscapeUtils"))

Note that the above is an illustration - a real refactor would need more inputs! But hopefully this demonstrates that Astra can be used to combine many smaller refactoring operations together to achieve a broader goal. You can imagine the intent here - a library maintainer making API changes could provide the Astra inputs for refactoring callers to a preferred alternative.