log error if cannot find property during json schema generation

GitOrigin-RevId: 2ce2b9d196c96eafb663052ef27260337faa292b
This commit is contained in:
Vladimir Krivosheev
2020-02-19 19:32:51 +01:00
committed by intellij-monorepo-bot
parent 39dd91399b
commit 55f5a36815
6 changed files with 25 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util.xmlb;
import com.intellij.openapi.util.text.StringUtil;
@@ -14,6 +14,7 @@ import gnu.trove.TObjectFloatHashMap;
import org.jdom.Comment;
import org.jdom.Content;
import org.jdom.Element;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -22,6 +23,7 @@ import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.*;
@ApiStatus.Internal
public class BeanBinding extends NotNullDeserializeBinding {
private static final XmlSerializerPropertyCollector PROPERTY_COLLECTOR = new XmlSerializerPropertyCollector();
@@ -58,8 +60,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
}
@Override
@Nullable
public final Object serialize(@NotNull Object o, @Nullable Object context, @Nullable SerializationFilter filter) {
public final @Nullable Object serialize(@NotNull Object o, @Nullable Object context, @Nullable SerializationFilter filter) {
return serializeInto(o, context == null ? null : new Element(myTagName), filter);
}
@@ -67,8 +68,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
return serializeInto(object, createElementIfEmpty ? new Element(myTagName) : null, filter);
}
@Nullable
public Element serializeInto(@NotNull Object o, @Nullable Element element, @Nullable SerializationFilter filter) {
public @Nullable Element serializeInto(@NotNull Object o, @Nullable Element element, @Nullable SerializationFilter filter) {
for (NestedBinding binding : myBindings) {
if (o instanceof SerializationFilter && !((SerializationFilter)o).accepts(binding.getAccessor(), o)) {
continue;
@@ -79,8 +79,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
return element;
}
@Nullable
protected final Element serializePropertyInto(@NotNull NestedBinding binding,
protected final @Nullable Element serializePropertyInto(@NotNull NestedBinding binding,
@NotNull Object o,
@Nullable Element element,
@Nullable SerializationFilter filter,
@@ -123,15 +122,13 @@ public class BeanBinding extends NotNullDeserializeBinding {
}
@Override
@NotNull
public final Object deserialize(@Nullable Object context, @NotNull Element element) {
public final @NotNull Object deserialize(@Nullable Object context, @NotNull Element element) {
Object instance = newInstance();
deserializeInto(instance, element);
return instance;
}
@NotNull
protected Object newInstance() {
protected @NotNull Object newInstance() {
return ReflectionUtil.newInstance(myBeanClass, false);
}
@@ -145,8 +142,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
return true;
}
@NotNull
public final TObjectFloatHashMap<String> computeBindingWeights(@NotNull LinkedHashSet<String> accessorNameTracker) {
public final @NotNull TObjectFloatHashMap<String> computeBindingWeights(@NotNull LinkedHashSet<String> accessorNameTracker) {
TObjectFloatHashMap<String> weights = new TObjectFloatHashMap<>(accessorNameTracker.size());
float weight = 0;
float step = (float)myBindings.length / (float)accessorNameTracker.size();
@@ -252,8 +248,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
return element.getName().equals(myTagName);
}
@NotNull
private static String getTagName(@NotNull Class<?> aClass) {
private static @NotNull String getTagName(@NotNull Class<?> aClass) {
for (Class<?> c = aClass; c != null; c = c.getSuperclass()) {
String name = getTagNameFromAnnotation(c);
if (name != null) {
@@ -273,14 +268,12 @@ public class BeanBinding extends NotNullDeserializeBinding {
return name;
}
@Nullable
private static String getTagNameFromAnnotation(@NotNull Class<?> aClass) {
private static @Nullable String getTagNameFromAnnotation(@NotNull Class<?> aClass) {
Tag tag = aClass.getAnnotation(Tag.class);
return tag != null && !tag.value().isEmpty() ? tag.value() : null;
}
@NotNull
public static List<MutableAccessor> getAccessors(@NotNull Class<?> aClass) {
public static @NotNull List<MutableAccessor> getAccessors(@NotNull Class<?> aClass) {
List<MutableAccessor> accessors = PROPERTY_COLLECTOR.collect(aClass);
if (accessors.isEmpty() && !isAssertBindings(aClass)) {
LOG.warn("no accessors for " + aClass);
@@ -343,8 +336,7 @@ public class BeanBinding extends NotNullDeserializeBinding {
return "BeanBinding[" + myBeanClass.getName() + ", tagName=" + myTagName + "]";
}
@NotNull
private static NestedBinding createBinding(@NotNull MutableAccessor accessor, @NotNull Serializer serializer, @NotNull Property.Style propertyStyle) {
private static @NotNull NestedBinding createBinding(@NotNull MutableAccessor accessor, @NotNull Serializer serializer, @NotNull Property.Style propertyStyle) {
Attribute attribute = accessor.getAnnotation(Attribute.class);
if (attribute != null) {
return new AttributeBinding(accessor, attribute);

View File

@@ -9,10 +9,16 @@ internal class ItemTypeInfoProvider(private val hostClass: Class<out BaseState>)
BeanBinding.getAccessors(hostClass)
}
fun getListItemType(propertyName: String): Class<out BaseState>? {
fun getListItemType(propertyName: String, logAsErrorIfPropertyNotFound: Boolean): Class<out BaseState>? {
val accessor = accessors.find { it.name == propertyName }
if (accessor == null) {
LOG.warn("Property not found (name=$propertyName, hostClass=${hostClass.name})")
val message = "Property not found (name=$propertyName, hostClass=${hostClass.name})"
if (logAsErrorIfPropertyNotFound) {
LOG.error(message)
}
else {
LOG.warn(message)
}
return null
}

View File

@@ -11,10 +11,6 @@ import org.snakeyaml.engine.v2.nodes.NodeTuple
import org.snakeyaml.engine.v2.nodes.ScalarNode
import org.snakeyaml.engine.v2.nodes.SequenceNode
internal fun <T : BaseState> readIntoObject(instance: T, node: MappingNode, affectedPropertyConsumer: ((StoredProperty<Any>) -> Unit)? = null): T {
return readIntoObject(instance, node.value, affectedPropertyConsumer)
}
internal fun <T : BaseState> readIntoObject(instance: T, nodes: List<NodeTuple>, affectedPropertyConsumer: ((StoredProperty<Any>) -> Unit)? = null): T {
val properties = instance.__getProperties()
val itemTypeInfoProvider = ItemTypeInfoProvider(instance.javaClass)
@@ -60,7 +56,7 @@ private fun readCollection(property: CollectionStoredProperty<*, *>, itemTypeInf
return
}
val itemType by lazy { itemTypeInfoProvider.getListItemType(property.name!!) }
val itemType by lazy { itemTypeInfoProvider.getListItemType(property.name!!, logAsErrorIfPropertyNotFound = false) }
for (itemNode in valueNode.value) {
if (itemNode is ScalarNode) {

View File

@@ -116,6 +116,6 @@ internal class RunConfigurationListReader(private val processor: (factory: Confi
// very important - set BEFORE read to ensure that user can set any value for isAllowRunningInParallel and it will be not overridden by us later
instance.isAllowRunningInParallel = factory.singletonPolicy.isAllowRunningInParallel
}
processor(factory, readIntoObject(instance, node))
processor(factory, readIntoObject(instance, node.value))
}
}

View File

@@ -100,7 +100,7 @@ internal fun buildJsonSchema(state: BaseState,
}
}
is CollectionStoredProperty<*, *> -> {
val listType = itemTypeInfoProvider.getListItemType(name) ?: return@map
val listType = itemTypeInfoProvider.getListItemType(name, logAsErrorIfPropertyNotFound = true) ?: return@map
when {
listType === java.lang.String::class.java -> {
map("items") {

View File

@@ -37,7 +37,7 @@ class PropertyValueReaderTest {
sdks:
- sdkName: foo
sdkPath: /home/foo
"""))
""").value)
assertThat(result.sdks).hasSize(1)
val sdks = result.sdks