All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.reflect.impl.CdsAnnotationReader Maven / Gradle / Ivy

There is a newer version: 3.8.0
Show newest version
/*******************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved. *
 *******************************************************************/
package com.sap.cds.reflect.impl;

import static com.sap.cds.reflect.impl.CdsAnnotatableImpl.CdsAnnotationImpl.annotation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.sap.cds.reflect.CdsAnnotation;

public class CdsAnnotationReader {
	private static final ObjectMapper jackson = new ObjectMapper();
	private static final JavaType UNKNOWN_TYPE = TypeFactory.unknownType();

	private CdsAnnotationReader() {
	}

	public static List> read(JsonNode jsonObject) {
		List> list = new ArrayList<>();
		Iterator> fields = jsonObject.fields();
		while (fields.hasNext()) {
			String name = fields.next().getKey();
			if (isAnnotation(name)) {
				Object val;
				JsonNode value = jsonObject.get(name);
				try {
					val = jackson.readValue(value.toString(), UNKNOWN_TYPE);
				} catch (IOException e) { // NOSONAR
					val = value.toString();
				}
				if (val != null) {
					list.add(annotation(name, val));
				}
			}
		}
		return list;
	}

	public static boolean isAnnotation(String key) {
		return key.startsWith("@");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy