use of com.disney.groovity.doc.Arg 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;
}
use of com.disney.groovity.doc.Arg 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;
}
Aggregations