@Override public ValueType describe(PropertyPath path, TypeDescriptor mapType, DescribeContext context) { TypeDescriptor keyType = mapType.resolveGenericParameter(Map.class, 0); TypeDescriptor valueType = mapType.resolveGenericParameter(Map.class, 1); context.describeAsync(path.any(), new TypeContext(mapType, valueType)); ValueType keyValueType = context.describeNow(null, new TypeContext(mapType, keyType)); if (!(keyValueType instanceof ScalarType)) { throw new IllegalArgumentException("Key of " + path + ": " + mapType + " is not a scalar (ScalarType)"); } return newMapType((ScalarType) keyValueType); }
private IdentifiableType requireIdentifiable(ValueType valueType) { if (valueType instanceof IdentifiableType) { return (IdentifiableType) valueType; } throw new IllegalArgumentException(typeContext.toString() + ": expected IdentifiableType, got " + valueType.getClass()); }
@Override public Optional<ValueType> describe(@Nullable PropertyPath path, TypeContext typeContext, DescribeContext context) { TypeDescriptor type = typeContext.type; MappingResolver mappingResolver = context.getMappingResolver(); MethodDescriptor valueMethod = getValueMethod(type, mappingResolver); if (valueMethod != null) { Class<?> rawDelegateType = valueMethod.getRawReturnType(); StaticExecutable creator = getCreator(type, rawDelegateType, mappingResolver); ValueType valueType = context.describeNow(path, new TypeContext(valueMethod)); return Optional.of(DelegateType.of(valueMethod, creator, valueType)); } return Optional.empty(); }
private ValueType identifiableSetType() { ValueType valueType = context.describeNow(path.any(), new TypeContext(setType, elementType)); return newSetType(requireIdentifiable(valueType)); }
@Override public ValueType describe(PropertyPath path, TypeDescriptor listType, DescribeContext context) { TypeDescriptor elementType = listType.resolveGenericParameter(List.class, 0); context.describeAsync(path.anyIndex(), new TypeContext(listType, elementType)); return new ListType(); }
private void add(FieldDescriptor fieldDescriptor) { if (acceptIdField(fieldDescriptor)) { String name = getName(fieldDescriptor, fieldDescriptor.getName()); setIdentifier(name, fieldDescriptor, new TypeContext(fieldDescriptor)); } else if (acceptField(fieldDescriptor)) { String name = getName(fieldDescriptor, fieldDescriptor.getName()); add(name, fieldDescriptor, new TypeContext(fieldDescriptor)); } }
@Override public ValueType describe(PropertyPath path, TypeDescriptor collectionType, DescribeContext context) { TypeDescriptor elementType = collectionType.resolveGenericParameter(Collection.class, 0); context.describeAsync(path.anyIndex(), new TypeContext(collectionType, elementType)); return new CollectionType(); }
private ValueType functionSetType() { context.describeAsync(path.any(), new TypeContext(setType, elementType)); TypeDescriptor functionType = setType.getTypeDescriptors().get(setKey.by()); TypeDescriptor input = functionType.resolveGenericParameter(Function.class, 0); TypeDescriptor output = functionType.resolveGenericParameter(Function.class, 1); if (!input.isSuperTypeOf(elementType)) { throw new IllegalArgumentException("Input type of Function provided by @SetKey(by=+" + input + ") should be super type of Set's element type " + elementType); } IdentifiableType delegate = requireIdentifiable(context.describeNow(null, new TypeContext(output))); return newSetType(ImmutableList.of((new FunctionKey((Function) functionType.newInstance(), delegate)))); }
public Schema<ValueType> describeSchema(TypeDescriptor rootType) { schemaRoot = new Schema.Builder<>(); TypeContext typeContext = new TypeContext(rootType); schemaRoot.setValue(createValueType(PropertyPath.ROOT, typeContext)); processMappings(); return schemaRoot.build(); }
private void add(BeanProperty property) { if (acceptIdProperty(property)) { String name = getName(property.getReadMethod(), property.getName()); setIdentifier(name, property, new TypeContext(property)); } else if (acceptProperty(property)) { String name = getName(property.getReadMethod(), property.getName()); add(name, property, new TypeContext(property)); } }
public static ValueType describeReference(TypeDescriptor type, PropertyPath targetPath, DescribeContext context) { TypeContext typeContext = new TypeContext(type); context.describeAsync(targetPath.anyIndex(), typeContext); IdentifiableType identifiableType = (IdentifiableType) context.describeNow(targetPath.anyKey(), typeContext); return new ReferenceType(identifiableType, targetPath); } }
private ValueType setKeyPropertiesType() { PropertyPath elementPath = getElementPath(path, setKey.value()); ObjectType objectType = (ObjectType) context.describeNow(elementPath, new TypeContext(setType, elementType)); if (objectType.getIdentifier() != null) { throw new IllegalArgumentException("Element should not have both @SetKey and @Id: " + typeContext); } // Ensure that nested mappings are processed before accessing them context.processMappings(); List<Key> keys = new ArrayList<>(); for (String idProperty : setKey.value()) { IdentifiableType idType = requireIdentifiable(context.getValueType(elementPath.property(idProperty))); AccessibleProperty property = objectType.getProperties().get(idProperty); keys.add(new PropertyKey(property, idType)); } return newSetType(keys); }