Repository: ignite
Updated Branches:
  refs/heads/master 11d8b6687 -> 712733030


IGNITE-7193 IgniteReflectionFactory must handle primitive data types - Fixes 
#3495.

Signed-off-by: Alexey Goncharuk <alexey.goncha...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/71273303
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/71273303
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/71273303

Branch: refs/heads/master
Commit: 7127330300b0b62e2724da074b8f1c53b9ab3be9
Parents: 11d8b66
Author: sk0x50 <slava.kopti...@gmail.com>
Authored: Thu Feb 22 18:22:56 2018 +0300
Committer: Alexey Goncharuk <alexey.goncha...@gmail.com>
Committed: Thu Feb 22 18:22:56 2018 +0300

----------------------------------------------------------------------
 .../configuration/IgniteReflectionFactory.java  |  43 +++-
 .../IgniteReflectionFactorySelfTest.java        | 235 +++++++++++++++++++
 .../testsuites/IgniteCacheTestSuite2.java       |   3 +
 3 files changed, 278 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/71273303/modules/core/src/main/java/org/apache/ignite/configuration/IgniteReflectionFactory.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/configuration/IgniteReflectionFactory.java
 
b/modules/core/src/main/java/org/apache/ignite/configuration/IgniteReflectionFactory.java
index c2feb57..85b86c3 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/configuration/IgniteReflectionFactory.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/configuration/IgniteReflectionFactory.java
@@ -248,9 +248,46 @@ public class IgniteReflectionFactory<T> implements 
Factory<T> {
 
         Class paramCls = val.getClass();
 
+        String mtdName = sb.toString();
+
+        boolean res = setWithMethod(obj, mtdName, val, paramCls);
+
+        if (!res) {
+            if (Byte.class == paramCls)
+                paramCls = byte.class;
+            else if (Short.class == paramCls)
+                paramCls = short.class;
+            else if (Integer.class == paramCls)
+                paramCls = int.class;
+            else if (Long.class == paramCls)
+                paramCls = long.class;
+            else if (Float.class == paramCls)
+                paramCls = float.class;
+            else if (Double.class == paramCls)
+                paramCls = double.class;
+            else if (Character.class == paramCls)
+                paramCls = char.class;
+            else if (Boolean.class == paramCls)
+                paramCls = boolean.class;
+            else
+                return false;
+
+            res = setWithMethod(obj, mtdName, val, paramCls);
+        }
+        return res;
+    }
+
+    /**
+     * @param obj Object to initialize with properties.
+     * @param mtdName Method name.
+     * @param val Value to set.
+     * @param paramCls Value class.
+     * @return {@code True} if property was set.
+     */
+    private boolean setWithMethod(T obj, String mtdName, Serializable val, 
Class paramCls) {
         while (paramCls != null) {
             try {
-                Method mtd = obj.getClass().getMethod(sb.toString(), paramCls);
+                Method mtd = obj.getClass().getMethod(mtdName, paramCls);
 
                 mtd.invoke(obj, val);
 
@@ -268,7 +305,7 @@ public class IgniteReflectionFactory<T> implements 
Factory<T> {
         // Try interfaces.
         for (Class<?> itf : val.getClass().getInterfaces()) {
             try {
-                Method mtd = obj.getClass().getMethod(sb.toString(), itf);
+                Method mtd = obj.getClass().getMethod(mtdName, itf);
 
                 mtd.invoke(obj, val);
 
@@ -284,4 +321,4 @@ public class IgniteReflectionFactory<T> implements 
Factory<T> {
 
         return false;
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/71273303/modules/core/src/test/java/org/apache/ignite/internal/IgniteReflectionFactorySelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/IgniteReflectionFactorySelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/IgniteReflectionFactorySelfTest.java
new file mode 100644
index 0000000..899728c
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/IgniteReflectionFactorySelfTest.java
@@ -0,0 +1,235 @@
+/*
+ * 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.ignite.internal;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import junit.framework.TestCase;
+import org.apache.ignite.configuration.IgniteReflectionFactory;
+
+/**
+ * Tests for {@link IgniteReflectionFactory} class.
+ */
+public class IgniteReflectionFactorySelfTest extends TestCase {
+    /**
+     * @throws Exception If failed.
+     */
+    public void testByteMethod() throws Exception {
+        byte    expByteVal    = 42;
+        short   expShortVal   = 42;
+        int     expIntVal     = 42;
+        long    expLongVal    = 42L;
+        float   expFloatVal   = 42.0f;
+        double  expDoubleVal  = 42.0d;
+        char    expCharVal    = 'z';
+        boolean expBooleanVal = true;
+
+        Map<String, Serializable> props = new HashMap<>();
+
+        props.put("byteField", expByteVal);
+        props.put("shortField", expShortVal);
+        props.put("intField", expIntVal);
+        props.put("longField", expLongVal);
+        props.put("floatField", expFloatVal);
+        props.put("doubleField", expDoubleVal);
+        props.put("charField", expCharVal);
+        props.put("booleanField", expBooleanVal);
+
+        IgniteReflectionFactory<TestClass> factory = new 
IgniteReflectionFactory<>(TestClass.class);
+
+        factory.setProperties(props);
+
+        TestClass instance = factory.create();
+
+        assertEquals(expByteVal, instance.getByteField());
+        assertEquals(expShortVal, instance.getShortField());
+        assertEquals(expIntVal, instance.getIntField());
+        assertEquals(expLongVal, instance.getLongField());
+        assertEquals(expFloatVal, instance.getFloatField());
+        assertEquals(expDoubleVal, instance.getDoubleField());
+        assertEquals(expCharVal, instance.getCharField());
+        assertEquals(expBooleanVal, instance.getBooleanField());
+    }
+
+    /**
+     * Test class that is created by {@link IgniteReflectionFactory}.
+     */
+    public static class TestClass {
+        /** */
+        private byte byteField;
+        /** */
+        private short shortField;
+        /** */
+        private int iField;
+        /** */
+        private long longField;
+        /** */
+        private float floatField;
+        /** */
+        private double doubleField;
+        /** */
+        private char cField;
+        /** */
+        private boolean booleanField;
+
+        /**
+         * Returns byte field.
+         *
+         * @return Byte field.
+         */
+        public byte getByteField() {
+            return byteField;
+        }
+
+        /**
+         * Sets byte field.
+         *
+         * @param byteField New field value.
+         */
+        public void setByteField(byte byteField) {
+            this.byteField = byteField;
+        }
+
+        /**
+         * Returns short field.
+         *
+         * @return Short field.
+         */
+        public short getShortField() {
+            return shortField;
+        }
+
+        /**
+         * Sets short field.
+         *
+         * @param shortField New field value.
+         */
+        public void setShortField(short shortField) {
+            this.shortField = shortField;
+        }
+
+        /**
+         * Returns long field.
+         *
+         * @return Long field.
+         */
+        public long getLongField() {
+            return longField;
+        }
+
+        /**
+         * Sets long field.
+         *
+         * @param longField New field value.
+         */
+        public void setLongField(long longField) {
+            this.longField = longField;
+        }
+
+        /**
+         * Returns float field.
+         *
+         * @return Float field.
+         */
+        public float getFloatField() {
+            return floatField;
+        }
+
+        /**
+         * Sets float field.
+         *
+         * @param floatField New field value.
+         */
+        public void setFloatField(float floatField) {
+            this.floatField = floatField;
+        }
+
+        /**
+         * Returns double field.
+         *
+         * @return Double field.
+         */
+        public double getDoubleField() {
+            return doubleField;
+        }
+
+        /**
+         * Sets double field.
+         *
+         * @param doubleField New field value.
+         */
+        public void setDoubleField(double doubleField) {
+            this.doubleField = doubleField;
+        }
+
+        /**
+         * Returns char field.
+         *
+         * @return Char field.
+         */
+        public char getCharField() {
+            return cField;
+        }
+
+        /**
+         * Sets char field.
+         *
+         * @param cField New field value.
+         */
+        public void setCharField(char cField) {
+            this.cField = cField;
+        }
+
+        /**
+         * Returns boolean field.
+         *
+         * @return Boolean field.
+         */
+        public boolean getBooleanField() {
+            return booleanField;
+        }
+
+        /**
+         * Sets boolean field.
+         *
+         * @param booleanField New field value.
+         */
+        public void setBooleanField(boolean booleanField) {
+            this.booleanField = booleanField;
+        }
+
+        /**
+         * Returns int field.
+         *
+         * @return Int field.
+         */
+        public int getIntField() {
+            return iField;
+        }
+
+        /**
+         * Sets int field.
+         *
+         * @param iField New field value.
+         */
+        public void setIntField(int iField) {
+            this.iField = iField;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/71273303/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
index 65cbcc0..055f610 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
@@ -22,6 +22,7 @@ import 
org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunctionBac
 import 
org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunctionExcludeNeighborsSelfTest;
 import 
org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest;
 import 
org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunctionStandardHashSelfTest;
+import org.apache.ignite.internal.IgniteReflectionFactorySelfTest;
 import org.apache.ignite.internal.processors.cache.CacheComparatorTest;
 import 
org.apache.ignite.internal.processors.cache.CacheConcurrentReadThroughTest;
 import org.apache.ignite.internal.processors.cache.CacheConfigurationLeakTest;
@@ -297,6 +298,8 @@ public class IgniteCacheTestSuite2 extends TestSuite {
 
         suite.addTest(new TestSuite(CacheComparatorTest.class));
 
+        suite.addTest(new TestSuite(IgniteReflectionFactorySelfTest.class));
+
         return suite;
     }
 }

Reply via email to