concept policy in category aspectj

appears as: policies, policies, policy
AspectJ in Action, Second Edition

This is an excerpt from Manning's book AspectJ in Action, Second Edition.

Policy enforcement is a mechanism for ensuring that system components follow certain programming practices, comply with specified rules, and meet any assumptions. If there is no enforcement, errors may go undetected during development and show up only in the deployed system. How would you enforce such policies today? Perhaps you’d use a static code-analysis tool such as PMD or FindBugs that looks for specific code patterns to flag potential programming problems. These tools detect violations of commonly accepted practices. You may also be able to write simple custom policies if you learn the tool’s specific programming language. Even then, you’re limited to policies that static code analysis can detect. Specifically, you can’t enforce policies that need information available only at runtime. Although these tools are excellent additions to most development environments, they aren’t adequate for catching all scenarios.

AOP provides an elegant way to encapsulate policies in aspects, allowing enforcement with little human diligence. You can also reuse those aspects and apply them to other projects without incurring additional development cost, thus building a company-wide library of accepted policies. Policy enforcement with AOP falls in the developmental aspect category. You can include these aspects during the development phase to help detect policy violations; and for deployment, you can exclude them without affecting the core system behavior. This can be part of an incremental adaptation of AOP; you don’t have to commit to using AOP in the deployed system in order to get the benefits from it.

This chapter presents many examples that illustrate how you can detect violations by compiling your code along with a few prewritten aspects. The examples include enforcement of layering in a typical application, enforcing good programming practices and idioms, as well as enforcing framework and concurrency control policies. You’ll use AspectJ byte-code weaving as well as Spring AOP to implement various policies. Because the code-analysis tools provide an alternative way to implement some of these policies, we’ll compare them with AOP-based solutions and provide guidelines for choosing the right tool for the job. Let’s start with an overview of the AOP way of enforcing policies.

Listing 11.14. Repository that violates the policy of not returning a null collection
package ajia.repository; import java.util.Collection; public class StubInventoryRepository { public Collection findAll() { return null; } }