From 539d94b4a68b1a161ac2ea3cf633b50d9299013b Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Tue, 8 Jan 2013 18:37:54 -0800 Subject: [PATCH] ApiXmlDocWriter: Reuse methods from ReflectUtil Signed-off-by: Rohit Yadav --- .../com/cloud/api/doc/ApiXmlDocWriter.java | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java index b493ec4b29c..84851c389ad 100644 --- a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java +++ b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java @@ -28,6 +28,7 @@ import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import com.cloud.utils.ReflectUtil; import org.apache.cloudstack.api.*; import org.apache.log4j.Logger; @@ -79,13 +80,15 @@ public class ApiXmlDocWriter { } public static void main(String[] args) { - // Populate api name and cmd class mappings - Reflections reflections = new Reflections("org.apache.cloudstack.api"); - Set> cmdClasses = reflections.getTypesAnnotatedWith(APICommand.class); - reflections = new Reflections("com.cloud.api"); - cmdClasses.addAll(reflections.getTypesAnnotatedWith(APICommand.class)); + + Set> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[]{"org.apache.cloudstack.api", "com.cloud.api"}); + for(Class cmdClass: cmdClasses) { String apiName = cmdClass.getAnnotation(APICommand.class).name(); + if (_apiNameCmdClassMap.containsKey(apiName)) { + System.out.println("Warning, API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName); + continue; + } _apiNameCmdClassMap.put(apiName, cmdClass); } @@ -340,33 +343,15 @@ public class ApiXmlDocWriter { if(!impl.since().isEmpty()){ apiCommand.setSinceVersion(impl.since()); } - - // Set request parameters - Field[] fields = clas.getDeclaredFields(); - // Get fields from superclass - Class superClass = clas.getSuperclass(); - boolean isAsync = false; - while (superClass != null && superClass != Object.class) { - String superName = superClass.getName(); - if (!superName.equals(BaseCmd.class.getName()) && !superName.equals(BaseAsyncCmd.class.getName()) && !superName.equals(BaseAsyncCreateCmd.class.getName())) { - Field[] superClassFields = superClass.getDeclaredFields(); - if (superClassFields != null) { - Field[] tmpFields = new Field[fields.length + superClassFields.length]; - System.arraycopy(fields, 0, tmpFields, 0, fields.length); - System.arraycopy(superClassFields, 0, tmpFields, fields.length, superClassFields.length); - fields = tmpFields; - } - } - superClass = superClass.getSuperclass(); - // Set Async information for the command - if (superName.equals(BaseAsyncCmd.class.getName()) || superName.equals(BaseAsyncCreateCmd.class.getName())) { - isAsync = true; - } - } - + boolean isAsync = ReflectUtil.isCmdClassAsync(clas, + new Class[] {BaseAsyncCmd.class, BaseAsyncCreateCmd.class}); + apiCommand.setAsync(isAsync); - + + Field[] fields = ReflectUtil.getAllFieldsForClass(clas, + new Class[] {BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class}); + request = setRequestFields(fields); // Get response parameters