Search in sources :

Example 1 with Function

use of com.disney.groovity.doc.Function in project groovity by disney.

the class Groovity method makeClassDoc.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map makeClassDoc(Class clazz) {
    ClassDescriptor descriptor = new ClassDescriptor(clazz);
    Map cmodel = new LinkedHashMap();
    cmodel.put(NAME, clazz.getName());
    if (descriptor.getSuperType() != null) {
        cmodel.put(EXTENDS, TypeLabel.get(descriptor.getSuperType()));
    }
    if (descriptor.getInterfaces() != null && descriptor.getInterfaces().length > 0) {
        List<String> interfaces = new ArrayList<>();
        for (Type t : descriptor.getInterfaces()) {
            interfaces.add(TypeLabel.get(t));
        }
        cmodel.put(IMPLEMENTS, interfaces);
    }
    List<TypedName> props = descriptor.getProperties();
    if (props != null) {
        List cfields = new ArrayList();
        for (TypedName prop : props) {
            if (!BINDING.equals(prop.name)) {
                Map pmap = new LinkedHashMap();
                pmap.put(NAME, prop.name);
                pmap.put(TYPE, TypeLabel.get(prop.type));
                cfields.add(pmap);
            }
        }
        if (cfields.size() > 0) {
            cmodel.put(PROPERTIES, cfields);
        }
    }
    List<Method> mtds = descriptor.getMethods();
    if (mtds != null) {
        List mlist = new ArrayList();
        for (Method desc : mtds) {
            if (!internalMethodNames.contains(desc.getName()) && !desc.getName().contains("__")) {
                Map mmap = new HashMap();
                mmap.put(NAME, desc.getName());
                mmap.put(MODIFIERS, Modifier.toString(desc.getModifiers()));
                Function func = desc.getAnnotation(Function.class);
                if (func != null) {
                    mmap.put(INFO, func.info());
                }
                Type[] pts = desc.getGenericParameterTypes();
                Annotation[][] anns = desc.getParameterAnnotations();
                if (pts != null && pts.length > 0) {
                    List parms = new ArrayList();
                    for (int i = 0; i < pts.length; i++) {
                        Map pm = new LinkedHashMap();
                        Annotation[] aa = anns[i];
                        String name = "arg" + (i + 1);
                        for (Annotation a : aa) {
                            if (a instanceof Arg) {
                                name = ((Arg) a).name();
                                pm.put(INFO, ((Arg) a).info());
                                break;
                            }
                        }
                        pm.put(NAME, name);
                        pm.put(TYPE, TypeLabel.get(pts[i]));
                        parms.add(pm);
                    }
                    mmap.put(PARAMETERS, parms);
                }
                Type returnType = desc.getGenericReturnType();
                if (returnType != null) {
                    mmap.put(RETURNS, TypeLabel.get(returnType));
                }
                mlist.add(mmap);
            }
        }
        cmodel.put(METHODS, mlist);
    }
    return cmodel;
}
Also used : ClassDescriptor(com.disney.groovity.doc.ClassDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TypedName(com.disney.groovity.doc.ClassDescriptor.TypedName) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) LinkedHashMap(java.util.LinkedHashMap) Function(com.disney.groovity.doc.Function) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Arg(com.disney.groovity.doc.Arg) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Function

use of com.disney.groovity.doc.Function in project groovity by disney.

the class Groovity method getLibraryDocs.

