Register EnvironmentTypes using their classes#545
Conversation
Bring back missing `@Immutable`s. Bump version.
…registration by class works.
|
@armiol, PTAL. |
Codecov Report
@@ Coverage Diff @@
## master #545 +/- ##
============================================
+ Coverage 73.79% 73.82% +0.03%
- Complexity 2951 2959 +8
============================================
Files 506 506
Lines 11943 11972 +29
Branches 669 669
============================================
+ Hits 8813 8838 +25
- Misses 2903 2907 +4
Partials 227 227 |
| return register(envTypeInstance); | ||
| } catch (NoSuchMethodException | IllegalAccessException | | ||
| InstantiationException | InvocationTargetException e) { | ||
| String message = "Could not register environment type `%s` by class. You may try " + |
There was a problem hiding this comment.
Let's say something about the parameterless ctor in the message.
|
@armiol, PTAL again. |
|
@armiol, PTAL again. |
only check for it to instantiate it. Clean up, add and tweak documentation.
|
@armiol, PTAL. |
| * determine whether it's enabled} later. | ||
| * | ||
| * <p>The specified {@code type} must have a parameterless constructor. | ||
| * {@linkplain EnvironmentType#enabled() activity} of the specified environment type is going |
There was a problem hiding this comment.
activity should start from the capital letter. But I am not sure I understand this sentence:
activity of the specified environment type is going to be checked against an instance created by invoking the parameterless constructor.
Please rewrite into two or more simple sentences.
| * assertThat(environment.is(AwsLambda.class)).isFalse(); | ||
| * </pre> | ||
| * | ||
| * <p>{@linkplain #setTo(EnvironmentType) explicitly setting} the environment type overrides |
There was a problem hiding this comment.
When set {@linkplain ... explicitly}, ...
Notice the capital letter.
| * <p>{@code Environment} caches the {@code EnvironmentType} once its calculated. | ||
| * This means that if one environment type has been found to be active, its instance is saved. | ||
| * If later it becomes logically inactive, e.g. the environment variable that's used to check the | ||
| * environment type changes, {@code Environment} is still going to return the cached value, unless |
There was a problem hiding this comment.
Don't use complex sentences.
... going to return the cached value. To overwrite the value (do something). Also, the value may be {@linkplain ... reset}.
|
@armiol, PTAL again. |
armiol
left a comment
There was a problem hiding this comment.
@serhii-lekariev LGTM with a single comment. Please make sure to bump time after this one is merged as well.
| AtomicBoolean envCached = new AtomicBoolean(false); | ||
| assertThat(environment.is(Tests.class)); | ||
| Thread thread = new Thread(() -> { | ||
| /* |
There was a problem hiding this comment.
Please convert this to the method-level comment.
Don't use inline comments except for emergency cases.
armiol
left a comment
There was a problem hiding this comment.
@serhii-lekariev please see my comments to the related PR in core-java first.
|
@armiol, PTAL. |
| /** | ||
| * Sets the current environment type to {@code type.getClass()}. Overrides the current value. | ||
| * | ||
| * <p>Calls {@link #register(EnvironmentType)} internally. |
There was a problem hiding this comment.
If the supplied type was not {@linkplain #register(EnvironmentType) registered} previously, it is registered.
| /** | ||
| * Sets the current environment type to the specified one. Overrides the current value. | ||
| * | ||
| * <p>Calls {@link #register(Class)} internally. |
This PR introduces several changes to
EnvironmentandEnvironmentTypes.TestsandProductionnow havepackage-privateconstructors;Environmentnow caches the class of the activeEnvironmentTypeinstead of an instance;Environmentnow exposes an@Internalmethod to register anEnvironmentTypeby class. Classes to be registered using this method must have a parameterless constructor. This limitation is caused by the fact that we have to instantiate the specifiedEnvironmentType.This leads to a signature change:
public EnvironmentType type()->public Class<? extends EnvironmentType> type().It also allows to add a
setTo(Class<? extends EnvironmentType>)method, which is now necessary, as users can no longersetTo(new Tests()), asTestsnow has a package-private constructor.InvokablesThis PR also extracts some of the reflection-related utilities into an
Invokablesclass.Users of
Methodsmay found the utilities there.