ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r477969 - in /ant/core/trunk/src/main/org/apache/tools/ant: ComponentHelper.java types/DataType.java
Date Tue, 21 Nov 2006 23:50:00 GMT
Author: mbenson
Date: Tue Nov 21 15:49:59 2006
New Revision: 477969

URL: http://svn.apache.org/viewvc?view=rev&rev=477969
Log:
reorder ComponentHelper imports, refactor getElementName to minimize
code duplication

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java
    ant/core/trunk/src/main/org/apache/tools/ant/types/DataType.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java?view=diff&rev=477969&r1=477968&r2=477969
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java Tue Nov 21 15:49:59
2006
@@ -18,6 +18,15 @@
 
 package org.apache.tools.ant;
 
+import java.lang.ref.WeakReference;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.InvocationTargetException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.File;
+import java.io.StringWriter;
+import java.io.PrintWriter;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.HashSet;
@@ -25,16 +34,7 @@
 import java.util.Properties;
 import java.util.Set;
 import java.util.Stack;
-
 import java.util.Vector;
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.StringWriter;
-import java.io.PrintWriter;
-import java.lang.ref.WeakReference;
-import java.lang.reflect.Modifier;
-import java.lang.reflect.InvocationTargetException;
 
 import org.apache.tools.ant.taskdefs.Typedef;
 import org.apache.tools.ant.taskdefs.Definer;
@@ -136,6 +136,9 @@
      * @return the project component for a specific project.
      */
     public static ComponentHelper getComponentHelper(Project project) {
+        if (project == null) {
+            return null;
+        }
         // Singleton for now, it may change ( per/classloader )
         ComponentHelper ph = (ComponentHelper) project.getReference(
                 COMPONENT_HELPER_REFERENCE);
@@ -606,32 +609,69 @@
      * <p>
      * This is useful for logging purposes.
      *
-     * @param element The element to describe.
-     *                Must not be <code>null</code>.
-     * @param brief   whether to use a brief description.
+     * @param o     The element to describe.
+     *              Must not be <code>null</code>.
+     * @param brief whether to use a brief description.
      * @return a description of the element type.
      *
      * @since Ant 1.7
      */
-    public String getElementName(Object element, boolean brief) {
+    public String getElementName(Object o, boolean brief) {
         //  PR: I do not know what to do if the object class
         //      has multiple defines
         //      but this is for logging only...
-        String name = null;
-        Class elementClass = element.getClass();
+        Class elementClass = o.getClass();
         String elementClassname = elementClass.getName();
         for (Iterator i = antTypeTable.values().iterator(); i.hasNext();) {
             AntTypeDefinition def = (AntTypeDefinition) i.next();
             if (elementClassname.equals(def.getClassName())
-                &&
-                (elementClass == def.getExposedClass(project))) {
-                name = def.getName();
+                    && (elementClass == def.getExposedClass(project))) {
+                String name = def.getName();
                 return brief ? name : "The <" + name + "> type";
             }
         }
-        name = elementClass.getName();
-        return brief
-            ? name.substring(name.lastIndexOf('.') + 1) : "Class " + name;
+        return getUnmappedElementName(o.getClass(), brief);
+    }
+
+    /**
+     * Convenient way to get some element name even when you may not have a
+     * Project context.
+     * @param p       The optional Project instance.
+     * @param o       The element to describe.
+     *                Must not be <code>null</code>.
+     * @param brief   whether to use a brief description.
+     * @return a description of the element type.
+     * @since Ant 1.7
+     */
+    public static String getElementName(Project p, Object o, boolean brief) {
+        if (p == null) {
+            p = getProject(o);
+        }
+        return p == null ? getUnmappedElementName(o.getClass(), brief)
+                : getComponentHelper(p).getElementName(o, brief);
+    }
+
+    private static String getUnmappedElementName(Class c, boolean brief) {
+        if (brief) {
+            String name = c.getName();
+            return name.substring(name.lastIndexOf('.') + 1);
+        }
+        return c.toString();
+    }
+
+    private static Project getProject(Object o) {
+        if (o instanceof ProjectComponent) {
+            return ((ProjectComponent) o).getProject();
+        }
+        try {
+            Method m = o.getClass().getMethod("getProject", null);
+            if (Project.class == m.getReturnType()) {
+                return (Project) m.invoke(o, null);
+            }
+        } catch (Exception e) {
+            //too bad
+        }
+        return null;
     }
 
     /**

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/DataType.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/DataType.java?view=diff&rev=477969&r1=477968&r2=477969
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/DataType.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/DataType.java Tue Nov 21 15:49:59 2006
@@ -120,13 +120,7 @@
      * @return <code>String</code> name.
      */
     protected String getDataTypeName() {
-        Project p = getProject();
-        if (p != null) {
-            return ComponentHelper.getComponentHelper(p)
-                .getElementName(this, true);
-        }
-        String classname = getClass().getName();
-        return classname.substring(classname.lastIndexOf('.') + 1);
+        return ComponentHelper.getElementName(getProject(), this, true);
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message