
com.sap.cds.reflect.impl.CdsAnnotationReader Maven / Gradle / Ivy
/*******************************************************************
* © 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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import com.fasterxml.jackson.core.JsonProcessingException;
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.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.sap.cds.ql.cqn.CqnSyntaxException;
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 static final HashFunction HASHER = Hashing.goodFastHash(160);
private CdsAnnotationReader() {
}
public static List> read(CdsModelReader.Config config, JsonNode jsonObject) {
List> list = new ArrayList<>();
Iterator> fields = jsonObject.fields();
while (fields.hasNext()) {
String name = fields.next().getKey();
if (isAnnotation(name) && (config.includeUIAnnotations() || !name.startsWith("@UI."))) {
JsonNode node = jsonObject.get(name);
if (!node.isNull()) {
list.add(getAnnotation(name, node));
}
}
}
return list;
}
private static CdsAnnotation> getAnnotation(String name, JsonNode node) {
String key = CdsAnnotatableImpl.removeAt(name) + ":" + node.toString();
HashCode hash = HASHER.hashString(key, StandardCharsets.UTF_8);
return CdsModelArtifactsCache.getInstance().getAnnotations().get(hash, h -> parseAnnotation(name, node));
}
private static CdsAnnotation> parseAnnotation(String name, JsonNode node) {
Object val;
try {
val = jackson.treeToValue(node, UNKNOWN_TYPE);
} catch (JsonProcessingException e) {
throw new CqnSyntaxException("Unparsable annotation: " + node.toString(), e);
}
return annotation(name, val);
}
public static boolean isAnnotation(String key) {
return key.startsWith("@");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy