cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r674914 - in /cxf/trunk: buildtools/pom.xml buildtools/src/main/java/org/apache/cxf/pmd/ buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java buildtools/src/main/resources/cxf-pmd-custom.xml parent/pom.xml
Date Tue, 08 Jul 2008 19:03:10 GMT
Author: bimargulies
Date: Tue Jul  8 12:03:10 2008
New Revision: 674914

URL: http://svn.apache.org/viewvc?rev=674914&view=rev
Log:
Add a PMD rule that catches String operations that use the default 
charset. These can lead to trouble when users use funny charsets.

Custom rules like this don't work on Eclipse, so this is not enabled
for the PMD plugin.

Added:
    cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/
    cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
  (with props)
    cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml   (with props)
Modified:
    cxf/trunk/buildtools/pom.xml
    cxf/trunk/parent/pom.xml

Modified: cxf/trunk/buildtools/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/buildtools/pom.xml?rev=674914&r1=674913&r2=674914&view=diff
==============================================================================
--- cxf/trunk/buildtools/pom.xml (original)
+++ cxf/trunk/buildtools/pom.xml Tue Jul  8 12:03:10 2008
@@ -80,6 +80,11 @@
             <version>1.0-alpha-15</version>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>pmd</groupId>
+            <artifactId>pmd</artifactId>
+            <version>4.2</version>
+        </dependency>
     </dependencies>
 
     <!--build>

Added: cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
URL: http://svn.apache.org/viewvc/cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java?rev=674914&view=auto
==============================================================================
--- cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
(added)
+++ cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
Tue Jul  8 12:03:10 2008
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.pmd;
+
+import java.util.List;
+
+import net.sourceforge.pmd.AbstractJavaRule;
+import net.sourceforge.pmd.RuleContext;
+import net.sourceforge.pmd.ast.ASTAdditiveExpression;
+import net.sourceforge.pmd.ast.ASTAllocationExpression;
+import net.sourceforge.pmd.ast.ASTArgumentList;
+import net.sourceforge.pmd.ast.ASTArrayDimsAndInits;
+import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
+import net.sourceforge.pmd.ast.ASTExpression;
+import net.sourceforge.pmd.ast.ASTName;
+import net.sourceforge.pmd.ast.Node;
+import net.sourceforge.pmd.ast.SimpleNode;
+import net.sourceforge.pmd.symboltable.NameDeclaration;
+import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
+import net.sourceforge.pmd.typeresolution.TypeHelper;
+
+/**
+ * Look for new String(byte[]) or new String(byte[], start, end)
+ * and complain.
+ */
+public class UnsafeStringConstructorRule extends AbstractJavaRule {
+
+    /** {@inheritDoc} */
+    @Override
+    public Object visit(ASTAllocationExpression node, Object data) {
+        if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
+            return data;
+        }
+
+        if (!TypeHelper.isA((ASTClassOrInterfaceType)node.jjtGetChild(0), String.class))
{
+            return data;
+        }
+        
+        ASTArgumentList arglist = node.getFirstChildOfType(ASTArgumentList.class);
+        if (arglist == null) { // unlikely
+            return data;
+        }
+        
+        // one of the two possibilities ...
+        if (arglist.jjtGetNumChildren() == 1 || arglist.jjtGetNumChildren() == 3) {
+            ASTExpression firstArgExpr = arglist.getFirstChildOfType(ASTExpression.class);
+            Class<?> exprType = firstArgExpr.getType();
+            // pmd reports the type as byte, not byte[]. But since
+            // there is no such thing as new String(byte), it seems
+            // safe enough to take that as good enough.
+            if (exprType != null) {
+                if (exprType == Byte.TYPE || 
+                    (exprType.isArray() && exprType.getComponentType() == Byte.TYPE))
{
+                    addViolation(data, node);
+                }
+            }
+        }
+        return data;
+
+    }
+
+}

Propchange: cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/buildtools/src/main/java/org/apache/cxf/pmd/UnsafeStringConstructorRule.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml?rev=674914&view=auto
==============================================================================
--- cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml (added)
+++ cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml Tue Jul  8 12:03:10 2008
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements. See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership. The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License. You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied. See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<ruleset name="pmd-eclipse">
+    <description>PMD Plugin rules with custom Javascript.</description>
+     <rule name="AvoidUnsafeStringConstructor"
+                  message="Don't use new String(byte[]...) without an encoding."
+                  class="org.apache.cxf.pmd.UnsafeStringConstructorRule">
+              <description>
+              Don't use new String(byte[]) or new String(byte[], int, int).
+              </description>
+                <priority>3</priority>
+
+              <example>
+        <![CDATA[
+            public String doSomething() {
+              return new String(byte[]);
+            }
+        ]]>
+              </example>
+            </rule>
+</ruleset>

Propchange: cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/buildtools/src/main/resources/cxf-pmd-custom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: cxf/trunk/parent/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/parent/pom.xml?rev=674914&r1=674913&r2=674914&view=diff
==============================================================================
--- cxf/trunk/parent/pom.xml (original)
+++ cxf/trunk/parent/pom.xml Tue Jul  8 12:03:10 2008
@@ -216,6 +216,7 @@
                     <configuration>
                         <rulesets>
                             <ruleset>cxf-pmd-ruleset.xml</ruleset>
+                            <ruleset>cxf-pmd-custom.xml</ruleset>
                         </rulesets>
                         <targetJdk>1.5</targetJdk>
                         <linkXRef>false</linkXRef>



Mime
View raw message