camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r826381 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/IntrospectionSupport.java test/java/org/apache/camel/util/IntrospectionSupportTest.java test/java/org/apache/camel/util/OtherExampleBean.java
Date Sun, 18 Oct 2009 09:19:41 GMT
Author: davsclaus
Date: Sun Oct 18 09:19:40 2009
New Revision: 826381

URL: http://svn.apache.org/viewvc?rev=826381&view=rev
Log:
CAMEL-2074: Added getter/setter checker methods to introspection support

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java   (with
props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java?rev=826381&r1=826380&r2=826381&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java Sun
Oct 18 09:19:40 2009
@@ -18,19 +18,17 @@
 
 import java.beans.PropertyEditor;
 import java.beans.PropertyEditorManager;
-import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.TypeConverter;
@@ -43,6 +41,8 @@
 public final class IntrospectionSupport {
 
     private static final transient Log LOG = LogFactory.getLog(IntrospectionSupport.class);
+    private static final Pattern GETTER_PATTERN = Pattern.compile("(get|is)[A-Z].*");
+    private static final Pattern SETTER_PATTERN = Pattern.compile("set[A-Z].*");
 
     /**
      * Utility classes should not have a public constructor.
@@ -50,6 +50,35 @@
     private IntrospectionSupport() {
     }
 
+    public static boolean isGetter(Method method) {
+        String name = method.getName();
+        Class type = method.getReturnType();
+        Class params[] = method.getParameterTypes();
+
+        if (!GETTER_PATTERN.matcher(name).matches()) {
+            return false;
+        }
+
+        // special for isXXX boolean
+        if (name.startsWith("is")) {
+            return params.length == 0 && type.getSimpleName().equalsIgnoreCase("boolean");
+        }
+
+        return params.length == 0 && !type.equals(Void.TYPE);
+    }
+
+    public static boolean isSetter(Method method) {
+        String name = method.getName();
+        Class type = method.getReturnType();
+        Class params[] = method.getParameterTypes();
+
+        if (!SETTER_PATTERN.matcher(name).matches()) {
+            return false;
+        }
+
+        return params.length == 1 && type.equals(Void.TYPE);
+    }
+
     @SuppressWarnings("unchecked")
     public static boolean getProperties(Object target, Map properties, String optionPrefix)
{
         ObjectHelper.notNull(target, "target");

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java?rev=826381&r1=826380&r2=826381&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
Sun Oct 18 09:19:40 2009
@@ -135,5 +135,57 @@
         assertEquals("getName", name.getName());
     }
 
+    public void testIsGetter() throws Exception {
+        ExampleBean bean = new ExampleBean();
+
+        Method name = bean.getClass().getMethod("getName", null);
+        assertEquals(true, IntrospectionSupport.isGetter(name));
+        assertEquals(false, IntrospectionSupport.isSetter(name));
+
+        Method price = bean.getClass().getMethod("getPrice", null);
+        assertEquals(true, IntrospectionSupport.isGetter(price));
+        assertEquals(false, IntrospectionSupport.isSetter(price));
+    }
+
+    public void testIsSetter() throws Exception {
+        ExampleBean bean = new ExampleBean();
+
+        Method name = bean.getClass().getMethod("setName", String.class);
+        assertEquals(false, IntrospectionSupport.isGetter(name));
+        assertEquals(true, IntrospectionSupport.isSetter(name));
+
+        Method price = bean.getClass().getMethod("setPrice", double.class);
+        assertEquals(false, IntrospectionSupport.isGetter(price));
+        assertEquals(true, IntrospectionSupport.isSetter(price));
+    }
+
+    public void testOtherIsGetter() throws Exception {
+        OtherExampleBean bean = new OtherExampleBean();
+
+        Method goldCustomer = bean.getClass().getMethod("isGoldCustomer", null);
+        assertEquals(true, IntrospectionSupport.isGetter(goldCustomer));
+        assertEquals(false, IntrospectionSupport.isSetter(goldCustomer));
+
+        Method silverCustomer = bean.getClass().getMethod("isSilverCustomer", null);
+        assertEquals(true, IntrospectionSupport.isGetter(silverCustomer));
+        assertEquals(false, IntrospectionSupport.isSetter(silverCustomer));
+
+        Method customerId = bean.getClass().getMethod("getCustomerId", null);
+        assertEquals(true, IntrospectionSupport.isGetter(customerId));
+        assertEquals(false, IntrospectionSupport.isSetter(customerId));
+
+        Method company = bean.getClass().getMethod("getCompany", null);
+        assertEquals(true, IntrospectionSupport.isGetter(company));
+        assertEquals(false, IntrospectionSupport.isSetter(company));
+
+        Method setupSomething = bean.getClass().getMethod("setupSomething", Object.class);
+        assertEquals(false, IntrospectionSupport.isGetter(setupSomething));
+        assertEquals(false, IntrospectionSupport.isSetter(setupSomething));
+    }
+
+    public void testOtherIsSetter() throws Exception {
+        OtherExampleBean bean = new OtherExampleBean();
+    }
+
 }
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java?rev=826381&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java Sun Oct
18 09:19:40 2009
@@ -0,0 +1,64 @@
+/**
+ * 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.camel.util;
+
+/**
+ * @version $Revision$
+ */
+public class OtherExampleBean {
+
+    private boolean goldCustomer;
+    private Boolean silverCustomer;
+    private int customerId;
+    private String company;
+
+    public boolean isGoldCustomer() {
+        return goldCustomer;
+    }
+
+    public void setGoldCustomer(boolean goldCustomer) {
+        this.goldCustomer = goldCustomer;
+    }
+
+    public int getCustomerId() {
+        return customerId;
+    }
+
+    public Boolean isSilverCustomer() {
+        return silverCustomer;
+    }
+
+    public void setSilverCustomer(Boolean silverCustomer) {
+        this.silverCustomer = silverCustomer;
+    }
+
+    public void setCustomerId(int customerId) {
+        this.customerId = customerId;
+    }
+
+    public String getCompany() {
+        return company;
+    }
+
+    public void setCompany(String company) {
+        this.company = company;
+    }
+
+    public void setupSomething(Object value) {
+        // noop
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/OtherExampleBean.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message