From d0b159de8b15b2d741cd6e2b8b9a4e5047a615fd Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 16:50:01 -0400 Subject: [PATCH 01/15] Added Jetbrains Annotations 22.0.0 dependency --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 5626e15a..e5fa31eb 100644 --- a/pom.xml +++ b/pom.xml @@ -205,6 +205,11 @@ provided 3.0.2 + + org.jetbrains + annotations + 22.0.0 + From 69a862c53203416ac6eede4c9f520682160e26e3 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 16:55:01 -0400 Subject: [PATCH 02/15] Initial IDE inferred nullity pass --- src/main/java/ch/jalu/configme/Comment.java | 4 +- .../jalu/configme/SettingsManagerBuilder.java | 23 +++++---- .../ch/jalu/configme/SettingsManagerImpl.java | 2 +- .../beanmapper/ConfigMeMapperException.java | 7 +-- .../jalu/configme/beanmapper/ExportName.java | 4 +- .../ch/jalu/configme/beanmapper/Mapper.java | 2 + .../jalu/configme/beanmapper/MapperImpl.java | 27 +++++----- .../configme/beanmapper/MappingContext.java | 3 +- .../beanmapper/MappingContextImpl.java | 9 ++-- .../AbstractLeafValueHandler.java | 6 ++- .../BigNumberLeafValueHandler.java | 10 ++-- .../BooleanLeafValueHandler.java | 4 +- .../CombiningLeafValueHandler.java | 7 +-- .../EnumLeafValueHandler.java | 5 +- .../leafvaluehandler/LeafValueHandler.java | 2 + .../NumberLeafValueHandler.java | 9 ++-- .../ObjectLeafValueHandler.java | 4 +- .../StringLeafValueHandler.java | 4 +- .../BeanDescriptionFactoryImpl.java | 17 ++++--- .../BeanPropertyDescription.java | 1 + .../BeanPropertyDescriptionImpl.java | 7 +-- .../CommentsConfiguration.java | 3 +- .../ConfigurationDataBuilder.java | 31 +++++------ .../ConfigurationDataImpl.java | 21 ++++---- .../PropertyListBuilder.java | 15 +++--- .../migration/PlainMigrationService.java | 7 +-- .../configme/properties/ArrayProperty.java | 5 +- .../configme/properties/BaseProperty.java | 9 ++-- .../configme/properties/BeanProperty.java | 3 +- .../properties/InlineArrayProperty.java | 3 +- .../configme/properties/ListProperty.java | 7 +-- .../LowercaseStringSetProperty.java | 10 ++-- .../jalu/configme/properties/MapProperty.java | 9 ++-- .../configme/properties/OptionalProperty.java | 10 ++-- .../ch/jalu/configme/properties/Property.java | 1 + .../configme/properties/PropertyBuilder.java | 25 ++++----- .../properties/PropertyInitializer.java | 51 ++++++++++--------- .../configme/properties/RegexProperty.java | 9 ++-- .../jalu/configme/properties/SetProperty.java | 12 +++-- .../properties/StringListProperty.java | 3 +- .../properties/StringSetProperty.java | 3 +- .../properties/TypeBasedProperty.java | 3 +- .../convertresult/PropertyValue.java | 7 +-- .../StandardInlineArrayConverters.java | 8 +-- .../properties/types/BeanPropertyType.java | 8 +-- .../properties/types/EnumPropertyType.java | 5 +- .../types/PrimitivePropertyType.java | 3 +- .../properties/types/PropertyType.java | 1 + .../jalu/configme/resource/MapNormalizer.java | 6 ++- .../resource/PropertyPathTraverser.java | 7 +-- .../configme/resource/PropertyReader.java | 6 +++ .../configme/resource/YamlFileReader.java | 29 ++++++----- .../configme/resource/YamlFileResource.java | 39 +++++++------- .../resource/YamlFileResourceOptions.java | 19 +++---- .../jalu/configme/utils/CollectionUtils.java | 6 ++- .../utils/SettingsHolderClassValidator.java | 31 +++++------ .../jalu/configme/utils/TypeInformation.java | 10 ++-- .../java/ch/jalu/configme/utils/Utils.java | 3 +- 58 files changed, 334 insertions(+), 251 deletions(-) diff --git a/src/main/java/ch/jalu/configme/Comment.java b/src/main/java/ch/jalu/configme/Comment.java index c35c38d0..edeb0301 100644 --- a/src/main/java/ch/jalu/configme/Comment.java +++ b/src/main/java/ch/jalu/configme/Comment.java @@ -1,5 +1,7 @@ package ch.jalu.configme; +import org.jetbrains.annotations.NotNull; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -18,6 +20,6 @@ * * @return the comment to associate with the property */ - String[] value(); + String @NotNull [] value(); } diff --git a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java index 22091183..666e11f7 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java @@ -8,6 +8,7 @@ import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResourceOptions; import ch.jalu.configme.utils.Utils; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.io.File; @@ -21,7 +22,7 @@ public final class SettingsManagerBuilder { private final PropertyResource resource; private ConfigurationData configurationData; - private MigrationService migrationService; + private @org.jetbrains.annotations.Nullable MigrationService migrationService; private SettingsManagerBuilder(PropertyResource resource) { this.resource = resource; @@ -33,7 +34,7 @@ private SettingsManagerBuilder(PropertyResource resource) { * @param file the yaml file to use * @return settings manager builder */ - public static SettingsManagerBuilder withYamlFile(Path file) { + public static @NotNull SettingsManagerBuilder withYamlFile(@NotNull Path file) { return withYamlFile(file, YamlFileResourceOptions.builder().build()); } @@ -43,7 +44,7 @@ public static SettingsManagerBuilder withYamlFile(Path file) { * @param file the yaml file to use * @return settings manager builder */ - public static SettingsManagerBuilder withYamlFile(File file) { + public static @NotNull SettingsManagerBuilder withYamlFile(@NotNull File file) { return withYamlFile(file.toPath()); } @@ -54,7 +55,7 @@ public static SettingsManagerBuilder withYamlFile(File file) { * @param resourceOptions the resource options * @return settings manager builder */ - public static SettingsManagerBuilder withYamlFile(Path path, YamlFileResourceOptions resourceOptions) { + public static @NotNull SettingsManagerBuilder withYamlFile(@NotNull Path path, @NotNull YamlFileResourceOptions resourceOptions) { Utils.createFileIfNotExists(path); return new SettingsManagerBuilder(new YamlFileResource(path, resourceOptions)); } @@ -66,7 +67,7 @@ public static SettingsManagerBuilder withYamlFile(Path path, YamlFileResourceOpt * @param resourceOptions the resource options * @return settings manager builder */ - public static SettingsManagerBuilder withYamlFile(File file, YamlFileResourceOptions resourceOptions) { + public static @NotNull SettingsManagerBuilder withYamlFile(@NotNull File file, @NotNull YamlFileResourceOptions resourceOptions) { return withYamlFile(file.toPath(), resourceOptions); } @@ -76,7 +77,7 @@ public static SettingsManagerBuilder withYamlFile(File file, YamlFileResourceOpt * @param resource the resource to use * @return settings manager builder */ - public static SettingsManagerBuilder withResource(PropertyResource resource) { + public static @NotNull SettingsManagerBuilder withResource(PropertyResource resource) { return new SettingsManagerBuilder(resource); } @@ -87,7 +88,7 @@ public static SettingsManagerBuilder withResource(PropertyResource resource) { * @return this builder */ @SafeVarargs - public final SettingsManagerBuilder configurationData(Class... classes) { + public final @NotNull SettingsManagerBuilder configurationData(Class... classes) { this.configurationData = ConfigurationDataBuilder.createConfiguration(classes); return this; } @@ -98,7 +99,7 @@ public final SettingsManagerBuilder configurationData(Class T convertToBean(@Nullable Object value, Class clazz, ConvertError * @return export value to use */ @Nullable + @org.jetbrains.annotations.Nullable Object toExportValue(@Nullable Object object); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java index e657b684..cd1db1f8 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java @@ -7,6 +7,7 @@ import ch.jalu.configme.beanmapper.propertydescription.BeanPropertyDescription; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.util.ArrayList; @@ -76,7 +77,7 @@ protected final LeafValueHandler getLeafValueHandler() { return leafValueHandler; } - protected MappingContext createRootMappingContext(TypeInformation beanType, ConvertErrorRecorder errorRecorder) { + protected @NotNull MappingContext createRootMappingContext(TypeInformation beanType, ConvertErrorRecorder errorRecorder) { return MappingContextImpl.createRoot(beanType, errorRecorder); } @@ -86,7 +87,7 @@ protected MappingContext createRootMappingContext(TypeInformation beanType, Conv // --------- @Override - public Object toExportValue(Object value) { + public Object toExportValue(@org.jetbrains.annotations.Nullable Object value) { // Step 1: attempt simple value transformation Object simpleValue = leafValueHandler.toExportValue(value); if (simpleValue != null || value == null) { @@ -142,7 +143,7 @@ protected Object createExportValueForSpecialTypes(Object value) { return null; } - protected static Object unwrapReturnNull(Object o) { + protected static @org.jetbrains.annotations.Nullable Object unwrapReturnNull(Object o) { return o == RETURN_NULL ? null : o; } @@ -152,7 +153,7 @@ protected static Object unwrapReturnNull(Object o) { @Nullable @Override - public Object convertToBean(Object value, TypeInformation beanType, ConvertErrorRecorder errorRecorder) { + public Object convertToBean(@org.jetbrains.annotations.Nullable Object value, TypeInformation beanType, ConvertErrorRecorder errorRecorder) { if (value == null) { return null; } @@ -168,7 +169,7 @@ public Object convertToBean(Object value, TypeInformation beanType, ConvertError * @return object whose type matches the one in the mapping context, or null if not applicable */ @Nullable - protected Object convertValueForType(MappingContext context, Object value) { + protected Object convertValueForType(@NotNull MappingContext context, Object value) { Class rawClass = context.getTypeInformation().getSafeToWriteClass(); if (rawClass == null) { throw new ConfigMeMapperException(context, "Cannot determine required type"); @@ -198,7 +199,7 @@ protected Object convertValueForType(MappingContext context, Object value) { * @return object whose type matches the one in the mapping context, or null if not applicable */ @Nullable - protected Object handleSpecialTypes(MappingContext context, Object value) { + protected Object handleSpecialTypes(@NotNull MappingContext context, Object value) { final Class rawClass = context.getTypeInformation().getSafeToWriteClass(); if (Collection.class.isAssignableFrom(rawClass)) { return createCollection(context, value); @@ -221,7 +222,7 @@ protected Object handleSpecialTypes(MappingContext context, Object value) { */ @Nullable @SuppressWarnings("unchecked") - protected Collection createCollection(MappingContext context, Object value) { + protected @org.jetbrains.annotations.Nullable Collection createCollection(@NotNull MappingContext context, Object value) { if (value instanceof Iterable) { TypeInformation entryType = context.getGenericTypeInfoOrFail(0); Collection result = createCollectionMatchingType(context); @@ -246,7 +247,7 @@ protected Collection createCollection(MappingContext context, Object value) { * @param mappingContext the current mapping context with a collection type * @return Collection of matching type */ - protected Collection createCollectionMatchingType(MappingContext mappingContext) { + protected @NotNull Collection createCollectionMatchingType(@NotNull MappingContext mappingContext) { Class collectionType = mappingContext.getTypeInformation().getSafeToWriteClass(); if (collectionType.isAssignableFrom(ArrayList.class)) { return new ArrayList(); @@ -268,7 +269,7 @@ protected Collection createCollectionMatchingType(MappingContext mappingContext) */ @Nullable @SuppressWarnings("unchecked") - protected Map createMap(MappingContext context, Object value) { + protected Map createMap(@NotNull MappingContext context, Object value) { if (value instanceof Map) { if (context.getGenericTypeInfoOrFail(0).getSafeToWriteClass() != String.class) { throw new ConfigMeMapperException(context, "The key type of maps may only be of String type"); @@ -297,7 +298,7 @@ protected Map createMap(MappingContext context, Object value) { * @param mappingContext the current mapping context with a map type * @return Map of matching type */ - protected Map createMapMatchingType(MappingContext mappingContext) { + protected @NotNull Map createMapMatchingType(@NotNull MappingContext mappingContext) { Class mapType = mappingContext.getTypeInformation().getSafeToWriteClass(); if (mapType.isAssignableFrom(LinkedHashMap.class)) { return new LinkedHashMap(); @@ -310,7 +311,7 @@ protected Map createMapMatchingType(MappingContext mappingContext) { // -- Optional - protected Object createOptional(MappingContext context, Object value) { + protected Object createOptional(@NotNull MappingContext context, Object value) { MappingContext childContext = context.createChild("[v]", context.getGenericTypeInfoOrFail(0)); Object result = convertValueForType(childContext, value); return Optional.ofNullable(result); @@ -326,7 +327,7 @@ protected Object createOptional(MappingContext context, Object value) { * @return the converted value, or null if not possible */ @Nullable - protected Object createBean(MappingContext context, Object value) { + protected Object createBean(@NotNull MappingContext context, Object value) { // Ensure that the value is a map so we can map it to a bean if (!(value instanceof Map)) { return null; @@ -363,7 +364,7 @@ protected Object createBean(MappingContext context, Object value) { * @param mappingContext current mapping context * @return new instance of the given type */ - protected Object createBeanMatchingType(MappingContext mappingContext) { + protected @NotNull Object createBeanMatchingType(@NotNull MappingContext mappingContext) { // clazz is never null given the only path that leads to this method already performs that check final Class clazz = mappingContext.getTypeInformation().getSafeToWriteClass(); try { diff --git a/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java b/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java index 4c1d7d89..d0d975d2 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java @@ -1,6 +1,7 @@ package ch.jalu.configme.beanmapper; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.Nullable; /** * Holds necessary information for a certain value that is being mapped in the bean mapper. @@ -28,7 +29,7 @@ public interface MappingContext { * @param index the index to get generic type info for * @return the generic type info (throws exception if absent or not precise enough) */ - default TypeInformation getGenericTypeInfoOrFail(int index) { + default @Nullable TypeInformation getGenericTypeInfoOrFail(int index) { TypeInformation genericType = getTypeInformation().getGenericType(index); if (genericType == null || genericType.getSafeToWriteClass() == null) { throw new ConfigMeMapperException(this, diff --git a/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java b/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java index d4976345..78940b04 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java @@ -2,6 +2,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; /** * Standard implementation of {@link MappingContext}. @@ -25,12 +26,12 @@ protected MappingContextImpl(String path, TypeInformation typeInformation, Conve * @param errorRecorder error recorder to register errors even if a valid value is returned * @return root mapping context */ - public static MappingContextImpl createRoot(TypeInformation typeInformation, ConvertErrorRecorder errorRecorder) { + public static @NotNull MappingContextImpl createRoot(TypeInformation typeInformation, ConvertErrorRecorder errorRecorder) { return new MappingContextImpl("", typeInformation, errorRecorder); } @Override - public MappingContext createChild(String subPath, TypeInformation typeInformation) { + public @NotNull MappingContext createChild(String subPath, TypeInformation typeInformation) { if (path.isEmpty()) { return new MappingContextImpl(subPath, typeInformation, errorRecorder); } @@ -43,7 +44,7 @@ public TypeInformation getTypeInformation() { } @Override - public String createDescription() { + public @NotNull String createDescription() { return "Path: '" + path + "', type: '" + typeInformation.getType() + "'"; } @@ -53,7 +54,7 @@ public void registerError(String reason) { } @Override - public String toString() { + public @NotNull String toString() { return getClass().getSimpleName() + "[" + createDescription() + "]"; } } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java index fabd972b..5a2b38dc 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java @@ -1,6 +1,8 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Common parent of simple leaf value handlers which only need the required type as {@link Class} @@ -9,10 +11,10 @@ public abstract class AbstractLeafValueHandler implements LeafValueHandler { @Override - public Object convert(TypeInformation typeInformation, Object value) { + public Object convert(@NotNull TypeInformation typeInformation, Object value) { return convert(typeInformation.getSafeToWriteClass(), value); } - protected abstract Object convert(Class clazz, Object value); + protected abstract @Nullable Object convert(Class clazz, Object value); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java index d1ceb298..5c5f0f34 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java @@ -1,5 +1,7 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.NotNull; + import javax.annotation.Nullable; import java.math.BigDecimal; import java.math.BigInteger; @@ -14,7 +16,7 @@ public class BigNumberLeafValueHandler extends AbstractLeafValueHandler { private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E100"); @Override - protected Object convert(Class clazz, Object value) { + protected @org.jetbrains.annotations.Nullable Object convert(@NotNull Class clazz, Object value) { if (clazz != BigInteger.class && clazz != BigDecimal.class) { return null; } @@ -27,7 +29,7 @@ protected Object convert(Class clazz, Object value) { } @Override - public Object toExportValue(Object value) { + public Object toExportValue(@org.jetbrains.annotations.Nullable Object value) { final Class valueType = value == null ? null : value.getClass(); if (valueType == BigInteger.class) { return value.toString(); @@ -48,7 +50,7 @@ public Object toExportValue(Object value) { * @return BigInteger or BigDecimal as defined by the target class, or null if no conversion was possible */ @Nullable - protected Object fromString(Class targetClass, String value) { + protected Object fromString(Class targetClass, @NotNull String value) { try { return targetClass == BigInteger.class ? new BigInteger(value) @@ -65,7 +67,7 @@ protected Object fromString(Class targetClass, String value) { * @param value the value to convert * @return BigInteger or BigDecimal as defined by the target class */ - protected Object fromNumber(Class targetClass, Number value) { + protected Object fromNumber(@NotNull Class targetClass, Number value) { if (targetClass.isInstance(value)) { return value; } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java index 31f59aed..eff29d27 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java @@ -1,10 +1,12 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.Nullable; + /** Boolean leaf value handler. */ public class BooleanLeafValueHandler extends AbstractLeafValueHandler { @Override - public Object convert(Class clazz, Object value) { + public @Nullable Object convert(Class clazz, Object value) { if ((clazz == boolean.class || clazz == Boolean.class) && value instanceof Boolean) { return value; } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java index 2934c719..f9e85f4f 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java @@ -1,6 +1,7 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Collection; @@ -14,7 +15,7 @@ */ public class CombiningLeafValueHandler implements LeafValueHandler { - private final Collection handlers; + private final @NotNull Collection handlers; /** * Constructor. @@ -30,7 +31,7 @@ public CombiningLeafValueHandler(LeafValueHandler... handlers) { * * @param handlers the handlers to delegate each call to (in the iteration order of the collection) */ - public CombiningLeafValueHandler(Collection handlers) { + public CombiningLeafValueHandler(@NotNull Collection handlers) { this.handlers = Collections.unmodifiableCollection(handlers); } @@ -44,7 +45,7 @@ public Object toExportValue(Object value) { return getFirstNonNull(t -> t.toExportValue(value)); } - protected final Collection getHandlers() { + protected final @NotNull Collection getHandlers() { return handlers; } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java index 6b208ce7..98c5c44f 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java @@ -1,10 +1,13 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + /** Enum handler. */ public class EnumLeafValueHandler extends AbstractLeafValueHandler { @Override - public Object convert(Class clazz, Object value) { + public @Nullable Object convert(@NotNull Class clazz, Object value) { if (value instanceof String && Enum.class.isAssignableFrom(clazz)) { String givenText = (String) value; for (Enum e : (Enum[]) clazz.getEnumConstants()) { diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java index dcb08ae8..85269208 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java @@ -26,6 +26,7 @@ public interface LeafValueHandler { * @return value of the given type, or null if not applicable */ @Nullable + @org.jetbrains.annotations.Nullable Object convert(TypeInformation typeInformation, @Nullable Object value); /** @@ -40,6 +41,7 @@ public interface LeafValueHandler { * @return value to use in the export, or null if not applicable */ @Nullable + @org.jetbrains.annotations.Nullable Object toExportValue(@Nullable Object value); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java index 67aefd5e..be5e90f4 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java @@ -1,5 +1,8 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -14,7 +17,7 @@ public class NumberLeafValueHandler extends AbstractLeafValueHandler { createMapOfTypeToTransformFunction(); @Override - public Object convert(Class clazz, Object value) { + public @Nullable Object convert(Class clazz, Object value) { if (value instanceof Number) { Function numberFunction = NUMBER_CLASSES_TO_CONVERSION.get(clazz); return numberFunction == null ? null : numberFunction.apply((Number) value); @@ -23,7 +26,7 @@ public Object convert(Class clazz, Object value) { } @Override - public Object toExportValue(Object value) { + public Object toExportValue(@Nullable Object value) { Class clazz = value == null ? null : value.getClass(); if (NUMBER_CLASSES_TO_CONVERSION.containsKey(clazz)) { return value; @@ -31,7 +34,7 @@ public Object toExportValue(Object value) { return null; } - private static Map, Function> createMapOfTypeToTransformFunction() { + private static @NotNull Map, Function> createMapOfTypeToTransformFunction() { Map, Function> map = new HashMap<>(); map.put(byte.class, Number::byteValue); map.put(Byte.class, Number::byteValue); diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java index da673e2b..78afb8f7 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java @@ -1,10 +1,12 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.Nullable; + /** Object handler. */ public class ObjectLeafValueHandler extends AbstractLeafValueHandler { @Override - public Object convert(Class clazz, Object value) { + public @Nullable Object convert(Class clazz, Object value) { if (clazz == Object.class) { return value; } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java index 51417363..6e57fbdf 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java @@ -1,10 +1,12 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.Nullable; + /** String handler. */ public class StringLeafValueHandler extends AbstractLeafValueHandler { @Override - public Object convert(Class clazz, Object value) { + public @Nullable Object convert(Class clazz, Object value) { if (clazz == String.class && (value instanceof String || value instanceof Number || value instanceof Boolean)) { return value.toString(); diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java index 69a5e49c..20524be7 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java @@ -3,6 +3,7 @@ import ch.jalu.configme.beanmapper.ConfigMeMapperException; import ch.jalu.configme.beanmapper.ExportName; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.beans.IntrospectionException; @@ -70,7 +71,7 @@ protected List collectAllProperties(Class clazz) { * @return the converted object, or null if the property should be skipped */ @Nullable - protected BeanPropertyDescription convert(PropertyDescriptor descriptor) { + protected BeanPropertyDescription convert(@NotNull PropertyDescriptor descriptor) { if (Boolean.TRUE.equals(descriptor.getValue("transient"))) { return null; } @@ -88,7 +89,7 @@ protected BeanPropertyDescription convert(PropertyDescriptor descriptor) { * @param clazz the class to which the properties belong * @param properties the properties that will be used on the class */ - protected void validateProperties(Class clazz, Collection properties) { + protected void validateProperties(Class clazz, @NotNull Collection properties) { Set names = new HashSet<>(properties.size()); properties.forEach(property -> { if (property.getName().isEmpty()) { @@ -107,7 +108,7 @@ protected void validateProperties(Class clazz, Collection getWritableProperties(Class clazz) { + protected @NotNull List getWritableProperties(Class clazz) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); @@ -151,7 +152,7 @@ protected List getWritableProperties(Class clazz) { * @param properties the properties to sort * @return sorted properties */ - protected List sortPropertiesList(Class clazz, List properties) { + protected @NotNull List sortPropertiesList(Class clazz, @NotNull List properties) { Map fieldNameByIndex = createFieldNameOrderMap(clazz); int maxIndex = fieldNameByIndex.size(); @@ -170,7 +171,7 @@ protected List sortPropertiesList(Class clazz, List createFieldNameOrderMap(Class clazz) { + protected @NotNull Map createFieldNameOrderMap(Class clazz) { Map nameByIndex = new HashMap<>(); int i = 0; for (Class currentClass : collectClassAndAllParents(clazz)) { @@ -189,7 +190,7 @@ protected Map createFieldNameOrderMap(Class clazz) { * @param clazz the class whose parents should be collected * @return list of all of the class' parents, sorted by highest class in the hierarchy to lowest */ - protected List> collectClassAndAllParents(Class clazz) { + protected @NotNull List> collectClassAndAllParents(Class clazz) { List> parents = new ArrayList<>(); Class curClass = clazz; while (curClass != null && curClass != Object.class) { diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java index 9dc26810..b3757c88 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java @@ -38,5 +38,6 @@ public interface BeanPropertyDescription { * @return the value of the property (can be null) */ @Nullable + @org.jetbrains.annotations.Nullable Object getValue(Object bean); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java index 65c9d678..773adee6 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java @@ -2,6 +2,7 @@ import ch.jalu.configme.beanmapper.ConfigMeMapperException; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; @@ -52,7 +53,7 @@ public TypeInformation getTypeInformation() { public Object getValue(Object bean) { try { return getter.invoke(bean); - } catch (IllegalAccessException | InvocationTargetException e) { + } catch (@NotNull IllegalAccessException | InvocationTargetException e) { throw new ConfigMeMapperException( "Could not get property '" + name + "' from instance '" + bean + "'", e); } @@ -67,14 +68,14 @@ public Object getValue(Object bean) { public void setValue(Object bean, Object value) { try { setter.invoke(bean, value); - } catch (IllegalAccessException | InvocationTargetException e) { + } catch (@NotNull IllegalAccessException | InvocationTargetException e) { throw new ConfigMeMapperException( "Could not set property '" + name + "' to value '" + value + "' on instance '" + bean + "'", e); } } @Override - public String toString() { + public @NotNull String toString() { return "Bean property '" + name + "' with getter '" + getter + "'"; } } diff --git a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java index f7fcc27d..8d2578b0 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java +++ b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java @@ -1,6 +1,7 @@ package ch.jalu.configme.configurationdata; import ch.jalu.configme.SettingsHolder; +import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Collections; @@ -47,7 +48,7 @@ public void setComment(String path, String... commentLines) { * * @return map with all comments */ - public Map> getAllComments() { + public @NotNull Map> getAllComments() { return Collections.unmodifiableMap(comments); } } diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java index c2ec6cb3..7e7abb07 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java @@ -4,6 +4,7 @@ import ch.jalu.configme.SettingsHolder; import ch.jalu.configme.exception.ConfigMeException; import ch.jalu.configme.properties.Property; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.lang.reflect.Constructor; @@ -25,9 +26,9 @@ public class ConfigurationDataBuilder { @SuppressWarnings("checkstyle:VisibilityModifier") - protected PropertyListBuilder propertyListBuilder = new PropertyListBuilder(); + protected @NotNull PropertyListBuilder propertyListBuilder = new PropertyListBuilder(); @SuppressWarnings("checkstyle:VisibilityModifier") - protected CommentsConfiguration commentsConfiguration = new CommentsConfiguration(); + protected @NotNull CommentsConfiguration commentsConfiguration = new CommentsConfiguration(); protected ConfigurationDataBuilder() { } @@ -40,7 +41,7 @@ protected ConfigurationDataBuilder() { * @return collected configuration data */ @SafeVarargs - public static ConfigurationData createConfiguration(Class... classes) { + public static @NotNull ConfigurationData createConfiguration(Class... classes) { return createConfiguration(Arrays.asList(classes)); } @@ -51,17 +52,17 @@ public static ConfigurationData createConfiguration(Class> classes) { + public static @NotNull ConfigurationData createConfiguration(@NotNull Iterable> classes) { ConfigurationDataBuilder builder = new ConfigurationDataBuilder(); return builder.collectData(classes); } - public static ConfigurationData createConfiguration(List> properties) { + public static @NotNull ConfigurationData createConfiguration(@NotNull List> properties) { return new ConfigurationDataImpl(properties, Collections.emptyMap()); } - public static ConfigurationData createConfiguration(List> properties, - CommentsConfiguration commentsConfiguration) { + public static @NotNull ConfigurationData createConfiguration(@NotNull List> properties, + @NotNull CommentsConfiguration commentsConfiguration) { return new ConfigurationDataImpl(properties, commentsConfiguration.getAllComments()); } @@ -72,7 +73,7 @@ public static ConfigurationData createConfiguration(List> * @param classes the classes to process * @return configuration data with the classes' data */ - protected ConfigurationData collectData(Iterable> classes) { + protected @NotNull ConfigurationData collectData(@NotNull Iterable> classes) { for (Class clazz : classes) { collectProperties(clazz); collectSectionComments(clazz); @@ -85,7 +86,7 @@ protected ConfigurationData collectData(Iterable * * @param clazz the class to process */ - protected void collectProperties(Class clazz) { + protected void collectProperties(@NotNull Class clazz) { findFieldsToProcess(clazz).forEach(field -> { Property property = getPropertyField(field); if (property != null) { @@ -95,7 +96,7 @@ protected void collectProperties(Class clazz) { }); } - protected void setCommentForPropertyField(Field field, String path) { + protected void setCommentForPropertyField(@NotNull Field field, String path) { Comment commentAnnotation = field.getAnnotation(Comment.class); if (commentAnnotation != null) { commentsConfiguration.setComment(path, commentAnnotation.value()); @@ -109,7 +110,7 @@ protected void setCommentForPropertyField(Field field, String path) { * @return the property the field defines, or null if not applicable */ @Nullable - protected Property getPropertyField(Field field) { + protected Property getPropertyField(@NotNull Field field) { if (Property.class.isAssignableFrom(field.getType()) && Modifier.isStatic(field.getModifiers())) { try { return (Property) field.get(null); @@ -121,7 +122,7 @@ protected Property getPropertyField(Field field) { return null; } - protected void collectSectionComments(Class clazz) { + protected void collectSectionComments(@NotNull Class clazz) { SettingsHolder settingsHolder = createSettingsHolderInstance(clazz); settingsHolder.registerComments(commentsConfiguration); } @@ -133,14 +134,14 @@ protected void collectSectionComments(Class clazz) { * @param the class type * @return instance of the class */ - protected T createSettingsHolderInstance(Class clazz) { + protected @NotNull T createSettingsHolderInstance(@NotNull Class clazz) { try { Constructor constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (NoSuchMethodException e) { throw new ConfigMeException("Expected no-args constructor to be available for " + clazz, e); - } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { + } catch (@NotNull IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new ConfigMeException("Could not create instance of " + clazz, e); } } @@ -152,7 +153,7 @@ protected T createSettingsHolderInstance(Class cla * @param clazz the class whose fields should be returned * @return stream of all the fields to process */ - protected Stream findFieldsToProcess(Class clazz) { + protected Stream findFieldsToProcess(@NotNull Class clazz) { // In most cases we expect the class not to have any parent, so we check here and "fast track" this case if (Object.class.equals(clazz.getSuperclass())) { return Arrays.stream(clazz.getDeclaredFields()); diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java index 306e617c..ab2aa5b5 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java @@ -5,6 +5,7 @@ import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.HashMap; @@ -18,9 +19,9 @@ */ public class ConfigurationDataImpl implements ConfigurationData { - private final List> properties; - private final Map> allComments; - private final Map values; + private final @NotNull List> properties; + private final @NotNull Map> allComments; + private final @NotNull Map values; private boolean allPropertiesValidInResource; /** @@ -29,14 +30,14 @@ public class ConfigurationDataImpl implements ConfigurationData { * @param allProperties all known properties * @param allComments map of comments by path */ - protected ConfigurationDataImpl(List> allProperties, Map> allComments) { + protected ConfigurationDataImpl(@NotNull List> allProperties, @NotNull Map> allComments) { this.properties = Collections.unmodifiableList(allProperties); this.allComments = Collections.unmodifiableMap(allComments); this.values = new HashMap<>(); } @Override - public List> getProperties() { + public @NotNull List> getProperties() { return properties; } @@ -46,13 +47,13 @@ public List getCommentsForSection(String path) { } @Override - public Map> getAllComments() { + public @NotNull Map> getAllComments() { return allComments; } @Override @SuppressWarnings("unchecked") - public T getValue(Property property) { + public @NotNull T getValue(@NotNull Property property) { Object value = values.get(property.getPath()); if (value == null) { throw new ConfigMeException(format("No value exists for property with path '%s'. This may happen if " @@ -63,7 +64,7 @@ public T getValue(Property property) { } @Override - public void setValue(Property property, T value) { + public void setValue(@NotNull Property property, T value) { if (property.isValidValue(value)) { values.put(property.getPath(), value); } else { @@ -84,7 +85,7 @@ public void initializeValues(PropertyReader reader) { * Saves the value for the provided property as determined from the reader and returns whether the * property is represented in a fully valid way in the resource. */ - protected boolean setValueForProperty(Property property, PropertyReader reader) { + protected boolean setValueForProperty(@NotNull Property property, PropertyReader reader) { PropertyValue propertyValue = property.determineValue(reader); setValue(property, propertyValue.getValue()); return propertyValue.isValidInResource(); @@ -95,7 +96,7 @@ public boolean areAllValuesValidInResource() { return allPropertiesValidInResource; } - protected Map getValues() { + protected @NotNull Map getValues() { return values; } } diff --git a/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java b/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java index 707fa242..2c51b556 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java +++ b/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java @@ -2,6 +2,7 @@ import ch.jalu.configme.exception.ConfigMeException; import ch.jalu.configme.properties.Property; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -23,14 +24,14 @@ */ public class PropertyListBuilder { - private Map rootEntries = new LinkedHashMap<>(); + private @NotNull Map rootEntries = new LinkedHashMap<>(); /** * Adds the property to the list builder. * * @param property the property to add */ - public void add(Property property) { + public void add(@NotNull Property property) { String[] paths = property.getPath().split("\\."); Map map = rootEntries; for (int i = 0; i < paths.length - 1; ++i) { @@ -50,17 +51,17 @@ public void add(Property property) { * * @return ordered list of registered properties */ - public List> create() { + public @NotNull List> create() { List> result = new ArrayList<>(); collectEntries(rootEntries, result); return result; } - protected final Map getRootEntries() { + protected final @NotNull Map getRootEntries() { return rootEntries; } - private static Map getChildMap(Map parent, String path) { + private static @NotNull Map getChildMap(@NotNull Map parent, String path) { Object o = parent.get(path); if (o instanceof Map) { return asTypedMap(o); @@ -77,7 +78,7 @@ private static Map getChildMap(Map parent, Strin } } - private static void collectEntries(Map map, List> results) { + private static void collectEntries(@NotNull Map map, @NotNull List> results) { for (Object o : map.values()) { if (o instanceof Map) { collectEntries(asTypedMap(o), results); @@ -88,7 +89,7 @@ private static void collectEntries(Map map, List> re } @SuppressWarnings("unchecked") - private static Map asTypedMap(Object o) { + private static @NotNull Map asTypedMap(@NotNull Object o) { return (Map) o; } } diff --git a/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java b/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java index 50fadce5..ec3e58c9 100644 --- a/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java +++ b/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java @@ -4,6 +4,7 @@ import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; /** * Simple migration service that can be extended. @@ -11,7 +12,7 @@ public class PlainMigrationService implements MigrationService { @Override - public boolean checkAndMigrate(PropertyReader reader, ConfigurationData configurationData) { + public boolean checkAndMigrate(PropertyReader reader, @NotNull ConfigurationData configurationData) { if (performMigrations(reader, configurationData) == MIGRATION_REQUIRED || !configurationData.areAllValuesValidInResource()) { return MIGRATION_REQUIRED; @@ -47,8 +48,8 @@ protected boolean performMigrations(PropertyReader reader, ConfigurationData con * @param the type of the property * @return true if the old path exists in the configuration file, false otherwise */ - protected static boolean moveProperty(Property oldProperty, Property newProperty, - PropertyReader reader, ConfigurationData configurationData) { + protected static boolean moveProperty(@NotNull Property oldProperty, @NotNull Property newProperty, + @NotNull PropertyReader reader, @NotNull ConfigurationData configurationData) { if (reader.contains(oldProperty.getPath())) { if (!reader.contains(newProperty.getPath())) { PropertyValue value = oldProperty.determineValue(reader); diff --git a/src/main/java/ch/jalu/configme/properties/ArrayProperty.java b/src/main/java/ch/jalu/configme/properties/ArrayProperty.java index 5c25f2d7..ef1cdb5b 100644 --- a/src/main/java/ch/jalu/configme/properties/ArrayProperty.java +++ b/src/main/java/ch/jalu/configme/properties/ArrayProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.Objects; @@ -30,7 +31,7 @@ public ArrayProperty(String path, T[] defaultValue, PropertyType type, IntFun } @Override - protected T[] getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected T[] getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { Object object = reader.getObject(this.getPath()); if (object instanceof Collection) { Collection collection = (Collection) object; @@ -43,7 +44,7 @@ protected T[] getFromReader(PropertyReader reader, ConvertErrorRecorder errorRec } @Override - public Object toExportValue(T[] value) { + public Object toExportValue(T @NotNull [] value) { Object[] array = new Object[value.length]; for (int i = 0; i < array.length; i++) { diff --git a/src/main/java/ch/jalu/configme/properties/BaseProperty.java b/src/main/java/ch/jalu/configme/properties/BaseProperty.java index 992b94d1..f0adb865 100644 --- a/src/main/java/ch/jalu/configme/properties/BaseProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BaseProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.util.Objects; @@ -44,7 +45,7 @@ public T getDefaultValue() { } @Override - public PropertyValue determineValue(PropertyReader reader) { + public @NotNull PropertyValue determineValue(PropertyReader reader) { ConvertErrorRecorder errorRecorder = new ConvertErrorRecorder(); T value = getFromReader(reader, errorRecorder); if (isValidValue(value)) { @@ -54,7 +55,7 @@ public PropertyValue determineValue(PropertyReader reader) { } @Override - public boolean isValidValue(T value) { + public boolean isValidValue(@org.jetbrains.annotations.Nullable T value) { return value != null; } @@ -67,10 +68,10 @@ public boolean isValidValue(T value) { * @return value based on the reader, or null if not applicable */ @Nullable - protected abstract T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder); + protected abstract @org.jetbrains.annotations.Nullable T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder); @Override - public String toString() { + public @NotNull String toString() { return "Property '" + path + "'"; } } diff --git a/src/main/java/ch/jalu/configme/properties/BeanProperty.java b/src/main/java/ch/jalu/configme/properties/BeanProperty.java index 7ab48e04..8c53f660 100644 --- a/src/main/java/ch/jalu/configme/properties/BeanProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BeanProperty.java @@ -5,6 +5,7 @@ import ch.jalu.configme.exception.ConfigMeException; import ch.jalu.configme.properties.types.BeanPropertyType; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; public class BeanProperty extends TypeBasedProperty { @@ -25,7 +26,7 @@ public BeanProperty(Class beanType, String path, T defaultValue, Mapper mappe * @param defaultValue the default value * @param mapper the mapper to map with */ - protected BeanProperty(TypeInformation beanType, String path, T defaultValue, Mapper mapper) { + protected BeanProperty(@NotNull TypeInformation beanType, String path, T defaultValue, Mapper mapper) { super(path, defaultValue, new BeanPropertyType<>(beanType, mapper)); if (!beanType.getSafeToWriteClass().isInstance(defaultValue)) { throw new ConfigMeException( diff --git a/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java b/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java index 21e08753..2ece251a 100644 --- a/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java +++ b/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.inlinearray.InlineArrayConverter; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -30,7 +31,7 @@ public InlineArrayProperty(String path, T[] defaultValue, InlineArrayConverter type, T... defaultValue) { * @param type the property type * @param defaultValue the default value of the property */ - public ListProperty(String path, PropertyType type, List defaultValue) { + public ListProperty(String path, PropertyType type, @NotNull List defaultValue) { super(path, Collections.unmodifiableList(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; @@ -47,7 +48,7 @@ public ListProperty(String path, PropertyType type, List defaultValue) { @Nullable @Override - protected List getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected List getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { List list = reader.getList(getPath()); if (list != null) { @@ -60,7 +61,7 @@ protected List getFromReader(PropertyReader reader, ConvertErrorRecorder erro } @Override - public Object toExportValue(List value) { + public @org.jetbrains.annotations.Nullable Object toExportValue(@NotNull List value) { return value.stream() .map(type::toExportValue) .collect(Collectors.toList()); diff --git a/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java b/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java index 4db14cbf..1473c702 100644 --- a/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java @@ -1,5 +1,7 @@ package ch.jalu.configme.properties; +import org.jetbrains.annotations.NotNull; + import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashSet; @@ -22,7 +24,7 @@ public class LowercaseStringSetProperty extends StringSetProperty { * @param path property path * @param defaultEntries entries in the Set that is the default value */ - public LowercaseStringSetProperty(String path, String... defaultEntries) { + public LowercaseStringSetProperty(String path, String @NotNull ... defaultEntries) { super(path, toLowercaseLinkedHashSet(Arrays.stream(defaultEntries))); } @@ -32,17 +34,17 @@ public LowercaseStringSetProperty(String path, String... defaultEntries) { * @param path property path * @param defaultEntries entries in the Set that is the default value */ - public LowercaseStringSetProperty(String path, Collection defaultEntries) { + public LowercaseStringSetProperty(String path, @NotNull Collection defaultEntries) { super(path, toLowercaseLinkedHashSet(defaultEntries.stream())); } @Override - protected Collector> setCollector() { + protected @NotNull Collector> setCollector() { Function toLowerCaseFn = value -> String.valueOf(value).toLowerCase(); return Collectors.mapping(toLowerCaseFn, super.setCollector()); } - protected static Set toLowercaseLinkedHashSet(Stream valuesStream) { + protected static @NotNull Set toLowercaseLinkedHashSet(@NotNull Stream valuesStream) { Set valuesLowercase = valuesStream .map(String::toLowerCase) .collect(Collectors.toCollection(LinkedHashSet::new)); diff --git a/src/main/java/ch/jalu/configme/properties/MapProperty.java b/src/main/java/ch/jalu/configme/properties/MapProperty.java index 83a97ce3..131c1782 100644 --- a/src/main/java/ch/jalu/configme/properties/MapProperty.java +++ b/src/main/java/ch/jalu/configme/properties/MapProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.LinkedHashMap; @@ -26,14 +27,14 @@ public class MapProperty extends BaseProperty> { * @param defaultValue the default value of the property * @param type the property type of the values */ - public MapProperty(String path, Map defaultValue, PropertyType type) { + public MapProperty(String path, @NotNull Map defaultValue, PropertyType type) { super(path, Collections.unmodifiableMap(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; } @Override - protected Map getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected Map getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { Object rawObject = reader.getObject(getPath()); if (!(rawObject instanceof Map)) { @@ -56,7 +57,7 @@ protected Map getFromReader(PropertyReader reader, ConvertErrorRecord } @Override - public Object toExportValue(Map value) { + public Object toExportValue(@NotNull Map value) { Map exportMap = new LinkedHashMap<>(); for (Map.Entry entry : value.entrySet()) { @@ -67,7 +68,7 @@ public Object toExportValue(Map value) { } /* Allows to modify the map once its fully built based on the values in the property reader. */ - protected Map postProcessMap(Map constructedMap) { + protected @NotNull Map postProcessMap(@NotNull Map constructedMap) { return Collections.unmodifiableMap(constructedMap); } } diff --git a/src/main/java/ch/jalu/configme/properties/OptionalProperty.java b/src/main/java/ch/jalu/configme/properties/OptionalProperty.java index 1bee0f04..179cd67a 100644 --- a/src/main/java/ch/jalu/configme/properties/OptionalProperty.java +++ b/src/main/java/ch/jalu/configme/properties/OptionalProperty.java @@ -2,6 +2,8 @@ import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Optional; @@ -21,7 +23,7 @@ public OptionalProperty(Property baseProperty) { this.defaultValue = Optional.empty(); } - public OptionalProperty(Property baseProperty, T defaultValue) { + public OptionalProperty(Property baseProperty, @NotNull T defaultValue) { this.baseProperty = baseProperty; this.defaultValue = Optional.of(defaultValue); } @@ -32,7 +34,7 @@ public String getPath() { } @Override - public PropertyValue> determineValue(PropertyReader reader) { + public @NotNull PropertyValue> determineValue(@NotNull PropertyReader reader) { PropertyValue basePropertyValue = baseProperty.determineValue(reader); Optional value = basePropertyValue.isValidInResource() ? Optional.ofNullable(basePropertyValue.getValue()) @@ -52,7 +54,7 @@ public Optional getDefaultValue() { } @Override - public boolean isValidValue(Optional value) { + public boolean isValidValue(@Nullable Optional value) { if (value == null) { return false; } @@ -60,7 +62,7 @@ public boolean isValidValue(Optional value) { } @Override - public Object toExportValue(Optional value) { + public Object toExportValue(@NotNull Optional value) { return value.map(baseProperty::toExportValue).orElse(null); } } diff --git a/src/main/java/ch/jalu/configme/properties/Property.java b/src/main/java/ch/jalu/configme/properties/Property.java index 6e428c15..ff8e6470 100644 --- a/src/main/java/ch/jalu/configme/properties/Property.java +++ b/src/main/java/ch/jalu/configme/properties/Property.java @@ -82,6 +82,7 @@ default boolean isValidInResource(PropertyReader propertyReader) { * @return value to use for export, null to skip the property */ @Nullable + @org.jetbrains.annotations.Nullable Object toExportValue(T value); } diff --git a/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java b/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java index f3bc6076..0f76084e 100644 --- a/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java +++ b/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java @@ -2,6 +2,7 @@ import ch.jalu.configme.properties.inlinearray.InlineArrayConverter; import ch.jalu.configme.properties.types.PropertyType; +import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.LinkedHashMap; @@ -41,7 +42,7 @@ public PropertyBuilder(PropertyType type) { * @param path the path * @return this builder */ - public B path(String path) { + public @NotNull B path(String path) { this.path = path; return (B) this; } @@ -62,7 +63,7 @@ public B defaultValue(T defaultValue) { * * @return the created property */ - public abstract Property build(); + public abstract @NotNull Property build(); protected final String getPath() { return path; @@ -88,13 +89,13 @@ public MapPropertyBuilder(PropertyType type) { defaultValue(new LinkedHashMap<>()); } - public MapPropertyBuilder defaultEntry(String key, T value) { + public @NotNull MapPropertyBuilder defaultEntry(String key, T value) { getDefaultValue().put(key, value); return this; } @Override - public MapProperty build() { + public @NotNull MapProperty build() { return new MapProperty<>(getPath(), getDefaultValue(), getType()); } } @@ -112,13 +113,13 @@ public TypeBasedPropertyBuilder(PropertyType type) { super(type); } - public TypeBasedPropertyBuilder createFunction(CreateFunction createFunction) { + public @NotNull TypeBasedPropertyBuilder createFunction(CreateFunction createFunction) { this.createFunction = createFunction; return this; } @Override - public Property build() { + public @NotNull Property build() { return createFunction.apply(getPath(), getDefaultValue(), getType()); } } @@ -143,7 +144,7 @@ public ArrayPropertyBuilder defaultValue(T... defaultValue) { } @Override - public Property build() { + public @NotNull Property build() { return new ArrayProperty<>(getPath(), getDefaultValue(), getType(), arrayProducer); } } @@ -168,7 +169,7 @@ public InlineArrayPropertyBuilder defaultValue(T... defaultValue) { } @Override - public Property build() { + public @NotNull Property build() { return new InlineArrayProperty<>(getPath(), getDefaultValue(), inlineConverter); } } @@ -189,7 +190,7 @@ public ListPropertyBuilder defaultValue(T... defaultValue) { } @Override - public Property> build() { + public @NotNull Property> build() { return new ListProperty<>(getPath(), getType(), getDefaultValue()); } } @@ -205,14 +206,14 @@ public SetPropertyBuilder(PropertyType type) { super(type); } - public SetPropertyBuilder defaultValue(T... defaultValue) { + public SetPropertyBuilder defaultValue(T @NotNull ... defaultValue) { Set defaultSet = Arrays.stream(defaultValue) .collect(Collectors.toCollection(LinkedHashSet::new)); return super.defaultValue(defaultSet); } @Override - public Property> build() { + public @NotNull Property> build() { return new SetProperty<>(getPath(), getType(), getDefaultValue()); } } @@ -226,7 +227,7 @@ public Property> build() { @FunctionalInterface public interface CreateFunction { - Property apply(String path, T defaultValue, PropertyType type); + @NotNull Property apply(String path, T defaultValue, PropertyType type); } diff --git a/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java b/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java index 0bc465c1..a21d2528 100644 --- a/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java +++ b/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java @@ -2,6 +2,7 @@ import ch.jalu.configme.properties.inlinearray.InlineArrayConverter; import ch.jalu.configme.properties.types.PropertyType; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; @@ -31,7 +32,7 @@ protected PropertyInitializer() { * @param defaultValue the default value * @return the created property */ - public static Property newProperty(String path, boolean defaultValue) { + public static @NotNull Property newProperty(String path, boolean defaultValue) { return new BooleanProperty(path, defaultValue); } @@ -42,7 +43,7 @@ public static Property newProperty(String path, boolean defaultValue) { * @param defaultValue the default value * @return the created property */ - public static Property newProperty(String path, int defaultValue) { + public static @NotNull Property newProperty(String path, int defaultValue) { return new IntegerProperty(path, defaultValue); } @@ -53,7 +54,7 @@ public static Property newProperty(String path, int defaultValue) { * @param defaultValue the default value * @return the created property */ - public static Property newProperty(String path, double defaultValue) { + public static @NotNull Property newProperty(String path, double defaultValue) { return new DoubleProperty(path, defaultValue); } @@ -64,7 +65,7 @@ public static Property newProperty(String path, double defaultValue) { * @param defaultValue the default value * @return the created property */ - public static Property newProperty(String path, String defaultValue) { + public static @NotNull Property newProperty(String path, String defaultValue) { return new StringProperty(path, defaultValue); } @@ -77,7 +78,7 @@ public static Property newProperty(String path, String defaultValue) { * @param the enum type * @return the created enum property */ - public static > Property newProperty(Class clazz, String path, E defaultValue) { + public static > @NotNull Property newProperty(Class clazz, String path, E defaultValue) { return new EnumProperty<>(clazz, path, defaultValue); } @@ -88,7 +89,7 @@ public static > Property newProperty(Class clazz, String * @param defaultRegexValue the default pattern of the property * @return the created regex property */ - public static RegexProperty newRegexProperty(String path, String defaultRegexValue) { + public static @NotNull RegexProperty newRegexProperty(String path, @NotNull String defaultRegexValue) { return new RegexProperty(path, defaultRegexValue); } @@ -99,7 +100,7 @@ public static RegexProperty newRegexProperty(String path, String defaultRegexVal * @param defaultRegexValue the default pattern of the property * @return the created regex property */ - public static RegexProperty newRegexProperty(String path, Pattern defaultRegexValue) { + public static @NotNull RegexProperty newRegexProperty(String path, Pattern defaultRegexValue) { return new RegexProperty(path, defaultRegexValue); } @@ -110,7 +111,7 @@ public static RegexProperty newRegexProperty(String path, Pattern defaultRegexVa * @param defaultValues the items in the default list * @return the created list property */ - public static Property> newListProperty(String path, String... defaultValues) { + public static @NotNull Property> newListProperty(String path, String... defaultValues) { // does not have the same name as not to clash with #newProperty(String, String) return new StringListProperty(path, defaultValues); } @@ -122,7 +123,7 @@ public static Property> newListProperty(String path, String... defa * @param defaultValues the default value of the property * @return the created list property */ - public static Property> newListProperty(String path, List defaultValues) { + public static @NotNull Property> newListProperty(String path, @NotNull List defaultValues) { // does not have the same name as not to clash with #newProperty(String, String) return new StringListProperty(path, defaultValues); } @@ -134,7 +135,7 @@ public static Property> newListProperty(String path, List d * @param defaultValues the items in the default set * @return the created set property */ - public static Property> newSetProperty(String path, String... defaultValues) { + public static @NotNull Property> newSetProperty(String path, String... defaultValues) { return new StringSetProperty(path, defaultValues); } @@ -145,7 +146,7 @@ public static Property> newSetProperty(String path, String... defaul * @param defaultValues the default value of the property * @return the created set property */ - public static Property> newSetProperty(String path, Set defaultValues) { + public static @NotNull Property> newSetProperty(String path, @NotNull Set defaultValues) { return new StringSetProperty(path, defaultValues); } @@ -156,7 +157,7 @@ public static Property> newSetProperty(String path, Set defa * @param defaultValues the items in the default set * @return the created set property */ - public static Property> newLowercaseStringSetProperty(String path, String... defaultValues) { + public static @NotNull Property> newLowercaseStringSetProperty(String path, String... defaultValues) { return new LowercaseStringSetProperty(path, defaultValues); } @@ -167,7 +168,7 @@ public static Property> newLowercaseStringSetProperty(String path, S * @param defaultValues the default value of the property * @return the created set property */ - public static Property> newLowercaseStringSetProperty(String path, Collection defaultValues) { + public static @NotNull Property> newLowercaseStringSetProperty(String path, @NotNull Collection defaultValues) { return new LowercaseStringSetProperty(path, defaultValues); } @@ -180,35 +181,35 @@ public static Property> newLowercaseStringSetProperty(String path, C * @param the bean type * @return the created bean property */ - public static Property newBeanProperty(Class beanClass, String path, B defaultValue) { + public static @NotNull Property newBeanProperty(Class beanClass, String path, B defaultValue) { return new BeanProperty<>(beanClass, path, defaultValue); } // -------------- // Property builders // -------------- - public static PropertyBuilder.TypeBasedPropertyBuilder typeBasedProperty(PropertyType type) { + public static PropertyBuilder.@NotNull TypeBasedPropertyBuilder typeBasedProperty(PropertyType type) { return new PropertyBuilder.TypeBasedPropertyBuilder<>(type); } - public static PropertyBuilder.ListPropertyBuilder listProperty(PropertyType type) { + public static PropertyBuilder.@NotNull ListPropertyBuilder listProperty(PropertyType type) { return new PropertyBuilder.ListPropertyBuilder<>(type); } - public static PropertyBuilder.SetPropertyBuilder setProperty(PropertyType type) { + public static PropertyBuilder.@NotNull SetPropertyBuilder setProperty(PropertyType type) { return new PropertyBuilder.SetPropertyBuilder<>(type); } - public static PropertyBuilder.MapPropertyBuilder mapProperty(PropertyType type) { + public static PropertyBuilder.@NotNull MapPropertyBuilder mapProperty(PropertyType type) { return new PropertyBuilder.MapPropertyBuilder<>(type); } - public static PropertyBuilder.ArrayPropertyBuilder arrayProperty(PropertyType type, - IntFunction arrayProducer) { + public static PropertyBuilder.@NotNull ArrayPropertyBuilder arrayProperty(PropertyType type, + IntFunction arrayProducer) { return new PropertyBuilder.ArrayPropertyBuilder<>(type, arrayProducer); } - public static PropertyBuilder.InlineArrayPropertyBuilder inlineArrayProperty( + public static PropertyBuilder.@NotNull InlineArrayPropertyBuilder inlineArrayProperty( InlineArrayConverter inlineConverter) { return new PropertyBuilder.InlineArrayPropertyBuilder<>(inlineConverter); } @@ -216,19 +217,19 @@ public static PropertyBuilder.InlineArrayPropertyBuilder inlineArrayPrope // -------------- // Optional flavors // -------------- - public static Property> optionalBooleanProperty(String path) { + public static @NotNull Property> optionalBooleanProperty(String path) { return new OptionalProperty<>(new BooleanProperty(path, false)); } - public static Property> optionalIntegerProperty(String path) { + public static @NotNull Property> optionalIntegerProperty(String path) { return new OptionalProperty<>(new IntegerProperty(path, 0)); } - public static Property> optionalStringProperty(String path) { + public static @NotNull Property> optionalStringProperty(String path) { return new OptionalProperty<>(new StringProperty(path, "")); } - public static > Property> optionalEnumProperty(Class clazz, String path) { + public static > @NotNull Property> optionalEnumProperty(@NotNull Class clazz, String path) { // default value may never be null, so get the first entry in the enum class return new OptionalProperty<>(new EnumProperty<>(clazz, path, clazz.getEnumConstants()[0])); } diff --git a/src/main/java/ch/jalu/configme/properties/RegexProperty.java b/src/main/java/ch/jalu/configme/properties/RegexProperty.java index defe6016..c873a9cc 100644 --- a/src/main/java/ch/jalu/configme/properties/RegexProperty.java +++ b/src/main/java/ch/jalu/configme/properties/RegexProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.SettingsManager; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -29,12 +30,12 @@ public RegexProperty(String path, Pattern defaultValue) { * @param path the path of the property * @param defaultRegexValue the default value of the property */ - public RegexProperty(String path, String defaultRegexValue) { + public RegexProperty(String path, @NotNull String defaultRegexValue) { this(path, Pattern.compile(defaultRegexValue)); } @Override - protected Pattern getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected Pattern getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { String pattern = reader.getString(getPath()); if (pattern != null) { try { @@ -46,7 +47,7 @@ protected Pattern getFromReader(PropertyReader reader, ConvertErrorRecorder erro } @Override - public Object toExportValue(Pattern value) { + public Object toExportValue(@NotNull Pattern value) { return value.pattern(); } @@ -57,7 +58,7 @@ public Object toExportValue(Pattern value) { * @param settingsManager settings manager with which the configured pattern is retrieved * @return true if the value matches the pattern, false otherwise */ - public boolean matches(String value, SettingsManager settingsManager) { + public boolean matches(@NotNull String value, @NotNull SettingsManager settingsManager) { Matcher matcher = settingsManager.getProperty(this).matcher(value); return matcher.matches(); } diff --git a/src/main/java/ch/jalu/configme/properties/SetProperty.java b/src/main/java/ch/jalu/configme/properties/SetProperty.java index 3d094bdf..68592fd9 100644 --- a/src/main/java/ch/jalu/configme/properties/SetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/SetProperty.java @@ -3,6 +3,8 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; @@ -30,7 +32,7 @@ public class SetProperty extends BaseProperty> { * @param defaultValue the values that make up the entries of the default set */ @SafeVarargs - public SetProperty(String path, PropertyType type, T... defaultValue) { + public SetProperty(String path, PropertyType type, T @NotNull ... defaultValue) { this(path, type, newSet(defaultValue)); } @@ -41,14 +43,14 @@ public SetProperty(String path, PropertyType type, T... defaultValue) { * @param type the property type * @param defaultValue the default value of the property */ - public SetProperty(String path, PropertyType type, Set defaultValue) { + public SetProperty(String path, PropertyType type, @NotNull Set defaultValue) { super(path, Collections.unmodifiableSet(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; } @Override - protected Set getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected Set getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { List list = reader.getList(getPath()); if (list != null) { @@ -61,7 +63,7 @@ protected Set getFromReader(PropertyReader reader, ConvertErrorRecorder error } @Override - public Object toExportValue(Set value) { + public @Nullable Object toExportValue(@NotNull Set value) { return value.stream() .map(type::toExportValue) .collect(Collectors.toList()); @@ -71,7 +73,7 @@ public Object toExportValue(Set value) { return Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet); } - private static Set newSet(E[] array) { + private static Set newSet(E @NotNull [] array) { return Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new)); } } diff --git a/src/main/java/ch/jalu/configme/properties/StringListProperty.java b/src/main/java/ch/jalu/configme/properties/StringListProperty.java index bade9cb9..6e77d0fc 100644 --- a/src/main/java/ch/jalu/configme/properties/StringListProperty.java +++ b/src/main/java/ch/jalu/configme/properties/StringListProperty.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; import java.util.List; @@ -13,7 +14,7 @@ public StringListProperty(String path, String... defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } - public StringListProperty(String path, List defaultValue) { + public StringListProperty(String path, @NotNull List defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } diff --git a/src/main/java/ch/jalu/configme/properties/StringSetProperty.java b/src/main/java/ch/jalu/configme/properties/StringSetProperty.java index fcde8c7c..ab99094e 100644 --- a/src/main/java/ch/jalu/configme/properties/StringSetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/StringSetProperty.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; import java.util.Set; @@ -25,7 +26,7 @@ public StringSetProperty(String path, String... defaultValue) { * @param path the path of the property * @param defaultValue the values that make up the entries of the default set */ - public StringSetProperty(String path, Set defaultValue) { + public StringSetProperty(String path, @NotNull Set defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } diff --git a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java index 0ab548d6..93bcab9a 100644 --- a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java +++ b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.util.Objects; @@ -31,7 +32,7 @@ public TypeBasedProperty(String path, T defaultValue, PropertyType type) { @Nullable @Override - protected T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected T getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { return type.convert(reader.getObject(getPath()), errorRecorder); } diff --git a/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java b/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java index 1653b2e1..238f8ab1 100644 --- a/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java +++ b/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties.convertresult; import ch.jalu.configme.properties.Property; +import org.jetbrains.annotations.NotNull; /** * Return value of {@link Property#determineValue}. Wraps the value to associate with the property, along with a field @@ -37,7 +38,7 @@ public PropertyValue(T value, boolean isValidInResource) { * @param the value type * @return property value with the given value and the valid flag set to true */ - public static PropertyValue withValidValue(T value) { + public static @NotNull PropertyValue withValidValue(T value) { return new PropertyValue<>(value, true); } @@ -48,7 +49,7 @@ public static PropertyValue withValidValue(T value) { * @param the value type * @return property value with the given value and the valid flag set to false */ - public static PropertyValue withValueRequiringRewrite(T value) { + public static @NotNull PropertyValue withValueRequiringRewrite(T value) { return new PropertyValue<>(value, false); } @@ -72,7 +73,7 @@ public boolean isValidInResource() { } @Override - public String toString() { + public @NotNull String toString() { return "PropertyValue[valid=" + isValidInResource + ", value='" + value + "']"; } } diff --git a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java index 78b8fe6f..fc8c2074 100644 --- a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java +++ b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java @@ -1,5 +1,7 @@ package ch.jalu.configme.properties.inlinearray; +import org.jetbrains.annotations.NotNull; + import javax.annotation.Nullable; import java.util.Arrays; import java.util.Objects; @@ -69,7 +71,7 @@ public StandardInlineArrayConverters(String separator, IntFunction arrayPro } @Override - public T[] fromString(String input) { + public T @NotNull [] fromString(@NotNull String input) { String[] inputArray = input.split(Pattern.quote(separator)); return Arrays.stream(inputArray) @@ -79,7 +81,7 @@ public T[] fromString(String input) { } @Override - public String toExportValue(T[] value) { + public String toExportValue(T @NotNull [] value) { String delimiter = useTrimAndSpaces ? separator + " " : separator; StringJoiner joiner = new StringJoiner(delimiter); for (T entry : value) { @@ -95,7 +97,7 @@ public String toExportValue(T[] value) { * @return converted value, or null if not possible */ @Nullable - protected T convert(String input) { + protected T convert(@NotNull String input) { try { String argument = useTrimAndSpaces ? input.trim() : input; return convertFunction.apply(argument); diff --git a/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java b/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java index 2cea42d2..2e97d6b5 100644 --- a/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java @@ -4,6 +4,8 @@ import ch.jalu.configme.beanmapper.Mapper; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class BeanPropertyType implements PropertyType { @@ -15,11 +17,11 @@ public BeanPropertyType(TypeInformation beanType, Mapper mapper) { this.mapper = mapper; } - public static BeanPropertyType of(Class type, Mapper mapper) { + public static @NotNull BeanPropertyType of(Class type, Mapper mapper) { return new BeanPropertyType<>(new TypeInformation(type), mapper); } - public static BeanPropertyType of(Class type) { + public static @NotNull BeanPropertyType of(Class type) { return of(type, DefaultMapper.getInstance()); } @@ -30,7 +32,7 @@ public B convert(Object object, ConvertErrorRecorder errorRecorder) { } @Override - public Object toExportValue(B value) { + public @Nullable Object toExportValue(B value) { return mapper.toExportValue(value); } } diff --git a/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java b/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java index 4636e835..00541b7b 100644 --- a/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties.types; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; +import org.jetbrains.annotations.NotNull; public class EnumPropertyType> implements PropertyType { @@ -10,7 +11,7 @@ public EnumPropertyType(Class enumType) { this.enumType = enumType; } - public static > EnumPropertyType of(Class type) { + public static > @NotNull EnumPropertyType of(Class type) { return new EnumPropertyType<>(type); } @@ -37,7 +38,7 @@ public E convert(Object object, ConvertErrorRecorder errorRecorder) { } @Override - public Object toExportValue(E value) { + public @NotNull Object toExportValue(@NotNull E value) { return value.name(); } } diff --git a/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java b/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java index 71ffe084..70f924d9 100644 --- a/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties.types; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; +import org.jetbrains.annotations.NotNull; import java.util.function.Function; @@ -66,7 +67,7 @@ public Object toExportValue(T value) { } /* Helper to create property types which convert from a Number object. */ - private static PrimitivePropertyType fromNumber(Function function) { + private static @NotNull PrimitivePropertyType fromNumber(@NotNull Function function) { return new PrimitivePropertyType<>(object -> object instanceof Number ? function.apply((Number) object) : null); } } diff --git a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java index b0424bee..702da4cf 100644 --- a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java @@ -21,6 +21,7 @@ public interface PropertyType { * @return the converted value, or null */ @Nullable + @org.jetbrains.annotations.Nullable T convert(@Nullable Object object, ConvertErrorRecorder errorRecorder); /** diff --git a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java index 32b8962c..5677e366 100644 --- a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java +++ b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java @@ -1,5 +1,7 @@ package ch.jalu.configme.resource; +import org.jetbrains.annotations.NotNull; + import javax.annotation.Nullable; import java.util.LinkedHashMap; import java.util.Map; @@ -73,7 +75,7 @@ protected boolean isKeyInvalid(Object key) { * @param path the path to store the value under * @param value the value to store */ - protected void addValueIntoMap(Map map, String path, Object value) { + protected void addValueIntoMap(@NotNull Map map, @NotNull String path, Object value) { int dotPosition = path.indexOf("."); if (dotPosition > -1) { String pathElement = path.substring(0, dotPosition); @@ -98,7 +100,7 @@ protected void addValueIntoMap(Map map, String path, Object valu * @param path the key with which the value should be looked up from the map * @return the nested map, as stored under the path in the given map */ - protected Map getOrInsertMap(Map parentMap, String path) { + protected @NotNull Map getOrInsertMap(@NotNull Map parentMap, String path) { Object value = parentMap.get(path); if (value instanceof Map) { return (Map) value; diff --git a/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java b/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java index 90eb3afa..6409a8a8 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java @@ -2,6 +2,7 @@ import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.utils.CollectionUtils; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Arrays; @@ -19,7 +20,7 @@ public class PropertyPathTraverser { private final ConfigurationData configurationData; /** Contains all path elements besides the last, e.g. {datasource, mysql} for "datasource.mysql.table". */ - private List parentPathElements = new ArrayList<>(0); + private @NotNull List parentPathElements = new ArrayList<>(0); private boolean isFirstProperty = true; public PropertyPathTraverser(ConfigurationData configurationData) { @@ -32,7 +33,7 @@ public PropertyPathTraverser(ConfigurationData configurationData) { * @param path the property path * @return the new path elements */ - public List getPathElements(String path) { + public @NotNull List getPathElements(@NotNull String path) { List propertyPath = Arrays.asList(path.split("\\.")); List commonPathParts = CollectionUtils.filterCommonStart( parentPathElements, propertyPath.subList(0, propertyPath.size() - 1)); @@ -45,7 +46,7 @@ public List getPathElements(String path) { return convertToPathElements(indentationLevel, prefix, newPathParts); } - private List convertToPathElements(int indentation, String prefix, List elements) { + private @NotNull List convertToPathElements(int indentation, String prefix, @NotNull List elements) { List pathElements = new ArrayList<>(elements.size()); for (String element : elements) { List comments = isFirstProperty diff --git a/src/main/java/ch/jalu/configme/resource/PropertyReader.java b/src/main/java/ch/jalu/configme/resource/PropertyReader.java index b991f3a2..914bfdef 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyReader.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyReader.java @@ -42,6 +42,7 @@ public interface PropertyReader { * @return the value, or null if there is none */ @Nullable + @org.jetbrains.annotations.Nullable Object getObject(String path); /** @@ -51,6 +52,7 @@ public interface PropertyReader { * @return the value as a String, or null if not applicable or unavailable */ @Nullable + @org.jetbrains.annotations.Nullable String getString(String path); /** @@ -60,6 +62,7 @@ public interface PropertyReader { * @return the value as integer, or null if not applicable or unavailable */ @Nullable + @org.jetbrains.annotations.Nullable Integer getInt(String path); /** @@ -69,6 +72,7 @@ public interface PropertyReader { * @return the value as a double, or null if not applicable or unavailable */ @Nullable + @org.jetbrains.annotations.Nullable Double getDouble(String path); /** @@ -78,6 +82,7 @@ public interface PropertyReader { * @return the value as a boolean, or null if not applicable or unavailable */ @Nullable + @org.jetbrains.annotations.Nullable Boolean getBoolean(String path); /** @@ -87,6 +92,7 @@ public interface PropertyReader { * @return the value as a list, or null if not applicable or unavailable */ @Nullable + @org.jetbrains.annotations.Nullable List getList(String path); } diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java index 83e44f46..ff10c961 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java @@ -1,6 +1,7 @@ package ch.jalu.configme.resource; import ch.jalu.configme.exception.ConfigMeException; +import org.jetbrains.annotations.NotNull; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; @@ -58,12 +59,12 @@ public YamlFileReader(Path path, Charset charset) { * @deprecated scheduled for removal in favor of Path */ @Deprecated - public YamlFileReader(File file) { + public YamlFileReader(@NotNull File file) { this(file.toPath(), StandardCharsets.UTF_8); } @Override - public Object getObject(String path) { + public Object getObject(@NotNull String path) { if (path.isEmpty()) { return root; } @@ -80,12 +81,12 @@ public Object getObject(String path) { } @Override - public String getString(String path) { + public String getString(@NotNull String path) { return getTypedObject(path, String.class); } @Override - public Integer getInt(String path) { + public Integer getInt(@NotNull String path) { Number n = getTypedObject(path, Number.class); return (n == null) ? null @@ -93,7 +94,7 @@ public Integer getInt(String path) { } @Override - public Double getDouble(String path) { + public Double getDouble(@NotNull String path) { Number n = getTypedObject(path, Number.class); return (n == null) ? null @@ -101,22 +102,22 @@ public Double getDouble(String path) { } @Override - public Boolean getBoolean(String path) { + public Boolean getBoolean(@NotNull String path) { return getTypedObject(path, Boolean.class); } @Override - public List getList(String path) { + public List getList(@NotNull String path) { return getTypedObject(path, List.class); } @Override - public boolean contains(String path) { + public boolean contains(@NotNull String path) { return getObject(path) != null; } @Override - public Set getKeys(boolean onlyLeafNodes) { + public @NotNull Set getKeys(boolean onlyLeafNodes) { if (root == null) { return Collections.emptySet(); } @@ -126,7 +127,7 @@ public Set getKeys(boolean onlyLeafNodes) { } @Override - public Set getChildKeys(String path) { + public Set getChildKeys(@NotNull String path) { Object object = getObject(path); if (object instanceof Map) { String pathPrefix = path.isEmpty() ? "" : path + "."; @@ -145,7 +146,7 @@ public Set getChildKeys(String path) { * @param result set to save keys to * @param onlyLeafNodes whether only leaf nodes should be added to the result set */ - private void collectKeysIntoSet(String path, Map map, Set result, boolean onlyLeafNodes) { + private void collectKeysIntoSet(@NotNull String path, @NotNull Map map, @NotNull Set result, boolean onlyLeafNodes) { for (Map.Entry entry : map.entrySet()) { String childPath = path.isEmpty() ? entry.getKey() : path + "." + entry.getKey(); if (!onlyLeafNodes || isLeafValue(entry.getValue())) { @@ -167,7 +168,7 @@ private static boolean isLeafValue(Object o) { * * @return map with the values from the file */ - protected Map loadFile() { + protected @org.jetbrains.annotations.Nullable Map loadFile() { try (InputStream is = Files.newInputStream(path); InputStreamReader isr = new InputStreamReader(is, charset)) { Map rootMap = new Yaml().load(isr); @@ -194,7 +195,7 @@ protected Map normalizeMap(@Nullable Map map) { // Scheduled for removal in favor of #getPath @Deprecated - protected final File getFile() { + protected final @NotNull File getFile() { return path.toFile(); } @@ -222,7 +223,7 @@ protected final Map getRoot() { * @return cast value at the given path, null if not applicable */ @Nullable - protected T getTypedObject(String path, Class clazz) { + protected T getTypedObject(@NotNull String path, @NotNull Class clazz) { Object value = getObject(path); if (clazz.isInstance(value)) { return clazz.cast(value); diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java index 87223c7b..58877d26 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java @@ -4,6 +4,7 @@ import ch.jalu.configme.exception.ConfigMeException; import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyPathTraverser.PathElement; +import org.jetbrains.annotations.NotNull; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -23,15 +24,15 @@ public class YamlFileResource implements PropertyResource { private final Path path; - private final YamlFileResourceOptions options; + private final @NotNull YamlFileResourceOptions options; private final String indentationSpace; - private Yaml yamlObject; + private @org.jetbrains.annotations.Nullable Yaml yamlObject; public YamlFileResource(Path path) { this(path, YamlFileResourceOptions.builder().build()); } - public YamlFileResource(Path path, YamlFileResourceOptions options) { + public YamlFileResource(Path path, @NotNull YamlFileResourceOptions options) { this.path = path; this.options = options; this.indentationSpace = options.getIndentation(); @@ -44,17 +45,17 @@ public YamlFileResource(Path path, YamlFileResourceOptions options) { * @deprecated scheduled for removal */ @Deprecated - public YamlFileResource(File file) { + public YamlFileResource(@NotNull File file) { this(file.toPath()); } @Override - public PropertyReader createReader() { + public @NotNull PropertyReader createReader() { return new YamlFileReader(path, options.getCharset()); } @Override - public void exportProperties(ConfigurationData configurationData) { + public void exportProperties(@NotNull ConfigurationData configurationData) { try (OutputStream os = Files.newOutputStream(path); OutputStreamWriter writer = new OutputStreamWriter(os, options.getCharset())) { PropertyPathTraverser pathTraverser = new PropertyPathTraverser(configurationData); @@ -77,7 +78,7 @@ protected final Path getPath() { // Scheduled for removal in favor of #getPath @Deprecated - protected final File getFile() { + protected final @NotNull File getFile() { return path.toFile(); } @@ -90,8 +91,8 @@ protected final File getFile() { * @param value the value to export * @throws IOException . */ - protected void exportValue(Writer writer, PropertyPathTraverser pathTraverser, - String path, Object value) throws IOException { + protected void exportValue(@NotNull Writer writer, @NotNull PropertyPathTraverser pathTraverser, + @NotNull String path, @org.jetbrains.annotations.Nullable Object value) throws IOException { if (value == null) { return; } @@ -133,7 +134,7 @@ protected void exportValue(Writer writer, PropertyPathTraverser pathTraverser, * @param pathElement the path element for which the comments are being generated * @throws IOException . */ - protected void writeComments(Writer writer, int indentation, PathElement pathElement) throws IOException { + protected void writeComments(@NotNull Writer writer, int indentation, @NotNull PathElement pathElement) throws IOException { if (pathElement.getComments().isEmpty()) { return; } @@ -151,14 +152,14 @@ protected void writeComments(Writer writer, int indentation, PathElement pathEle } } - private void writeIndentingBetweenLines(Writer writer, PathElement pathElement) throws IOException { + private void writeIndentingBetweenLines(@NotNull Writer writer, PathElement pathElement) throws IOException { int numberOfEmptyLines = options.getNumberOfEmptyLinesBefore(pathElement); for (int i = 0; i < numberOfEmptyLines; ++i) { writer.append("\n"); } } - private String getNewLineIfNotFirstElement(PathElement pathElement) { + private @NotNull String getNewLineIfNotFirstElement(@NotNull PathElement pathElement) { return pathElement.isFirstElement() && pathElement.getComments().isEmpty() ? "" : "\n"; } @@ -171,7 +172,7 @@ private String getNewLineIfNotFirstElement(PathElement pathElement) { * @param indent level of indentation to use * @return the value as YAML at the given indentation level */ - protected String toYamlIndented(@Nullable Object value, int indent) { + protected @NotNull String toYamlIndented(@Nullable Object value, int indent) { String representation = toYaml(value); String[] lines = representation.split("\\n"); return String.join("\n" + indent(indent), lines); @@ -223,7 +224,7 @@ protected String indent(int level) { return result.toString(); } - protected String escapePathElementIfNeeded(String path) { + protected @NotNull String escapePathElementIfNeeded(String path) { return getYamlObject().dump(path).trim(); } @@ -239,29 +240,29 @@ protected void onWriteComplete() { * * @return the YAML instance to use */ - protected Yaml getYamlObject() { + protected @org.jetbrains.annotations.Nullable Yaml getYamlObject() { if (yamlObject == null) { yamlObject = createNewYaml(); } return yamlObject; } - protected Yaml createNewYaml() { + protected @NotNull Yaml createNewYaml() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setAllowUnicode(true); return new Yaml(options); } - protected final YamlFileResourceOptions getOptions() { + protected final @NotNull YamlFileResourceOptions getOptions() { return options; } - private Object getExportValue(Property property, ConfigurationData configurationData) { + private @org.jetbrains.annotations.Nullable Object getExportValue(@NotNull Property property, @NotNull ConfigurationData configurationData) { return property.toExportValue(configurationData.getValue(property)); } - private static List collectionToList(Collection collection) { + private static @NotNull List collectionToList(Collection collection) { return collection instanceof List ? (List) collection : new ArrayList<>(collection); diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java index 290e2f9b..1ad2289e 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java @@ -1,6 +1,7 @@ package ch.jalu.configme.resource; import ch.jalu.configme.resource.PropertyPathTraverser.PathElement; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.nio.charset.Charset; @@ -9,8 +10,8 @@ public class YamlFileResourceOptions { - private final Charset charset; - private final ToIntFunction numberOfLinesBeforeFunction; + private final @NotNull Charset charset; + private final @org.jetbrains.annotations.Nullable ToIntFunction numberOfLinesBeforeFunction; private final int indentationSize; /** @@ -28,11 +29,11 @@ protected YamlFileResourceOptions(@Nullable Charset charset, this.indentationSize = indentationSize; } - public static Builder builder() { + public static @NotNull Builder builder() { return new Builder(); } - public Charset getCharset() { + public @NotNull Charset getCharset() { return charset; } @@ -47,7 +48,7 @@ public int getIndentationSize() { /** * @return the indentation to use for one level */ - public String getIndentation() { + public @NotNull String getIndentation() { if (indentationSize == 4) { return " "; } @@ -68,22 +69,22 @@ public static class Builder { private ToIntFunction numberOfLinesBeforeFunction; private int indentationSize = 4; - public Builder charset(Charset charset) { + public @NotNull Builder charset(Charset charset) { this.charset = charset; return this; } - public Builder numberOfLinesBeforeFunction(ToIntFunction numberOfLinesBeforeFunction) { + public @NotNull Builder numberOfLinesBeforeFunction(ToIntFunction numberOfLinesBeforeFunction) { this.numberOfLinesBeforeFunction = numberOfLinesBeforeFunction; return this; } - public Builder indentationSize(final int indentationSize) { + public @NotNull Builder indentationSize(final int indentationSize) { this.indentationSize = indentationSize; return this; } - public YamlFileResourceOptions build() { + public @NotNull YamlFileResourceOptions build() { return new YamlFileResourceOptions(charset, numberOfLinesBeforeFunction, indentationSize); } } diff --git a/src/main/java/ch/jalu/configme/utils/CollectionUtils.java b/src/main/java/ch/jalu/configme/utils/CollectionUtils.java index 2a569f7a..b644751b 100644 --- a/src/main/java/ch/jalu/configme/utils/CollectionUtils.java +++ b/src/main/java/ch/jalu/configme/utils/CollectionUtils.java @@ -1,5 +1,7 @@ package ch.jalu.configme.utils; +import org.jetbrains.annotations.NotNull; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -21,7 +23,7 @@ private CollectionUtils() { * @return the sublist of all elements from index {@code start} and on; empty list * if the start index exceeds the list's size */ - public static List getRange(List list, int start) { + public static @NotNull List getRange(@NotNull List list, int start) { if (start >= list.size()) { return new ArrayList<>(); } @@ -36,7 +38,7 @@ public static List getRange(List list, int start) { * @param type of the lists * @return list of all equal entries at the start of both lists */ - public static List filterCommonStart(List list1, List list2) { + public static @NotNull List filterCommonStart(@NotNull List list1, @NotNull List list2) { List commonStart = new ArrayList<>(); int minSize = Math.min(list1.size(), list2.size()); int i = 0; diff --git a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java index b43e4e27..0738b503 100644 --- a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java +++ b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java @@ -7,6 +7,7 @@ import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyResource; +import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.lang.reflect.Constructor; @@ -52,7 +53,7 @@ public final void validate(Class... settingHolders) { * * @param settingHolders settings holder classes that make up the configuration data of the project */ - public void validate(Iterable> settingHolders) { + public void validate(@NotNull Iterable> settingHolders) { validateAllPropertiesAreConstants(settingHolders); validateSettingsHolderClassesFinal(settingHolders); validateClassesHaveHiddenNoArgConstructor(settingHolders); @@ -79,8 +80,8 @@ public void validate(Iterable> settingHolders) { * @param migrationService the migration service to check */ public void validateConfigurationDataValidForMigrationService(ConfigurationData configurationData, - PropertyResource resource, - MigrationService migrationService) { + @NotNull PropertyResource resource, + @NotNull MigrationService migrationService) { resource.exportProperties(configurationData); PropertyReader reader = resource.createReader(); @@ -97,7 +98,7 @@ public void validateConfigurationDataValidForMigrationService(ConfigurationData * * @param settingHolders the classes to check */ - public void validateAllPropertiesAreConstants(Iterable> settingHolders) { + public void validateAllPropertiesAreConstants(@NotNull Iterable> settingHolders) { List invalidFields = new ArrayList<>(); for (Class clazz : settingHolders) { @@ -120,7 +121,7 @@ public void validateAllPropertiesAreConstants(Iterable> settingHolders) { + public void validateSettingsHolderClassesFinal(@NotNull Iterable> settingHolders) { List invalidClasses = new ArrayList<>(); for (Class clazz : settingHolders) { @@ -141,7 +142,7 @@ public void validateSettingsHolderClassesFinal(Iterable> settingHolders) { + public void validateClassesHaveHiddenNoArgConstructor(@NotNull Iterable> settingHolders) { List invalidClasses = new ArrayList<>(); for (Class clazz : settingHolders) { @@ -162,7 +163,7 @@ public void validateClassesHaveHiddenNoArgConstructor(Iterable> propertyFilter) { Predicate> filter = propertyFilter == null ? (p -> true) : propertyFilter; List invalidProperties = new ArrayList<>(); @@ -192,7 +193,7 @@ public void validateHasCommentOnEveryProperty(ConfigurationData configurationDat * @param minLength the number of characters each comment line must at least have (null to disable check) * @param maxLength the number of characters each comment may not surpass (null to disable check) */ - public void validateCommentLengthsAreWithinBounds(ConfigurationData configurationData, + public void validateCommentLengthsAreWithinBounds(@NotNull ConfigurationData configurationData, @Nullable Integer minLength, @Nullable Integer maxLength) { Predicate hasInvalidLengthPredicate = createValidLengthPredicate(minLength, maxLength).negate(); @@ -220,7 +221,7 @@ public void validateCommentLengthsAreWithinBounds(ConfigurationData configuratio * @param configurationData the configuration data whose properties and comments should be checked * @param propertyFilter predicate determining which properties are checked (if null, are properties are checked) */ - public void validateHasAllEnumEntriesInComment(ConfigurationData configurationData, + public void validateHasAllEnumEntriesInComment(@NotNull ConfigurationData configurationData, @Nullable Predicate> propertyFilter) { List commentErrors = new ArrayList<>(); @@ -251,7 +252,7 @@ public void validateHasAllEnumEntriesInComment(ConfigurationData configurationDa // ---- Validation helpers - protected boolean isValidConstantField(Field field) { + protected boolean isValidConstantField(@NotNull Field field) { int modifiers = field.getModifiers(); return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) @@ -276,7 +277,7 @@ protected boolean hasNonEmptyComment(@Nullable List comments) { * @param maxLength the max length (nullable) * @return predicate based on the supplied length parameters */ - protected Predicate createValidLengthPredicate(@Nullable Integer minLength, @Nullable Integer maxLength) { + protected @NotNull Predicate createValidLengthPredicate(@Nullable Integer minLength, @Nullable Integer maxLength) { if (minLength == null && maxLength == null) { throw new IllegalArgumentException("min length or max length must be not null"); } @@ -292,7 +293,7 @@ protected Predicate createValidLengthPredicate(@Nullable Integer minLeng */ @Nullable @SuppressWarnings("unchecked") - protected Class> getEnumTypeOfProperty(Property property) { + protected Class> getEnumTypeOfProperty(@NotNull Property property) { Class defaultValueType = property.getDefaultValue().getClass(); if (defaultValueType.isAnonymousClass()) { // If an enum entry implements methods, it is an anonymous class -> we're interested in the enclosing class @@ -301,13 +302,13 @@ protected Class> getEnumTypeOfProperty(Property property) { return defaultValueType.isEnum() ? (Class>) defaultValueType : null; } - protected List gatherExpectedEnumNames(Class> enumClass) { + protected List gatherExpectedEnumNames(@NotNull Class> enumClass) { return Arrays.stream(enumClass.getEnumConstants()) .map(Enum::name) .collect(Collectors.toList()); } - protected boolean hasValidConstructorSetup(Class clazz) { + protected boolean hasValidConstructorSetup(@NotNull Class clazz) { Constructor[] constructors = clazz.getDeclaredConstructors(); return constructors.length == 1 && constructors[0].getParameterCount() == 0 @@ -320,7 +321,7 @@ protected boolean hasValidConstructorSetup(Class clazz * @param clazz the class whose fields should be retrieved * @return all fields of the class, including its parents */ - protected Stream getAllFields(Class clazz) { + protected Stream getAllFields(@NotNull Class clazz) { // Shortcut: Class does not inherit from another class, so just go through its fields if (Object.class.equals(clazz.getSuperclass())) { return Arrays.stream(clazz.getDeclaredFields()); diff --git a/src/main/java/ch/jalu/configme/utils/TypeInformation.java b/src/main/java/ch/jalu/configme/utils/TypeInformation.java index b1537d83..c5d0eba9 100644 --- a/src/main/java/ch/jalu/configme/utils/TypeInformation.java +++ b/src/main/java/ch/jalu/configme/utils/TypeInformation.java @@ -1,5 +1,7 @@ package ch.jalu.configme.utils; +import org.jetbrains.annotations.NotNull; + import javax.annotation.Nullable; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; @@ -32,7 +34,7 @@ public TypeInformation(Type type) { * @param field the field to create a type information for * @return type information wrapping the field's type */ - public static TypeInformation fromField(Field field) { + public static @NotNull TypeInformation fromField(@NotNull Field field) { return new TypeInformation(field.getGenericType()); } @@ -60,7 +62,7 @@ public Type getType() { * (e.g. setting a value to a field or adding to a collection); * null if not applicable */ - public Class getSafeToWriteClass() { + public @org.jetbrains.annotations.Nullable Class getSafeToWriteClass() { return getSafeToWriteClassInternal(type); } @@ -78,7 +80,7 @@ public Class getSafeToWriteClass() { * * @return the type as Class which is safe for reading (e.g. getting field value or reading from a collection) */ - public Class getSafeToReadClass() { + public @NotNull Class getSafeToReadClass() { Class safeToReadClass = getSafeToReadClassInternal(type); return safeToReadClass == null ? Object.class : safeToReadClass; } @@ -166,7 +168,7 @@ private Class getSafeToReadClassInternal(Type type) { } @Override - public String toString() { + public @NotNull String toString() { return "TypeInformation[type=" + type + "]"; } diff --git a/src/main/java/ch/jalu/configme/utils/Utils.java b/src/main/java/ch/jalu/configme/utils/Utils.java index 08faf3e0..9ba61683 100644 --- a/src/main/java/ch/jalu/configme/utils/Utils.java +++ b/src/main/java/ch/jalu/configme/utils/Utils.java @@ -1,6 +1,7 @@ package ch.jalu.configme.utils; import ch.jalu.configme.exception.ConfigMeException; +import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.nio.file.Files; @@ -21,7 +22,7 @@ private Utils() { * * @param file the file to create if it doesn't exist */ - public static void createFileIfNotExists(Path file) { + public static void createFileIfNotExists(@NotNull Path file) { if (Files.exists(file)) { if (!Files.isRegularFile(file)) { throw new ConfigMeException("Expected file but '" + file + "' is not a file"); From ab34f65641c718f0f4a0f57db5d674e89372b001 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 17:22:33 -0400 Subject: [PATCH 03/15] Replaced all javax annotations with jetbrains annotations, removed fqn from annotations --- .../jalu/configme/SettingsManagerBuilder.java | 6 ++--- .../ch/jalu/configme/SettingsManagerImpl.java | 4 +-- .../ch/jalu/configme/beanmapper/Mapper.java | 10 +++---- .../jalu/configme/beanmapper/MapperImpl.java | 11 ++++---- .../BigNumberLeafValueHandler.java | 6 ++--- .../leafvaluehandler/LeafValueHandler.java | 10 +++---- .../BeanDescriptionFactoryImpl.java | 2 +- .../BeanPropertyDescription.java | 6 ++--- .../BeanPropertyDescriptionImpl.java | 2 +- .../ConfigurationDataBuilder.java | 2 +- .../configme/properties/BaseProperty.java | 7 +++-- .../configme/properties/ListProperty.java | 4 +-- .../ch/jalu/configme/properties/Property.java | 6 ++--- .../properties/TypeBasedProperty.java | 2 +- .../StandardInlineArrayConverters.java | 2 +- .../properties/types/PropertyType.java | 6 ++--- .../jalu/configme/resource/MapNormalizer.java | 2 +- .../configme/resource/PropertyReader.java | 26 +++++-------------- .../configme/resource/YamlFileReader.java | 4 +-- .../configme/resource/YamlFileResource.java | 10 +++---- .../resource/YamlFileResourceOptions.java | 4 +-- .../utils/SettingsHolderClassValidator.java | 2 +- .../jalu/configme/utils/TypeInformation.java | 4 +-- .../BeanWithCustomTypeHandlerTest.java | 5 ++-- 24 files changed, 57 insertions(+), 86 deletions(-) diff --git a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java index 666e11f7..36d90fab 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java @@ -10,7 +10,7 @@ import ch.jalu.configme.utils.Utils; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.nio.file.Path; import java.util.Objects; @@ -22,7 +22,7 @@ public final class SettingsManagerBuilder { private final PropertyResource resource; private ConfigurationData configurationData; - private @org.jetbrains.annotations.Nullable MigrationService migrationService; + private @Nullable MigrationService migrationService; private SettingsManagerBuilder(PropertyResource resource) { this.resource = resource; @@ -132,7 +132,7 @@ private SettingsManagerBuilder(PropertyResource resource) { * * @return the settings manager */ - public @org.jetbrains.annotations.Nullable SettingsManager create() { + public @Nullable SettingsManager create() { Objects.requireNonNull(resource, "resource"); Objects.requireNonNull(configurationData, "configurationData"); return new SettingsManagerImpl(resource, configurationData, migrationService); diff --git a/src/main/java/ch/jalu/configme/SettingsManagerImpl.java b/src/main/java/ch/jalu/configme/SettingsManagerImpl.java index e4279897..169449a1 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerImpl.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerImpl.java @@ -6,7 +6,7 @@ import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyResource; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; /** * Default implementation of {@link SettingsManager}. Use the {@link SettingsManagerBuilder} to create instances. @@ -28,7 +28,7 @@ public class SettingsManagerImpl implements SettingsManager { private final ConfigurationData configurationData; private final PropertyResource resource; - private final @org.jetbrains.annotations.Nullable MigrationService migrationService; + private final @Nullable MigrationService migrationService; /** * Constructor. Use {@link SettingsManagerBuilder} to create instances. diff --git a/src/main/java/ch/jalu/configme/beanmapper/Mapper.java b/src/main/java/ch/jalu/configme/beanmapper/Mapper.java index a67db9ce..520359a1 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/Mapper.java +++ b/src/main/java/ch/jalu/configme/beanmapper/Mapper.java @@ -3,7 +3,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; /** * Creates JavaBeans based on the values coming from a property reader. See the JavaDoc on the default implementation, @@ -21,9 +21,7 @@ public interface Mapper { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return object of the given type, or null if not possible */ - @Nullable - @org.jetbrains.annotations.Nullable - Object convertToBean(@Nullable Object value, TypeInformation typeInformation, ConvertErrorRecorder errorRecorder); + @Nullable Object convertToBean(@Nullable Object value, TypeInformation typeInformation, ConvertErrorRecorder errorRecorder); /** * Converts the given value to an object of the given class, if possible. Returns null otherwise. @@ -50,8 +48,6 @@ default T convertToBean(@Nullable Object value, Class clazz, ConvertError * @param object the object to convert to its export value * @return export value to use */ - @Nullable - @org.jetbrains.annotations.Nullable - Object toExportValue(@Nullable Object object); + @Nullable Object toExportValue(@Nullable Object object); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java index cd1db1f8..e95baddb 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java @@ -9,7 +9,7 @@ import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -87,7 +87,7 @@ protected final LeafValueHandler getLeafValueHandler() { // --------- @Override - public Object toExportValue(@org.jetbrains.annotations.Nullable Object value) { + public Object toExportValue(@Nullable Object value) { // Step 1: attempt simple value transformation Object simpleValue = leafValueHandler.toExportValue(value); if (simpleValue != null || value == null) { @@ -143,7 +143,7 @@ protected Object createExportValueForSpecialTypes(Object value) { return null; } - protected static @org.jetbrains.annotations.Nullable Object unwrapReturnNull(Object o) { + protected static @Nullable Object unwrapReturnNull(Object o) { return o == RETURN_NULL ? null : o; } @@ -153,7 +153,7 @@ protected Object createExportValueForSpecialTypes(Object value) { @Nullable @Override - public Object convertToBean(@org.jetbrains.annotations.Nullable Object value, TypeInformation beanType, ConvertErrorRecorder errorRecorder) { + public Object convertToBean(@Nullable Object value, TypeInformation beanType, ConvertErrorRecorder errorRecorder) { if (value == null) { return null; } @@ -220,9 +220,8 @@ protected Object handleSpecialTypes(@NotNull MappingContext context, Object valu * @param value the value to map from * @return Collection property from the value, or null if not applicable */ - @Nullable @SuppressWarnings("unchecked") - protected @org.jetbrains.annotations.Nullable Collection createCollection(@NotNull MappingContext context, Object value) { + protected @Nullable Collection createCollection(@NotNull MappingContext context, Object value) { if (value instanceof Iterable) { TypeInformation entryType = context.getGenericTypeInfoOrFail(0); Collection result = createCollectionMatchingType(context); diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java index 5c5f0f34..379b015a 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java @@ -1,8 +1,8 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.math.BigDecimal; import java.math.BigInteger; @@ -16,7 +16,7 @@ public class BigNumberLeafValueHandler extends AbstractLeafValueHandler { private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E100"); @Override - protected @org.jetbrains.annotations.Nullable Object convert(@NotNull Class clazz, Object value) { + protected @Nullable Object convert(@NotNull Class clazz, Object value) { if (clazz != BigInteger.class && clazz != BigDecimal.class) { return null; } @@ -29,7 +29,7 @@ public class BigNumberLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(@org.jetbrains.annotations.Nullable Object value) { + public Object toExportValue(@Nullable Object value) { final Class valueType = value == null ? null : value.getClass(); if (valueType == BigInteger.class) { return value.toString(); diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java index 85269208..426f1b6f 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java @@ -1,8 +1,8 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; /** * A leaf value handler is used in {@link ch.jalu.configme.beanmapper.MapperImpl} to convert "simple" values from their @@ -25,9 +25,7 @@ public interface LeafValueHandler { * @param value the value to convert * @return value of the given type, or null if not applicable */ - @Nullable - @org.jetbrains.annotations.Nullable - Object convert(TypeInformation typeInformation, @Nullable Object value); + @Nullable Object convert(TypeInformation typeInformation, @Nullable Object value); /** * Converts the given value to a type more suitable for exporting. Used by the mapper in @@ -40,8 +38,6 @@ public interface LeafValueHandler { * @param value the value to convert to an export value, if possible * @return value to use in the export, or null if not applicable */ - @Nullable - @org.jetbrains.annotations.Nullable - Object toExportValue(@Nullable Object value); + @Nullable Object toExportValue(@Nullable Object value); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java index 20524be7..448ea0ce 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java @@ -5,7 +5,7 @@ import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java index b3757c88..ca7daaab 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java @@ -2,7 +2,7 @@ import ch.jalu.configme.utils.TypeInformation; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; /** * Represents a property within a bean class, as used in {@link ch.jalu.configme.beanmapper.MapperImpl}. @@ -37,7 +37,5 @@ public interface BeanPropertyDescription { * @param bean the bean to read the property from * @return the value of the property (can be null) */ - @Nullable - @org.jetbrains.annotations.Nullable - Object getValue(Object bean); + @Nullable Object getValue(Object bean); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java index 773adee6..5c48e42e 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java @@ -4,7 +4,7 @@ import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java index 7e7abb07..6d541378 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java @@ -6,7 +6,7 @@ import ch.jalu.configme.properties.Property; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/ch/jalu/configme/properties/BaseProperty.java b/src/main/java/ch/jalu/configme/properties/BaseProperty.java index f0adb865..fb3045eb 100644 --- a/src/main/java/ch/jalu/configme/properties/BaseProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BaseProperty.java @@ -5,7 +5,7 @@ import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.Objects; /** @@ -55,7 +55,7 @@ public T getDefaultValue() { } @Override - public boolean isValidValue(@org.jetbrains.annotations.Nullable T value) { + public boolean isValidValue(@Nullable T value) { return value != null; } @@ -67,8 +67,7 @@ public boolean isValidValue(@org.jetbrains.annotations.Nullable T value) { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return value based on the reader, or null if not applicable */ - @Nullable - protected abstract @org.jetbrains.annotations.Nullable T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder); + protected abstract @Nullable T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder); @Override public @NotNull String toString() { diff --git a/src/main/java/ch/jalu/configme/properties/ListProperty.java b/src/main/java/ch/jalu/configme/properties/ListProperty.java index 859a6293..d3b1e0eb 100644 --- a/src/main/java/ch/jalu/configme/properties/ListProperty.java +++ b/src/main/java/ch/jalu/configme/properties/ListProperty.java @@ -5,7 +5,7 @@ import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -61,7 +61,7 @@ protected List getFromReader(@NotNull PropertyReader reader, ConvertErrorReco } @Override - public @org.jetbrains.annotations.Nullable Object toExportValue(@NotNull List value) { + public @Nullable Object toExportValue(@NotNull List value) { return value.stream() .map(type::toExportValue) .collect(Collectors.toList()); diff --git a/src/main/java/ch/jalu/configme/properties/Property.java b/src/main/java/ch/jalu/configme/properties/Property.java index ff8e6470..a8c36408 100644 --- a/src/main/java/ch/jalu/configme/properties/Property.java +++ b/src/main/java/ch/jalu/configme/properties/Property.java @@ -3,7 +3,7 @@ import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; /** * Property interface. A property knows its path, its type, and can convert the values from @@ -81,8 +81,6 @@ default boolean isValidInResource(PropertyReader propertyReader) { * @param value the value to convert to an export value * @return value to use for export, null to skip the property */ - @Nullable - @org.jetbrains.annotations.Nullable - Object toExportValue(T value); + @Nullable Object toExportValue(T value); } diff --git a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java index 93bcab9a..0f6c2b50 100644 --- a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java +++ b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java @@ -5,7 +5,7 @@ import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.Objects; /** diff --git a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java index fc8c2074..8bd567fd 100644 --- a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java +++ b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java @@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Objects; import java.util.StringJoiner; diff --git a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java index 702da4cf..186bbd69 100644 --- a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java @@ -2,7 +2,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; /** * Property type: provides methods for converting between property resource and a defined type @@ -20,9 +20,7 @@ public interface PropertyType { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return the converted value, or null */ - @Nullable - @org.jetbrains.annotations.Nullable - T convert(@Nullable Object object, ConvertErrorRecorder errorRecorder); + @Nullable T convert(@Nullable Object object, ConvertErrorRecorder errorRecorder); /** * Converts the given value to its export value. (Converts in the opposite way of {@link #convert}.) diff --git a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java index 5677e366..b4579e92 100644 --- a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java +++ b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java @@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; diff --git a/src/main/java/ch/jalu/configme/resource/PropertyReader.java b/src/main/java/ch/jalu/configme/resource/PropertyReader.java index 914bfdef..c6c27d85 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyReader.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyReader.java @@ -1,6 +1,6 @@ package ch.jalu.configme.resource; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Set; @@ -41,9 +41,7 @@ public interface PropertyReader { * @param path the path to retrieve the value for * @return the value, or null if there is none */ - @Nullable - @org.jetbrains.annotations.Nullable - Object getObject(String path); + @Nullable Object getObject(String path); /** * Returns the value of the given path as a String if available. @@ -51,9 +49,7 @@ public interface PropertyReader { * @param path the path to retrieve a String for * @return the value as a String, or null if not applicable or unavailable */ - @Nullable - @org.jetbrains.annotations.Nullable - String getString(String path); + @Nullable String getString(String path); /** * Returns the value of the given path as an integer if available. @@ -61,9 +57,7 @@ public interface PropertyReader { * @param path the path to retrieve an integer for * @return the value as integer, or null if not applicable or unavailable */ - @Nullable - @org.jetbrains.annotations.Nullable - Integer getInt(String path); + @Nullable Integer getInt(String path); /** * Returns the value of the given path as a double if available. @@ -71,9 +65,7 @@ public interface PropertyReader { * @param path the path to retrieve a double for * @return the value as a double, or null if not applicable or unavailable */ - @Nullable - @org.jetbrains.annotations.Nullable - Double getDouble(String path); + @Nullable Double getDouble(String path); /** * Returns the value of the given path as a boolean if available. @@ -81,9 +73,7 @@ public interface PropertyReader { * @param path the path to retrieve a boolean for * @return the value as a boolean, or null if not applicable or unavailable */ - @Nullable - @org.jetbrains.annotations.Nullable - Boolean getBoolean(String path); + @Nullable Boolean getBoolean(String path); /** * Returns the value of the given path as a list if available. @@ -91,8 +81,6 @@ public interface PropertyReader { * @param path the path to retrieve a list for * @return the value as a list, or null if not applicable or unavailable */ - @Nullable - @org.jetbrains.annotations.Nullable - List getList(String path); + @Nullable List getList(String path); } diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java index ff10c961..ac499044 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java @@ -5,7 +5,7 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -168,7 +168,7 @@ private static boolean isLeafValue(Object o) { * * @return map with the values from the file */ - protected @org.jetbrains.annotations.Nullable Map loadFile() { + protected @Nullable Map loadFile() { try (InputStream is = Files.newInputStream(path); InputStreamReader isr = new InputStreamReader(is, charset)) { Map rootMap = new Yaml().load(isr); diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java index 58877d26..9ac14e93 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java @@ -8,7 +8,7 @@ import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -26,7 +26,7 @@ public class YamlFileResource implements PropertyResource { private final Path path; private final @NotNull YamlFileResourceOptions options; private final String indentationSpace; - private @org.jetbrains.annotations.Nullable Yaml yamlObject; + private @Nullable Yaml yamlObject; public YamlFileResource(Path path) { this(path, YamlFileResourceOptions.builder().build()); @@ -92,7 +92,7 @@ protected final Path getPath() { * @throws IOException . */ protected void exportValue(@NotNull Writer writer, @NotNull PropertyPathTraverser pathTraverser, - @NotNull String path, @org.jetbrains.annotations.Nullable Object value) throws IOException { + @NotNull String path, @Nullable Object value) throws IOException { if (value == null) { return; } @@ -240,7 +240,7 @@ protected void onWriteComplete() { * * @return the YAML instance to use */ - protected @org.jetbrains.annotations.Nullable Yaml getYamlObject() { + protected @Nullable Yaml getYamlObject() { if (yamlObject == null) { yamlObject = createNewYaml(); } @@ -258,7 +258,7 @@ protected void onWriteComplete() { return options; } - private @org.jetbrains.annotations.Nullable Object getExportValue(@NotNull Property property, @NotNull ConfigurationData configurationData) { + private @Nullable Object getExportValue(@NotNull Property property, @NotNull ConfigurationData configurationData) { return property.toExportValue(configurationData.getValue(property)); } diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java index 1ad2289e..a2c36eb3 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java @@ -3,7 +3,7 @@ import ch.jalu.configme.resource.PropertyPathTraverser.PathElement; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.function.ToIntFunction; @@ -11,7 +11,7 @@ public class YamlFileResourceOptions { private final @NotNull Charset charset; - private final @org.jetbrains.annotations.Nullable ToIntFunction numberOfLinesBeforeFunction; + private final @Nullable ToIntFunction numberOfLinesBeforeFunction; private final int indentationSize; /** diff --git a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java index 0738b503..834f9e9b 100644 --- a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java +++ b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java @@ -9,7 +9,7 @@ import ch.jalu.configme.resource.PropertyResource; import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; diff --git a/src/main/java/ch/jalu/configme/utils/TypeInformation.java b/src/main/java/ch/jalu/configme/utils/TypeInformation.java index c5d0eba9..818e68fe 100644 --- a/src/main/java/ch/jalu/configme/utils/TypeInformation.java +++ b/src/main/java/ch/jalu/configme/utils/TypeInformation.java @@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -62,7 +62,7 @@ public Type getType() { * (e.g. setting a value to a field or adding to a collection); * null if not applicable */ - public @org.jetbrains.annotations.Nullable Class getSafeToWriteClass() { + public @Nullable Class getSafeToWriteClass() { return getSafeToWriteClassInternal(type); } diff --git a/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java b/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java index 0a3fa43a..15a2ff8d 100644 --- a/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java +++ b/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -101,9 +101,8 @@ protected Object convert(Class clazz, Object value) { return null; } - @Nullable @Override - public Object toExportValue(@Nullable Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { if (value instanceof CustomInteger) { return ((CustomInteger) value).value; } From 1106f789907d2fededc41292bae5d6db80e74c43 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 17:37:51 -0400 Subject: [PATCH 04/15] Removed findbugs jsr305 dependency --- pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pom.xml b/pom.xml index e5fa31eb..99fd7196 100644 --- a/pom.xml +++ b/pom.xml @@ -199,12 +199,6 @@ - - com.google.code.findbugs - jsr305 - provided - 3.0.2 - org.jetbrains annotations From 41d58631a919f662281c0dd870dc9de65f3dd507 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 17:39:58 -0400 Subject: [PATCH 05/15] Fixed missing and incorrectly inferred nullity annotations for leaf handlers --- .../leafvaluehandler/AbstractLeafValueHandler.java | 4 ++-- .../leafvaluehandler/BigNumberLeafValueHandler.java | 9 ++++----- .../leafvaluehandler/BooleanLeafValueHandler.java | 4 ++-- .../leafvaluehandler/CombiningLeafValueHandler.java | 9 +++++---- .../leafvaluehandler/EnumLeafValueHandler.java | 5 ++--- .../beanmapper/leafvaluehandler/LeafValueHandler.java | 3 ++- .../leafvaluehandler/NumberLeafValueHandler.java | 4 ++-- .../leafvaluehandler/ObjectLeafValueHandler.java | 4 ++-- .../leafvaluehandler/StandardLeafValueHandlers.java | 4 +++- .../leafvaluehandler/StringLeafValueHandler.java | 4 ++-- .../beanmapper/BeanWithCustomTypeHandlerTest.java | 2 +- .../leafvaluehandler/CombiningLeafValueHandlerTest.java | 4 +++- 12 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java index 5a2b38dc..bfeb50e9 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/AbstractLeafValueHandler.java @@ -11,10 +11,10 @@ public abstract class AbstractLeafValueHandler implements LeafValueHandler { @Override - public Object convert(@NotNull TypeInformation typeInformation, Object value) { + public @Nullable Object convert(@NotNull TypeInformation typeInformation, @Nullable Object value) { return convert(typeInformation.getSafeToWriteClass(), value); } - protected abstract @Nullable Object convert(Class clazz, Object value); + protected abstract @Nullable Object convert(@Nullable Class clazz, @Nullable Object value); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java index 379b015a..87cfc090 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BigNumberLeafValueHandler.java @@ -16,7 +16,7 @@ public class BigNumberLeafValueHandler extends AbstractLeafValueHandler { private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E100"); @Override - protected @Nullable Object convert(@NotNull Class clazz, Object value) { + protected @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (clazz != BigInteger.class && clazz != BigDecimal.class) { return null; } @@ -29,7 +29,7 @@ public class BigNumberLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(@Nullable Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { final Class valueType = value == null ? null : value.getClass(); if (valueType == BigInteger.class) { return value.toString(); @@ -49,8 +49,7 @@ public Object toExportValue(@Nullable Object value) { * @param value the value to convert * @return BigInteger or BigDecimal as defined by the target class, or null if no conversion was possible */ - @Nullable - protected Object fromString(Class targetClass, @NotNull String value) { + protected @Nullable Object fromString(@NotNull Class targetClass, @NotNull String value) { try { return targetClass == BigInteger.class ? new BigInteger(value) @@ -67,7 +66,7 @@ protected Object fromString(Class targetClass, @NotNull String value) { * @param value the value to convert * @return BigInteger or BigDecimal as defined by the target class */ - protected Object fromNumber(@NotNull Class targetClass, Number value) { + protected @NotNull Object fromNumber(@NotNull Class targetClass, @NotNull Number value) { if (targetClass.isInstance(value)) { return value; } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java index eff29d27..16fc28ba 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/BooleanLeafValueHandler.java @@ -6,7 +6,7 @@ public class BooleanLeafValueHandler extends AbstractLeafValueHandler { @Override - public @Nullable Object convert(Class clazz, Object value) { + public @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if ((clazz == boolean.class || clazz == Boolean.class) && value instanceof Boolean) { return value; } @@ -14,7 +14,7 @@ public class BooleanLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { return (value instanceof Boolean) ? value : null; } } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java index f9e85f4f..a7a406dc 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandler.java @@ -2,6 +2,7 @@ import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collection; @@ -22,7 +23,7 @@ public class CombiningLeafValueHandler implements LeafValueHandler { * * @param handlers the handlers to delegate each call to (in the given order) */ - public CombiningLeafValueHandler(LeafValueHandler... handlers) { + public CombiningLeafValueHandler(@NotNull LeafValueHandler... handlers) { this(Arrays.asList(handlers)); } @@ -36,12 +37,12 @@ public CombiningLeafValueHandler(@NotNull Collection handlers) } @Override - public Object convert(TypeInformation typeInformation, Object value) { + public @Nullable Object convert(@NotNull TypeInformation typeInformation, @Nullable Object value) { return getFirstNonNull(t -> t.convert(typeInformation, value)); } @Override - public Object toExportValue(Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { return getFirstNonNull(t -> t.toExportValue(value)); } @@ -49,7 +50,7 @@ public Object toExportValue(Object value) { return handlers; } - private Object getFirstNonNull(Function callback) { + private @Nullable Object getFirstNonNull(@NotNull Function callback) { return handlers.stream() .map(callback) .filter(Objects::nonNull) diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java index 98c5c44f..8ab16294 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/EnumLeafValueHandler.java @@ -1,13 +1,12 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** Enum handler. */ public class EnumLeafValueHandler extends AbstractLeafValueHandler { @Override - public @Nullable Object convert(@NotNull Class clazz, Object value) { + public @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (value instanceof String && Enum.class.isAssignableFrom(clazz)) { String givenText = (String) value; for (Enum e : (Enum[]) clazz.getEnumConstants()) { @@ -20,7 +19,7 @@ public class EnumLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { if (value instanceof Enum) { return ((Enum) value).name(); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java index 426f1b6f..d7bc23f8 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java @@ -1,6 +1,7 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -25,7 +26,7 @@ public interface LeafValueHandler { * @param value the value to convert * @return value of the given type, or null if not applicable */ - @Nullable Object convert(TypeInformation typeInformation, @Nullable Object value); + @Nullable Object convert(@NotNull TypeInformation typeInformation, @Nullable Object value); /** * Converts the given value to a type more suitable for exporting. Used by the mapper in diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java index be5e90f4..e39d801c 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java @@ -17,7 +17,7 @@ public class NumberLeafValueHandler extends AbstractLeafValueHandler { createMapOfTypeToTransformFunction(); @Override - public @Nullable Object convert(Class clazz, Object value) { + public @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (value instanceof Number) { Function numberFunction = NUMBER_CLASSES_TO_CONVERSION.get(clazz); return numberFunction == null ? null : numberFunction.apply((Number) value); @@ -26,7 +26,7 @@ public class NumberLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(@Nullable Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { Class clazz = value == null ? null : value.getClass(); if (NUMBER_CLASSES_TO_CONVERSION.containsKey(clazz)) { return value; diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java index 78afb8f7..27f0a2a3 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/ObjectLeafValueHandler.java @@ -6,7 +6,7 @@ public class ObjectLeafValueHandler extends AbstractLeafValueHandler { @Override - public @Nullable Object convert(Class clazz, Object value) { + public @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (clazz == Object.class) { return value; } @@ -14,7 +14,7 @@ public class ObjectLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { return null; } } diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StandardLeafValueHandlers.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StandardLeafValueHandlers.java index 14b769ff..82b1d4e4 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StandardLeafValueHandlers.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StandardLeafValueHandlers.java @@ -1,5 +1,7 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; +import org.jetbrains.annotations.NotNull; + /** * Provides the leaf value handler which is used by default. * @@ -18,7 +20,7 @@ private StandardLeafValueHandlers() { * * @return default leaf value handler */ - public static LeafValueHandler getDefaultLeafValueHandler() { + public static @NotNull LeafValueHandler getDefaultLeafValueHandler() { if (defaultHandler == null) { defaultHandler = new CombiningLeafValueHandler(new StringLeafValueHandler(), new EnumLeafValueHandler(), new BooleanLeafValueHandler(), new NumberLeafValueHandler(), new BigNumberLeafValueHandler(), diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java index 6e57fbdf..2bd9307a 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/StringLeafValueHandler.java @@ -6,7 +6,7 @@ public class StringLeafValueHandler extends AbstractLeafValueHandler { @Override - public @Nullable Object convert(Class clazz, Object value) { + public @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (clazz == String.class && (value instanceof String || value instanceof Number || value instanceof Boolean)) { return value.toString(); @@ -15,7 +15,7 @@ public class StringLeafValueHandler extends AbstractLeafValueHandler { } @Override - public Object toExportValue(Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { return (value instanceof String) ? value : null; } } diff --git a/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java b/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java index 15a2ff8d..cf150500 100644 --- a/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java +++ b/src/test/java/ch/jalu/configme/beanmapper/BeanWithCustomTypeHandlerTest.java @@ -94,7 +94,7 @@ public static final class MapperWithCustomIntSupport extends MapperImpl { public static final class CustomIntegerLeafValueHandler extends AbstractLeafValueHandler { @Override - protected Object convert(Class clazz, Object value) { + protected @Nullable Object convert(@Nullable Class clazz, @Nullable Object value) { if (clazz == CustomInteger.class && value instanceof Number) { return new CustomInteger(((Number) value).intValue(), false); } diff --git a/src/test/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandlerTest.java b/src/test/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandlerTest.java index b52ad2b6..a1314b87 100644 --- a/src/test/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandlerTest.java +++ b/src/test/java/ch/jalu/configme/beanmapper/leafvaluehandler/CombiningLeafValueHandlerTest.java @@ -1,6 +1,8 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -54,7 +56,7 @@ private TestValueTransformerImpl(String suffix) { } @Override - public Object convert(TypeInformation typeInformation, Object value) { + public @Nullable Object convert(@NotNull TypeInformation typeInformation, @Nullable Object value) { return suffix == null ? null : "value_" + suffix; } From 522d78d5eb76744f460aa71e81ba71a44a9fb57c Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 16 Oct 2021 18:27:20 -0400 Subject: [PATCH 06/15] Fixed missing and incorrectly inferred nullity annotations for property description package --- .../BeanDescriptionFactory.java | 4 +++- .../BeanDescriptionFactoryImpl.java | 21 +++++++++---------- .../BeanPropertyDescription.java | 9 ++++---- .../BeanPropertyDescriptionImpl.java | 13 ++++++------ 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactory.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactory.java index a5e36512..6551d2f5 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactory.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactory.java @@ -1,5 +1,7 @@ package ch.jalu.configme.beanmapper.propertydescription; +import org.jetbrains.annotations.NotNull; + import java.util.Collection; /** @@ -16,6 +18,6 @@ public interface BeanDescriptionFactory { * @param clazz the class whose properties should be returned * @return the relevant properties on the class */ - Collection getAllProperties(Class clazz); + @NotNull Collection getAllProperties(@NotNull Class clazz); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java index 448ea0ce..b4ab65fd 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanDescriptionFactoryImpl.java @@ -4,8 +4,8 @@ import ch.jalu.configme.beanmapper.ExportName; import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; + import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; @@ -42,7 +42,7 @@ public class BeanDescriptionFactoryImpl implements BeanDescriptionFactory { * @return the bean class' properties to handle */ @Override - public Collection getAllProperties(Class clazz) { + public @NotNull Collection getAllProperties(@NotNull Class clazz) { return classProperties.computeIfAbsent(clazz, this::collectAllProperties); } @@ -52,7 +52,7 @@ public Collection getAllProperties(Class clazz) { * @param clazz the class to process * @return properties of the class */ - protected List collectAllProperties(Class clazz) { + protected @NotNull List collectAllProperties(@NotNull Class clazz) { List descriptors = getWritableProperties(clazz); List properties = descriptors.stream() @@ -70,8 +70,7 @@ protected List collectAllProperties(Class clazz) { * @param descriptor the descriptor to convert * @return the converted object, or null if the property should be skipped */ - @Nullable - protected BeanPropertyDescription convert(@NotNull PropertyDescriptor descriptor) { + protected @Nullable BeanPropertyDescription convert(@NotNull PropertyDescriptor descriptor) { if (Boolean.TRUE.equals(descriptor.getValue("transient"))) { return null; } @@ -89,7 +88,7 @@ protected BeanPropertyDescription convert(@NotNull PropertyDescriptor descriptor * @param clazz the class to which the properties belong * @param properties the properties that will be used on the class */ - protected void validateProperties(Class clazz, @NotNull Collection properties) { + protected void validateProperties(@NotNull Class clazz, @NotNull Collection properties) { Set names = new HashSet<>(properties.size()); properties.forEach(property -> { if (property.getName().isEmpty()) { @@ -108,7 +107,7 @@ protected void validateProperties(Class clazz, @NotNull Collection getWritableProperties(Class clazz) { + protected @NotNull List getWritableProperties(@NotNull Class clazz) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); @@ -152,7 +151,7 @@ protected String getPropertyName(@NotNull PropertyDescriptor descriptor) { * @param properties the properties to sort * @return sorted properties */ - protected @NotNull List sortPropertiesList(Class clazz, @NotNull List properties) { + protected @NotNull List sortPropertiesList(@NotNull Class clazz, @NotNull List properties) { Map fieldNameByIndex = createFieldNameOrderMap(clazz); int maxIndex = fieldNameByIndex.size(); @@ -171,7 +170,7 @@ protected String getPropertyName(@NotNull PropertyDescriptor descriptor) { * @param clazz the class to create the field index map for * @return map with all field names as keys and its index as value */ - protected @NotNull Map createFieldNameOrderMap(Class clazz) { + protected @NotNull Map createFieldNameOrderMap(@NotNull Class clazz) { Map nameByIndex = new HashMap<>(); int i = 0; for (Class currentClass : collectClassAndAllParents(clazz)) { @@ -190,7 +189,7 @@ protected String getPropertyName(@NotNull PropertyDescriptor descriptor) { * @param clazz the class whose parents should be collected * @return list of all of the class' parents, sorted by highest class in the hierarchy to lowest */ - protected @NotNull List> collectClassAndAllParents(Class clazz) { + protected @NotNull List> collectClassAndAllParents(@NotNull Class clazz) { List> parents = new ArrayList<>(); Class curClass = clazz; while (curClass != null && curClass != Object.class) { diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java index ca7daaab..3a1e9be8 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescription.java @@ -2,6 +2,7 @@ import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -15,12 +16,12 @@ public interface BeanPropertyDescription { /** * @return the name of the property in the configuration file */ - String getName(); + @NotNull String getName(); /** * @return property type */ - TypeInformation getTypeInformation(); + @NotNull TypeInformation getTypeInformation(); /** * Sets the given value on the provided bean for this property. The value should correspond @@ -29,7 +30,7 @@ public interface BeanPropertyDescription { * @param bean the bean to set the property on * @param value the value to set */ - void setValue(Object bean, Object value); + void setValue(@NotNull Object bean, @NotNull Object value); /** * Returns the value of the property for the given bean. @@ -37,5 +38,5 @@ public interface BeanPropertyDescription { * @param bean the bean to read the property from * @return the value of the property (can be null) */ - @Nullable Object getValue(Object bean); + @Nullable Object getValue(@NotNull Object bean); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java index 5c48e42e..a785026e 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/propertydescription/BeanPropertyDescriptionImpl.java @@ -3,8 +3,8 @@ import ch.jalu.configme.beanmapper.ConfigMeMapperException; import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -26,7 +26,7 @@ public class BeanPropertyDescriptionImpl implements BeanPropertyDescription { * @param getter getter for the property * @param setter setter for the property */ - public BeanPropertyDescriptionImpl(String name, TypeInformation typeInformation, Method getter, Method setter) { + public BeanPropertyDescriptionImpl(@NotNull String name, @NotNull TypeInformation typeInformation, @NotNull Method getter, @NotNull Method setter) { this.name = name; this.typeInformation = typeInformation; this.getter = getter; @@ -34,12 +34,12 @@ public BeanPropertyDescriptionImpl(String name, TypeInformation typeInformation, } @Override - public String getName() { + public @NotNull String getName() { return name; } @Override - public TypeInformation getTypeInformation() { + public @NotNull TypeInformation getTypeInformation() { return typeInformation; } @@ -49,8 +49,7 @@ public TypeInformation getTypeInformation() { * @param bean the bean to read the property from * @return bean value */ - @Nullable - public Object getValue(Object bean) { + public @Nullable Object getValue(@NotNull Object bean) { try { return getter.invoke(bean); } catch (@NotNull IllegalAccessException | InvocationTargetException e) { @@ -65,7 +64,7 @@ public Object getValue(Object bean) { * @param bean the bean to modify * @param value the value to set the property to */ - public void setValue(Object bean, Object value) { + public void setValue(@NotNull Object bean, @NotNull Object value) { try { setter.invoke(bean, value); } catch (@NotNull IllegalAccessException | InvocationTargetException e) { From 228b58973ac9088b896201698ebdd59477555c5d Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 00:36:09 -0400 Subject: [PATCH 07/15] Fixed missing and incorrectly inferred nullity annotations for bean mapper package --- .../beanmapper/ConfigMeMapperException.java | 11 +++--- .../configme/beanmapper/DefaultMapper.java | 4 ++- .../ch/jalu/configme/beanmapper/Mapper.java | 6 ++-- .../jalu/configme/beanmapper/MapperImpl.java | 36 ++++++++----------- .../configme/beanmapper/MappingContext.java | 9 ++--- .../beanmapper/MappingContextImpl.java | 10 +++--- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/main/java/ch/jalu/configme/beanmapper/ConfigMeMapperException.java b/src/main/java/ch/jalu/configme/beanmapper/ConfigMeMapperException.java index de18699d..250522f4 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/ConfigMeMapperException.java +++ b/src/main/java/ch/jalu/configme/beanmapper/ConfigMeMapperException.java @@ -2,6 +2,7 @@ import ch.jalu.configme.exception.ConfigMeException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Exception during a bean mapping process. @@ -15,7 +16,7 @@ public class ConfigMeMapperException extends ConfigMeException { * * @param message the exception message */ - public ConfigMeMapperException(String message) { + public ConfigMeMapperException(@NotNull String message) { super(message); } @@ -25,7 +26,7 @@ public ConfigMeMapperException(String message) { * @param message the exception message * @param cause the cause */ - public ConfigMeMapperException(String message, Throwable cause) { + public ConfigMeMapperException(@NotNull String message, @Nullable Throwable cause) { super(message, cause); } @@ -35,7 +36,7 @@ public ConfigMeMapperException(String message, Throwable cause) { * @param mappingContext the mapping context with which the message should be extended * @param message basic message to extend */ - public ConfigMeMapperException(@NotNull MappingContext mappingContext, String message) { + public ConfigMeMapperException(@NotNull MappingContext mappingContext, @NotNull String message) { super(constructMessage(mappingContext, message)); } @@ -46,11 +47,11 @@ public ConfigMeMapperException(@NotNull MappingContext mappingContext, String me * @param message basic message to extend * @param cause the cause */ - public ConfigMeMapperException(@NotNull MappingContext mappingContext, String message, Throwable cause) { + public ConfigMeMapperException(@NotNull MappingContext mappingContext, @NotNull String message, @Nullable Throwable cause) { super(constructMessage(mappingContext, message), cause); } - private static @NotNull String constructMessage(@NotNull MappingContext mappingContext, String message) { + private static @NotNull String constructMessage(@NotNull MappingContext mappingContext, @NotNull String message) { return message + ", for mapping of: [" + mappingContext.createDescription() + "]"; } } diff --git a/src/main/java/ch/jalu/configme/beanmapper/DefaultMapper.java b/src/main/java/ch/jalu/configme/beanmapper/DefaultMapper.java index 26a833de..0c45f448 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/DefaultMapper.java +++ b/src/main/java/ch/jalu/configme/beanmapper/DefaultMapper.java @@ -1,5 +1,7 @@ package ch.jalu.configme.beanmapper; +import org.jetbrains.annotations.NotNull; + /** * Provides the {@link Mapper} instance which is used by default. */ @@ -13,7 +15,7 @@ private DefaultMapper() { /** * @return default mapper instance */ - public static Mapper getInstance() { + public static @NotNull Mapper getInstance() { if (instance == null) { instance = new DefaultMapper(); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/Mapper.java b/src/main/java/ch/jalu/configme/beanmapper/Mapper.java index 520359a1..6825cfab 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/Mapper.java +++ b/src/main/java/ch/jalu/configme/beanmapper/Mapper.java @@ -2,7 +2,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; - +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -21,7 +21,7 @@ public interface Mapper { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return object of the given type, or null if not possible */ - @Nullable Object convertToBean(@Nullable Object value, TypeInformation typeInformation, ConvertErrorRecorder errorRecorder); + @Nullable Object convertToBean(@Nullable Object value, @NotNull TypeInformation typeInformation, @NotNull ConvertErrorRecorder errorRecorder); /** * Converts the given value to an object of the given class, if possible. Returns null otherwise. @@ -36,7 +36,7 @@ public interface Mapper { */ @Nullable @SuppressWarnings("unchecked") - default T convertToBean(@Nullable Object value, Class clazz, ConvertErrorRecorder errorRecorder) { + default T convertToBean(@Nullable Object value, @NotNull Class clazz, @NotNull ConvertErrorRecorder errorRecorder) { return (T) convertToBean(value, new TypeInformation(clazz), errorRecorder); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java index e95baddb..628954d2 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java @@ -8,8 +8,8 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.utils.TypeInformation; import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; + import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -64,20 +64,20 @@ public MapperImpl() { this(new BeanDescriptionFactoryImpl(), StandardLeafValueHandlers.getDefaultLeafValueHandler()); } - public MapperImpl(BeanDescriptionFactory beanDescriptionFactory, LeafValueHandler leafValueHandler) { + public MapperImpl(@NotNull BeanDescriptionFactory beanDescriptionFactory, @NotNull LeafValueHandler leafValueHandler) { this.beanDescriptionFactory = beanDescriptionFactory; this.leafValueHandler = leafValueHandler; } - protected final BeanDescriptionFactory getBeanDescriptionFactory() { + protected final @NotNull BeanDescriptionFactory getBeanDescriptionFactory() { return beanDescriptionFactory; } - protected final LeafValueHandler getLeafValueHandler() { + protected final @NotNull LeafValueHandler getLeafValueHandler() { return leafValueHandler; } - protected @NotNull MappingContext createRootMappingContext(TypeInformation beanType, ConvertErrorRecorder errorRecorder) { + protected @NotNull MappingContext createRootMappingContext(@NotNull TypeInformation beanType, @NotNull ConvertErrorRecorder errorRecorder) { return MappingContextImpl.createRoot(beanType, errorRecorder); } @@ -87,7 +87,7 @@ protected final LeafValueHandler getLeafValueHandler() { // --------- @Override - public Object toExportValue(@Nullable Object value) { + public @Nullable Object toExportValue(@Nullable Object value) { // Step 1: attempt simple value transformation Object simpleValue = leafValueHandler.toExportValue(value); if (simpleValue != null || value == null) { @@ -119,8 +119,7 @@ public Object toExportValue(@Nullable Object value) { * @param value the value to convert * @return the export value to use or {@link #RETURN_NULL}, or null if not applicable */ - @Nullable - protected Object createExportValueForSpecialTypes(Object value) { + protected @Nullable Object createExportValueForSpecialTypes(@Nullable Object value) { if (value instanceof Collection) { return ((Collection) value).stream() .map(this::toExportValue) @@ -143,7 +142,7 @@ protected Object createExportValueForSpecialTypes(Object value) { return null; } - protected static @Nullable Object unwrapReturnNull(Object o) { + protected static @Nullable Object unwrapReturnNull(@Nullable Object o) { return o == RETURN_NULL ? null : o; } @@ -151,9 +150,8 @@ protected Object createExportValueForSpecialTypes(Object value) { // Bean mapping // --------- - @Nullable @Override - public Object convertToBean(@Nullable Object value, TypeInformation beanType, ConvertErrorRecorder errorRecorder) { + public @Nullable Object convertToBean(@Nullable Object value, @NotNull TypeInformation beanType, @NotNull ConvertErrorRecorder errorRecorder) { if (value == null) { return null; } @@ -168,8 +166,7 @@ public Object convertToBean(@Nullable Object value, TypeInformation beanType, Co * @param value the value to convert from * @return object whose type matches the one in the mapping context, or null if not applicable */ - @Nullable - protected Object convertValueForType(@NotNull MappingContext context, Object value) { + protected @Nullable Object convertValueForType(@NotNull MappingContext context, @Nullable Object value) { Class rawClass = context.getTypeInformation().getSafeToWriteClass(); if (rawClass == null) { throw new ConfigMeMapperException(context, "Cannot determine required type"); @@ -198,8 +195,7 @@ protected Object convertValueForType(@NotNull MappingContext context, Object val * @param value the value to convert from * @return object whose type matches the one in the mapping context, or null if not applicable */ - @Nullable - protected Object handleSpecialTypes(@NotNull MappingContext context, Object value) { + protected @Nullable Object handleSpecialTypes(@NotNull MappingContext context, @Nullable Object value) { final Class rawClass = context.getTypeInformation().getSafeToWriteClass(); if (Collection.class.isAssignableFrom(rawClass)) { return createCollection(context, value); @@ -221,7 +217,7 @@ protected Object handleSpecialTypes(@NotNull MappingContext context, Object valu * @return Collection property from the value, or null if not applicable */ @SuppressWarnings("unchecked") - protected @Nullable Collection createCollection(@NotNull MappingContext context, Object value) { + protected @Nullable Collection createCollection(@NotNull MappingContext context, @Nullable Object value) { if (value instanceof Iterable) { TypeInformation entryType = context.getGenericTypeInfoOrFail(0); Collection result = createCollectionMatchingType(context); @@ -266,9 +262,8 @@ protected Object handleSpecialTypes(@NotNull MappingContext context, Object valu * @param value value to map from * @return Map property, or null if not applicable */ - @Nullable @SuppressWarnings("unchecked") - protected Map createMap(@NotNull MappingContext context, Object value) { + protected @Nullable Map createMap(@NotNull MappingContext context, @Nullable Object value) { if (value instanceof Map) { if (context.getGenericTypeInfoOrFail(0).getSafeToWriteClass() != String.class) { throw new ConfigMeMapperException(context, "The key type of maps may only be of String type"); @@ -310,7 +305,7 @@ protected Map createMap(@NotNull MappingContext context, Object value) { // -- Optional - protected Object createOptional(@NotNull MappingContext context, Object value) { + protected @NotNull Object createOptional(@NotNull MappingContext context, @Nullable Object value) { MappingContext childContext = context.createChild("[v]", context.getGenericTypeInfoOrFail(0)); Object result = convertValueForType(childContext, value); return Optional.ofNullable(result); @@ -325,8 +320,7 @@ protected Object createOptional(@NotNull MappingContext context, Object value) { * @param value the value from the property resource * @return the converted value, or null if not possible */ - @Nullable - protected Object createBean(@NotNull MappingContext context, Object value) { + protected @Nullable Object createBean(@NotNull MappingContext context, @Nullable Object value) { // Ensure that the value is a map so we can map it to a bean if (!(value instanceof Map)) { return null; diff --git a/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java b/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java index d0d975d2..4794eb5e 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MappingContext.java @@ -1,6 +1,7 @@ package ch.jalu.configme.beanmapper; import ch.jalu.configme.utils.TypeInformation; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -15,12 +16,12 @@ public interface MappingContext { * @param typeInformation the required type * @return new child context */ - MappingContext createChild(String name, TypeInformation typeInformation); + @NotNull MappingContext createChild(@NotNull String name, @NotNull TypeInformation typeInformation); /** * @return the required type the value needs to be mapped to */ - TypeInformation getTypeInformation(); + @NotNull TypeInformation getTypeInformation(); /** * Convenience method: gets the generic type info for the given index and ensures that the generic type information @@ -41,7 +42,7 @@ public interface MappingContext { /** * @return textual representation of the info in the context, used in exceptions */ - String createDescription(); + @NotNull String createDescription(); /** * Registers an error during the mapping process, which delegates to the supplied @@ -50,6 +51,6 @@ public interface MappingContext { * * @param reason the error reason (ignored by the default context implementation) */ - void registerError(String reason); + void registerError(@NotNull String reason); } diff --git a/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java b/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java index 78940b04..5655ad62 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java +++ b/src/main/java/ch/jalu/configme/beanmapper/MappingContextImpl.java @@ -13,7 +13,7 @@ public class MappingContextImpl implements MappingContext { private final TypeInformation typeInformation; private final ConvertErrorRecorder errorRecorder; - protected MappingContextImpl(String path, TypeInformation typeInformation, ConvertErrorRecorder errorRecorder) { + protected MappingContextImpl(@NotNull String path, @NotNull TypeInformation typeInformation, @NotNull ConvertErrorRecorder errorRecorder) { this.path = path; this.typeInformation = typeInformation; this.errorRecorder = errorRecorder; @@ -26,12 +26,12 @@ protected MappingContextImpl(String path, TypeInformation typeInformation, Conve * @param errorRecorder error recorder to register errors even if a valid value is returned * @return root mapping context */ - public static @NotNull MappingContextImpl createRoot(TypeInformation typeInformation, ConvertErrorRecorder errorRecorder) { + public static @NotNull MappingContextImpl createRoot(@NotNull TypeInformation typeInformation, @NotNull ConvertErrorRecorder errorRecorder) { return new MappingContextImpl("", typeInformation, errorRecorder); } @Override - public @NotNull MappingContext createChild(String subPath, TypeInformation typeInformation) { + public @NotNull MappingContext createChild(@NotNull String subPath, @NotNull TypeInformation typeInformation) { if (path.isEmpty()) { return new MappingContextImpl(subPath, typeInformation, errorRecorder); } @@ -39,7 +39,7 @@ protected MappingContextImpl(String path, TypeInformation typeInformation, Conve } @Override - public TypeInformation getTypeInformation() { + public @NotNull TypeInformation getTypeInformation() { return typeInformation; } @@ -49,7 +49,7 @@ public TypeInformation getTypeInformation() { } @Override - public void registerError(String reason) { + public void registerError(@NotNull String reason) { errorRecorder.setHasError("At path '" + path + "': " + reason); } From 38302bd27c59a8aa473a8760485a6b81a0b78266 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 02:32:08 -0400 Subject: [PATCH 08/15] Fixed missing and incorrectly inferred nullity annotations for configuration data package --- .../configurationdata/CommentsConfiguration.java | 4 ++-- .../configurationdata/ConfigurationData.java | 14 ++++++++------ .../ConfigurationDataBuilder.java | 13 ++++++------- .../configurationdata/ConfigurationDataImpl.java | 10 +++++----- .../configurationdata/PropertyListBuilder.java | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java index 8d2578b0..57551687 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java +++ b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java @@ -28,7 +28,7 @@ public CommentsConfiguration() { * * @param comments map to store comments in */ - public CommentsConfiguration(Map> comments) { + public CommentsConfiguration(@NotNull Map> comments) { this.comments = comments; } @@ -39,7 +39,7 @@ public CommentsConfiguration(Map> comments) { * @param path the path to register the comment lines for * @param commentLines the comment lines to set for the path */ - public void setComment(String path, String... commentLines) { + public void setComment(@NotNull String path, @NotNull String... commentLines) { comments.put(path, Collections.unmodifiableList(Arrays.asList(commentLines))); } diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java index 43288ab5..5ef20b79 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java @@ -3,6 +3,8 @@ import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Map; @@ -26,7 +28,7 @@ public interface ConfigurationData { * * @return list of properties, in order */ - List> getProperties(); + @NotNull List> getProperties(); /** * Returns the comments associated with the given path. @@ -34,7 +36,7 @@ public interface ConfigurationData { * @param path the path for which the comments should be retrieved * @return list of comments, never null */ - List getCommentsForSection(String path); + @NotNull List getCommentsForSection(String path); /** * Returns all comments registered to this configuration data. Typically for tests and @@ -43,7 +45,7 @@ public interface ConfigurationData { * * @return read-only view of all comments */ - Map> getAllComments(); + @NotNull Map> getAllComments(); /** * Initializes the values of all {@link #getProperties known properties} based on the provided reader. @@ -51,7 +53,7 @@ public interface ConfigurationData { * * @param propertyReader the reader to use to determine the property's values */ - void initializeValues(PropertyReader propertyReader); + void initializeValues(@NotNull PropertyReader propertyReader); /** * Returns the value associated with the given property. Only to be used with properties contained in @@ -62,7 +64,7 @@ public interface ConfigurationData { * @param property type * @return value associated with the property, or null if not present */ - T getValue(Property property); + T getValue(@NotNull Property property); /** * Sets the given value for the given property. May throw an exception @@ -72,7 +74,7 @@ public interface ConfigurationData { * @param value the value to set * @param the property type */ - void setValue(Property property, T value); + void setValue(@NotNull Property property, @NotNull T value); /** * Returns if the last call of {@link #initializeValues} had fully valid values in the resource diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java index 6d541378..49083dfa 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataBuilder.java @@ -5,8 +5,8 @@ import ch.jalu.configme.exception.ConfigMeException; import ch.jalu.configme.properties.Property; import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; + import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -41,7 +41,7 @@ protected ConfigurationDataBuilder() { * @return collected configuration data */ @SafeVarargs - public static @NotNull ConfigurationData createConfiguration(Class... classes) { + public static @NotNull ConfigurationData createConfiguration(@NotNull Class... classes) { return createConfiguration(Arrays.asList(classes)); } @@ -96,7 +96,7 @@ protected void collectProperties(@NotNull Class clazz) { }); } - protected void setCommentForPropertyField(@NotNull Field field, String path) { + protected void setCommentForPropertyField(@NotNull Field field, @NotNull String path) { Comment commentAnnotation = field.getAnnotation(Comment.class); if (commentAnnotation != null) { commentsConfiguration.setComment(path, commentAnnotation.value()); @@ -109,8 +109,7 @@ protected void setCommentForPropertyField(@NotNull Field field, String path) { * @param field the field's value to return * @return the property the field defines, or null if not applicable */ - @Nullable - protected Property getPropertyField(@NotNull Field field) { + protected @Nullable Property getPropertyField(@NotNull Field field) { if (Property.class.isAssignableFrom(field.getType()) && Modifier.isStatic(field.getModifiers())) { try { return (Property) field.get(null); @@ -141,7 +140,7 @@ protected void collectSectionComments(@NotNull Class c return constructor.newInstance(); } catch (NoSuchMethodException e) { throw new ConfigMeException("Expected no-args constructor to be available for " + clazz, e); - } catch (@NotNull IllegalAccessException | InstantiationException | InvocationTargetException e) { + } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new ConfigMeException("Could not create instance of " + clazz, e); } } @@ -153,7 +152,7 @@ protected void collectSectionComments(@NotNull Class c * @param clazz the class whose fields should be returned * @return stream of all the fields to process */ - protected Stream findFieldsToProcess(@NotNull Class clazz) { + protected @NotNull Stream findFieldsToProcess(@NotNull Class clazz) { // In most cases we expect the class not to have any parent, so we check here and "fast track" this case if (Object.class.equals(clazz.getSuperclass())) { return Arrays.stream(clazz.getDeclaredFields()); diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java index ab2aa5b5..33bca6b3 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationDataImpl.java @@ -42,7 +42,7 @@ protected ConfigurationDataImpl(@NotNull List> allProperti } @Override - public List getCommentsForSection(String path) { + public @NotNull List getCommentsForSection(String path) { return allComments.getOrDefault(path, Collections.emptyList()); } @@ -53,7 +53,7 @@ public List getCommentsForSection(String path) { @Override @SuppressWarnings("unchecked") - public @NotNull T getValue(@NotNull Property property) { + public T getValue(@NotNull Property property) { Object value = values.get(property.getPath()); if (value == null) { throw new ConfigMeException(format("No value exists for property with path '%s'. This may happen if " @@ -64,7 +64,7 @@ public List getCommentsForSection(String path) { } @Override - public void setValue(@NotNull Property property, T value) { + public void setValue(@NotNull Property property, @NotNull T value) { if (property.isValidValue(value)) { values.put(property.getPath(), value); } else { @@ -73,7 +73,7 @@ public void setValue(@NotNull Property property, T value) { } @Override - public void initializeValues(PropertyReader reader) { + public void initializeValues(@NotNull PropertyReader reader) { values.clear(); allPropertiesValidInResource = getProperties().stream() @@ -85,7 +85,7 @@ public void initializeValues(PropertyReader reader) { * Saves the value for the provided property as determined from the reader and returns whether the * property is represented in a fully valid way in the resource. */ - protected boolean setValueForProperty(@NotNull Property property, PropertyReader reader) { + protected boolean setValueForProperty(@NotNull Property property, @NotNull PropertyReader reader) { PropertyValue propertyValue = property.determineValue(reader); setValue(property, propertyValue.getValue()); return propertyValue.isValidInResource(); diff --git a/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java b/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java index 2c51b556..ea010a04 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java +++ b/src/main/java/ch/jalu/configme/configurationdata/PropertyListBuilder.java @@ -61,7 +61,7 @@ public void add(@NotNull Property property) { return rootEntries; } - private static @NotNull Map getChildMap(@NotNull Map parent, String path) { + private static @NotNull Map getChildMap(@NotNull Map parent, @NotNull String path) { Object o = parent.get(path); if (o instanceof Map) { return asTypedMap(o); From e13e8f3d886b12c1596b17014042e848576323ae Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 09:29:19 -0400 Subject: [PATCH 09/15] Fixed missing and incorrectly inferred nullity annotations for exception, migration, and base package --- .../java/ch/jalu/configme/SettingsHolder.java | 3 ++- .../java/ch/jalu/configme/SettingsManager.java | 5 +++-- .../ch/jalu/configme/SettingsManagerBuilder.java | 8 ++++---- .../ch/jalu/configme/SettingsManagerImpl.java | 16 ++++++++-------- .../configme/exception/ConfigMeException.java | 7 +++++-- .../configme/migration/MigrationService.java | 3 ++- .../migration/PlainMigrationService.java | 4 ++-- .../migration/PlainMigrationServiceTest.java | 3 ++- 8 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/ch/jalu/configme/SettingsHolder.java b/src/main/java/ch/jalu/configme/SettingsHolder.java index 29fabc70..59606d1a 100644 --- a/src/main/java/ch/jalu/configme/SettingsHolder.java +++ b/src/main/java/ch/jalu/configme/SettingsHolder.java @@ -1,6 +1,7 @@ package ch.jalu.configme; import ch.jalu.configme.configurationdata.CommentsConfiguration; +import org.jetbrains.annotations.NotNull; /** * Marker interface for classes that define Property objects. @@ -24,7 +25,7 @@ public interface SettingsHolder { * * @param conf the comments configuration */ - default void registerComments(CommentsConfiguration conf) { + default void registerComments(@NotNull CommentsConfiguration conf) { // override to register comments for sections } } diff --git a/src/main/java/ch/jalu/configme/SettingsManager.java b/src/main/java/ch/jalu/configme/SettingsManager.java index df2c8a75..fb382a48 100644 --- a/src/main/java/ch/jalu/configme/SettingsManager.java +++ b/src/main/java/ch/jalu/configme/SettingsManager.java @@ -2,6 +2,7 @@ import ch.jalu.configme.migration.MigrationService; import ch.jalu.configme.properties.Property; +import org.jetbrains.annotations.NotNull; /** * Settings manager. @@ -25,7 +26,7 @@ public interface SettingsManager { * @param The property's type * @return The property's value */ - T getProperty(Property property); + T getProperty(@NotNull Property property); /** * Sets a new value for the given property. @@ -34,7 +35,7 @@ public interface SettingsManager { * @param value The new value to assign to the property * @param The property's type */ - void setProperty(Property property, T value); + void setProperty(@NotNull Property property, @NotNull T value); /** * Reloads the configuration. diff --git a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java index 36d90fab..25afe8dd 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java @@ -24,7 +24,7 @@ public final class SettingsManagerBuilder { private ConfigurationData configurationData; private @Nullable MigrationService migrationService; - private SettingsManagerBuilder(PropertyResource resource) { + private SettingsManagerBuilder(@NotNull PropertyResource resource) { this.resource = resource; } @@ -77,7 +77,7 @@ private SettingsManagerBuilder(PropertyResource resource) { * @param resource the resource to use * @return settings manager builder */ - public static @NotNull SettingsManagerBuilder withResource(PropertyResource resource) { + public static @NotNull SettingsManagerBuilder withResource(@NotNull PropertyResource resource) { return new SettingsManagerBuilder(resource); } @@ -88,7 +88,7 @@ private SettingsManagerBuilder(PropertyResource resource) { * @return this builder */ @SafeVarargs - public final @NotNull SettingsManagerBuilder configurationData(Class... classes) { + public final @NotNull SettingsManagerBuilder configurationData(@NotNull Class... classes) { this.configurationData = ConfigurationDataBuilder.createConfiguration(classes); return this; } @@ -99,7 +99,7 @@ private SettingsManagerBuilder(PropertyResource resource) { * @param configurationData the configuration data * @return this builder */ - public @NotNull SettingsManagerBuilder configurationData(ConfigurationData configurationData) { + public @NotNull SettingsManagerBuilder configurationData(@NotNull ConfigurationData configurationData) { this.configurationData = configurationData; return this; } diff --git a/src/main/java/ch/jalu/configme/SettingsManagerImpl.java b/src/main/java/ch/jalu/configme/SettingsManagerImpl.java index 169449a1..8e65f460 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerImpl.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerImpl.java @@ -6,6 +6,7 @@ import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyResource; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -28,7 +29,7 @@ public class SettingsManagerImpl implements SettingsManager { private final ConfigurationData configurationData; private final PropertyResource resource; - private final @Nullable MigrationService migrationService; + private final MigrationService migrationService; /** * Constructor. Use {@link SettingsManagerBuilder} to create instances. @@ -37,7 +38,7 @@ public class SettingsManagerImpl implements SettingsManager { * @param configurationData the configuration data * @param migrationService migration service to check the property resource with */ - protected SettingsManagerImpl(PropertyResource resource, ConfigurationData configurationData, + protected SettingsManagerImpl(@NotNull PropertyResource resource, @NotNull ConfigurationData configurationData, @Nullable MigrationService migrationService) { this.configurationData = configurationData; this.resource = resource; @@ -53,7 +54,7 @@ protected SettingsManagerImpl(PropertyResource resource, ConfigurationData confi * @return the property's value */ @Override - public T getProperty(Property property) { + public T getProperty(@NotNull Property property) { return configurationData.getValue(property); } @@ -65,7 +66,7 @@ public T getProperty(Property property) { * @param the property's type */ @Override - public void setProperty(Property property, T value) { + public void setProperty(@NotNull Property property, @NotNull T value) { configurationData.setValue(property, value); } @@ -93,16 +94,15 @@ protected void loadFromResourceAndValidate() { } } - protected final PropertyResource getPropertyResource() { + protected final @NotNull PropertyResource getPropertyResource() { return resource; } - protected final ConfigurationData getConfigurationData() { + protected final @NotNull ConfigurationData getConfigurationData() { return configurationData; } - @Nullable - protected final MigrationService getMigrationService() { + protected final @Nullable MigrationService getMigrationService() { return migrationService; } } diff --git a/src/main/java/ch/jalu/configme/exception/ConfigMeException.java b/src/main/java/ch/jalu/configme/exception/ConfigMeException.java index 4e58c84e..92f158ee 100644 --- a/src/main/java/ch/jalu/configme/exception/ConfigMeException.java +++ b/src/main/java/ch/jalu/configme/exception/ConfigMeException.java @@ -1,5 +1,8 @@ package ch.jalu.configme.exception; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + /** * ConfigMe exception. */ @@ -7,11 +10,11 @@ public class ConfigMeException extends RuntimeException { private static final long serialVersionUID = -865062331853823084L; - public ConfigMeException(String message) { + public ConfigMeException(@NotNull String message) { super(message); } - public ConfigMeException(String message, Throwable cause) { + public ConfigMeException(@NotNull String message, @Nullable Throwable cause) { super(message, cause); } } diff --git a/src/main/java/ch/jalu/configme/migration/MigrationService.java b/src/main/java/ch/jalu/configme/migration/MigrationService.java index 52c2f208..71c230bb 100644 --- a/src/main/java/ch/jalu/configme/migration/MigrationService.java +++ b/src/main/java/ch/jalu/configme/migration/MigrationService.java @@ -2,6 +2,7 @@ import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; /** * The migration service is called when the settings manager is instantiated. It allows to @@ -24,6 +25,6 @@ public interface MigrationService { * @return true if a migration has been performed, false otherwise. Indicates whether the configuration data should * be saved to the configuration file or not */ - boolean checkAndMigrate(PropertyReader reader, ConfigurationData configurationData); + boolean checkAndMigrate(@NotNull PropertyReader reader, @NotNull ConfigurationData configurationData); } diff --git a/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java b/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java index ec3e58c9..3a7e157d 100644 --- a/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java +++ b/src/main/java/ch/jalu/configme/migration/PlainMigrationService.java @@ -12,7 +12,7 @@ public class PlainMigrationService implements MigrationService { @Override - public boolean checkAndMigrate(PropertyReader reader, @NotNull ConfigurationData configurationData) { + public boolean checkAndMigrate(@NotNull PropertyReader reader, @NotNull ConfigurationData configurationData) { if (performMigrations(reader, configurationData) == MIGRATION_REQUIRED || !configurationData.areAllValuesValidInResource()) { return MIGRATION_REQUIRED; @@ -32,7 +32,7 @@ public boolean checkAndMigrate(PropertyReader reader, @NotNull ConfigurationData * @param configurationData the configuration data * @return true if a migration has been performed, false otherwise (see constants on {@link MigrationService}) */ - protected boolean performMigrations(PropertyReader reader, ConfigurationData configurationData) { + protected boolean performMigrations(@NotNull PropertyReader reader, @NotNull ConfigurationData configurationData) { return NO_MIGRATION_NEEDED; } diff --git a/src/test/java/ch/jalu/configme/migration/PlainMigrationServiceTest.java b/src/test/java/ch/jalu/configme/migration/PlainMigrationServiceTest.java index 03aa0d4a..fefe83e5 100644 --- a/src/test/java/ch/jalu/configme/migration/PlainMigrationServiceTest.java +++ b/src/test/java/ch/jalu/configme/migration/PlainMigrationServiceTest.java @@ -7,6 +7,7 @@ import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.YamlFileReader; import ch.jalu.configme.samples.TestConfiguration; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -158,7 +159,7 @@ private PropertyReader createReaderSpy(String file) { private static class PlainMigrationServiceTestExtension extends PlainMigrationService { @Override - protected boolean performMigrations(PropertyReader reader, ConfigurationData configurationData) { + protected boolean performMigrations(@NotNull PropertyReader reader, @NotNull ConfigurationData configurationData) { // If contains -> return true = migration is necessary if (reader.contains("old.property")) { return MIGRATION_REQUIRED; From a40d8191c5b92de6a568704d3193f97122510620 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 09:36:26 -0400 Subject: [PATCH 10/15] Fixed missing and incorrectly inferred nullity annotations for utils package --- .../utils/SettingsHolderClassValidator.java | 13 ++++++------- .../ch/jalu/configme/utils/TypeInformation.java | 17 ++++++----------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java index 834f9e9b..7c8a0c61 100644 --- a/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java +++ b/src/main/java/ch/jalu/configme/utils/SettingsHolderClassValidator.java @@ -41,7 +41,7 @@ public class SettingsHolderClassValidator { * @param settingHolders settings holder classes that make up the configuration data of the project */ @SafeVarargs - public final void validate(Class... settingHolders) { + public final void validate(@NotNull Class... settingHolders) { validate(Arrays.asList(settingHolders)); } @@ -79,7 +79,7 @@ public void validate(@NotNull Iterable> settingH * @param resource property resource to save to and read from (temporary medium for testing) * @param migrationService the migration service to check */ - public void validateConfigurationDataValidForMigrationService(ConfigurationData configurationData, + public void validateConfigurationDataValidForMigrationService(@NotNull ConfigurationData configurationData, @NotNull PropertyResource resource, @NotNull MigrationService migrationService) { resource.exportProperties(configurationData); @@ -259,7 +259,7 @@ protected boolean isValidConstantField(@NotNull Field field) { && Modifier.isFinal(modifiers); } - protected ConfigurationData createConfigurationData(Iterable> classes) { + protected @NotNull ConfigurationData createConfigurationData(@NotNull Iterable> classes) { return ConfigurationDataBuilder.createConfiguration(classes); } @@ -291,9 +291,8 @@ protected boolean hasNonEmptyComment(@Nullable List comments) { * @param property the property to process * @return the enum type it wraps, or null if not applicable */ - @Nullable @SuppressWarnings("unchecked") - protected Class> getEnumTypeOfProperty(@NotNull Property property) { + protected @Nullable Class> getEnumTypeOfProperty(@NotNull Property property) { Class defaultValueType = property.getDefaultValue().getClass(); if (defaultValueType.isAnonymousClass()) { // If an enum entry implements methods, it is an anonymous class -> we're interested in the enclosing class @@ -302,7 +301,7 @@ protected Class> getEnumTypeOfProperty(@NotNull Property pr return defaultValueType.isEnum() ? (Class>) defaultValueType : null; } - protected List gatherExpectedEnumNames(@NotNull Class> enumClass) { + protected @NotNull List gatherExpectedEnumNames(@NotNull Class> enumClass) { return Arrays.stream(enumClass.getEnumConstants()) .map(Enum::name) .collect(Collectors.toList()); @@ -321,7 +320,7 @@ protected boolean hasValidConstructorSetup(@NotNull Class getAllFields(@NotNull Class clazz) { + protected @NotNull Stream getAllFields(@NotNull Class clazz) { // Shortcut: Class does not inherit from another class, so just go through its fields if (Object.class.equals(clazz.getSuperclass())) { return Arrays.stream(clazz.getDeclaredFields()); diff --git a/src/main/java/ch/jalu/configme/utils/TypeInformation.java b/src/main/java/ch/jalu/configme/utils/TypeInformation.java index 818e68fe..9104e834 100644 --- a/src/main/java/ch/jalu/configme/utils/TypeInformation.java +++ b/src/main/java/ch/jalu/configme/utils/TypeInformation.java @@ -24,7 +24,7 @@ public class TypeInformation { * * @param type the type the instance should wrap */ - public TypeInformation(Type type) { + public TypeInformation(@Nullable Type type) { this.type = type; } @@ -41,8 +41,7 @@ public TypeInformation(Type type) { /** * @return the type this instance is wrapping */ - @Nullable - public Type getType() { + public @Nullable Type getType() { return type; } @@ -99,8 +98,7 @@ public Type getType() { * @param index the index of the generic type to get (0-based) * @return type information representing the generic type info for the given index, null if not applicable */ - @Nullable - public TypeInformation getGenericType(int index) { + public @Nullable TypeInformation getGenericType(int index) { if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; return pt.getActualTypeArguments().length > index @@ -125,14 +123,12 @@ public TypeInformation getGenericType(int index) { * @param index the index of the generic type to get (0-based) * @return type information representing the generic type info for the given index, null if not applicable */ - @Nullable - public Class getGenericTypeAsClass(int index) { + public @Nullable Class getGenericTypeAsClass(int index) { TypeInformation genericType = getGenericType(index); return genericType == null ? null : genericType.getSafeToWriteClass(); } - @Nullable - private Class getSafeToWriteClassInternal(Type type) { + private @Nullable Class getSafeToWriteClassInternal(@Nullable Type type) { if (type instanceof Class) { return (Class) type; } else if (type instanceof ParameterizedType) { @@ -143,8 +139,7 @@ private Class getSafeToWriteClassInternal(Type type) { return null; } - @Nullable - private Class getSafeToReadClassInternal(Type type) { + private @Nullable Class getSafeToReadClassInternal(@Nullable Type type) { Class safeToWriteClass = getSafeToWriteClassInternal(type); if (safeToWriteClass != null) { return safeToWriteClass; From 7b303f2536a59d8b3adb3a6739b4abfe2e1fe501 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 10:45:24 -0400 Subject: [PATCH 11/15] Fixed missing and incorrectly inferred nullity annotations for properties package --- .../configme/properties/ArrayProperty.java | 7 +-- .../configme/properties/BaseProperty.java | 10 ++-- .../configme/properties/BeanProperty.java | 6 +-- .../configme/properties/BooleanProperty.java | 3 +- .../configme/properties/DoubleProperty.java | 3 +- .../configme/properties/EnumProperty.java | 3 +- .../properties/InlineArrayProperty.java | 5 +- .../configme/properties/IntegerProperty.java | 3 +- .../configme/properties/ListProperty.java | 9 ++-- .../LowercaseStringSetProperty.java | 4 +- .../jalu/configme/properties/MapProperty.java | 7 +-- .../configme/properties/OptionalProperty.java | 10 ++-- .../ch/jalu/configme/properties/Property.java | 11 ++-- .../configme/properties/PropertyBuilder.java | 38 +++++++------- .../properties/PropertyInitializer.java | 50 +++++++++---------- .../configme/properties/RegexProperty.java | 9 ++-- .../jalu/configme/properties/SetProperty.java | 12 ++--- .../properties/StringListProperty.java | 6 +-- .../configme/properties/StringProperty.java | 3 +- .../properties/StringSetProperty.java | 6 +-- .../properties/TypeBasedProperty.java | 8 ++- .../convertresult/ConvertErrorRecorder.java | 3 +- .../convertresult/PropertyValue.java | 8 +-- .../inlinearray/InlineArrayConverter.java | 6 ++- .../StandardInlineArrayConverters.java | 15 +++--- .../properties/types/BeanPropertyType.java | 10 ++-- .../properties/types/EnumPropertyType.java | 7 +-- .../types/PrimitivePropertyType.java | 9 ++-- .../properties/types/PropertyType.java | 5 +- .../configme/properties/MapPropertyTest.java | 3 +- 30 files changed, 146 insertions(+), 133 deletions(-) diff --git a/src/main/java/ch/jalu/configme/properties/ArrayProperty.java b/src/main/java/ch/jalu/configme/properties/ArrayProperty.java index ef1cdb5b..d7f61d27 100644 --- a/src/main/java/ch/jalu/configme/properties/ArrayProperty.java +++ b/src/main/java/ch/jalu/configme/properties/ArrayProperty.java @@ -4,6 +4,7 @@ import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Objects; @@ -22,7 +23,7 @@ public class ArrayProperty extends BaseProperty { * @param type the property type * @param arrayProducer array constructor (desired array size as argument) */ - public ArrayProperty(String path, T[] defaultValue, PropertyType type, IntFunction arrayProducer) { + public ArrayProperty(@NotNull String path, T @NotNull [] defaultValue, @NotNull PropertyType type, @NotNull IntFunction arrayProducer) { super(path, defaultValue); Objects.requireNonNull(type, "type"); Objects.requireNonNull(arrayProducer, "arrayProducer"); @@ -31,7 +32,7 @@ public ArrayProperty(String path, T[] defaultValue, PropertyType type, IntFun } @Override - protected T[] getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected T @Nullable [] getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { Object object = reader.getObject(this.getPath()); if (object instanceof Collection) { Collection collection = (Collection) object; @@ -44,7 +45,7 @@ protected T[] getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder } @Override - public Object toExportValue(T @NotNull [] value) { + public @NotNull Object toExportValue(T @NotNull [] value) { Object[] array = new Object[value.length]; for (int i = 0; i < array.length; i++) { diff --git a/src/main/java/ch/jalu/configme/properties/BaseProperty.java b/src/main/java/ch/jalu/configme/properties/BaseProperty.java index fb3045eb..d5518f6c 100644 --- a/src/main/java/ch/jalu/configme/properties/BaseProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BaseProperty.java @@ -27,7 +27,7 @@ public abstract class BaseProperty implements Property { * @param path the path of the property * @param defaultValue the default value of the property */ - public BaseProperty(String path, T defaultValue) { + public BaseProperty(@NotNull String path, @NotNull T defaultValue) { Objects.requireNonNull(path, "path"); Objects.requireNonNull(defaultValue, "defaultValue"); this.path = path; @@ -35,17 +35,17 @@ public BaseProperty(String path, T defaultValue) { } @Override - public String getPath() { + public @NotNull String getPath() { return path; } @Override - public T getDefaultValue() { + public @NotNull T getDefaultValue() { return defaultValue; } @Override - public @NotNull PropertyValue determineValue(PropertyReader reader) { + public @NotNull PropertyValue determineValue(@NotNull PropertyReader reader) { ConvertErrorRecorder errorRecorder = new ConvertErrorRecorder(); T value = getFromReader(reader, errorRecorder); if (isValidValue(value)) { @@ -67,7 +67,7 @@ public boolean isValidValue(@Nullable T value) { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return value based on the reader, or null if not applicable */ - protected abstract @Nullable T getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder); + protected abstract @Nullable T getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder); @Override public @NotNull String toString() { diff --git a/src/main/java/ch/jalu/configme/properties/BeanProperty.java b/src/main/java/ch/jalu/configme/properties/BeanProperty.java index 8c53f660..cec8e6e4 100644 --- a/src/main/java/ch/jalu/configme/properties/BeanProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BeanProperty.java @@ -9,11 +9,11 @@ public class BeanProperty extends TypeBasedProperty { - public BeanProperty(Class beanType, String path, T defaultValue) { + public BeanProperty(@NotNull Class beanType, @NotNull String path, @NotNull T defaultValue) { this(beanType, path, defaultValue, DefaultMapper.getInstance()); } - public BeanProperty(Class beanType, String path, T defaultValue, Mapper mapper) { + public BeanProperty(@NotNull Class beanType, @NotNull String path, @NotNull T defaultValue, @NotNull Mapper mapper) { super(path, defaultValue, BeanPropertyType.of(beanType, mapper)); } @@ -26,7 +26,7 @@ public BeanProperty(Class beanType, String path, T defaultValue, Mapper mappe * @param defaultValue the default value * @param mapper the mapper to map with */ - protected BeanProperty(@NotNull TypeInformation beanType, String path, T defaultValue, Mapper mapper) { + protected BeanProperty(@NotNull TypeInformation beanType, @NotNull String path, @NotNull T defaultValue, @NotNull Mapper mapper) { super(path, defaultValue, new BeanPropertyType<>(beanType, mapper)); if (!beanType.getSafeToWriteClass().isInstance(defaultValue)) { throw new ConfigMeException( diff --git a/src/main/java/ch/jalu/configme/properties/BooleanProperty.java b/src/main/java/ch/jalu/configme/properties/BooleanProperty.java index 0261d2cc..1621ddee 100644 --- a/src/main/java/ch/jalu/configme/properties/BooleanProperty.java +++ b/src/main/java/ch/jalu/configme/properties/BooleanProperty.java @@ -1,13 +1,14 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; /** * Boolean property. This extension exists for convenience and backwards compatibility. */ public class BooleanProperty extends TypeBasedProperty { - public BooleanProperty(String path, Boolean defaultValue) { + public BooleanProperty(@NotNull String path, @NotNull Boolean defaultValue) { super(path, defaultValue, PrimitivePropertyType.BOOLEAN); } } diff --git a/src/main/java/ch/jalu/configme/properties/DoubleProperty.java b/src/main/java/ch/jalu/configme/properties/DoubleProperty.java index 9000def2..d70f93d5 100644 --- a/src/main/java/ch/jalu/configme/properties/DoubleProperty.java +++ b/src/main/java/ch/jalu/configme/properties/DoubleProperty.java @@ -1,13 +1,14 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; /** * Double property. This extension exists for convenience and backwards compatibility. */ public class DoubleProperty extends TypeBasedProperty { - public DoubleProperty(String path, double defaultValue) { + public DoubleProperty(@NotNull String path, double defaultValue) { super(path, defaultValue, PrimitivePropertyType.DOUBLE); } } diff --git a/src/main/java/ch/jalu/configme/properties/EnumProperty.java b/src/main/java/ch/jalu/configme/properties/EnumProperty.java index d89716c5..29597ffa 100644 --- a/src/main/java/ch/jalu/configme/properties/EnumProperty.java +++ b/src/main/java/ch/jalu/configme/properties/EnumProperty.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.EnumPropertyType; +import org.jetbrains.annotations.NotNull; /** * Enum property. @@ -9,7 +10,7 @@ */ public class EnumProperty> extends TypeBasedProperty { - public EnumProperty(Class clazz, String path, E defaultValue) { + public EnumProperty(@NotNull Class clazz, @NotNull String path, @NotNull E defaultValue) { super(path, defaultValue, EnumPropertyType.of(clazz)); } } diff --git a/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java b/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java index 2ece251a..40659e98 100644 --- a/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java +++ b/src/main/java/ch/jalu/configme/properties/InlineArrayProperty.java @@ -4,6 +4,7 @@ import ch.jalu.configme.properties.inlinearray.InlineArrayConverter; import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -24,14 +25,14 @@ public class InlineArrayProperty extends BaseProperty { * @param defaultValue the default value of the property * @param inlineConverter the inline converter to use */ - public InlineArrayProperty(String path, T[] defaultValue, InlineArrayConverter inlineConverter) { + public InlineArrayProperty(@NotNull String path, T @NotNull [] defaultValue, @NotNull InlineArrayConverter inlineConverter) { super(path, defaultValue); Objects.requireNonNull(inlineConverter, "inlineConverter"); this.inlineConverter = inlineConverter; } @Override - protected T[] getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected T @Nullable [] getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { String value = reader.getString(getPath()); return value == null ? null : inlineConverter.fromString(value); } diff --git a/src/main/java/ch/jalu/configme/properties/IntegerProperty.java b/src/main/java/ch/jalu/configme/properties/IntegerProperty.java index 1f88478b..62db0225 100644 --- a/src/main/java/ch/jalu/configme/properties/IntegerProperty.java +++ b/src/main/java/ch/jalu/configme/properties/IntegerProperty.java @@ -1,13 +1,14 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; /** * Integer property. This extension exists for convenience and backwards compatibility. */ public class IntegerProperty extends TypeBasedProperty { - public IntegerProperty(String path, Integer defaultValue) { + public IntegerProperty(@NotNull String path, @NotNull Integer defaultValue) { super(path, defaultValue, PrimitivePropertyType.INTEGER); } } diff --git a/src/main/java/ch/jalu/configme/properties/ListProperty.java b/src/main/java/ch/jalu/configme/properties/ListProperty.java index d3b1e0eb..c8c2a3ab 100644 --- a/src/main/java/ch/jalu/configme/properties/ListProperty.java +++ b/src/main/java/ch/jalu/configme/properties/ListProperty.java @@ -29,7 +29,7 @@ public class ListProperty extends BaseProperty> { * @param defaultValue the entries in the list of the default value */ @SafeVarargs - public ListProperty(String path, PropertyType type, T... defaultValue) { + public ListProperty(@NotNull String path, @NotNull PropertyType type, @NotNull T @NotNull ... defaultValue) { this(path, type, Arrays.asList(defaultValue)); } @@ -40,15 +40,14 @@ public ListProperty(String path, PropertyType type, T... defaultValue) { * @param type the property type * @param defaultValue the default value of the property */ - public ListProperty(String path, PropertyType type, @NotNull List defaultValue) { + public ListProperty(@NotNull String path, @NotNull PropertyType type, @NotNull List defaultValue) { super(path, Collections.unmodifiableList(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; } - @Nullable @Override - protected List getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected @Nullable List getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { List list = reader.getList(getPath()); if (list != null) { @@ -61,7 +60,7 @@ protected List getFromReader(@NotNull PropertyReader reader, ConvertErrorReco } @Override - public @Nullable Object toExportValue(@NotNull List value) { + public @NotNull Object toExportValue(@NotNull List value) { return value.stream() .map(type::toExportValue) .collect(Collectors.toList()); diff --git a/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java b/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java index 1473c702..37ff4a05 100644 --- a/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/LowercaseStringSetProperty.java @@ -24,7 +24,7 @@ public class LowercaseStringSetProperty extends StringSetProperty { * @param path property path * @param defaultEntries entries in the Set that is the default value */ - public LowercaseStringSetProperty(String path, String @NotNull ... defaultEntries) { + public LowercaseStringSetProperty(@NotNull String path, @NotNull String @NotNull ... defaultEntries) { super(path, toLowercaseLinkedHashSet(Arrays.stream(defaultEntries))); } @@ -34,7 +34,7 @@ public LowercaseStringSetProperty(String path, String @NotNull ... defaultEntrie * @param path property path * @param defaultEntries entries in the Set that is the default value */ - public LowercaseStringSetProperty(String path, @NotNull Collection defaultEntries) { + public LowercaseStringSetProperty(@NotNull String path, @NotNull Collection defaultEntries) { super(path, toLowercaseLinkedHashSet(defaultEntries.stream())); } diff --git a/src/main/java/ch/jalu/configme/properties/MapProperty.java b/src/main/java/ch/jalu/configme/properties/MapProperty.java index 131c1782..f15eb75b 100644 --- a/src/main/java/ch/jalu/configme/properties/MapProperty.java +++ b/src/main/java/ch/jalu/configme/properties/MapProperty.java @@ -4,6 +4,7 @@ import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.LinkedHashMap; @@ -27,14 +28,14 @@ public class MapProperty extends BaseProperty> { * @param defaultValue the default value of the property * @param type the property type of the values */ - public MapProperty(String path, @NotNull Map defaultValue, PropertyType type) { + public MapProperty(@NotNull String path, @NotNull Map defaultValue, @NotNull PropertyType type) { super(path, Collections.unmodifiableMap(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; } @Override - protected Map getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected @Nullable Map getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { Object rawObject = reader.getObject(getPath()); if (!(rawObject instanceof Map)) { @@ -57,7 +58,7 @@ protected Map getFromReader(@NotNull PropertyReader reader, ConvertEr } @Override - public Object toExportValue(@NotNull Map value) { + public @NotNull Object toExportValue(@NotNull Map value) { Map exportMap = new LinkedHashMap<>(); for (Map.Entry entry : value.entrySet()) { diff --git a/src/main/java/ch/jalu/configme/properties/OptionalProperty.java b/src/main/java/ch/jalu/configme/properties/OptionalProperty.java index 179cd67a..3f304942 100644 --- a/src/main/java/ch/jalu/configme/properties/OptionalProperty.java +++ b/src/main/java/ch/jalu/configme/properties/OptionalProperty.java @@ -18,18 +18,18 @@ public class OptionalProperty implements Property> { private final Property baseProperty; private final Optional defaultValue; - public OptionalProperty(Property baseProperty) { + public OptionalProperty(@NotNull Property baseProperty) { this.baseProperty = baseProperty; this.defaultValue = Optional.empty(); } - public OptionalProperty(Property baseProperty, @NotNull T defaultValue) { + public OptionalProperty(@NotNull Property baseProperty, @NotNull T defaultValue) { this.baseProperty = baseProperty; this.defaultValue = Optional.of(defaultValue); } @Override - public String getPath() { + public @NotNull String getPath() { return baseProperty.getPath(); } @@ -49,7 +49,7 @@ public String getPath() { } @Override - public Optional getDefaultValue() { + public @NotNull Optional getDefaultValue() { return defaultValue; } @@ -62,7 +62,7 @@ public boolean isValidValue(@Nullable Optional value) { } @Override - public Object toExportValue(@NotNull Optional value) { + public @Nullable Object toExportValue(@NotNull Optional value) { return value.map(baseProperty::toExportValue).orElse(null); } } diff --git a/src/main/java/ch/jalu/configme/properties/Property.java b/src/main/java/ch/jalu/configme/properties/Property.java index a8c36408..aee9c059 100644 --- a/src/main/java/ch/jalu/configme/properties/Property.java +++ b/src/main/java/ch/jalu/configme/properties/Property.java @@ -3,6 +3,7 @@ import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -20,7 +21,7 @@ public interface Property { /** * @return the path of the property */ - String getPath(); + @NotNull String getPath(); /** * Returns the value, based on the given reader, which should be used for this property. By default @@ -31,7 +32,7 @@ public interface Property { * @param propertyReader the reader to construct the value from (if possible) * @return the value to associate to this property */ - PropertyValue determineValue(PropertyReader propertyReader); + @NotNull PropertyValue determineValue(@NotNull PropertyReader propertyReader); /** * Convenience method to check whether the property is present in the given reader and a valid representation @@ -42,7 +43,7 @@ public interface Property { * @param propertyReader the reader to check the value in * @return true if a value is available for the property and it is valid, false otherwise */ - default boolean isValidInResource(PropertyReader propertyReader) { + default boolean isValidInResource(@NotNull PropertyReader propertyReader) { return determineValue(propertyReader).isValidInResource(); } @@ -51,7 +52,7 @@ default boolean isValidInResource(PropertyReader propertyReader) { * * @return the default value */ - T getDefaultValue(); + @NotNull T getDefaultValue(); /** * Returns whether the value can be associated to the given property, i.e. whether it fulfills all @@ -66,7 +67,7 @@ default boolean isValidInResource(PropertyReader propertyReader) { * @param value the value to check * @return true if the value can be used for the property, false otherwise */ - boolean isValidValue(T value); + boolean isValidValue(@Nullable T value); /** * Converts the given value to a representation that is suitable for exporting by a property resource. The diff --git a/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java b/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java index 0f76084e..14dc1075 100644 --- a/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java +++ b/src/main/java/ch/jalu/configme/properties/PropertyBuilder.java @@ -32,7 +32,7 @@ public abstract class PropertyBuilder> * * @param type the property type */ - public PropertyBuilder(PropertyType type) { + public PropertyBuilder(@NotNull PropertyType type) { this.type = type; } @@ -42,7 +42,7 @@ public PropertyBuilder(PropertyType type) { * @param path the path * @return this builder */ - public @NotNull B path(String path) { + public @NotNull B path(@NotNull String path) { this.path = path; return (B) this; } @@ -53,7 +53,7 @@ public PropertyBuilder(PropertyType type) { * @param defaultValue the default value to set * @return this builder */ - public B defaultValue(T defaultValue) { + public @NotNull B defaultValue(@NotNull T defaultValue) { this.defaultValue = defaultValue; return (B) this; } @@ -65,15 +65,15 @@ public B defaultValue(T defaultValue) { */ public abstract @NotNull Property build(); - protected final String getPath() { + protected final @NotNull String getPath() { return path; } - protected final T getDefaultValue() { + protected final @NotNull T getDefaultValue() { return defaultValue; } - protected final PropertyType getType() { + protected final @NotNull PropertyType getType() { return type; } @@ -84,12 +84,12 @@ protected final PropertyType getType() { */ public static class MapPropertyBuilder extends PropertyBuilder, MapPropertyBuilder> { - public MapPropertyBuilder(PropertyType type) { + public MapPropertyBuilder(@NotNull PropertyType type) { super(type); defaultValue(new LinkedHashMap<>()); } - public @NotNull MapPropertyBuilder defaultEntry(String key, T value) { + public @NotNull MapPropertyBuilder defaultEntry(@NotNull String key, @NotNull T value) { getDefaultValue().put(key, value); return this; } @@ -109,11 +109,11 @@ public static class TypeBasedPropertyBuilder extends PropertyBuilder createFunction = TypeBasedProperty::new; - public TypeBasedPropertyBuilder(PropertyType type) { + public TypeBasedPropertyBuilder(@NotNull PropertyType type) { super(type); } - public @NotNull TypeBasedPropertyBuilder createFunction(CreateFunction createFunction) { + public @NotNull TypeBasedPropertyBuilder createFunction(@NotNull CreateFunction createFunction) { this.createFunction = createFunction; return this; } @@ -133,13 +133,13 @@ public static class ArrayPropertyBuilder extends PropertyBuilder arrayProducer; - public ArrayPropertyBuilder(PropertyType type, IntFunction arrayProducer) { + public ArrayPropertyBuilder(@NotNull PropertyType type, @NotNull IntFunction arrayProducer) { super(type); this.arrayProducer = arrayProducer; } @Override - public ArrayPropertyBuilder defaultValue(T... defaultValue) { + public @NotNull ArrayPropertyBuilder defaultValue(@NotNull T @NotNull ... defaultValue) { return super.defaultValue(defaultValue); } @@ -158,13 +158,13 @@ public static class InlineArrayPropertyBuilder extends PropertyBuilder inlineConverter; - public InlineArrayPropertyBuilder(InlineArrayConverter inlineConverter) { + public InlineArrayPropertyBuilder(@NotNull InlineArrayConverter inlineConverter) { super(null); this.inlineConverter = inlineConverter; } @Override - public InlineArrayPropertyBuilder defaultValue(T... defaultValue) { + public @NotNull InlineArrayPropertyBuilder defaultValue(@NotNull T @NotNull ... defaultValue) { return super.defaultValue(defaultValue); } @@ -181,11 +181,11 @@ public InlineArrayPropertyBuilder defaultValue(T... defaultValue) { */ public static class ListPropertyBuilder extends PropertyBuilder, ListPropertyBuilder> { - public ListPropertyBuilder(PropertyType type) { + public ListPropertyBuilder(@NotNull PropertyType type) { super(type); } - public ListPropertyBuilder defaultValue(T... defaultValue) { + public @NotNull ListPropertyBuilder defaultValue(@NotNull T @NotNull ... defaultValue) { return super.defaultValue(Arrays.asList(defaultValue)); } @@ -202,11 +202,11 @@ public ListPropertyBuilder defaultValue(T... defaultValue) { */ public static class SetPropertyBuilder extends PropertyBuilder, SetPropertyBuilder> { - public SetPropertyBuilder(PropertyType type) { + public SetPropertyBuilder(@NotNull PropertyType type) { super(type); } - public SetPropertyBuilder defaultValue(T @NotNull ... defaultValue) { + public @NotNull SetPropertyBuilder defaultValue(@NotNull T @NotNull ... defaultValue) { Set defaultSet = Arrays.stream(defaultValue) .collect(Collectors.toCollection(LinkedHashSet::new)); return super.defaultValue(defaultSet); @@ -227,7 +227,7 @@ public SetPropertyBuilder defaultValue(T @NotNull ... defaultValue) { @FunctionalInterface public interface CreateFunction { - @NotNull Property apply(String path, T defaultValue, PropertyType type); + @NotNull Property apply(@NotNull String path, @NotNull T defaultValue, @NotNull PropertyType type); } diff --git a/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java b/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java index a21d2528..9d3a35f2 100644 --- a/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java +++ b/src/main/java/ch/jalu/configme/properties/PropertyInitializer.java @@ -32,7 +32,7 @@ protected PropertyInitializer() { * @param defaultValue the default value * @return the created property */ - public static @NotNull Property newProperty(String path, boolean defaultValue) { + public static @NotNull Property newProperty(@NotNull String path, boolean defaultValue) { return new BooleanProperty(path, defaultValue); } @@ -43,7 +43,7 @@ protected PropertyInitializer() { * @param defaultValue the default value * @return the created property */ - public static @NotNull Property newProperty(String path, int defaultValue) { + public static @NotNull Property newProperty(@NotNull String path, int defaultValue) { return new IntegerProperty(path, defaultValue); } @@ -54,7 +54,7 @@ protected PropertyInitializer() { * @param defaultValue the default value * @return the created property */ - public static @NotNull Property newProperty(String path, double defaultValue) { + public static @NotNull Property newProperty(@NotNull String path, double defaultValue) { return new DoubleProperty(path, defaultValue); } @@ -65,7 +65,7 @@ protected PropertyInitializer() { * @param defaultValue the default value * @return the created property */ - public static @NotNull Property newProperty(String path, String defaultValue) { + public static @NotNull Property newProperty(@NotNull String path, @NotNull String defaultValue) { return new StringProperty(path, defaultValue); } @@ -78,7 +78,7 @@ protected PropertyInitializer() { * @param the enum type * @return the created enum property */ - public static > @NotNull Property newProperty(Class clazz, String path, E defaultValue) { + public static > @NotNull Property newProperty(@NotNull Class clazz, @NotNull String path, @NotNull E defaultValue) { return new EnumProperty<>(clazz, path, defaultValue); } @@ -89,7 +89,7 @@ protected PropertyInitializer() { * @param defaultRegexValue the default pattern of the property * @return the created regex property */ - public static @NotNull RegexProperty newRegexProperty(String path, @NotNull String defaultRegexValue) { + public static @NotNull RegexProperty newRegexProperty(@NotNull String path, @NotNull String defaultRegexValue) { return new RegexProperty(path, defaultRegexValue); } @@ -100,7 +100,7 @@ protected PropertyInitializer() { * @param defaultRegexValue the default pattern of the property * @return the created regex property */ - public static @NotNull RegexProperty newRegexProperty(String path, Pattern defaultRegexValue) { + public static @NotNull RegexProperty newRegexProperty(@NotNull String path, @NotNull Pattern defaultRegexValue) { return new RegexProperty(path, defaultRegexValue); } @@ -111,7 +111,7 @@ protected PropertyInitializer() { * @param defaultValues the items in the default list * @return the created list property */ - public static @NotNull Property> newListProperty(String path, String... defaultValues) { + public static @NotNull Property> newListProperty(@NotNull String path, @NotNull String @NotNull ... defaultValues) { // does not have the same name as not to clash with #newProperty(String, String) return new StringListProperty(path, defaultValues); } @@ -123,7 +123,7 @@ protected PropertyInitializer() { * @param defaultValues the default value of the property * @return the created list property */ - public static @NotNull Property> newListProperty(String path, @NotNull List defaultValues) { + public static @NotNull Property> newListProperty(@NotNull String path, @NotNull List defaultValues) { // does not have the same name as not to clash with #newProperty(String, String) return new StringListProperty(path, defaultValues); } @@ -135,7 +135,7 @@ protected PropertyInitializer() { * @param defaultValues the items in the default set * @return the created set property */ - public static @NotNull Property> newSetProperty(String path, String... defaultValues) { + public static @NotNull Property> newSetProperty(@NotNull String path, @NotNull String @NotNull ... defaultValues) { return new StringSetProperty(path, defaultValues); } @@ -146,7 +146,7 @@ protected PropertyInitializer() { * @param defaultValues the default value of the property * @return the created set property */ - public static @NotNull Property> newSetProperty(String path, @NotNull Set defaultValues) { + public static @NotNull Property> newSetProperty(@NotNull String path, @NotNull Set defaultValues) { return new StringSetProperty(path, defaultValues); } @@ -157,7 +157,7 @@ protected PropertyInitializer() { * @param defaultValues the items in the default set * @return the created set property */ - public static @NotNull Property> newLowercaseStringSetProperty(String path, String... defaultValues) { + public static @NotNull Property> newLowercaseStringSetProperty(@NotNull String path, @NotNull String @NotNull ... defaultValues) { return new LowercaseStringSetProperty(path, defaultValues); } @@ -168,7 +168,7 @@ protected PropertyInitializer() { * @param defaultValues the default value of the property * @return the created set property */ - public static @NotNull Property> newLowercaseStringSetProperty(String path, @NotNull Collection defaultValues) { + public static @NotNull Property> newLowercaseStringSetProperty(@NotNull String path, @NotNull Collection defaultValues) { return new LowercaseStringSetProperty(path, defaultValues); } @@ -181,55 +181,55 @@ protected PropertyInitializer() { * @param the bean type * @return the created bean property */ - public static @NotNull Property newBeanProperty(Class beanClass, String path, B defaultValue) { + public static @NotNull Property newBeanProperty(@NotNull Class beanClass, @NotNull String path, @NotNull B defaultValue) { return new BeanProperty<>(beanClass, path, defaultValue); } // -------------- // Property builders // -------------- - public static PropertyBuilder.@NotNull TypeBasedPropertyBuilder typeBasedProperty(PropertyType type) { + public static PropertyBuilder.@NotNull TypeBasedPropertyBuilder typeBasedProperty(@NotNull PropertyType type) { return new PropertyBuilder.TypeBasedPropertyBuilder<>(type); } - public static PropertyBuilder.@NotNull ListPropertyBuilder listProperty(PropertyType type) { + public static PropertyBuilder.@NotNull ListPropertyBuilder listProperty(@NotNull PropertyType type) { return new PropertyBuilder.ListPropertyBuilder<>(type); } - public static PropertyBuilder.@NotNull SetPropertyBuilder setProperty(PropertyType type) { + public static PropertyBuilder.@NotNull SetPropertyBuilder setProperty(@NotNull PropertyType type) { return new PropertyBuilder.SetPropertyBuilder<>(type); } - public static PropertyBuilder.@NotNull MapPropertyBuilder mapProperty(PropertyType type) { + public static PropertyBuilder.@NotNull MapPropertyBuilder mapProperty(@NotNull PropertyType type) { return new PropertyBuilder.MapPropertyBuilder<>(type); } - public static PropertyBuilder.@NotNull ArrayPropertyBuilder arrayProperty(PropertyType type, - IntFunction arrayProducer) { + public static PropertyBuilder.@NotNull ArrayPropertyBuilder arrayProperty(@NotNull PropertyType type, + @NotNull IntFunction arrayProducer) { return new PropertyBuilder.ArrayPropertyBuilder<>(type, arrayProducer); } public static PropertyBuilder.@NotNull InlineArrayPropertyBuilder inlineArrayProperty( - InlineArrayConverter inlineConverter) { + @NotNull InlineArrayConverter inlineConverter) { return new PropertyBuilder.InlineArrayPropertyBuilder<>(inlineConverter); } // -------------- // Optional flavors // -------------- - public static @NotNull Property> optionalBooleanProperty(String path) { + public static @NotNull Property> optionalBooleanProperty(@NotNull String path) { return new OptionalProperty<>(new BooleanProperty(path, false)); } - public static @NotNull Property> optionalIntegerProperty(String path) { + public static @NotNull Property> optionalIntegerProperty(@NotNull String path) { return new OptionalProperty<>(new IntegerProperty(path, 0)); } - public static @NotNull Property> optionalStringProperty(String path) { + public static @NotNull Property> optionalStringProperty(@NotNull String path) { return new OptionalProperty<>(new StringProperty(path, "")); } - public static > @NotNull Property> optionalEnumProperty(@NotNull Class clazz, String path) { + public static > @NotNull Property> optionalEnumProperty(@NotNull Class clazz, @NotNull String path) { // default value may never be null, so get the first entry in the enum class return new OptionalProperty<>(new EnumProperty<>(clazz, path, clazz.getEnumConstants()[0])); } diff --git a/src/main/java/ch/jalu/configme/properties/RegexProperty.java b/src/main/java/ch/jalu/configme/properties/RegexProperty.java index c873a9cc..1ec94ed8 100644 --- a/src/main/java/ch/jalu/configme/properties/RegexProperty.java +++ b/src/main/java/ch/jalu/configme/properties/RegexProperty.java @@ -4,6 +4,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.resource.PropertyReader; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,7 +21,7 @@ public class RegexProperty extends BaseProperty { * @param path the path of the property * @param defaultValue the default value of the property */ - public RegexProperty(String path, Pattern defaultValue) { + public RegexProperty(@NotNull String path, @NotNull Pattern defaultValue) { super(path, defaultValue); } @@ -30,12 +31,12 @@ public RegexProperty(String path, Pattern defaultValue) { * @param path the path of the property * @param defaultRegexValue the default value of the property */ - public RegexProperty(String path, @NotNull String defaultRegexValue) { + public RegexProperty(@NotNull String path, @NotNull String defaultRegexValue) { this(path, Pattern.compile(defaultRegexValue)); } @Override - protected Pattern getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected @Nullable Pattern getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { String pattern = reader.getString(getPath()); if (pattern != null) { try { @@ -47,7 +48,7 @@ protected Pattern getFromReader(@NotNull PropertyReader reader, ConvertErrorReco } @Override - public Object toExportValue(@NotNull Pattern value) { + public @NotNull Object toExportValue(@NotNull Pattern value) { return value.pattern(); } diff --git a/src/main/java/ch/jalu/configme/properties/SetProperty.java b/src/main/java/ch/jalu/configme/properties/SetProperty.java index 68592fd9..d404454d 100644 --- a/src/main/java/ch/jalu/configme/properties/SetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/SetProperty.java @@ -32,7 +32,7 @@ public class SetProperty extends BaseProperty> { * @param defaultValue the values that make up the entries of the default set */ @SafeVarargs - public SetProperty(String path, PropertyType type, T @NotNull ... defaultValue) { + public SetProperty(@NotNull String path, @NotNull PropertyType type, @NotNull T @NotNull ... defaultValue) { this(path, type, newSet(defaultValue)); } @@ -43,14 +43,14 @@ public SetProperty(String path, PropertyType type, T @NotNull ... defaultValu * @param type the property type * @param defaultValue the default value of the property */ - public SetProperty(String path, PropertyType type, @NotNull Set defaultValue) { + public SetProperty(@NotNull String path, @NotNull PropertyType type, @NotNull Set defaultValue) { super(path, Collections.unmodifiableSet(defaultValue)); Objects.requireNonNull(type, "type"); this.type = type; } @Override - protected Set getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected Set getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { List list = reader.getList(getPath()); if (list != null) { @@ -63,17 +63,17 @@ protected Set getFromReader(@NotNull PropertyReader reader, ConvertErrorRecor } @Override - public @Nullable Object toExportValue(@NotNull Set value) { + public @NotNull Object toExportValue(@NotNull Set value) { return value.stream() .map(type::toExportValue) .collect(Collectors.toList()); } - protected Collector> setCollector() { + protected @NotNull Collector> setCollector() { return Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet); } - private static Set newSet(E @NotNull [] array) { + private static @NotNull Set newSet(E @NotNull [] array) { return Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new)); } } diff --git a/src/main/java/ch/jalu/configme/properties/StringListProperty.java b/src/main/java/ch/jalu/configme/properties/StringListProperty.java index 6e77d0fc..47f60906 100644 --- a/src/main/java/ch/jalu/configme/properties/StringListProperty.java +++ b/src/main/java/ch/jalu/configme/properties/StringListProperty.java @@ -10,16 +10,16 @@ */ public class StringListProperty extends ListProperty { - public StringListProperty(String path, String... defaultValue) { + public StringListProperty(@NotNull String path, @NotNull String... defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } - public StringListProperty(String path, @NotNull List defaultValue) { + public StringListProperty(@NotNull String path, @NotNull List defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } @Override - public Object toExportValue(List value) { + public @NotNull Object toExportValue(@NotNull List value) { return value; } } diff --git a/src/main/java/ch/jalu/configme/properties/StringProperty.java b/src/main/java/ch/jalu/configme/properties/StringProperty.java index 3f453b7f..c0858c5b 100644 --- a/src/main/java/ch/jalu/configme/properties/StringProperty.java +++ b/src/main/java/ch/jalu/configme/properties/StringProperty.java @@ -1,13 +1,14 @@ package ch.jalu.configme.properties; import ch.jalu.configme.properties.types.PrimitivePropertyType; +import org.jetbrains.annotations.NotNull; /** * String property. This extension exists for convenience and backwards compatibility. */ public class StringProperty extends TypeBasedProperty { - public StringProperty(String path, String defaultValue) { + public StringProperty(@NotNull String path, @NotNull String defaultValue) { super(path, defaultValue, PrimitivePropertyType.STRING); } } diff --git a/src/main/java/ch/jalu/configme/properties/StringSetProperty.java b/src/main/java/ch/jalu/configme/properties/StringSetProperty.java index ab99094e..1a9f2b67 100644 --- a/src/main/java/ch/jalu/configme/properties/StringSetProperty.java +++ b/src/main/java/ch/jalu/configme/properties/StringSetProperty.java @@ -16,7 +16,7 @@ public class StringSetProperty extends SetProperty { * @param path the path of the property * @param defaultValue the values that make up the entries of the default set */ - public StringSetProperty(String path, String... defaultValue) { + public StringSetProperty(@NotNull String path, @NotNull String @NotNull... defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } @@ -26,12 +26,12 @@ public StringSetProperty(String path, String... defaultValue) { * @param path the path of the property * @param defaultValue the values that make up the entries of the default set */ - public StringSetProperty(String path, @NotNull Set defaultValue) { + public StringSetProperty(@NotNull String path, @NotNull Set defaultValue) { super(path, PrimitivePropertyType.STRING, defaultValue); } @Override - public Object toExportValue(Set value) { + public @NotNull Object toExportValue(@NotNull Set value) { return value; } } diff --git a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java index 0f6c2b50..671b3afe 100644 --- a/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java +++ b/src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java @@ -24,21 +24,19 @@ public class TypeBasedProperty extends BaseProperty { * @param defaultValue the default value of the property * @param type the property type */ - public TypeBasedProperty(String path, T defaultValue, PropertyType type) { + public TypeBasedProperty(@NotNull String path, @NotNull T defaultValue, @NotNull PropertyType type) { super(path, defaultValue); Objects.requireNonNull(type, "type"); this.type = type; } - @Nullable @Override - protected T getFromReader(@NotNull PropertyReader reader, ConvertErrorRecorder errorRecorder) { + protected @Nullable T getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) { return type.convert(reader.getObject(getPath()), errorRecorder); } - @Nullable @Override - public Object toExportValue(T value) { + public @Nullable Object toExportValue(T value) { return type.toExportValue(value); } } diff --git a/src/main/java/ch/jalu/configme/properties/convertresult/ConvertErrorRecorder.java b/src/main/java/ch/jalu/configme/properties/convertresult/ConvertErrorRecorder.java index f3867744..16e98bb3 100644 --- a/src/main/java/ch/jalu/configme/properties/convertresult/ConvertErrorRecorder.java +++ b/src/main/java/ch/jalu/configme/properties/convertresult/ConvertErrorRecorder.java @@ -1,6 +1,7 @@ package ch.jalu.configme.properties.convertresult; import ch.jalu.configme.resource.PropertyReader; +import org.jetbrains.annotations.NotNull; /** * Records errors during the conversion of a property to its Java value. @@ -23,7 +24,7 @@ public class ConvertErrorRecorder { * * @param reason the reason (not used in this implementation but may be extended for debugging) */ - public void setHasError(String reason) { + public void setHasError(@NotNull String reason) { hasError = true; } diff --git a/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java b/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java index 238f8ab1..aff5f1da 100644 --- a/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java +++ b/src/main/java/ch/jalu/configme/properties/convertresult/PropertyValue.java @@ -26,7 +26,7 @@ public class PropertyValue { * @param value the value associated with the property * @param isValidInResource true if the value in the resource was fully valid */ - public PropertyValue(T value, boolean isValidInResource) { + public PropertyValue(@NotNull T value, boolean isValidInResource) { this.value = value; this.isValidInResource = isValidInResource; } @@ -38,7 +38,7 @@ public PropertyValue(T value, boolean isValidInResource) { * @param the value type * @return property value with the given value and the valid flag set to true */ - public static @NotNull PropertyValue withValidValue(T value) { + public static @NotNull PropertyValue withValidValue(@NotNull T value) { return new PropertyValue<>(value, true); } @@ -49,14 +49,14 @@ public PropertyValue(T value, boolean isValidInResource) { * @param the value type * @return property value with the given value and the valid flag set to false */ - public static @NotNull PropertyValue withValueRequiringRewrite(T value) { + public static @NotNull PropertyValue withValueRequiringRewrite(@NotNull T value) { return new PropertyValue<>(value, false); } /** * @return the value to associate with the property */ - public T getValue() { + public @NotNull T getValue() { return value; } diff --git a/src/main/java/ch/jalu/configme/properties/inlinearray/InlineArrayConverter.java b/src/main/java/ch/jalu/configme/properties/inlinearray/InlineArrayConverter.java index 4abb05d3..90418e30 100644 --- a/src/main/java/ch/jalu/configme/properties/inlinearray/InlineArrayConverter.java +++ b/src/main/java/ch/jalu/configme/properties/inlinearray/InlineArrayConverter.java @@ -1,5 +1,7 @@ package ch.jalu.configme.properties.inlinearray; +import org.jetbrains.annotations.NotNull; + /** * Converter from a simple String value to an array of multiple values. * Used in {@link ch.jalu.configme.properties.InlineArrayProperty}. @@ -14,7 +16,7 @@ public interface InlineArrayConverter { * @param in the string to convert from * @return array with elements based on the input String, never null */ - T[] fromString(String in); + @NotNull T @NotNull [] fromString(String in); /** * Converts the provided array to its String representation (opposite of {@link #fromString(String)}). @@ -22,6 +24,6 @@ public interface InlineArrayConverter { * @param value the value to convert * @return String representation of the array */ - String toExportValue(T[] value); + @NotNull String toExportValue(@NotNull T @NotNull [] value); } diff --git a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java index 8bd567fd..c9555e9b 100644 --- a/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java +++ b/src/main/java/ch/jalu/configme/properties/inlinearray/StandardInlineArrayConverters.java @@ -48,8 +48,8 @@ public class StandardInlineArrayConverters implements InlineArrayConverter private final Function convertFunction; private final boolean useTrimAndSpaces; - public StandardInlineArrayConverters(String separator, IntFunction arrayProducer, - Function convertFunction) { + public StandardInlineArrayConverters(@NotNull String separator, @NotNull IntFunction arrayProducer, + @NotNull Function convertFunction) { this(separator, arrayProducer, convertFunction, true); } @@ -62,8 +62,8 @@ public StandardInlineArrayConverters(String separator, IntFunction arrayPro * @param useTrimAndSpaces true if a space should be put after the separator in the export and if the split elements * from the input String should be trimmed before being passed to the convert function */ - public StandardInlineArrayConverters(String separator, IntFunction arrayProducer, - Function convertFunction, boolean useTrimAndSpaces) { + public StandardInlineArrayConverters(@NotNull String separator, @NotNull IntFunction arrayProducer, + @NotNull Function convertFunction, boolean useTrimAndSpaces) { this.separator = separator; this.arrayProducer = arrayProducer; this.convertFunction = convertFunction; @@ -71,7 +71,7 @@ public StandardInlineArrayConverters(String separator, IntFunction arrayPro } @Override - public T @NotNull [] fromString(@NotNull String input) { + public @NotNull T @NotNull [] fromString(@NotNull String input) { String[] inputArray = input.split(Pattern.quote(separator)); return Arrays.stream(inputArray) @@ -81,7 +81,7 @@ public StandardInlineArrayConverters(String separator, IntFunction arrayPro } @Override - public String toExportValue(T @NotNull [] value) { + public @NotNull String toExportValue(@NotNull T @NotNull [] value) { String delimiter = useTrimAndSpaces ? separator + " " : separator; StringJoiner joiner = new StringJoiner(delimiter); for (T entry : value) { @@ -96,8 +96,7 @@ public String toExportValue(T @NotNull [] value) { * @param input String to convert from * @return converted value, or null if not possible */ - @Nullable - protected T convert(@NotNull String input) { + protected @Nullable T convert(@NotNull String input) { try { String argument = useTrimAndSpaces ? input.trim() : input; return convertFunction.apply(argument); diff --git a/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java b/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java index 2e97d6b5..404d3184 100644 --- a/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/BeanPropertyType.java @@ -12,27 +12,27 @@ public class BeanPropertyType implements PropertyType { private final TypeInformation beanType; private final Mapper mapper; - public BeanPropertyType(TypeInformation beanType, Mapper mapper) { + public BeanPropertyType(@NotNull TypeInformation beanType, @NotNull Mapper mapper) { this.beanType = beanType; this.mapper = mapper; } - public static @NotNull BeanPropertyType of(Class type, Mapper mapper) { + public static @NotNull BeanPropertyType of(@NotNull Class type, @NotNull Mapper mapper) { return new BeanPropertyType<>(new TypeInformation(type), mapper); } - public static @NotNull BeanPropertyType of(Class type) { + public static @NotNull BeanPropertyType of(@NotNull Class type) { return of(type, DefaultMapper.getInstance()); } @Override @SuppressWarnings("unchecked") - public B convert(Object object, ConvertErrorRecorder errorRecorder) { + public B convert(@Nullable Object object, @NotNull ConvertErrorRecorder errorRecorder) { return (B) mapper.convertToBean(object, beanType, errorRecorder); } @Override - public @Nullable Object toExportValue(B value) { + public @Nullable Object toExportValue(@Nullable B value) { return mapper.toExportValue(value); } } diff --git a/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java b/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java index 00541b7b..b808d3e5 100644 --- a/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/EnumPropertyType.java @@ -2,21 +2,22 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class EnumPropertyType> implements PropertyType { private Class enumType; - public EnumPropertyType(Class enumType) { + public EnumPropertyType(@NotNull Class enumType) { this.enumType = enumType; } - public static > @NotNull EnumPropertyType of(Class type) { + public static > @NotNull EnumPropertyType of(@NotNull Class type) { return new EnumPropertyType<>(type); } @Override - public E convert(Object object, ConvertErrorRecorder errorRecorder) { + public @Nullable E convert(@Nullable Object object, @NotNull ConvertErrorRecorder errorRecorder) { // If object is enum, then return this object, casting to E if (enumType.isInstance(object)) { return (E) object; diff --git a/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java b/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java index 70f924d9..0b51092a 100644 --- a/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/PrimitivePropertyType.java @@ -2,6 +2,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.function.Function; @@ -41,7 +42,7 @@ public class PrimitivePropertyType implements PropertyType { * * @param convertFunction function to convert to the given type */ - public PrimitivePropertyType(Function convertFunction) { + public PrimitivePropertyType(@NotNull Function convertFunction) { this(convertFunction, t -> t); } @@ -51,18 +52,18 @@ public PrimitivePropertyType(Function convertFunction) { * @param convertFunction function to convert to the given type * @param exportValueFunction function to convert a value to its export value */ - public PrimitivePropertyType(Function convertFunction, Function exportValueFunction) { + public PrimitivePropertyType(@NotNull Function convertFunction, @NotNull Function exportValueFunction) { this.convertFunction = convertFunction; this.exportValueFunction = exportValueFunction; } @Override - public T convert(Object object, ConvertErrorRecorder errorRecorder) { + public @Nullable T convert(@Nullable Object object, @NotNull ConvertErrorRecorder errorRecorder) { return convertFunction.apply(object); } @Override - public Object toExportValue(T value) { + public @Nullable Object toExportValue(@Nullable T value) { return exportValueFunction.apply(value); } diff --git a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java index 186bbd69..f688e5cb 100644 --- a/src/main/java/ch/jalu/configme/properties/types/PropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/PropertyType.java @@ -2,6 +2,7 @@ import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -20,7 +21,7 @@ public interface PropertyType { * @param errorRecorder error recorder to register errors even if a valid value is returned * @return the converted value, or null */ - @Nullable T convert(@Nullable Object object, ConvertErrorRecorder errorRecorder); + @Nullable T convert(@Nullable Object object, @NotNull ConvertErrorRecorder errorRecorder); /** * Converts the given value to its export value. (Converts in the opposite way of {@link #convert}.) @@ -28,6 +29,6 @@ public interface PropertyType { * @param value the value to convert * @return the value to use in the property export */ - Object toExportValue(T value); + @Nullable Object toExportValue(T value); } diff --git a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java index ed84089f..6f8dff01 100644 --- a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java +++ b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java @@ -7,6 +7,7 @@ import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.YamlFileResource; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; @@ -118,7 +119,7 @@ private static Map createSampleMap() { private static class AlwaysFourPropertyType implements PropertyType { @Override - public Integer convert(Object object, ConvertErrorRecorder errorRecorder) { + public Integer convert(Object object, @NotNull ConvertErrorRecorder errorRecorder) { return object == null ? null : 4; } From 35713bfa23fc19c906e85e3dba7c1dbd56eb0fbe Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 17 Oct 2021 12:12:10 -0400 Subject: [PATCH 12/15] Fixed missing and incorrectly inferred nullity annotations for resource package --- .../jalu/configme/resource/MapNormalizer.java | 13 ++++--- .../resource/PropertyPathTraverser.java | 14 ++++---- .../configme/resource/PropertyReader.java | 19 +++++----- .../configme/resource/PropertyResource.java | 5 +-- .../configme/resource/YamlFileReader.java | 36 +++++++++---------- .../configme/resource/YamlFileResource.java | 16 ++++----- .../resource/YamlFileResourceOptions.java | 7 ++-- 7 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java index b4579e92..5d38dcec 100644 --- a/src/main/java/ch/jalu/configme/resource/MapNormalizer.java +++ b/src/main/java/ch/jalu/configme/resource/MapNormalizer.java @@ -1,8 +1,8 @@ package ch.jalu.configme.resource; import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; + import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -19,9 +19,8 @@ public class MapNormalizer { * @param loadedMap the map to normalize * @return new map with sanitized structure (or same if no changes are needed) */ - @Nullable @SuppressWarnings("unchecked") - public Map normalizeMap(@Nullable Map loadedMap) { + public @Nullable Map normalizeMap(@Nullable Map loadedMap) { if (loadedMap == null) { return null; } @@ -37,7 +36,7 @@ public Map normalizeMap(@Nullable Map loadedMap) * @param value the value to process * @return optional with a new map to replace the given one with, empty optional if not needed or not applicable */ - protected Optional> createNormalizedMapIfNeeded(Object value) { + protected @NotNull Optional> createNormalizedMapIfNeeded(@NotNull Object value) { if (!(value instanceof Map)) { return Optional.empty(); } @@ -63,7 +62,7 @@ protected Optional> createNormalizedMapIfNeeded(Object value return Optional.empty(); } - protected boolean isKeyInvalid(Object key) { + protected boolean isKeyInvalid(@NotNull Object key) { return !(key instanceof String) || ((String) key).contains("."); } @@ -75,7 +74,7 @@ protected boolean isKeyInvalid(Object key) { * @param path the path to store the value under * @param value the value to store */ - protected void addValueIntoMap(@NotNull Map map, @NotNull String path, Object value) { + protected void addValueIntoMap(@NotNull Map map, @NotNull String path, @NotNull Object value) { int dotPosition = path.indexOf("."); if (dotPosition > -1) { String pathElement = path.substring(0, dotPosition); @@ -100,7 +99,7 @@ protected void addValueIntoMap(@NotNull Map map, @NotNull String * @param path the key with which the value should be looked up from the map * @return the nested map, as stored under the path in the given map */ - protected @NotNull Map getOrInsertMap(@NotNull Map parentMap, String path) { + protected @NotNull Map getOrInsertMap(@NotNull Map parentMap, @NotNull String path) { Object value = parentMap.get(path); if (value instanceof Map) { return (Map) value; diff --git a/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java b/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java index 6409a8a8..87f26a0f 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyPathTraverser.java @@ -20,10 +20,10 @@ public class PropertyPathTraverser { private final ConfigurationData configurationData; /** Contains all path elements besides the last, e.g. {datasource, mysql} for "datasource.mysql.table". */ - private @NotNull List parentPathElements = new ArrayList<>(0); + private List parentPathElements = new ArrayList<>(0); private boolean isFirstProperty = true; - public PropertyPathTraverser(ConfigurationData configurationData) { + public PropertyPathTraverser(@NotNull ConfigurationData configurationData) { this.configurationData = configurationData; } @@ -46,7 +46,7 @@ public PropertyPathTraverser(ConfigurationData configurationData) { return convertToPathElements(indentationLevel, prefix, newPathParts); } - private @NotNull List convertToPathElements(int indentation, String prefix, @NotNull List elements) { + private @NotNull List convertToPathElements(int indentation, @NotNull String prefix, @NotNull List elements) { List pathElements = new ArrayList<>(elements.size()); for (String element : elements) { List comments = isFirstProperty @@ -61,7 +61,7 @@ public PropertyPathTraverser(ConfigurationData configurationData) { return pathElements; } - private List getCommentsIncludingRoot(String path) { + private @NotNull List getCommentsIncludingRoot(@NotNull String path) { List rootComments = configurationData.getCommentsForSection(""); if ("".equals(path)) { return rootComments; @@ -88,7 +88,7 @@ public static class PathElement { private final boolean isFirstElement; private boolean isFirstOfGroup; - public PathElement(int indentationLevel, String name, List comments, boolean isFirstElement) { + public PathElement(int indentationLevel, @NotNull String name, @NotNull List comments, boolean isFirstElement) { this.indentationLevel = indentationLevel; this.name = name; this.comments = comments; @@ -99,11 +99,11 @@ public int getIndentationLevel() { return indentationLevel; } - public String getName() { + public @NotNull String getName() { return name; } - public List getComments() { + public @NotNull List getComments() { return comments; } diff --git a/src/main/java/ch/jalu/configme/resource/PropertyReader.java b/src/main/java/ch/jalu/configme/resource/PropertyReader.java index c6c27d85..3e8f5ccf 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyReader.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyReader.java @@ -1,5 +1,6 @@ package ch.jalu.configme.resource; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Set; @@ -15,7 +16,7 @@ public interface PropertyReader { * @param path the path to check * @return true if there is a value, false otherwise */ - boolean contains(String path); + boolean contains(@NotNull String path); /** * Returns the keys available in the file. Depending on the parameter either all keys are returned, @@ -24,7 +25,7 @@ public interface PropertyReader { * @param onlyLeafNodes true if only the paths of leaf nodes should be returned (no intermediate paths) * @return set of all existing keys (ordered) */ - Set getKeys(boolean onlyLeafNodes); + @NotNull Set getKeys(boolean onlyLeafNodes); /** * Returns the direct children of the given path which are available in the file. Returns an empty set @@ -33,7 +34,7 @@ public interface PropertyReader { * @param path the path whose direct child paths should be looked up * @return set of all direct children (ordered, never null) */ - Set getChildKeys(String path); + @NotNull Set getChildKeys(@NotNull String path); /** * Returns the object at the given path, or null if absent. @@ -41,7 +42,7 @@ public interface PropertyReader { * @param path the path to retrieve the value for * @return the value, or null if there is none */ - @Nullable Object getObject(String path); + @Nullable Object getObject(@NotNull String path); /** * Returns the value of the given path as a String if available. @@ -49,7 +50,7 @@ public interface PropertyReader { * @param path the path to retrieve a String for * @return the value as a String, or null if not applicable or unavailable */ - @Nullable String getString(String path); + @Nullable String getString(@NotNull String path); /** * Returns the value of the given path as an integer if available. @@ -57,7 +58,7 @@ public interface PropertyReader { * @param path the path to retrieve an integer for * @return the value as integer, or null if not applicable or unavailable */ - @Nullable Integer getInt(String path); + @Nullable Integer getInt(@NotNull String path); /** * Returns the value of the given path as a double if available. @@ -65,7 +66,7 @@ public interface PropertyReader { * @param path the path to retrieve a double for * @return the value as a double, or null if not applicable or unavailable */ - @Nullable Double getDouble(String path); + @Nullable Double getDouble(@NotNull String path); /** * Returns the value of the given path as a boolean if available. @@ -73,7 +74,7 @@ public interface PropertyReader { * @param path the path to retrieve a boolean for * @return the value as a boolean, or null if not applicable or unavailable */ - @Nullable Boolean getBoolean(String path); + @Nullable Boolean getBoolean(@NotNull String path); /** * Returns the value of the given path as a list if available. @@ -81,6 +82,6 @@ public interface PropertyReader { * @param path the path to retrieve a list for * @return the value as a list, or null if not applicable or unavailable */ - @Nullable List getList(String path); + @Nullable List getList(@NotNull String path); } diff --git a/src/main/java/ch/jalu/configme/resource/PropertyResource.java b/src/main/java/ch/jalu/configme/resource/PropertyResource.java index a48552ed..71077450 100644 --- a/src/main/java/ch/jalu/configme/resource/PropertyResource.java +++ b/src/main/java/ch/jalu/configme/resource/PropertyResource.java @@ -1,6 +1,7 @@ package ch.jalu.configme.resource; import ch.jalu.configme.configurationdata.ConfigurationData; +import org.jetbrains.annotations.NotNull; /** * Represents a medium (typically a file on disk) from which property values should be built and allows to @@ -15,13 +16,13 @@ public interface PropertyResource { * * @return reader providing values in the medium (e.g. file) */ - PropertyReader createReader(); + @NotNull PropertyReader createReader(); /** * Exports the provided configuration data to the medium (typically a file). * * @param configurationData the configuration data to export */ - void exportProperties(ConfigurationData configurationData); + void exportProperties(@NotNull ConfigurationData configurationData); } diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java index ac499044..39fa352b 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileReader.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileReader.java @@ -2,10 +2,10 @@ import ch.jalu.configme.exception.ConfigMeException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -36,7 +36,7 @@ public class YamlFileReader implements PropertyReader { * * @param path the file to load */ - public YamlFileReader(Path path) { + public YamlFileReader(@NotNull Path path) { this(path, StandardCharsets.UTF_8); } @@ -46,7 +46,7 @@ public YamlFileReader(Path path) { * @param path the file to load * @param charset the charset to read the data as */ - public YamlFileReader(Path path, Charset charset) { + public YamlFileReader(@NotNull Path path, @NotNull Charset charset) { this.path = path; this.charset = charset; this.root = loadFile(); @@ -64,7 +64,7 @@ public YamlFileReader(@NotNull File file) { } @Override - public Object getObject(@NotNull String path) { + public @Nullable Object getObject(@NotNull String path) { if (path.isEmpty()) { return root; } @@ -81,12 +81,12 @@ public Object getObject(@NotNull String path) { } @Override - public String getString(@NotNull String path) { + public @Nullable String getString(@NotNull String path) { return getTypedObject(path, String.class); } @Override - public Integer getInt(@NotNull String path) { + public @Nullable Integer getInt(@NotNull String path) { Number n = getTypedObject(path, Number.class); return (n == null) ? null @@ -94,7 +94,7 @@ public Integer getInt(@NotNull String path) { } @Override - public Double getDouble(@NotNull String path) { + public @Nullable Double getDouble(@NotNull String path) { Number n = getTypedObject(path, Number.class); return (n == null) ? null @@ -102,12 +102,12 @@ public Double getDouble(@NotNull String path) { } @Override - public Boolean getBoolean(@NotNull String path) { + public @Nullable Boolean getBoolean(@NotNull String path) { return getTypedObject(path, Boolean.class); } @Override - public List getList(@NotNull String path) { + public @Nullable List getList(@NotNull String path) { return getTypedObject(path, List.class); } @@ -127,7 +127,7 @@ public boolean contains(@NotNull String path) { } @Override - public Set getChildKeys(@NotNull String path) { + public @NotNull Set getChildKeys(@NotNull String path) { Object object = getObject(path); if (object instanceof Map) { String pathPrefix = path.isEmpty() ? "" : path + "."; @@ -159,7 +159,7 @@ private void collectKeysIntoSet(@NotNull String path, @NotNull Map normalizeMap(@Nullable Map map) { + protected @Nullable Map normalizeMap(@Nullable Map map) { return new MapNormalizer().normalizeMap(map); } @@ -199,7 +198,7 @@ protected Map normalizeMap(@Nullable Map map) { return path.toFile(); } - protected final Path getPath() { + protected final @NotNull Path getPath() { return path; } @@ -207,9 +206,8 @@ protected final Path getPath() { * @return the root value; may be null if the file was empty * @deprecated use {@code getObject("")} instead */ - @Nullable @Deprecated - protected final Map getRoot() { + protected final @Nullable Map getRoot() { return root; } @@ -222,8 +220,7 @@ protected final Map getRoot() { * @param the class type * @return cast value at the given path, null if not applicable */ - @Nullable - protected T getTypedObject(@NotNull String path, @NotNull Class clazz) { + protected @Nullable T getTypedObject(@NotNull String path, @NotNull Class clazz) { Object value = getObject(path); if (clazz.isInstance(value)) { return clazz.cast(value); @@ -231,8 +228,7 @@ protected T getTypedObject(@NotNull String path, @NotNull Class clazz) { return null; } - @Nullable - private static Object getEntryIfIsMap(String key, Object value) { + private static @Nullable Object getEntryIfIsMap(@NotNull String key, @Nullable Object value) { if (value instanceof Map) { return ((Map) value).get(key); } diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java index 9ac14e93..993f1091 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResource.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResource.java @@ -5,10 +5,10 @@ import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyPathTraverser.PathElement; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -28,11 +28,11 @@ public class YamlFileResource implements PropertyResource { private final String indentationSpace; private @Nullable Yaml yamlObject; - public YamlFileResource(Path path) { + public YamlFileResource(@NotNull Path path) { this(path, YamlFileResourceOptions.builder().build()); } - public YamlFileResource(Path path, @NotNull YamlFileResourceOptions options) { + public YamlFileResource(@NotNull Path path, @NotNull YamlFileResourceOptions options) { this.path = path; this.options = options; this.indentationSpace = options.getIndentation(); @@ -72,7 +72,7 @@ public void exportProperties(@NotNull ConfigurationData configurationData) { } } - protected final Path getPath() { + protected final @NotNull Path getPath() { return path; } @@ -152,7 +152,7 @@ protected void writeComments(@NotNull Writer writer, int indentation, @NotNull P } } - private void writeIndentingBetweenLines(@NotNull Writer writer, PathElement pathElement) throws IOException { + private void writeIndentingBetweenLines(@NotNull Writer writer, @NotNull PathElement pathElement) throws IOException { int numberOfEmptyLines = options.getNumberOfEmptyLinesBefore(pathElement); for (int i = 0; i < numberOfEmptyLines; ++i) { writer.append("\n"); @@ -224,7 +224,7 @@ protected String indent(int level) { return result.toString(); } - protected @NotNull String escapePathElementIfNeeded(String path) { + protected @NotNull String escapePathElementIfNeeded(@NotNull String path) { return getYamlObject().dump(path).trim(); } @@ -240,7 +240,7 @@ protected void onWriteComplete() { * * @return the YAML instance to use */ - protected @Nullable Yaml getYamlObject() { + protected @NotNull Yaml getYamlObject() { if (yamlObject == null) { yamlObject = createNewYaml(); } @@ -262,7 +262,7 @@ protected void onWriteComplete() { return property.toExportValue(configurationData.getValue(property)); } - private static @NotNull List collectionToList(Collection collection) { + private static @NotNull List collectionToList(@NotNull Collection collection) { return collection instanceof List ? (List) collection : new ArrayList<>(collection); diff --git a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java index a2c36eb3..21dd634b 100644 --- a/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java +++ b/src/main/java/ch/jalu/configme/resource/YamlFileResourceOptions.java @@ -37,7 +37,7 @@ protected YamlFileResourceOptions(@Nullable Charset charset, return charset; } - public int getNumberOfEmptyLinesBefore(PathElement pathElement) { + public int getNumberOfEmptyLinesBefore(@NotNull PathElement pathElement) { return numberOfLinesBeforeFunction == null ? 0 : numberOfLinesBeforeFunction.applyAsInt(pathElement); } @@ -59,8 +59,7 @@ public int getIndentationSize() { return sb.toString(); } - @Nullable - protected final ToIntFunction getIndentFunction() { + protected final @Nullable ToIntFunction getIndentFunction() { return numberOfLinesBeforeFunction; } @@ -74,7 +73,7 @@ public static class Builder { return this; } - public @NotNull Builder numberOfLinesBeforeFunction(ToIntFunction numberOfLinesBeforeFunction) { + public @NotNull Builder numberOfLinesBeforeFunction(@NotNull ToIntFunction numberOfLinesBeforeFunction) { this.numberOfLinesBeforeFunction = numberOfLinesBeforeFunction; return this; } From fe07f53ee57ce832c43474bc69d61de42f5c3787 Mon Sep 17 00:00:00 2001 From: Ranald Taylor Date: Mon, 11 Apr 2022 13:06:19 -0400 Subject: [PATCH 13/15] Updated jetbrains annotations scope to provided --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 19739ed2..be55450b 100644 --- a/pom.xml +++ b/pom.xml @@ -203,6 +203,7 @@ org.jetbrains annotations 22.0.0 + provided From d345d3976ea426c54eded013a5f15fb1a0319a2c Mon Sep 17 00:00:00 2001 From: Ranald Taylor Date: Mon, 11 Apr 2022 13:06:34 -0400 Subject: [PATCH 14/15] Updated jetbrains annotations to version 23.0.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index be55450b..a8d34d97 100644 --- a/pom.xml +++ b/pom.xml @@ -202,7 +202,7 @@ org.jetbrains annotations - 22.0.0 + 23.0.0 provided From 9ea6971cc2397a1da8f517efef3e1ae3c5bf4e4a Mon Sep 17 00:00:00 2001 From: Ranald Taylor Date: Mon, 11 Apr 2022 13:44:00 -0400 Subject: [PATCH 15/15] Added and modified remaining annotations --- src/main/java/ch/jalu/configme/SettingsManagerBuilder.java | 2 +- .../configme/configurationdata/CommentsConfiguration.java | 5 +++-- .../jalu/configme/configurationdata/ConfigurationData.java | 2 +- src/main/java/ch/jalu/configme/properties/Property.java | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java index 25afe8dd..f6db6549 100644 --- a/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java +++ b/src/main/java/ch/jalu/configme/SettingsManagerBuilder.java @@ -132,7 +132,7 @@ private SettingsManagerBuilder(@NotNull PropertyResource resource) { * * @return the settings manager */ - public @Nullable SettingsManager create() { + public @NotNull SettingsManager create() { Objects.requireNonNull(resource, "resource"); Objects.requireNonNull(configurationData, "configurationData"); return new SettingsManagerImpl(resource, configurationData, migrationService); diff --git a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java index 57551687..225e0c43 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java +++ b/src/main/java/ch/jalu/configme/configurationdata/CommentsConfiguration.java @@ -2,6 +2,7 @@ import ch.jalu.configme.SettingsHolder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.UnmodifiableView; import java.util.Arrays; import java.util.Collections; @@ -14,7 +15,7 @@ */ public class CommentsConfiguration { - private final Map> comments; + private final @NotNull Map> comments; /** * Constructor. @@ -48,7 +49,7 @@ public void setComment(@NotNull String path, @NotNull String... commentLines) { * * @return map with all comments */ - public @NotNull Map> getAllComments() { + public @NotNull @UnmodifiableView Map> getAllComments() { return Collections.unmodifiableMap(comments); } } diff --git a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java index 5ef20b79..daf17f05 100644 --- a/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java +++ b/src/main/java/ch/jalu/configme/configurationdata/ConfigurationData.java @@ -64,7 +64,7 @@ public interface ConfigurationData { * @param property type * @return value associated with the property, or null if not present */ - T getValue(@NotNull Property property); + @Nullable T getValue(@NotNull Property property); /** * Sets the given value for the given property. May throw an exception diff --git a/src/main/java/ch/jalu/configme/properties/Property.java b/src/main/java/ch/jalu/configme/properties/Property.java index aee9c059..9ee12b45 100644 --- a/src/main/java/ch/jalu/configme/properties/Property.java +++ b/src/main/java/ch/jalu/configme/properties/Property.java @@ -82,6 +82,6 @@ default boolean isValidInResource(@NotNull PropertyReader propertyReader) { * @param value the value to convert to an export value * @return value to use for export, null to skip the property */ - @Nullable Object toExportValue(T value); + @Nullable Object toExportValue(@Nullable T value); }