@SuppressWarnings({ "rawtypes", "unchecked" })
public List getLibraryDocs() {
    ArrayList docs = new ArrayList();
    ArrayList<Map.Entry<String, Class<Script>>> entries = new ArrayList<Map.Entry<String, Class<Script>>>();
    entries.addAll(embeddedScripts.entrySet());
    entries.addAll(scripts.entrySet());
    // alphabetize for consistency
    Collections.sort(entries, new Comparator<Map.Entry<String, Class<Script>>>() {

        public int compare(Entry<String, Class<Script>> o1, Entry<String, Class<Script>> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    });
    for (Map.Entry<String, Class<Script>> entry : entries) {
        Boolean isLibrary = Boolean.FALSE;
        Class scriptClass = entry.getValue();
        try {
            isLibrary = (Boolean) scriptClass.getField(IS_GROOVITY_LIBRARY).get(scriptClass);
        } catch (Exception e) {
        }
        if (isLibrary.booleanValue()) {
            String sourcePath = getSourcePath(scriptClass);
            String className = scriptClass.getName();
            if (className.startsWith("_")) {
                className = className.substring(1);
            }
            HashSet<Class> publicClasses = new HashSet<Class>();
            LinkedHashMap libModel = new LinkedHashMap();
            libModel.put(NAME, className);
            libModel.put(PATH, sourcePath);
            List functions = new ArrayList();
            libModel.put(FUNCTIONS, functions);
            Method[] methods = entry.getValue().getDeclaredMethods();
            Arrays.sort(methods, new Comparator<Method>() {

                public int compare(Method o1, Method o2) {
                    int o = o1.getName().compareTo(o2.getName());
                    if (o == 0) {
                        o = o1.getParameterTypes().length - o2.getParameterTypes().length;
                    }
                    return o;
                }
            });
            for (Method method : methods) {
                Function func = method.getAnnotation(Function.class);
                if (func != null && !internalMethodNames.contains(method.getName())) {
                    LinkedHashMap funcModel = new LinkedHashMap();
                    funcModel.put(NAME, method.getName());
                    funcModel.put(INFO, func.info());
                    functions.add(funcModel);
                    // add argument info
                    Type[] pts = method.getGenericParameterTypes();
                    Annotation[][] panns = method.getParameterAnnotations();
                    if (pts != null && pts.length > 0 && pts.length == panns.length) {
                        List params = new ArrayList();
                        funcModel.put(ARGS, params);
                        for (int i = 0; i < pts.length; i++) {
                            Type type = pts[i];
                            LinkedHashMap argModel = new LinkedHashMap();
                            params.add(argModel);
                            Annotation[] arganns = panns[i];
                            for (Annotation a : arganns) {
                                if (a.annotationType().equals(Arg.class)) {
                                    Arg arg = (Arg) a;
                                    argModel.put(NAME, arg.name());
                                    argModel.put(INFO, arg.info());
                                    argModel.put(NULLABLE, arg.nullable());
                                }
                            }
                            collectClasses(type, publicClasses, scriptClass.getClassLoader());
                            argModel.put(TYPE, TypeLabel.get(type));
                        }
                    }
                    // add return type
                    Type returnType = method.getGenericReturnType();
                    if (returnType != null) {
                        collectClasses(returnType, publicClasses, scriptClass.getClassLoader());
                        funcModel.put(RETURNS, TypeLabel.get(returnType));
                    }
                }
            }
            if (publicClasses.size() > 0) {
                List classDocs = new ArrayList();
                libModel.put(CLASSES, classDocs);
                for (Class c : publicClasses) {
                    classDocs.add(makeClassDoc(c));
                }
            }
            docs.add(libModel);
        }
    }
    return docs;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Function(com.disney.groovity.doc.Function) Entry(java.util.Map.Entry) JarEntry(java.util.jar.JarEntry) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashSet(java.util.HashSet) Script(groovy.lang.Script) Method(java.lang.reflect.Method) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Arg(com.disney.groovity.doc.Arg) GroovyClass(org.codehaus.groovy.tools.GroovyClass) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Arg (com.disney.groovity.doc.Arg)2 Function (com.disney.groovity.doc.Function)2 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ClassDescriptor (com.disney.groovity.doc.ClassDescriptor)1 TypedName (com.disney.groovity.doc.ClassDescriptor.TypedName)1 Script (groovy.lang.Script)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1