You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/11/29 06:48:56 UTC

svn commit: r599283 - /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java

Author: nbubna
Date: Wed Nov 28 21:48:53 2007
New Revision: 599283

URL: http://svn.apache.org/viewvc?rev=599283&view=rev
Log:
make inspect() respect safeMode better, also add various other new features

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java?rev=599283&r1=599282&r2=599283&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ClassTool.java Wed Nov 28 21:48:53 2007
@@ -202,6 +202,9 @@
      * the specified {@link Class}.  If the specified class
      * is null, then this will return {@code null}. All other
      * configuration settings will be copied to the new instance.
+     * If safeMode is set to {@code true} and the specified Class
+     * is not declared {@code public}, then this will return
+     * {@code null}.
      */
     public ClassTool inspect(Class type)
     {
@@ -209,7 +212,14 @@
         {
             return null;
         }
-        return new ClassTool(this, type);
+        // create the new tool, but only return it if
+        // it is public or safeMode is off
+        ClassTool tool = new ClassTool(this, type);
+        if (this.safeMode && !tool.isPublic())
+        {
+            return null;
+        }
+        return tool;
     }
 
     /**
@@ -457,6 +467,11 @@
         }
     }
 
+    public String toString()
+    {
+        return getType().toString();
+    }
+
 
 
     /**
@@ -487,6 +502,14 @@
             return field.getName();
         }
 
+        /**
+         * Simply returns the name of the field.
+         */
+        public String getJavadocRef()
+        {
+            return field.getName();
+        }
+
         public Class getType()
         {
             return field.getType();
@@ -517,6 +540,11 @@
         {
             return field.getModifiers();
         }
+
+        protected String getSubType()
+        {
+            return "field";
+        }
     }
 
     /**
@@ -555,6 +583,11 @@
         {
             return constructor.getModifiers();
         }
+
+        protected String getSubType()
+        {
+            return "constructor";
+        }
     }
 
     /**
@@ -576,6 +609,52 @@
         }
 
         /**
+         * If this method can be treated as a bean property in Velocity
+         * (which does not exactly follow the javabean spec for such things)
+         * then it will return the "bean property" equivalent of the method name.
+         * (e.g. for getFoo(), isFoo() or setFoo(foo) it will return "foo")
+         */
+        public String getPropertyName()
+        {
+            String name = getName();
+            switch (getParameterCount())
+            {
+                case 0:
+                    if (name.startsWith("get") && name.length() > 3)
+                    {
+                        return uncapitalize(name.substring(3, name.length()));
+                    }
+                    else if (name.startsWith("is") && name.length() > 2)
+                    {
+                        return uncapitalize(name.substring(2, name.length()));
+                    }
+                    break;
+                case 1:
+                    if (name.startsWith("set") && name.length() > 3)
+                    {
+                        return uncapitalize(name.substring(3, name.length()));
+                    }
+                default:
+            }
+            return null;
+        }
+
+        private String uncapitalize(String string)
+        {
+            if (string.length() > 1)
+            {
+                StringBuilder out = new StringBuilder(string.length());
+                out.append(string.substring(0,1).toLowerCase());
+                out.append(string.substring(1, string.length()));
+                return out.toString();
+            }
+            else
+            {
+                return string.toLowerCase();
+            }
+        }
+
+        /**
          * Returns true if the final parameter for the method was declared
          * as a vararg.
          */
@@ -606,14 +685,26 @@
         {
             return method.getModifiers();
         }
+
+        protected String getSubType()
+        {
+            return "method";
+        }
     }
 
     public abstract static class CallableSub<T extends CallableSub> extends Sub<T>
     {
         protected String uniqueName;
+        protected String javadocRef;
+        protected String signature;
 
         public abstract Class[] getParameters();
 
+        public boolean takesParameters()
+        {
+            return (getParameterCount() > 0);
+        }
+
         /**
          * Returns the number of expected parameters. If this method or
          * constructor is declared with varargs, the vararg only counts as one.
@@ -661,16 +752,90 @@
             }
             return uniqueName;
         }
+
+        public String getSignature()
+        {
+            if (signature == null)
+            {
+                signature = signature(false);
+            }
+            return signature;
+        }
+
+        public String getJavadocRef()
+        {
+            if (javadocRef == null)
+            {
+                javadocRef = signature(true);
+            }
+            return javadocRef;
+        }
+
+        protected String signature(boolean fullNames)
+        {
+            Class[] params = getParameters();
+            if (params.length == 0)
+            {
+                return getName() + "()";
+            }
+            else
+            {
+                StringBuilder out = new StringBuilder(30);
+                out.append(getName());
+                out.append('(');
+                boolean first = true;
+                for (Class param : params)
+                {
+                    if (first)
+                    {
+                        first = false;
+                    }
+                    else
+                    {
+                        out.append(',');
+                    }
+                    if (param.isArray())
+                    {
+                        if (fullNames)
+                        {
+                            out.append(param.getComponentType().getName());
+                        }
+                        else
+                        {
+                            out.append(param.getComponentType().getSimpleName());
+                        }
+                        out.append("[]");
+                    }
+                    else
+                    {
+                        if (fullNames)
+                        {
+                            out.append(param.getName());
+                        }
+                        else
+                        {
+                            out.append(param.getSimpleName());
+                        }
+                    }
+                }
+                out.append(')');
+                return out.toString();
+            }
+        }
     }
 
     public abstract static class Sub<T extends Sub> implements Comparable<T>
     {
         protected abstract int getModifiers();
 
+        protected abstract String getSubType();
+
         public abstract String getName();
 
         public abstract String getUniqueName();
 
+        public abstract String getJavadocRef();
+
         public boolean isPublic()
         {
             return Modifier.isPublic(getModifiers());
@@ -749,6 +914,11 @@
                 return this.getUniqueName().equals(that.getUniqueName());
             }
             return false;
+        }
+
+        public String toString()
+        {
+            return getSubType() + ' ' + getJavadocRef();
         }
     }