ehatcher 2002/06/05 18:25:46
Modified: proposal/xdocs/src/org/apache/tools/ant/xdoclet Tag:
ANT_15_BRANCH TaskTagsHandler.java
Log:
substantial refactoring - replacing home-grown logic that attempted to mimic
IntrospectionHelper's rules with the actual use of IntrospectionHelper. This
has the unfortunate side-effect of forcing more dependencies. For example, I
had to add JAR files for ANTLR, JDepend, and StarTeam.
In other words - the attributes and elements picked up should now be even
more accurate.
Revision Changes Path
No revision
No revision
1.1.2.3 +106 -77
jakarta-ant/proposal/xdocs/src/org/apache/tools/ant/xdoclet/TaskTagsHandler.java
Index: TaskTagsHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/proposal/xdocs/src/org/apache/tools/ant/xdoclet/TaskTagsHandler.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- TaskTagsHandler.java 3 Jun 2002 03:28:45 -0000 1.1.2.2
+++ TaskTagsHandler.java 6 Jun 2002 01:25:46 -0000 1.1.2.3
@@ -56,6 +56,7 @@
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.Parameter;
+import org.apache.tools.ant.IntrospectionHelper;
import xdoclet.XDocletException;
import xdoclet.XDocletTagSupport;
import xdoclet.tags.AbstractProgramElementTagsHandler;
@@ -64,6 +65,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -275,121 +277,148 @@
/**
- * Returns true if the method corresponds to an Ant task attribute using
- * the rules from IntrospectionHelper
- *
- * only filter org.apache.tools.ant.Task setters that are hidden
- * check that it returns void and only has single argument
- * incorporate rules for argument types from IntrospectionHelper
- * - i.e. not an array
- * - primitives/wrappers
- * - File
- * - Path
- * - EnumeratedAttribute
- * - Class with string constructor
- */
- private boolean isAntAttribute(MethodDoc method) {
-/*
- String[] excludeList = new String[]{"setLocation", "setDescription",
"setOwningTarget", "setRuntimeConfigurableWrapper",
- "setTaskName", "setTaskType",
"setProject"};
- for (int i = 0; i < excludeList.length; i++) {
- if (excludeList[i].equals(method.name())) {
- return true;
- }
- }
-*/
- return false;
- }
-
+ * @todo refactor to cache methods per class, and save some time
+ */
private MethodDoc[] getAttributeMethods(ClassDoc cur_class) throws
XDocletException {
- MethodDoc[] methods = getMethods(cur_class);
- List attributeMethods = new ArrayList();
- Map nameTypeMap = new HashMap();
-
- for (int i = 0; i < methods.length; i++) {
- MethodDoc method = methods[i];
-
- if (!method.isPublic()) {
- continue;
- }
+ // Use Ant's own introspection mechanism to gather the
+ // attributes this class supports
+ IntrospectionHelper is = null;
+ try {
+ is =
IntrospectionHelper.getHelper(Class.forName(cur_class.qualifiedName()));
+ } catch (ClassNotFoundException e) {
+ throw new XDocletException(e.getMessage());
+ }
+
+ // Regroup the attributes, since IntrospectionHelper
+ // doesn't give us the whole data structure directly
+ Enumeration enum = is.getAttributes();
+ Properties attributeTypeMap = new Properties();
+ while (enum.hasMoreElements()) {
+ String name = (String) enum.nextElement();
+ Class type = is.getAttributeType(name);
+ attributeTypeMap.setProperty(name, type.getName());
+// System.out.println(name + " = " + type.getName());
+ }
+
+ // We need to return MethodDoc[] from this method
+ // so get all methods from the current class
+ MethodDoc[] allMethods = getMethods(cur_class);
- if (!method.name().startsWith("set")) {
+ // And now filter the MethodDoc's based
+ // on what IntrospectionHelper says
+ List attributeMethods = new ArrayList();
+ for (int i = 0; i < allMethods.length; i++) {
+ MethodDoc method = allMethods[i];
+ String methodName = method.name();
+ if (!methodName.startsWith("set")) {
continue;
}
- // if superclass is org.apache.tools.ant.Task then
- // remove some known unallowed properties
- if (isAntAttribute(method)) {
+ String attributeName = methodName.substring(3).toLowerCase();
+ if ((method.parameters().length != 1) || (!method.isPublic())) {
continue;
}
+ String attributeType = method.parameters()[0].typeName();
- // ensure method only has one parameter
- Parameter[] params = method.parameters();
- if (params.length != 1) {
+ if
(!attributeType.equals(attributeTypeMap.getProperty(attributeName))) {
continue;
}
- Parameter param = params[0];
- // Screen out attribute setters if there are duplicates,
- // and only return the first non-String one
- // (this may or may not jive with IntrospectionHelper)
- MethodDoc oldMethod = (MethodDoc) nameTypeMap.get(method.name());
- if (oldMethod == null) {
- nameTypeMap.put(method.name(), method);
- } else {
- if
("java.lang.String".equals(oldMethod.parameters()[0].typeName())) {
- attributeMethods.remove(oldMethod);
- nameTypeMap.put(method.name(), method);
- }
- }
+// System.out.println(methodName + " : " + attributeName + " : "
+ attributeType);
attributeMethods.add(method);
}
return (MethodDoc[]) attributeMethods.toArray(new MethodDoc[0]);
+ ;
}
/**
* @todo add checks for number parameters and appropriate return value
* check for proper exception too?
* method prefixes: add, create, addConfigured (but not addText)
+ *
+ * @todo add DynamicConfigurator (this should be noted in the template,
not dealt with here)
*/
private MethodDoc[] getElementMethods(ClassDoc cur_class) throws
XDocletException {
- MethodDoc[] methods = getMethods(cur_class);
- List attributeMethods = new ArrayList();
+ // Use Ant's own introspection mechanism to gather the
+ // elements this class supports
+ IntrospectionHelper is = null;
+ try {
+ is =
IntrospectionHelper.getHelper(Class.forName(cur_class.qualifiedName()));
+ } catch (ClassNotFoundException e) {
+ throw new XDocletException(e.getMessage());
+ }
+
+ // Regroup the elements, since IntrospectionHelper
+ // doesn't give us the whole data structure directly
+ Enumeration enum = is.getNestedElements();
+ Properties elementTypeMap = new Properties();
+ while (enum.hasMoreElements()) {
+ String name = (String) enum.nextElement();
+ Class type = is.getElementType(name);
+ elementTypeMap.setProperty(name, type.getName());
+// System.out.println(name + " = " + type.getName());
+ }
+
+ // We need to return MethodDoc[] from this method
+ // so get all methods from the current class
+ MethodDoc[] allMethods = getMethods(cur_class);
+
+ // And now filter the MethodDoc's based
+ // on what IntrospectionHelper says
+ List elementMethods = new ArrayList();
+ for (int i = 0; i < allMethods.length; i++) {
+ MethodDoc method = allMethods[i];
+ String methodName = method.name();
+
+ // Object create(), void add(Object), void addConfigured(Object)
+ String elementName = null;
+ boolean adder = false; // true if addXXX or addConfiguredXXX
+ if (methodName.startsWith("create")) {
+ elementName = methodName.substring(6).toLowerCase();
+ }
+
+ if (methodName.startsWith("add")) {
+ int length = 3;
+ if (methodName.startsWith("addConfigured")) {
+ length = 13;
+ }
- for (int i = 0; i < methods.length; i++) {
- if (!methods[i].isPublic()) {
- continue;
+ elementName = methodName.substring(length).toLowerCase();
+ adder = true;
}
- String name = methods[i].name();
+ if (elementName == null) {
+ continue;
+ }
- // ensure if there are no parameters, there is a return type,
- // otherwise ensure there's only one parameter.
- Parameter[] params = methods[i].parameters();
- if (params.length == 0) {
- if (methods[i].returnType().asClassDoc() == null) {
+ //System.out.println("elementName = " + elementName);
+ String elementType = null;
+ if (adder) {
+ if (method.parameters().length != 1) {
continue;
}
+ elementType = method.parameters()[0].typeName();
+ } else {
+ elementType = method.returnType().qualifiedTypeName();
+ }
- // only the "createXXX" method has zero params
- // the "addXXX" and "addConfiguredXXX" have 1 param
- if (!name.startsWith("create")) {
- continue;
- }
- } else if (params.length != 1) {
+ if (!method.isPublic()) {
continue;
}
- if ((name.startsWith("add") && !name.equals("addText")) ||
- name.startsWith("create")) {
- attributeMethods.add(methods[i]);
+ //System.out.println(elementName + " = " + elementType);
+ if
(!elementType.equals(elementTypeMap.getProperty(elementName))) {
+ continue;
}
+
+ elementMethods.add(method);
}
- return (MethodDoc[]) attributeMethods.toArray(new MethodDoc[0]);
+ return (MethodDoc[]) elementMethods.toArray(new MethodDoc[0]);
+ ;
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>