Search in sources :

Example 1 with ChannelInfo

use of io.openems.api.doc.ChannelInfo in project openems by OpenEMS.

the class ClassRepository method getThingDoc.

/**
 * Returns the cached ThingDoc or parses the class and adds it to the cache.
 * Field annotations have higher priority than method annotations!
 *
 * @param clazz
 */
public ThingDoc getThingDoc(Class<? extends Thing> clazz) {
    if (this.thingDocs.containsKey(clazz)) {
        // return from cache
        return this.thingDocs.get(clazz);
    }
    ThingDoc thingDoc = new ThingDoc(clazz);
    // get info about thing
    ThingInfo thing = clazz.getAnnotation(ThingInfo.class);
    if (thing == null) {
        log.warn("Thing [" + clazz.getName() + "] has no @ThingInfo annotation");
    } else {
        thingDoc.setThingDescription(thing);
    }
    // parse all methods
    for (Method method : clazz.getMethods()) {
        Class<?> type = null;
        if (method.getReturnType().isArray()) {
            Class<?> rtype = method.getReturnType();
            type = rtype.getComponentType();
        } else {
            type = method.getReturnType();
        }
        if (Channel.class.isAssignableFrom(type)) {
            Optional<ChannelInfo> channelInfoOpt = getAnnotationForMethod(clazz, method.getName());
            String channelId = method.getName();
            ChannelDoc channelDoc = new ChannelDoc(method, channelId, channelInfoOpt);
            thingDoc.addChannelDoc(channelDoc);
            if (ConfigChannel.class.isAssignableFrom(type)) {
                thingDoc.addConfigChannelDoc(channelDoc);
            }
        }
    }
    // parse all fields
    for (Field field : clazz.getFields()) {
        Class<?> type = field.getType();
        if (Channel.class.isAssignableFrom(type)) {
            String channelId = field.getName();
            ChannelDoc channelDoc = new ChannelDoc(field, channelId, Optional.ofNullable(field.getAnnotation(ChannelInfo.class)));
            thingDoc.addChannelDoc(channelDoc);
            if (ConfigChannel.class.isAssignableFrom(type)) {
                thingDoc.addConfigChannelDoc(channelDoc);
            }
        }
    }
    // add to cache
    this.thingDocs.put(clazz, thingDoc);
    return thingDoc;
}
Also used : Field(java.lang.reflect.Field) ChannelInfo(io.openems.api.doc.ChannelInfo) Method(java.lang.reflect.Method) ThingInfo(io.openems.api.doc.ThingInfo) ChannelDoc(io.openems.api.doc.ChannelDoc) ThingDoc(io.openems.api.doc.ThingDoc)

Aggregations

ChannelDoc (io.openems.api.doc.ChannelDoc)1 ChannelInfo (io.openems.api.doc.ChannelInfo)1 ThingDoc (io.openems.api.doc.ThingDoc)1 ThingInfo (io.openems.api.doc.ThingInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1