001package net.gdface.codegen.thrift.javadoc;
002
003import java.beans.IntrospectionException;
004import java.beans.PropertyDescriptor;
005import java.lang.reflect.Method;
006import java.util.List;
007
008import com.facebook.swift.codec.metadata.ClassCommentProvider;
009import com.google.common.collect.ImmutableList;
010
011import gu.doc.ExtClassDoc;
012
013import static gu.doc.JavadocReader.read;
014
015public class ClassCommentProviderImpl implements ClassCommentProvider {
016        
017        private final ExtClassDoc classDoc;
018        private final Class<?> clazz;
019        
020        public ClassCommentProviderImpl(Class<?> clazz) {
021                this.clazz = clazz;
022                this.classDoc = read(clazz);
023        }
024
025        @Override
026        public ImmutableList<String> commentOfClass() {
027                if(classDoc  == null){
028                        return ImmutableList.of();
029                }
030                List<String> cmt = classDoc.getClassCommentAsList(false,false);
031                return cmt  == null ? ImmutableList.<String>of() : ImmutableList.copyOf(cmt);
032        }
033
034        @Override
035        public ImmutableList<String> commentOfMethod(Method method) {
036                if(classDoc  == null){
037                        return ImmutableList.of();
038                }
039                List<String> cmt = classDoc.getMethodCommentAsList(method,false,false);
040                return cmt  == null ? ImmutableList.<String>of() : ImmutableList.copyOf(cmt);
041        }
042
043        @Override
044        public ImmutableList<String> commentOfField(String name) {
045                if(classDoc  == null){
046                        return ImmutableList.of();
047                }
048                List<String> cmt = classDoc.getFieldCommentAsList(name,false,false);
049                if(cmt == null){
050                        try {
051                                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name,clazz);
052                                Method rm = propertyDescriptor.getReadMethod();
053                                if(rm != null){
054                                        return commentOfMethod(rm);
055                                }
056                                Method wm = propertyDescriptor.getWriteMethod();
057                                if(wm != null){
058                                        return commentOfMethod(wm);
059                                }
060                        } catch (IntrospectionException e) {
061                                // DO NOTHING
062                        }
063
064                }
065                return cmt  == null ? ImmutableList.<String>of() : ImmutableList.copyOf(cmt);
066        }
067
068}