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;
}
Aggregations