|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.mutabilitydetector.ConfigurationBuilder
@NotThreadSafe public abstract class ConfigurationBuilder
Builds a Configuration
for customising Mutability Detector's analysis.
The most significant feature of ConfigurationBuilder
is to allow
defining hardcoded results for particular classes, which should be respected
during analysis. For more details, see MutabilityAssert
.
Users should subclass ConfigurationBuilder
and override the
configure()
method, which should then be used to construct a
MutabilityAsserter instance.
For example:
MutabilityAsserter myAsserter = MutabilityAsserter.configured(new ConfigurationBuilder() {
@Override public void configure() {
hardcodeAsDefinitelyImmutable(SomeClass.class);
setExceptionPolicy(ExceptionPolicy.CARRY_ON);
mergeHardcodedResultsFrom(ConfigurationBuilder.DEFAULT_CONFIGURATION);
}
});
This class also provides an out-of-the-box configuration with hardcoded
results for common JDK classes. This includes classes where Mutability
Detector's analysis is incorrect, for example, java.lang.String,
java.lang.Integer (and other primitive wrapper types) and
java.math.BigDecimal.
Configurations.JDK_CONFIGURATION
Constructor Summary | |
---|---|
ConfigurationBuilder()
|
Method Summary | |
---|---|
Configuration |
build()
|
abstract void |
configure()
Subclasses should override this method to configure analysis. |
protected Set<AnalysisResult> |
getCurrentlyHardcodedResults()
Returns an immutable snapshot of the hardcoded results as at time of calling. |
protected void |
hardcodeAsDefinitelyImmutable(Class<?> immutableClass)
Hardcodes a result indicating the given class is Immutable. |
protected void |
hardcodeAsDefinitelyImmutable(String immutableClassName)
Hardcodes a result indicating the given class is Immutable. |
protected void |
hardcodeResult(AnalysisResult result)
Add a predefined result used during analysis. |
protected void |
hardcodeResults(AnalysisResult... result)
Adds all the given AnalysisResults, as if hardcodeResult was called for each element in the var args parameter. |
protected void |
hardcodeResults(Iterable<AnalysisResult> result)
Adds all the given AnalysisResults, as if hardcodeResult was called for each element in the iterable. |
protected void |
mergeHardcodedResultsFrom(Configuration otherConfiguration)
Merges the hardcoded results of this Configuration with the given Configuration. |
protected void |
setExceptionPolicy(CheckerRunner.ExceptionPolicy exceptionPolicy)
Configures how Mutability Detector's analysis should respond to exceptions during analysis. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ConfigurationBuilder()
Method Detail |
---|
public abstract void configure()
It is recommended that any custom Configuration
's merge with the
Configurations.OUT_OF_THE_BOX_CONFIGURATION
in order to remain consistent with
MutabilityAssert
, and the command line settings. For example:
MutabilityAsserter myAsserter = MutabilityAsserter.configured(new ConfigurationBuilder() {
@Override public void configure() {
mergeHardcodedResultsFrom(ConfigurationBuilder.OUT_OF_THE_BOX_CONFIGURATION);
}
});
Similarly for ThreadUnsafeAnalysisSession.createWithCurrentClassPath(Configuration)
The available configuration methods are listed below.
hardcodeResult(AnalysisResult)
,
hardcodeResults(AnalysisResult...)
,
hardcodeResults(Iterable)
,
hardcodeAsDefinitelyImmutable(Class)
,
hardcodeAsDefinitelyImmutable(String)
,
mergeHardcodedResultsFrom(Configuration)
,
#setExceptionPolicy(ExceptionPolicy)
,
Configurations.OUT_OF_THE_BOX_CONFIGURATION
public final Configuration build()
protected final void setExceptionPolicy(CheckerRunner.ExceptionPolicy exceptionPolicy)
During analysis, an exception may occur which is recoverable. That is, Mutability Detector is able to continue it's analysis, and may produce valid results.
The default behaviour is to use CheckerRunner.ExceptionPolicy.FAIL_FAST
,
meaning any unhandled exceptions will propogate up past the assertion,
and cause a failing test.
Setting this configuration flag to CheckerRunner.ExceptionPolicy.CARRY_ON
may
allow unit tests to function where exceptions don't necessarily preclude
a useful output. For example, consider a class which you wish to make
immutable; a test for that class fails with an unhandled exception. If
that test has, say 10 reasons for mutability, and 1 of those causes the
test to abort with an exception, you have just lost out on 90% of the
required information. CheckerRunner.ExceptionPolicy.CARRY_ON
will allow the
test to report 9 out of 10 reasons. The test may be useful, although it
won't be comprehensive.
If you are unlucky enough to have a test which results in an exception, please report it to the Mutability Detector project, at the project homepage.
exceptionPolicy
- - how to respond to exceptions during analysis. Defaults to
CheckerRunner.ExceptionPolicy.FAIL_FAST
Configuration.exceptionPolicy()
protected final void hardcodeResult(AnalysisResult result)
Hardcoding a result means that information queried about a class will
honour the result you have set. For example, if during analysis,
Mutability Detector has to discover whether a field type is mutable or
not. However, requesting the AnalysisResult
of the class in
question directly will return the real result from the actual analysis.
This holds for unit tests, command line runs, and runtime analysis. As
such, calling this method will have no effect when querying an
AnalysisResult directly.
Configuration.hardcodedResults()
,
AnalysisResult
,
AnalysisSession.resultFor(org.mutabilitydetector.locations.Dotted)
protected final void hardcodeResults(Iterable<AnalysisResult> result)
AnalysisResult
,
hardcodeResult(AnalysisResult)
protected final void hardcodeResults(AnalysisResult... result)
AnalysisResult
,
hardcodeResult(AnalysisResult)
protected final void hardcodeAsDefinitelyImmutable(Class<?> immutableClass)
AnalysisResult.definitelyImmutable(String)
protected final void hardcodeAsDefinitelyImmutable(String immutableClassName)
ClassNameConverter
,
AnalysisResult.definitelyImmutable(String)
protected final Set<AnalysisResult> getCurrentlyHardcodedResults()
protected void mergeHardcodedResultsFrom(Configuration otherConfiguration)
mergeHardcodedResultsFrom(Configuration)
.
otherConfiguration
- - Configuration to merge hardcoded results with.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |