├── .gitignore
├── LICENSE
├── README.md
├── faster-than-reflection-benchmark
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── github
│ └── javalbert
│ ├── FasterThanReflectionBenchmark.java
│ └── Foo.java
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── github
│ └── javalbert
│ ├── bytecode
│ └── utils
│ │ └── AsmUtils.java
│ └── reflection
│ ├── AccessClassLoader.java
│ ├── ClassAccess.java
│ ├── ClassAccessFactory.java
│ ├── FieldAccess.java
│ ├── MethodAccess.java
│ └── PropertyAccess.java
└── test
└── java
└── com
└── github
└── javalbert
└── reflection
└── test
├── ClassAccessCommonReferenceTypesTest.java
├── FieldAccessBoxedPrimitivesTest.java
├── FieldAccessGeneralTest.java
├── FieldAccessPrimitivesTest.java
├── Foo.java
├── FooFactory.java
├── Main.java
├── MethodAccessCallTest.java
├── MethodAccessInvokeTest.java
├── PropertyAccessBoxedPrimitivesTest.java
├── PropertyAccessGeneralTest.java
└── PropertyAccessPrimitivesTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings/
2 | /faster-than-reflection-benchmark/.settings/
3 | /faster-than-reflection-benchmark/target/
4 | /faster-than-reflection-benchmark/.classpath
5 | /faster-than-reflection-benchmark/.project
6 | /target/
7 | /.classpath
8 | /.project
9 | /dependency-reduced-pom.xml
10 | /hs_err_pid*
11 | /replay_pid*
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FasterThanReflection
2 |
3 | Mainly inspired by [ReflectASM library](https://github.com/EsotericSoftware/reflectasm) and a desire to provide faster performance in [SqlbuilderORM library](https://github.com/Javalbert/sql-builder-orm). Many ORMs use bytecode generation to generate classes that can set the fields of different entities, which is faster than using the Reflection API.
4 |
5 | ## Design Principles
6 | - Get and set fields directly (including private fields)
7 | - Get and set via JavaBean properties
8 | - Methods for getting and setting primitive fields or properties
9 | - Methods for getting and setting boxed versions of primitive fields or properties
10 | - Methods for getting and setting fields or properties of common classes e.g. String, Date, BigDecimal
11 | - 23 Methods for calling methods with 0 to 22 parameters (no expensive varargs creation)
12 | - Method that accepts Object varargs for calling any method
13 | - Not a reinvention of Reflection API (but faster field, property and method access)
14 | - Uses `new ClassWriter(0)` for best performance, instead of `new ClassWriter(ClassWriter.COMPUTE_MAXS)` (10% slower) or `new ClassWriter(ClassWriter.COMPUTE_FRAMES)` (2x slower)
15 |
16 | ## Installation
17 |
18 | Maven
19 |
20 | ```xml
21 |
22 | com.github.javalbert
23 | faster-than-reflection
24 | 1.0.0
25 |
26 | ```
27 |
28 | ## Usage
29 |
30 | Retrieve an instance of ClassAccess.
31 |
32 | ```java
33 | ClassAccess classAccess = ClassAccessFactory.get(Foo.class);
34 | ```
35 |
36 | ClassAccess interface extends interfaces: FieldAccess, PropertyAccess, and MethodAccess.
37 |
38 | ```java
39 | FieldAccess fieldAccess = ClassAccessFactory.get(Foo.class);
40 | PropertyAccess propertyAccess = ClassAccessFactory.get(Foo.class);
41 | MethodAccess methodAccess = ClassAccessFactory.get(Foo.class);
42 | ```
43 |
44 | Get and set fields or properties
45 |
46 | ```java
47 | // Instance of a class whose fields or properties you want to get or set
48 | Foo foo = new Foo();
49 |
50 | /* Fields */
51 |
52 | int intValFieldIndex = fieldAccess.fieldIndex("intVal");
53 |
54 | fieldAccess.getIntField(foo, intValFieldIndex); // i.e. foo.intVal
55 | fieldAccess.setIntField(foo, intValFieldIndex, 1); // i.e. foo.intVal = 1
56 |
57 | /* Properties */
58 |
59 | int intValPropertyIndex = propertyAccess.propertyIndex("intVal");
60 |
61 | propertyAccess.getIntProperty(foo, intValPropertyIndex); // i.e. foo.getIntVal()
62 | propertyAccess.setIntProperty(foo, intValPropertyIndex, 2); // i.e. foo.setIntVal(2);
63 | ```
64 |
65 | Call methods
66 |
67 | ```java
68 | int methodIndex = methodAccess.methodIndex("newInstance");
69 |
70 | // Call a method that has 2 parameters
71 | methodAccess.call(fooFactory, methodIndex, true, 1337); // i.e. fooFactory.newInstance(true, 1337)
72 |
73 | // Or use invoke for methods that have more than 22 parameters
74 | methodAccess.invoke(fooFactory, someCrazyMethod, (Object[])lotsOfVarargs);
75 | ```
76 |
77 | ## Performance
78 |
79 | JMH benchmark code can be found in [faster-than-reflection-benchmark](faster-than-reflection-benchmark) folder in the root of this project.
80 |
81 | Test environment: Intel Core i7-4790k, Windows 10 x64 + High Performance Power Plan, Java version "1.8.0_121" Java Hotspot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
82 |
83 | | Benchmark | Mode | Samples | Score | Score error | Units |
84 | |---|---|---:|---:|---:|---|
85 | | testFieldAccessDirect | avgt | 200 | 0.686 | 0.001 | ns/op |
86 | | testFieldAccessFasterThanReflection | avgt | 200 | 1.028 | 0.002 | ns/op |
87 | | testFieldAccessReflectAsm | avgt | 200 | 0.912 | 0.001 | ns/op |
88 | | testFieldAccessReflectionApi | avgt | 200 | 2.901 | 0.002 | ns/op |
89 | | testPropertyAccessDirect | avgt | 200 | 0.685 | 0.001 | ns/op |
90 | | testPropertyAccessFasterThanReflection | avgt | 200 | 1.027 | 0.002 | ns/op |
91 | | testPropertyAccessReflectAsm | avgt | 200 | 1.301 | 0.025 | ns/op |
92 | | testPropertyAccessReflectionApi | avgt | 200 | 4.026 | 0.006 | ns/op |
93 |
96 |
97 | ## Contributing
98 |
99 | This is a one-man project but feel free to fork.
100 |
101 | ## Acknowledgments
102 |
103 | * [ReflectASM](https://github.com/EsotericSoftware/reflectasm), main inspiration, AccessClassLoader.java
104 | * dimzon's [fork](https://github.com/dimzon/reflectasm) of ReflectASM, where I figured out how his changes allow access to private class members (which is a limitation of ReflectASM)
--------------------------------------------------------------------------------
/faster-than-reflection-benchmark/pom.xml:
--------------------------------------------------------------------------------
1 |
13 |
15 | 4.0.0
16 |
17 | com.github.javalbert
18 | faster-than-reflection-benchmark
19 | 0.1.0
20 | jar
21 |
22 | FasterThanReflection JMH benchmark
23 |
24 |
25 | 3.0
26 |
27 |
28 |
29 |
30 | org.openjdk.jmh
31 | jmh-core
32 | ${jmh.version}
33 |
34 |
35 | org.openjdk.jmh
36 | jmh-generator-annprocess
37 | ${jmh.version}
38 | provided
39 |
40 |
41 | com.esotericsoftware
42 | reflectasm
43 | ${reflectasm.version}
44 | shaded
45 |
46 |
47 | org.ow2.asm
48 | asm
49 |
50 |
51 |
52 |
53 | com.github.javalbert
54 | faster-than-reflection
55 | ${faster-than-reflection.version}
56 |
57 |
58 | org.slf4j
59 | slf4j-simple
60 | ${slf4j.version}
61 |
62 |
63 |
64 |
65 | UTF-8
66 | 1.0
67 | 1.8
68 | benchmarks
69 | 1.0.0
70 | 1.11.3
71 | 1.7.24
72 |
73 |
74 |
75 |
76 |
77 | org.apache.maven.plugins
78 | maven-compiler-plugin
79 | 3.1
80 |
81 | ${javac.target}
82 | ${javac.target}
83 | ${javac.target}
84 |
85 |
86 |
87 | org.apache.maven.plugins
88 | maven-shade-plugin
89 | 2.2
90 |
91 |
92 | package
93 |
94 | shade
95 |
96 |
97 | ${uberjar.name}
98 |
99 |
100 | org.openjdk.jmh.Main
101 |
102 |
103 |
104 |
105 |
109 | *:*
110 |
111 | META-INF/*.SF
112 | META-INF/*.DSA
113 | META-INF/*.RSA
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 | maven-clean-plugin
126 | 2.5
127 |
128 |
129 | maven-deploy-plugin
130 | 2.8.1
131 |
132 |
133 | maven-install-plugin
134 | 2.5.1
135 |
136 |
137 | maven-jar-plugin
138 | 2.4
139 |
140 |
141 | maven-javadoc-plugin
142 | 2.9.1
143 |
144 |
145 | maven-resources-plugin
146 | 2.6
147 |
148 |
149 | maven-site-plugin
150 | 3.3
151 |
152 |
153 | maven-source-plugin
154 | 2.2.1
155 |
156 |
157 | maven-surefire-plugin
158 | 2.17
159 |
160 |
161 |
162 |
163 |
164 |
165 |
--------------------------------------------------------------------------------
/faster-than-reflection-benchmark/src/main/java/com/github/javalbert/FasterThanReflectionBenchmark.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert;
14 |
15 | import java.lang.reflect.Field;
16 | import java.lang.reflect.InvocationTargetException;
17 | import java.lang.reflect.Method;
18 | import java.util.concurrent.TimeUnit;
19 |
20 | import org.openjdk.jmh.annotations.Benchmark;
21 | import org.openjdk.jmh.annotations.BenchmarkMode;
22 | import org.openjdk.jmh.annotations.Level;
23 | import org.openjdk.jmh.annotations.Mode;
24 | import org.openjdk.jmh.annotations.OutputTimeUnit;
25 | import org.openjdk.jmh.annotations.Scope;
26 | import org.openjdk.jmh.annotations.Setup;
27 | import org.openjdk.jmh.annotations.State;
28 |
29 | import com.github.javalbert.reflection.ClassAccessFactory;
30 | import com.github.javalbert.reflection.FieldAccess;
31 | import com.github.javalbert.reflection.PropertyAccess;
32 |
33 | public class FasterThanReflectionBenchmark {
34 | /* START Field access */
35 |
36 | @State(Scope.Thread)
37 | public static class FieldAccessDirectState {
38 | public Foo foo = new Foo();
39 | }
40 |
41 | @State(Scope.Thread)
42 | public static class FieldAccessFasterThanReflectionState {
43 | public Foo foo = new Foo();
44 | public FieldAccess fieldAccess;
45 | public int fieldIndex;
46 |
47 | @Setup(Level.Trial)
48 | public void doSetup() {
49 | fieldAccess = ClassAccessFactory.get(Foo.class);
50 | fieldIndex = fieldAccess.fieldIndex("intVal");
51 | }
52 | }
53 |
54 | @State(Scope.Thread)
55 | public static class FieldAccessReflectAsmState {
56 | public Foo foo = new Foo();
57 | public com.esotericsoftware.reflectasm.FieldAccess fieldAccess;
58 | public int fieldIndex;
59 |
60 | @Setup(Level.Trial)
61 | public void doSetup() {
62 | fieldAccess = com.esotericsoftware.reflectasm.FieldAccess.get(Foo.class);
63 | fieldIndex = fieldAccess.getIndex("intVal");
64 | }
65 | }
66 |
67 | @State(Scope.Thread)
68 | public static class FieldAccessReflectionApiState {
69 | public Foo foo = new Foo();
70 | public Field intValField;
71 |
72 | @Setup(Level.Trial)
73 | public void doSetup() {
74 | try {
75 | intValField = Foo.class.getDeclaredField("intVal");
76 | intValField.setAccessible(true);
77 | } catch (NoSuchFieldException | SecurityException e) {
78 | throw new RuntimeException(e);
79 | }
80 | }
81 | }
82 |
83 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
84 | @BenchmarkMode(Mode.AverageTime)
85 | @Benchmark
86 | public int testFieldAccessDirect(FieldAccessDirectState state) {
87 | return state.foo.intVal;
88 | }
89 |
90 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
91 | @BenchmarkMode(Mode.AverageTime)
92 | @Benchmark
93 | public int testFieldAccessFasterThanReflection(FieldAccessFasterThanReflectionState state) {
94 | return state.fieldAccess.getIntField(state.foo, state.fieldIndex);
95 | }
96 |
97 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
98 | @BenchmarkMode(Mode.AverageTime)
99 | @Benchmark
100 | public int testFieldAccessReflectAsm(FieldAccessReflectAsmState state) {
101 | return state.fieldAccess.getInt(state.foo, state.fieldIndex);
102 | }
103 |
104 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
105 | @BenchmarkMode(Mode.AverageTime)
106 | @Benchmark
107 | public int testFieldAccessReflectionApi(FieldAccessReflectionApiState state)
108 | throws IllegalArgumentException, IllegalAccessException {
109 | return state.intValField.getInt(state.foo);
110 | }
111 |
112 | /* END Field access */
113 |
114 | /* START Property access */
115 |
116 | @State(Scope.Thread)
117 | public static class PropertyAccessDirectState {
118 | public Foo foo = new Foo();
119 | }
120 |
121 | @State(Scope.Thread)
122 | public static class PropertyAccessFasterThanReflectionState {
123 | public Foo foo = new Foo();
124 | public PropertyAccess propertyAccess;
125 | public int propertyIndex;
126 |
127 | @Setup(Level.Trial)
128 | public void doSetup() {
129 | propertyAccess = ClassAccessFactory.get(Foo.class);
130 | propertyIndex = propertyAccess.propertyIndex("intVal");
131 | }
132 | }
133 |
134 | @State(Scope.Thread)
135 | public static class PropertyAccessReflectAsmState {
136 | public Foo foo = new Foo();
137 | public com.esotericsoftware.reflectasm.MethodAccess methodAccess;
138 | public int methodIndex;
139 |
140 | @Setup(Level.Trial)
141 | public void doSetup() {
142 | methodAccess = com.esotericsoftware.reflectasm.MethodAccess.get(Foo.class);
143 | methodIndex = methodAccess.getIndex("getIntVal");
144 | }
145 | }
146 |
147 | @State(Scope.Thread)
148 | public static class PropertyAccessReflectionApiState {
149 | public Foo foo = new Foo();
150 | public Method intValAccessorMethod;
151 |
152 | @Setup(Level.Trial)
153 | public void doSetup() {
154 | try {
155 | intValAccessorMethod = Foo.class.getDeclaredMethod("getIntVal");
156 | intValAccessorMethod.setAccessible(true);
157 | } catch (NoSuchMethodException | SecurityException e) {
158 | throw new RuntimeException(e);
159 | }
160 | }
161 | }
162 |
163 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
164 | @BenchmarkMode(Mode.AverageTime)
165 | @Benchmark
166 | public int testPropertyAccessDirect(PropertyAccessDirectState state) {
167 | return state.foo.getIntVal();
168 | }
169 |
170 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
171 | @BenchmarkMode(Mode.AverageTime)
172 | @Benchmark
173 | public int testPropertyAccessFasterThanReflection(PropertyAccessFasterThanReflectionState state) {
174 | return state.propertyAccess.getIntProperty(state.foo, state.propertyIndex);
175 | }
176 |
177 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
178 | @BenchmarkMode(Mode.AverageTime)
179 | @Benchmark
180 | public int testPropertyAccessReflectAsm(PropertyAccessReflectAsmState state) {
181 | return (int)state.methodAccess.invoke(state.foo, state.methodIndex);
182 | }
183 |
184 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
185 | @BenchmarkMode(Mode.AverageTime)
186 | @Benchmark
187 | public int testPropertyAccessReflectionApi(PropertyAccessReflectionApiState state)
188 | throws IllegalAccessException, InvocationTargetException {
189 | return (int)state.intValAccessorMethod.invoke(state.foo);
190 | }
191 |
192 | /* END Property access */
193 | }
194 |
--------------------------------------------------------------------------------
/faster-than-reflection-benchmark/src/main/java/com/github/javalbert/Foo.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert;
14 |
15 | public class Foo {
16 | protected int intVal;
17 |
18 | public int getIntVal() {
19 | return intVal;
20 | }
21 |
22 | public void setIntVal(int intVal) {
23 | this.intVal = intVal;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
13 |
16 | 4.0.0
17 | com.github.javalbert
18 | faster-than-reflection
19 | 1.1.0-SNAPSHOT
20 |
21 |
22 |
23 | Apache License 2.0
24 | https://opensource.org/licenses/Apache-2.0
25 |
26 |
27 |
28 | FasterThanReflection
29 | High performance field, property, and method access using bytecode generation
30 | https://github.com/javalbert/faster-than-reflection
31 |
32 |
33 |
34 | UTF-8
35 |
36 | 5.2
37 | 3.5
38 | 1.8
39 | 3.6.0
40 | 1.6
41 | 2.10.4
42 | 2.4.3
43 | 3.0.1
44 | 1.7.22
45 |
46 |
47 |
48 | 0.60
49 | 0.7.8
50 | 4.12
51 | 1.10.19
52 | 2.19.1
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.apache.commons
62 | commons-lang3
63 | ${commons-lang.version}
64 |
65 |
66 |
67 | org.ow2.asm
68 | asm
69 | ${asm.version}
70 |
71 |
72 |
73 | org.slf4j
74 | slf4j-api
75 | ${slf4j.version}
76 |
77 |
78 |
79 |
80 |
81 | junit
82 | junit
83 | ${junit.version}
84 | test
85 |
86 |
87 |
88 | org.mockito
89 | mockito-all
90 | ${mockito.version}
91 | test
92 |
93 |
94 |
95 | org.slf4j
96 | slf4j-simple
97 | ${slf4j.version}
98 | test
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | maven-compiler-plugin
110 | ${maven-compiler.version}
111 |
112 | ${jdk.version}
113 | ${jdk.version}
114 |
115 |
116 |
117 |
118 | org.apache.maven.plugins
119 | maven-shade-plugin
120 | ${maven-shade.version}
121 |
122 |
123 | package
124 |
125 | shade
126 |
127 |
128 |
129 |
130 | org.objectweb.asm
131 | com.github.javalbert.asm
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 | org.apache.maven.plugins
141 | maven-surefire-plugin
142 | ${surefire-plugin.version}
143 |
144 | false
145 |
146 | **/*Test.java
147 |
148 |
149 |
150 |
151 |
152 | org.jacoco
153 | jacoco-maven-plugin
154 | ${jacoco.version}
155 |
156 |
157 | default-prepare-agent
158 |
159 | prepare-agent
160 |
161 |
162 |
163 | default-report
164 | prepare-package
165 |
166 | report
167 |
168 |
169 |
170 | default-check
171 |
172 | check
173 |
174 |
175 |
176 |
177 | BUNDLE
178 |
179 |
180 | COMPLEXITY
181 | COVEREDRATIO
182 | ${jacoco.coveredratio}
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 | org.apache.maven.plugins
194 | maven-source-plugin
195 | ${maven-source.version}
196 |
197 |
198 | attach-sources
199 |
200 | jar-no-fork
201 |
202 |
203 |
204 |
205 |
206 | org.apache.maven.plugins
207 | maven-javadoc-plugin
208 | ${maven-javadoc.version}
209 |
210 |
211 | attach-javadocs
212 |
213 | jar
214 |
215 |
216 |
217 |
218 |
219 | org.apache.maven.plugins
220 | maven-gpg-plugin
221 | ${maven-gpg.version}
222 |
223 |
224 | sign-artifacts
225 | verify
226 |
227 | sign
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 | https://github.com/javalbert/faster-than-reflection/tree/master
238 | scm:git:git://github.com/javalbert/faster-than-reflection.git
239 | scm:git:ssh://github.com:javalbert/faster-than-reflection.git
240 |
241 |
242 |
243 |
244 | Albert Shun-Dat Chan
245 | albert.shun.dat.chan@gmail.com
246 | Albert Shun-Dat Chan
247 | https://github.com/javalbert
248 |
249 |
250 |
251 |
252 |
253 | ossrh
254 | https://oss.sonatype.org/content/repositories/snapshots
255 |
256 |
257 | ossrh
258 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
259 |
260 |
261 |
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/bytecode/utils/AsmUtils.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.bytecode.utils;
14 |
15 | import static org.objectweb.asm.Opcodes.*;
16 |
17 | import java.util.ArrayList;
18 | import java.util.List;
19 |
20 | import org.objectweb.asm.ClassWriter;
21 | import org.objectweb.asm.Label;
22 | import org.objectweb.asm.MethodVisitor;
23 |
24 | public final class AsmUtils {
25 | /**
26 | *
27 | * @param defaultCaseLabel
28 | * @param cases should be a sorted int array
29 | * @return an array of {@link Label}s when calling {@link MethodVisitor#visitTableSwitchInsn(int, int, Label, Label...)}
30 | */
31 | public static Label[] getTableSwitchLabels(
32 | Label defaultCaseLabel,
33 | int[] cases) {
34 | List labels = new ArrayList<>();
35 |
36 | int currentCase = 0;
37 | int caseValue = cases[0];
38 |
39 | int hi = cases[cases.length - 1];
40 | int lo = cases[0];
41 |
42 | for (int i = lo; i <= hi; i++) {
43 | int currentCaseValue = cases[currentCase];
44 |
45 | if (caseValue < currentCaseValue) {
46 | labels.add(defaultCaseLabel);
47 | caseValue++;
48 | } else {
49 | labels.add(new Label());
50 | caseValue = currentCaseValue + 1;
51 | currentCase++;
52 | }
53 | }
54 | return labels.stream().toArray(s -> new Label[s]);
55 | }
56 |
57 | /**
58 | * Determines what switch
Java instruction to use:
59 | * {@link MethodVisitor#visitTableSwitchInsn(int, int, Label, Label...)}
60 | * or
61 | * {@link MethodVisitor#visitLookupSwitchInsn(Label, int[], Label[])}
62 | * @param cases should be a sorted int array
63 | * @return
64 | */
65 | public static boolean useTableSwitch(int[] cases) {
66 | int hi = cases[cases.length - 1];
67 | int lo = cases[0];
68 | int nlabels = cases.length;
69 |
70 | // CREDIT: http://stackoverflow.com/a/31032054
71 | // CREDIT: http://hg.openjdk.java.net/jdk8/jdk8/langtools/file/30db5e0aaf83/src/share/classes/com/sun/tools/javac/jvm/Gen.java#l1153
72 | // Determine whether to issue a tableswitch or a lookupswitch
73 | // instruction.
74 | long table_space_cost = 4 + ((long) hi - lo + 1); // words
75 | long table_time_cost = 3; // comparisons
76 | long lookup_space_cost = 3 + 2 * (long) nlabels;
77 | long lookup_time_cost = nlabels;
78 | return
79 | nlabels > 0 &&
80 | table_space_cost + 3 * table_time_cost <=
81 | lookup_space_cost + 3 * lookup_time_cost;
82 | }
83 |
84 | public static void visitDefaultConstructor(ClassWriter cw, String classTypeDescriptor) {
85 | MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null);
86 | mv.visitCode();
87 | Label l0 = new Label();
88 | mv.visitLabel(l0);
89 | mv.visitVarInsn(ALOAD, 0);
90 | mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false);
91 | mv.visitInsn(RETURN);
92 | Label l1 = new Label();
93 | mv.visitLabel(l1);
94 | mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l1, 0);
95 | mv.visitMaxs(1, 1);
96 | mv.visitEnd();
97 | }
98 |
99 | /**
100 | *
101 | * @param mv
102 | * @param i the int value
103 | */
104 | public static void visitZeroOperandInt(MethodVisitor mv, int i) {
105 | if (i > 5) {
106 | mv.visitIntInsn(BIPUSH, i);
107 | } else {
108 | int opcode = NOP;
109 | switch (i) {
110 | case 0: opcode = ICONST_0; break;
111 | case 1: opcode = ICONST_1; break;
112 | case 2: opcode = ICONST_2; break;
113 | case 3: opcode = ICONST_3; break;
114 | case 4: opcode = ICONST_4; break;
115 | case 5: opcode = ICONST_5; break;
116 | }
117 | mv.visitInsn(opcode);
118 | }
119 | }
120 |
121 | private AsmUtils() {}
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/reflection/AccessClassLoader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2008, Nathan Sweet
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 | *
7 | * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 | * 3. Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 | *
11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 | *
13 | */
14 |
15 | package com.github.javalbert.reflection;
16 |
17 | import java.lang.ref.WeakReference;
18 | import java.lang.reflect.Method;
19 | import java.security.ProtectionDomain;
20 | import java.util.WeakHashMap;
21 |
22 | class AccessClassLoader extends ClassLoader {
23 | // Weak-references to class loaders, to avoid perm gen memory leaks, for example in app servers/web containters if the
24 | // reflectasm library (including this class) is loaded outside the deployed applications (WAR/EAR) using ReflectASM/Kryo (exts,
25 | // user classpath, etc).
26 | // The key is the parent class loader and the value is the AccessClassLoader, both are weak-referenced in the hash table.
27 | static private final WeakHashMap> accessClassLoaders = new WeakHashMap<>();
28 |
29 | // Fast-path for classes loaded in the same ClassLoader as this class.
30 | static private final ClassLoader selfContextParentClassLoader = getParentClassLoader(AccessClassLoader.class);
31 | static private volatile AccessClassLoader selfContextAccessClassLoader = new AccessClassLoader(selfContextParentClassLoader);
32 |
33 | static private volatile Method defineClassMethod;
34 |
35 | static AccessClassLoader get (Class> type) {
36 | ClassLoader parent = getParentClassLoader(type);
37 | // 1. fast-path:
38 | if (selfContextParentClassLoader.equals(parent)) {
39 | if (selfContextAccessClassLoader == null) {
40 | synchronized (accessClassLoaders) { // DCL with volatile semantics
41 | if (selfContextAccessClassLoader == null)
42 | selfContextAccessClassLoader = new AccessClassLoader(selfContextParentClassLoader);
43 | }
44 | }
45 | return selfContextAccessClassLoader;
46 | }
47 | // 2. normal search:
48 | synchronized (accessClassLoaders) {
49 | WeakReference ref = accessClassLoaders.get(parent);
50 | if (ref != null) {
51 | AccessClassLoader accessClassLoader = ref.get();
52 | if (accessClassLoader != null)
53 | return accessClassLoader;
54 | else
55 | accessClassLoaders.remove(parent); // the value has been GC-reclaimed, but still not the key (defensive sanity)
56 | }
57 | AccessClassLoader accessClassLoader = new AccessClassLoader(parent);
58 | accessClassLoaders.put(parent, new WeakReference(accessClassLoader));
59 | return accessClassLoader;
60 | }
61 | }
62 |
63 | public static void remove (ClassLoader parent) {
64 | // 1. fast-path:
65 | if (selfContextParentClassLoader.equals(parent)) {
66 | selfContextAccessClassLoader = null;
67 | } else {
68 | // 2. normal search:
69 | synchronized (accessClassLoaders) {
70 | accessClassLoaders.remove(parent);
71 | }
72 | }
73 | }
74 |
75 | public static int activeAccessClassLoaders () {
76 | int sz = accessClassLoaders.size();
77 | if (selfContextAccessClassLoader != null) sz++;
78 | return sz;
79 | }
80 |
81 | private AccessClassLoader (ClassLoader parent) {
82 | super(parent);
83 | }
84 |
85 | // protected java.lang.Class> loadClass (String name, boolean resolve) throws ClassNotFoundException {
86 | // // These classes come from the classloader that loaded AccessClassLoader.
87 | // if (name.equals(FieldAccess.class.getName())) return FieldAccess.class;
88 | // if (name.equals(MethodAccess.class.getName())) return MethodAccess.class;
89 | // if (name.equals(ConstructorAccess.class.getName())) return ConstructorAccess.class;
90 | // if (name.equals(PublicConstructorAccess.class.getName())) return PublicConstructorAccess.class;
91 | // // All other classes come from the classloader that loaded the type we are accessing.
92 | // return super.loadClass(name, resolve);
93 | // }
94 |
95 | Class> defineClass (String name, byte[] bytes) throws ClassFormatError {
96 | try {
97 | // Attempt to load the access class in the same loader, which makes protected and default access members accessible.
98 | return (Class>)getDefineClassMethod().invoke(getParent(), new Object[] {name, bytes, Integer.valueOf(0), Integer.valueOf(bytes.length),
99 | getClass().getProtectionDomain()});
100 | } catch (Exception ignored) {
101 | // continue with the definition in the current loader (won't have access to protected and package-protected members)
102 | }
103 | return defineClass(name, bytes, 0, bytes.length, getClass().getProtectionDomain());
104 | }
105 |
106 | // As per JLS, section 5.3,
107 | // "The runtime package of a class or interface is determined by the package name and defining class loader of the class or interface."
108 | static boolean areInSameRuntimeClassLoader(Class> type1, Class> type2) {
109 |
110 | if (type1.getPackage()!=type2.getPackage()) {
111 | return false;
112 | }
113 | ClassLoader loader1 = type1.getClassLoader();
114 | ClassLoader loader2 = type2.getClassLoader();
115 | ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
116 | if (loader1==null) {
117 | return (loader2==null || loader2==systemClassLoader);
118 | }
119 | if (loader2==null) {
120 | return loader1==systemClassLoader;
121 | }
122 | return loader1==loader2;
123 | }
124 |
125 | private static ClassLoader getParentClassLoader (Class> type) {
126 | ClassLoader parent = type.getClassLoader();
127 | if (parent == null) parent = ClassLoader.getSystemClassLoader();
128 | return parent;
129 | }
130 |
131 | private static Method getDefineClassMethod() throws Exception {
132 | // DCL on volatile
133 | if (defineClassMethod==null) {
134 | synchronized(accessClassLoaders) {
135 | defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", new Class[] {String.class, byte[].class, int.class,
136 | int.class, ProtectionDomain.class});
137 | try {
138 | defineClassMethod.setAccessible(true);
139 | }
140 | catch (Exception ignored) {
141 | }
142 | }
143 | }
144 | return defineClassMethod;
145 | }
146 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/reflection/ClassAccess.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection;
14 |
15 | public interface ClassAccess extends FieldAccess, PropertyAccess, MethodAccess {
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/reflection/FieldAccess.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection;
14 |
15 | import java.math.BigDecimal;
16 | import java.time.LocalDate;
17 | import java.time.LocalDateTime;
18 | import java.util.Date;
19 |
20 | public interface FieldAccess {
21 | int fieldIndex(String name);
22 |
23 | // Get primitive type fields
24 | //
25 | boolean getBooleanField(T obj, int fieldIndex);
26 | byte getByteField(T obj, int fieldIndex);
27 | char getCharField(T obj, int fieldIndex);
28 | double getDoubleField(T obj, int fieldIndex);
29 | float getFloatField(T obj, int fieldIndex);
30 | int getIntField(T obj, int fieldIndex);
31 | long getLongField(T obj, int fieldIndex);
32 | short getShortField(T obj, int fieldIndex);
33 |
34 | // Get boxed primitive type fields
35 | //
36 | Boolean getBoxedBooleanField(T obj, int fieldIndex);
37 | Byte getBoxedByteField(T obj, int fieldIndex);
38 | Character getBoxedCharField(T obj, int fieldIndex);
39 | Double getBoxedDoubleField(T obj, int fieldIndex);
40 | Float getBoxedFloatField(T obj, int fieldIndex);
41 | Integer getBoxedIntField(T obj, int fieldIndex);
42 | Long getBoxedLongField(T obj, int fieldIndex);
43 | Short getBoxedShortField(T obj, int fieldIndex);
44 |
45 | // Set primitive type fields
46 | //
47 | void setBooleanField(T obj, int fieldIndex, boolean x);
48 | void setByteField(T obj, int fieldIndex, byte x);
49 | void setCharField(T obj, int fieldIndex, char x);
50 | void setDoubleField(T obj, int fieldIndex, double x);
51 | void setFloatField(T obj, int fieldIndex, float x);
52 | void setIntField(T obj, int fieldIndex, int x);
53 | void setLongField(T obj, int fieldIndex, long x);
54 | void setShortField(T obj, int fieldIndex, short x);
55 |
56 | // Set primitive wrapper type fields
57 | //
58 | void setBoxedBooleanField(T obj, int fieldIndex, Boolean x);
59 | void setBoxedByteField(T obj, int fieldIndex, Byte x);
60 | void setBoxedCharField(T obj, int fieldIndex, Character x);
61 | void setBoxedDoubleField(T obj, int fieldIndex, Double x);
62 | void setBoxedFloatField(T obj, int fieldIndex, Float x);
63 | void setBoxedIntField(T obj, int fieldIndex, Integer x);
64 | void setBoxedLongField(T obj, int fieldIndex, Long x);
65 | void setBoxedShortField(T obj, int fieldIndex, Short x);
66 |
67 | // Common reference types
68 | //
69 | BigDecimal getBigDecimalField(T obj, int fieldIndex);
70 | void setBigDecimalField(T obj, int fieldIndex, BigDecimal x);
71 | Date getDateField(T obj, int fieldIndex);
72 | void setDateField(T obj, int fieldIndex, Date x);
73 | LocalDate getLocalDateField(T obj, int fieldIndex);
74 | void setLocalDateField(T obj, int fieldIndex, LocalDate x);
75 | LocalDateTime getLocalDateTimeField(T obj, int fieldIndex);
76 | void setLocalDateTimeField(T obj, int fieldIndex, LocalDateTime x);
77 | String getStringField(T obj, int fieldIndex);
78 | void setStringField(T obj, int fieldIndex, String x);
79 |
80 | // For all fields
81 | //
82 | Object getField(T obj, int fieldIndex);
83 | void setField(T obj, int fieldIndex, Object x);
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/reflection/MethodAccess.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection;
14 |
15 | public interface MethodAccess {
16 | int methodIndex(String name, Class>... parameterTypes);
17 |
18 | Object invoke(T obj, int methodIndex, Object... args);
19 |
20 | Object call(T obj, int methodIndex);
21 | Object call(T obj, int methodIndex, Object arg0);
22 | Object call(T obj, int methodIndex, Object arg0, Object arg1);
23 | Object call(T obj, int methodIndex, Object arg0, Object arg1, Object arg2);
24 | Object call(T obj, int methodIndex, Object arg0, Object arg1, Object arg2, Object arg3);
25 | Object call(
26 | T obj,
27 | int methodIndex,
28 | Object arg0,
29 | Object arg1,
30 | Object arg2,
31 | Object arg3,
32 | Object arg4);
33 | Object call(
34 | T obj,
35 | int methodIndex,
36 | Object arg0,
37 | Object arg1,
38 | Object arg2,
39 | Object arg3,
40 | Object arg4,
41 | Object arg5);
42 | Object call(
43 | T obj,
44 | int methodIndex,
45 | Object arg0,
46 | Object arg1,
47 | Object arg2,
48 | Object arg3,
49 | Object arg4,
50 | Object arg5,
51 | Object arg6);
52 | Object call(
53 | T obj,
54 | int methodIndex,
55 | Object arg0,
56 | Object arg1,
57 | Object arg2,
58 | Object arg3,
59 | Object arg4,
60 | Object arg5,
61 | Object arg6,
62 | Object arg7);
63 | Object call(
64 | T obj,
65 | int methodIndex,
66 | Object arg0,
67 | Object arg1,
68 | Object arg2,
69 | Object arg3,
70 | Object arg4,
71 | Object arg5,
72 | Object arg6,
73 | Object arg7,
74 | Object arg8);
75 | Object call(
76 | T obj,
77 | int methodIndex,
78 | Object arg0,
79 | Object arg1,
80 | Object arg2,
81 | Object arg3,
82 | Object arg4,
83 | Object arg5,
84 | Object arg6,
85 | Object arg7,
86 | Object arg8,
87 | Object arg9);
88 | Object call(
89 | T obj,
90 | int methodIndex,
91 | Object arg0,
92 | Object arg1,
93 | Object arg2,
94 | Object arg3,
95 | Object arg4,
96 | Object arg5,
97 | Object arg6,
98 | Object arg7,
99 | Object arg8,
100 | Object arg9,
101 | Object arg10);
102 | Object call(
103 | T obj,
104 | int methodIndex,
105 | Object arg0,
106 | Object arg1,
107 | Object arg2,
108 | Object arg3,
109 | Object arg4,
110 | Object arg5,
111 | Object arg6,
112 | Object arg7,
113 | Object arg8,
114 | Object arg9,
115 | Object arg10,
116 | Object arg11);
117 | Object call(
118 | T obj,
119 | int methodIndex,
120 | Object arg0,
121 | Object arg1,
122 | Object arg2,
123 | Object arg3,
124 | Object arg4,
125 | Object arg5,
126 | Object arg6,
127 | Object arg7,
128 | Object arg8,
129 | Object arg9,
130 | Object arg10,
131 | Object arg11,
132 | Object arg12);
133 | Object call(
134 | T obj,
135 | int methodIndex,
136 | Object arg0,
137 | Object arg1,
138 | Object arg2,
139 | Object arg3,
140 | Object arg4,
141 | Object arg5,
142 | Object arg6,
143 | Object arg7,
144 | Object arg8,
145 | Object arg9,
146 | Object arg10,
147 | Object arg11,
148 | Object arg12,
149 | Object arg13);
150 | Object call(
151 | T obj,
152 | int methodIndex,
153 | Object arg0,
154 | Object arg1,
155 | Object arg2,
156 | Object arg3,
157 | Object arg4,
158 | Object arg5,
159 | Object arg6,
160 | Object arg7,
161 | Object arg8,
162 | Object arg9,
163 | Object arg10,
164 | Object arg11,
165 | Object arg12,
166 | Object arg13,
167 | Object arg14);
168 | Object call(
169 | T obj,
170 | int methodIndex,
171 | Object arg0,
172 | Object arg1,
173 | Object arg2,
174 | Object arg3,
175 | Object arg4,
176 | Object arg5,
177 | Object arg6,
178 | Object arg7,
179 | Object arg8,
180 | Object arg9,
181 | Object arg10,
182 | Object arg11,
183 | Object arg12,
184 | Object arg13,
185 | Object arg14,
186 | Object arg15);
187 | Object call(
188 | T obj,
189 | int methodIndex,
190 | Object arg0,
191 | Object arg1,
192 | Object arg2,
193 | Object arg3,
194 | Object arg4,
195 | Object arg5,
196 | Object arg6,
197 | Object arg7,
198 | Object arg8,
199 | Object arg9,
200 | Object arg10,
201 | Object arg11,
202 | Object arg12,
203 | Object arg13,
204 | Object arg14,
205 | Object arg15,
206 | Object arg16);
207 | Object call(
208 | T obj,
209 | int methodIndex,
210 | Object arg0,
211 | Object arg1,
212 | Object arg2,
213 | Object arg3,
214 | Object arg4,
215 | Object arg5,
216 | Object arg6,
217 | Object arg7,
218 | Object arg8,
219 | Object arg9,
220 | Object arg10,
221 | Object arg11,
222 | Object arg12,
223 | Object arg13,
224 | Object arg14,
225 | Object arg15,
226 | Object arg16,
227 | Object arg17);
228 | Object call(
229 | T obj,
230 | int methodIndex,
231 | Object arg0,
232 | Object arg1,
233 | Object arg2,
234 | Object arg3,
235 | Object arg4,
236 | Object arg5,
237 | Object arg6,
238 | Object arg7,
239 | Object arg8,
240 | Object arg9,
241 | Object arg10,
242 | Object arg11,
243 | Object arg12,
244 | Object arg13,
245 | Object arg14,
246 | Object arg15,
247 | Object arg16,
248 | Object arg17,
249 | Object arg18);
250 | Object call(
251 | T obj,
252 | int methodIndex,
253 | Object arg0,
254 | Object arg1,
255 | Object arg2,
256 | Object arg3,
257 | Object arg4,
258 | Object arg5,
259 | Object arg6,
260 | Object arg7,
261 | Object arg8,
262 | Object arg9,
263 | Object arg10,
264 | Object arg11,
265 | Object arg12,
266 | Object arg13,
267 | Object arg14,
268 | Object arg15,
269 | Object arg16,
270 | Object arg17,
271 | Object arg18,
272 | Object arg19);
273 | Object call(
274 | T obj,
275 | int methodIndex,
276 | Object arg0,
277 | Object arg1,
278 | Object arg2,
279 | Object arg3,
280 | Object arg4,
281 | Object arg5,
282 | Object arg6,
283 | Object arg7,
284 | Object arg8,
285 | Object arg9,
286 | Object arg10,
287 | Object arg11,
288 | Object arg12,
289 | Object arg13,
290 | Object arg14,
291 | Object arg15,
292 | Object arg16,
293 | Object arg17,
294 | Object arg18,
295 | Object arg19,
296 | Object arg20);
297 | Object call(
298 | T obj,
299 | int methodIndex,
300 | Object arg0,
301 | Object arg1,
302 | Object arg2,
303 | Object arg3,
304 | Object arg4,
305 | Object arg5,
306 | Object arg6,
307 | Object arg7,
308 | Object arg8,
309 | Object arg9,
310 | Object arg10,
311 | Object arg11,
312 | Object arg12,
313 | Object arg13,
314 | Object arg14,
315 | Object arg15,
316 | Object arg16,
317 | Object arg17,
318 | Object arg18,
319 | Object arg19,
320 | Object arg20,
321 | Object arg21);
322 | }
323 |
--------------------------------------------------------------------------------
/src/main/java/com/github/javalbert/reflection/PropertyAccess.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection;
14 |
15 | import java.math.BigDecimal;
16 | import java.time.LocalDate;
17 | import java.time.LocalDateTime;
18 | import java.util.Date;
19 |
20 | public interface PropertyAccess {
21 | int propertyIndex(String name);
22 |
23 | // Get primitive type properties
24 | //
25 | boolean getBooleanProperty(T obj, int propertyIndex);
26 | byte getByteProperty(T obj, int propertyIndex);
27 | char getCharProperty(T obj, int propertyIndex);
28 | double getDoubleProperty(T obj, int propertyIndex);
29 | float getFloatProperty(T obj, int propertyIndex);
30 | int getIntProperty(T obj, int propertyIndex);
31 | long getLongProperty(T obj, int propertyIndex);
32 | short getShortProperty(T obj, int propertyIndex);
33 |
34 | // Get boxed primitive type properties
35 | //
36 | Boolean getBoxedBooleanProperty(T obj, int propertyIndex);
37 | Byte getBoxedByteProperty(T obj, int propertyIndex);
38 | Character getBoxedCharProperty(T obj, int propertyIndex);
39 | Double getBoxedDoubleProperty(T obj, int propertyIndex);
40 | Float getBoxedFloatProperty(T obj, int propertyIndex);
41 | Integer getBoxedIntProperty(T obj, int propertyIndex);
42 | Long getBoxedLongProperty(T obj, int propertyIndex);
43 | Short getBoxedShortProperty(T obj, int propertyIndex);
44 |
45 | // Set primitive type properties
46 | //
47 | void setBooleanProperty(T obj, int propertyIndex, boolean x);
48 | void setByteProperty(T obj, int propertyIndex, byte x);
49 | void setCharProperty(T obj, int propertyIndex, char x);
50 | void setDoubleProperty(T obj, int propertyIndex, double x);
51 | void setFloatProperty(T obj, int propertyIndex, float x);
52 | void setIntProperty(T obj, int propertyIndex, int x);
53 | void setLongProperty(T obj, int propertyIndex, long x);
54 | void setShortProperty(T obj, int propertyIndex, short x);
55 |
56 | // Set primitive wrapper type properties
57 | //
58 | void setBoxedBooleanProperty(T obj, int propertyIndex, Boolean x);
59 | void setBoxedByteProperty(T obj, int propertyIndex, Byte x);
60 | void setBoxedCharProperty(T obj, int propertyIndex, Character x);
61 | void setBoxedDoubleProperty(T obj, int propertyIndex, Double x);
62 | void setBoxedFloatProperty(T obj, int propertyIndex, Float x);
63 | void setBoxedIntProperty(T obj, int propertyIndex, Integer x);
64 | void setBoxedLongProperty(T obj, int propertyIndex, Long x);
65 | void setBoxedShortProperty(T obj, int propertyIndex, Short x);
66 |
67 | // Common reference types
68 | //
69 | BigDecimal getBigDecimalProperty(T obj, int propertyIndex);
70 | void setBigDecimalProperty(T obj, int propertyIndex, BigDecimal x);
71 | Date getDateProperty(T obj, int propertyIndex);
72 | void setDateProperty(T obj, int propertyIndex, Date x);
73 | LocalDate getLocalDateProperty(T obj, int propertyIndex);
74 | void setLocalDateProperty(T obj, int propertyIndex, LocalDate x);
75 | LocalDateTime getLocalDateTimeProperty(T obj, int propertyIndex);
76 | void setLocalDateTimeProperty(T obj, int propertyIndex, LocalDateTime x);
77 | String getStringProperty(T obj, int propertyIndex);
78 | void setStringProperty(T obj, int propertyIndex, String x);
79 |
80 | // For all properties
81 | //
82 | Object getProperty(T obj, int propertyIndex);
83 | void setProperty(T obj, int propertyIndex, Object x);
84 | }
85 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/ClassAccessCommonReferenceTypesTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import java.math.BigDecimal;
19 | import java.time.LocalDate;
20 | import java.time.LocalDateTime;
21 | import java.util.Date;
22 |
23 | import org.junit.Test;
24 |
25 | import com.github.javalbert.reflection.ClassAccessFactory;
26 | import com.github.javalbert.reflection.FieldAccess;
27 | import com.github.javalbert.reflection.PropertyAccess;
28 |
29 | public class ClassAccessCommonReferenceTypesTest {
30 | /* START Fields */
31 |
32 | @Test
33 | public void getBigDecimalFieldValueAndVerify() {
34 | FieldAccess access = ClassAccessFactory.get(Foo.class);
35 | Foo obj = new Foo();
36 | obj.setBigDecimal(new BigDecimal("123.456"));
37 |
38 | BigDecimal bigDecimal = access.getBigDecimalField(obj, access.fieldIndex("bigDecimal"));
39 |
40 | assertThat(bigDecimal, equalTo(obj.getBigDecimal()));
41 | }
42 |
43 | @Test
44 | public void setBigDecimalFieldValueAndVerify() {
45 | FieldAccess access = ClassAccessFactory.get(Foo.class);
46 | Foo obj = new Foo();
47 | BigDecimal bigDecimal = new BigDecimal("123.456");
48 |
49 | access.setBigDecimalField(obj, access.fieldIndex("bigDecimal"), bigDecimal);
50 |
51 | assertThat(obj.getBigDecimal(), equalTo(bigDecimal));
52 | }
53 |
54 | @Test
55 | public void getDateFieldValueAndVerify() {
56 | FieldAccess access = ClassAccessFactory.get(Foo.class);
57 | Foo obj = new Foo();
58 | obj.setDate(new Date());
59 |
60 | Date date = access.getDateField(obj, access.fieldIndex("date"));
61 |
62 | assertThat(date, equalTo(obj.getDate()));
63 | }
64 |
65 | @Test
66 | public void setDateFieldValueAndVerify() {
67 | FieldAccess access = ClassAccessFactory.get(Foo.class);
68 | Foo obj = new Foo();
69 | Date date = new Date();
70 |
71 | access.setDateField(obj, access.fieldIndex("date"), date);
72 |
73 | assertThat(obj.getDate(), equalTo(date));
74 | }
75 |
76 | @Test
77 | public void getLocalDateFieldValueAndVerify() {
78 | FieldAccess access = ClassAccessFactory.get(Foo.class);
79 | Foo obj = new Foo();
80 | obj.setLocalDate(LocalDate.now());
81 |
82 | LocalDate localDate = access.getLocalDateField(obj, access.fieldIndex("localDate"));
83 |
84 | assertThat(localDate, equalTo(obj.getLocalDate()));
85 | }
86 |
87 | @Test
88 | public void setLocalDateFieldValueAndVerify() {
89 | FieldAccess access = ClassAccessFactory.get(Foo.class);
90 | Foo obj = new Foo();
91 | LocalDate localDate = LocalDate.now();
92 |
93 | access.setLocalDateField(obj, access.fieldIndex("localDate"), localDate);
94 |
95 | assertThat(obj.getLocalDate(), equalTo(localDate));
96 | }
97 |
98 | @Test
99 | public void getLocalDateTimeFieldValueAndVerify() {
100 | FieldAccess access = ClassAccessFactory.get(Foo.class);
101 | Foo obj = new Foo();
102 | obj.setLocalDateTime(LocalDateTime.now());
103 |
104 | LocalDateTime localDateTime = access.getLocalDateTimeField(obj, access.fieldIndex("localDateTime"));
105 |
106 | assertThat(localDateTime, equalTo(obj.getLocalDateTime()));
107 | }
108 |
109 | @Test
110 | public void setLocalDateTimeFieldValueAndVerify() {
111 | FieldAccess access = ClassAccessFactory.get(Foo.class);
112 | Foo obj = new Foo();
113 | LocalDateTime localDateTime = LocalDateTime.now();
114 |
115 | access.setLocalDateTimeField(obj, access.fieldIndex("localDateTime"), localDateTime);
116 |
117 | assertThat(obj.getLocalDateTime(), equalTo(localDateTime));
118 | }
119 |
120 | @Test
121 | public void getStringFieldValueAndVerify() {
122 | FieldAccess access = ClassAccessFactory.get(Foo.class);
123 | Foo obj = new Foo();
124 | obj.setString("Pizza Hut");
125 |
126 | String string = access.getStringField(obj, access.fieldIndex("string"));
127 |
128 | assertThat(string, equalTo(obj.getString()));
129 | }
130 |
131 | @Test
132 | public void setStringFieldValueAndVerify() {
133 | FieldAccess access = ClassAccessFactory.get(Foo.class);
134 | Foo obj = new Foo();
135 | String string = "Pizza Hut";
136 |
137 | access.setStringField(obj, access.fieldIndex("string"), string);
138 |
139 | assertThat(obj.getString(), equalTo(string));
140 | }
141 |
142 | /* END Fields */
143 |
144 | /* START Properties */
145 |
146 | @Test
147 | public void getBigDecimalPropertyValueAndVerify() {
148 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
149 | Foo obj = new Foo();
150 | obj.setBigDecimal(new BigDecimal("123.456"));
151 |
152 | BigDecimal bigDecimal = access.getBigDecimalProperty(obj, access.propertyIndex("bigDecimal"));
153 |
154 | assertThat(bigDecimal, equalTo(obj.getBigDecimal()));
155 | }
156 |
157 | @Test
158 | public void setBigDecimalPropertyValueAndVerify() {
159 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
160 | Foo obj = new Foo();
161 | BigDecimal bigDecimal = new BigDecimal("123.456");
162 |
163 | access.setBigDecimalProperty(obj, access.propertyIndex("bigDecimal"), bigDecimal);
164 |
165 | assertThat(obj.getBigDecimal(), equalTo(bigDecimal));
166 | }
167 |
168 | @Test
169 | public void getDatePropertyValueAndVerify() {
170 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
171 | Foo obj = new Foo();
172 | obj.setDate(new Date());
173 |
174 | Date date = access.getDateProperty(obj, access.propertyIndex("date"));
175 |
176 | assertThat(date, equalTo(obj.getDate()));
177 | }
178 |
179 | @Test
180 | public void setDatePropertyValueAndVerify() {
181 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
182 | Foo obj = new Foo();
183 | Date date = new Date();
184 |
185 | access.setDateProperty(obj, access.propertyIndex("date"), date);
186 |
187 | assertThat(obj.getDate(), equalTo(date));
188 | }
189 |
190 | @Test
191 | public void getLocalDatePropertyValueAndVerify() {
192 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
193 | Foo obj = new Foo();
194 | obj.setLocalDate(LocalDate.now());
195 |
196 | LocalDate localDate = access.getLocalDateProperty(obj, access.propertyIndex("localDate"));
197 |
198 | assertThat(localDate, equalTo(obj.getLocalDate()));
199 | }
200 |
201 | @Test
202 | public void setLocalDatePropertyValueAndVerify() {
203 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
204 | Foo obj = new Foo();
205 | LocalDate localDate = LocalDate.now();
206 |
207 | access.setLocalDateProperty(obj, access.propertyIndex("localDate"), localDate);
208 |
209 | assertThat(obj.getLocalDate(), equalTo(localDate));
210 | }
211 |
212 | @Test
213 | public void getLocalDateTimePropertyValueAndVerify() {
214 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
215 | Foo obj = new Foo();
216 | obj.setLocalDateTime(LocalDateTime.now());
217 |
218 | LocalDateTime localDateTime = access.getLocalDateTimeProperty(obj, access.propertyIndex("localDateTime"));
219 |
220 | assertThat(localDateTime, equalTo(obj.getLocalDateTime()));
221 | }
222 |
223 | @Test
224 | public void setLocalDateTimePropertyValueAndVerify() {
225 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
226 | Foo obj = new Foo();
227 | LocalDateTime localDateTime = LocalDateTime.now();
228 |
229 | access.setLocalDateTimeProperty(obj, access.propertyIndex("localDateTime"), localDateTime);
230 |
231 | assertThat(obj.getLocalDateTime(), equalTo(localDateTime));
232 | }
233 |
234 | @Test
235 | public void getStringPropertyValueAndVerify() {
236 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
237 | Foo obj = new Foo();
238 | obj.setString("Pizza Hut");
239 |
240 | String string = access.getStringProperty(obj, access.propertyIndex("string"));
241 |
242 | assertThat(string, equalTo(obj.getString()));
243 | }
244 |
245 | @Test
246 | public void setStringPropertyValueAndVerify() {
247 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
248 | Foo obj = new Foo();
249 | String string = "Pizza Hut";
250 |
251 | access.setStringProperty(obj, access.propertyIndex("string"), string);
252 |
253 | assertThat(obj.getString(), equalTo(string));
254 | }
255 |
256 | /* END Properties */
257 | }
258 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/FieldAccessBoxedPrimitivesTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.junit.Assert.*;
16 | import static org.hamcrest.CoreMatchers.*;
17 |
18 | import org.junit.Test;
19 |
20 | import com.github.javalbert.reflection.ClassAccessFactory;
21 | import com.github.javalbert.reflection.FieldAccess;
22 |
23 | public class FieldAccessBoxedPrimitivesTest {
24 | @Test
25 | public void getBoxedBooleanFieldValueAndVerify() {
26 | FieldAccess access = ClassAccessFactory.get(Foo.class);
27 | Foo obj = new Foo();
28 | obj.setBoxedBoolean(true);
29 |
30 | Boolean boxedBoolean = access.getBoxedBooleanField(obj, access.fieldIndex("boxedBoolean"));
31 |
32 | assertThat(boxedBoolean, equalTo(obj.getBoxedBoolean()));
33 | }
34 |
35 | @Test
36 | public void getBoxedByteFieldValueAndVerify() {
37 | FieldAccess access = ClassAccessFactory.get(Foo.class);
38 | Foo obj = new Foo();
39 | obj.setBoxedByte((byte)-55);
40 |
41 | Byte boxedByte = access.getBoxedByteField(obj, access.fieldIndex("boxedByte"));
42 |
43 | assertThat(boxedByte, equalTo(obj.getBoxedByte()));
44 | }
45 |
46 | @Test
47 | public void getBoxedCharFieldValueAndVerify() {
48 | FieldAccess access = ClassAccessFactory.get(Foo.class);
49 | Foo obj = new Foo();
50 | obj.setBoxedChar('\n');
51 |
52 | Character boxedChar = access.getBoxedCharField(obj, access.fieldIndex("boxedChar"));
53 |
54 | assertThat(boxedChar, equalTo(obj.getBoxedChar()));
55 | }
56 |
57 | @Test
58 | public void getBoxedDoubleFieldValueAndVerify() {
59 | FieldAccess access = ClassAccessFactory.get(Foo.class);
60 | Foo obj = new Foo();
61 | obj.setBoxedDouble(0.9553896885798474d);
62 |
63 | Double boxedDouble = access.getBoxedDoubleField(obj, access.fieldIndex("boxedDouble"));
64 |
65 | assertThat(boxedDouble, equalTo(obj.getBoxedDouble()));
66 | }
67 |
68 | @Test
69 | public void getBoxedFloatFieldValueAndVerify() {
70 | FieldAccess access = ClassAccessFactory.get(Foo.class);
71 | Foo obj = new Foo();
72 | obj.setBoxedFloat(0.2747032f);
73 |
74 | Float boxedFloat = access.getBoxedFloatField(obj, access.fieldIndex("boxedFloat"));
75 |
76 | assertThat(boxedFloat, equalTo(obj.getBoxedFloat()));
77 | }
78 |
79 | @Test
80 | public void getBoxedIntFieldValueAndVerify() {
81 | FieldAccess access = ClassAccessFactory.get(Foo.class);
82 | Foo obj = new Foo();
83 | obj.setBoxedInt(274991538);
84 |
85 | Integer boxedInt = access.getBoxedIntField(obj, access.fieldIndex("boxedInt"));
86 |
87 | assertThat(boxedInt, equalTo(obj.getBoxedInt()));
88 | }
89 |
90 | @Test
91 | public void getBoxedLongFieldValueAndVerify() {
92 | FieldAccess access = ClassAccessFactory.get(Foo.class);
93 | Foo obj = new Foo();
94 | obj.setBoxedLong(6774924159498401640L);
95 |
96 | Long boxedLong = access.getBoxedLongField(obj, access.fieldIndex("boxedLong"));
97 |
98 | assertThat(boxedLong, equalTo(obj.getBoxedLong()));
99 | }
100 |
101 | @Test
102 | public void getBoxedShortFieldValueAndVerify() {
103 | FieldAccess access = ClassAccessFactory.get(Foo.class);
104 | Foo obj = new Foo();
105 | obj.setBoxedShort((short)-31848);
106 |
107 | Short boxedShort = access.getBoxedShortField(obj, access.fieldIndex("boxedShort"));
108 |
109 | assertThat(boxedShort, equalTo(obj.getBoxedShort()));
110 | }
111 |
112 | @Test
113 | public void setBoxedBooleanFieldValueAndVerify() {
114 | FieldAccess access = ClassAccessFactory.get(Foo.class);
115 | Foo obj = new Foo();
116 | Boolean boxedBoolean = true;
117 |
118 | access.setBoxedBooleanField(obj, access.fieldIndex("boxedBoolean"), boxedBoolean);
119 |
120 | assertThat(obj.getBoxedBoolean(), equalTo(boxedBoolean));
121 | }
122 |
123 | @Test
124 | public void setBoxedByteFieldValueAndVerify() {
125 | FieldAccess access = ClassAccessFactory.get(Foo.class);
126 | Foo obj = new Foo();
127 | Byte boxedByte = (byte)-55;
128 |
129 | access.setBoxedByteField(obj, access.fieldIndex("boxedByte"), boxedByte);
130 |
131 | assertThat(obj.getBoxedByte(), equalTo(boxedByte));
132 | }
133 |
134 | @Test
135 | public void setBoxedCharFieldValueAndVerify() {
136 | FieldAccess access = ClassAccessFactory.get(Foo.class);
137 | Foo obj = new Foo();
138 | Character boxedChar = '\n';
139 |
140 | access.setBoxedCharField(obj, access.fieldIndex("boxedChar"), boxedChar);
141 |
142 | assertThat(obj.getBoxedChar(), equalTo(boxedChar));
143 | }
144 |
145 | @Test
146 | public void setBoxedDoubleFieldValueAndVerify() {
147 | FieldAccess access = ClassAccessFactory.get(Foo.class);
148 | Foo obj = new Foo();
149 | Double boxedDouble = 0.9553896885798474d;
150 |
151 | access.setBoxedDoubleField(obj, access.fieldIndex("boxedDouble"), boxedDouble);
152 |
153 | assertThat(obj.getBoxedDouble(), equalTo(boxedDouble));
154 | }
155 |
156 | @Test
157 | public void setBoxedFloatFieldValueAndVerify() {
158 | FieldAccess access = ClassAccessFactory.get(Foo.class);
159 | Foo obj = new Foo();
160 | Float boxedFloat = 0.2747032f;
161 |
162 | access.setBoxedFloatField(obj, access.fieldIndex("boxedFloat"), boxedFloat);
163 |
164 | assertThat(obj.getBoxedFloat(), equalTo(boxedFloat));
165 | }
166 |
167 | @Test
168 | public void setBoxedIntFieldValueAndVerify() {
169 | FieldAccess access = ClassAccessFactory.get(Foo.class);
170 | Foo obj = new Foo();
171 | Integer boxedInt = 274991538;
172 |
173 | access.setBoxedIntField(obj, access.fieldIndex("boxedInt"), boxedInt);
174 |
175 | assertThat(obj.getBoxedInt(), equalTo(boxedInt));
176 | }
177 |
178 | @Test
179 | public void setBoxedLongFieldValueAndVerify() {
180 | FieldAccess access = ClassAccessFactory.get(Foo.class);
181 | Foo obj = new Foo();
182 | Long boxedLong = 6774924159498401640L;
183 |
184 | access.setBoxedLongField(obj, access.fieldIndex("boxedLong"), boxedLong);
185 |
186 | assertThat(obj.getBoxedLong(), equalTo(boxedLong));
187 | }
188 |
189 | @Test
190 | public void setBoxedShortFieldValueAndVerify() {
191 | FieldAccess access = ClassAccessFactory.get(Foo.class);
192 | Foo obj = new Foo();
193 | Short boxedShort = (short)-31848;
194 |
195 | access.setBoxedShortField(obj, access.fieldIndex("boxedShort"), boxedShort);
196 |
197 | assertThat(obj.getBoxedShort(), equalTo(boxedShort));
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/FieldAccessGeneralTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import java.math.BigDecimal;
19 | import java.time.LocalDate;
20 | import java.time.LocalDateTime;
21 | import java.util.Date;
22 |
23 | import org.junit.Test;
24 |
25 | import com.github.javalbert.reflection.ClassAccessFactory;
26 | import com.github.javalbert.reflection.FieldAccess;
27 |
28 | public class FieldAccessGeneralTest {
29 | /* START Primitive types */
30 |
31 | @Test
32 | public void getBooleanFieldValueAndVerify() {
33 | FieldAccess access = ClassAccessFactory.get(Foo.class);
34 | Foo obj = new Foo();
35 | obj.setBooleanVal(true);
36 |
37 | boolean booleanVal = (boolean)access.getField(obj, access.fieldIndex("booleanVal"));
38 |
39 | assertThat(booleanVal, equalTo(obj.getBooleanVal()));
40 | }
41 |
42 | @Test
43 | public void getByteFieldValueAndVerify() {
44 | FieldAccess access = ClassAccessFactory.get(Foo.class);
45 | Foo obj = new Foo();
46 | obj.setByteVal((byte)-21);
47 |
48 | byte byteVal = (byte)access.getField(obj, access.fieldIndex("byteVal"));
49 |
50 | assertThat(byteVal, equalTo(obj.getByteVal()));
51 | }
52 |
53 | @Test
54 | public void getCharFieldValueAndVerify() {
55 | FieldAccess access = ClassAccessFactory.get(Foo.class);
56 | Foo obj = new Foo();
57 | obj.setCharVal('.');
58 |
59 | char charVal = (char)access.getField(obj, access.fieldIndex("charVal"));
60 |
61 | assertThat(charVal, equalTo(obj.getCharVal()));
62 | }
63 |
64 | @Test
65 | public void getDoubleFieldValueAndVerify() {
66 | FieldAccess access = ClassAccessFactory.get(Foo.class);
67 | Foo obj = new Foo();
68 | obj.setDoubleVal(0.14753383239666462d);
69 |
70 | double doubleVal = (double)access.getField(obj, access.fieldIndex("doubleVal"));
71 |
72 | assertThat(doubleVal, equalTo(obj.getDoubleVal()));
73 | }
74 |
75 | @Test
76 | public void getFloatFieldValueAndVerify() {
77 | FieldAccess access = ClassAccessFactory.get(Foo.class);
78 | Foo obj = new Foo();
79 | obj.setFloatVal(0.27210158f);
80 |
81 | float floatVal = (float)access.getField(obj, access.fieldIndex("floatVal"));
82 |
83 | assertThat(floatVal, equalTo(obj.getFloatVal()));
84 | }
85 |
86 | @Test
87 | public void getIntFieldValueAndVerify() {
88 | FieldAccess access = ClassAccessFactory.get(Foo.class);
89 | Foo obj = new Foo();
90 | obj.setIntVal(1);
91 |
92 | int intVal = (int)access.getField(obj, access.fieldIndex("intVal"));
93 |
94 | assertThat(intVal, equalTo(obj.getIntVal()));
95 | }
96 |
97 | @Test
98 | public void getLongFieldValueAndVerify() {
99 | FieldAccess access = ClassAccessFactory.get(Foo.class);
100 | Foo obj = new Foo();
101 | obj.setLongVal(3285927007071017350L);
102 |
103 | long longVal = (long)access.getField(obj, access.fieldIndex("longVal"));
104 |
105 | assertThat(longVal, equalTo(obj.getLongVal()));
106 | }
107 |
108 | @Test
109 | public void getShortFieldValueAndVerify() {
110 | FieldAccess access = ClassAccessFactory.get(Foo.class);
111 | Foo obj = new Foo();
112 | obj.setShortVal((short)-8406);
113 |
114 | short shortVal = (short)access.getField(obj, access.fieldIndex("shortVal"));
115 |
116 | assertThat(shortVal, equalTo(obj.getShortVal()));
117 | }
118 |
119 | @Test
120 | public void setBooleanFieldValueAndVerify() {
121 | FieldAccess access = ClassAccessFactory.get(Foo.class);
122 | Foo obj = new Foo();
123 | boolean booleanVal = true;
124 |
125 | access.setField(obj, access.fieldIndex("booleanVal"), booleanVal);
126 |
127 | assertThat(obj.getBooleanVal(), equalTo(booleanVal));
128 | }
129 |
130 | @Test
131 | public void setByteFieldValueAndVerify() {
132 | FieldAccess access = ClassAccessFactory.get(Foo.class);
133 | Foo obj = new Foo();
134 | byte byteVal = (byte)-21;
135 |
136 | access.setField(obj, access.fieldIndex("byteVal"), byteVal);
137 |
138 | assertThat(obj.getByteVal(), equalTo(byteVal));
139 | }
140 |
141 | @Test
142 | public void setCharFieldValueAndVerify() {
143 | FieldAccess access = ClassAccessFactory.get(Foo.class);
144 | Foo obj = new Foo();
145 | char charVal = '.';
146 |
147 | access.setField(obj, access.fieldIndex("charVal"), charVal);
148 |
149 | assertThat(obj.getCharVal(), equalTo(charVal));
150 | }
151 |
152 | @Test
153 | public void setDoubleFieldValueAndVerify() {
154 | FieldAccess access = ClassAccessFactory.get(Foo.class);
155 | Foo obj = new Foo();
156 | double doubleVal = 0.14753383239666462d;
157 |
158 | access.setField(obj, access.fieldIndex("doubleVal"), doubleVal);
159 |
160 | assertThat(obj.getDoubleVal(), equalTo(doubleVal));
161 | }
162 |
163 | @Test
164 | public void setFloatFieldValueAndVerify() {
165 | FieldAccess access = ClassAccessFactory.get(Foo.class);
166 | Foo obj = new Foo();
167 | float floatVal = 0.27210158f;
168 |
169 | access.setField(obj, access.fieldIndex("floatVal"), floatVal);
170 |
171 | assertThat(obj.getFloatVal(), equalTo(floatVal));
172 | }
173 |
174 | @Test
175 | public void setIntFieldValueAndVerify() {
176 | FieldAccess access = ClassAccessFactory.get(Foo.class);
177 | Foo obj = new Foo();
178 | int intVal = 1;
179 |
180 | access.setField(obj, access.fieldIndex("intVal"), intVal);
181 |
182 | assertThat(obj.getIntVal(), equalTo(intVal));
183 | }
184 |
185 | @Test
186 | public void setLongFieldValueAndVerify() {
187 | FieldAccess access = ClassAccessFactory.get(Foo.class);
188 | Foo obj = new Foo();
189 | long longVal = 3285927007071017350L;
190 |
191 | access.setField(obj, access.fieldIndex("longVal"), longVal);
192 |
193 | assertThat(obj.getLongVal(), equalTo(longVal));
194 | }
195 |
196 | @Test
197 | public void setShortFieldValueAndVerify() {
198 | FieldAccess access = ClassAccessFactory.get(Foo.class);
199 | Foo obj = new Foo();
200 | short shortVal = (short)-8406;
201 |
202 | access.setShortField(obj, access.fieldIndex("shortVal"), shortVal);
203 |
204 | assertThat(obj.getShortVal(), equalTo(shortVal));
205 | }
206 |
207 | /* END Primitive types */
208 |
209 | /* START Primitive wrapper types */
210 |
211 | @Test
212 | public void getBoxedBooleanFieldValueAndVerify() {
213 | FieldAccess access = ClassAccessFactory.get(Foo.class);
214 | Foo obj = new Foo();
215 | obj.setBoxedBoolean(true);
216 |
217 | Boolean boxedBoolean = (Boolean)access.getField(obj, access.fieldIndex("boxedBoolean"));
218 |
219 | assertThat(boxedBoolean, equalTo(obj.getBoxedBoolean()));
220 | }
221 |
222 | @Test
223 | public void getBoxedByteFieldValueAndVerify() {
224 | FieldAccess access = ClassAccessFactory.get(Foo.class);
225 | Foo obj = new Foo();
226 | obj.setBoxedByte((byte)-55);
227 |
228 | Byte boxedByte = (Byte)access.getField(obj, access.fieldIndex("boxedByte"));
229 |
230 | assertThat(boxedByte, equalTo(obj.getBoxedByte()));
231 | }
232 |
233 | @Test
234 | public void getBoxedCharFieldValueAndVerify() {
235 | FieldAccess access = ClassAccessFactory.get(Foo.class);
236 | Foo obj = new Foo();
237 | obj.setBoxedChar('\n');
238 |
239 | Character boxedChar = (Character)access.getField(obj, access.fieldIndex("boxedChar"));
240 |
241 | assertThat(boxedChar, equalTo(obj.getBoxedChar()));
242 | }
243 |
244 | @Test
245 | public void getBoxedDoubleFieldValueAndVerify() {
246 | FieldAccess access = ClassAccessFactory.get(Foo.class);
247 | Foo obj = new Foo();
248 | obj.setBoxedDouble(0.9553896885798474d);
249 |
250 | Double boxedDouble = (Double)access.getField(obj, access.fieldIndex("boxedDouble"));
251 |
252 | assertThat(boxedDouble, equalTo(obj.getBoxedDouble()));
253 | }
254 |
255 | @Test
256 | public void getBoxedFloatFieldValueAndVerify() {
257 | FieldAccess access = ClassAccessFactory.get(Foo.class);
258 | Foo obj = new Foo();
259 | obj.setBoxedFloat(0.2747032f);
260 |
261 | Float boxedFloat = (Float)access.getField(obj, access.fieldIndex("boxedFloat"));
262 |
263 | assertThat(boxedFloat, equalTo(obj.getBoxedFloat()));
264 | }
265 |
266 | @Test
267 | public void getBoxedIntFieldValueAndVerify() {
268 | FieldAccess access = ClassAccessFactory.get(Foo.class);
269 | Foo obj = new Foo();
270 | obj.setBoxedInt(274991538);
271 |
272 | Integer boxedInt = (Integer)access.getField(obj, access.fieldIndex("boxedInt"));
273 |
274 | assertThat(boxedInt, equalTo(obj.getBoxedInt()));
275 | }
276 |
277 | @Test
278 | public void getBoxedLongFieldValueAndVerify() {
279 | FieldAccess access = ClassAccessFactory.get(Foo.class);
280 | Foo obj = new Foo();
281 | obj.setBoxedLong(6774924159498401640L);
282 |
283 | Long boxedLong = (Long)access.getField(obj, access.fieldIndex("boxedLong"));
284 |
285 | assertThat(boxedLong, equalTo(obj.getBoxedLong()));
286 | }
287 |
288 | @Test
289 | public void getBoxedShortFieldValueAndVerify() {
290 | FieldAccess access = ClassAccessFactory.get(Foo.class);
291 | Foo obj = new Foo();
292 | obj.setBoxedShort((short)-31848);
293 |
294 | Short boxedShort = (Short)access.getField(obj, access.fieldIndex("boxedShort"));
295 |
296 | assertThat(boxedShort, equalTo(obj.getBoxedShort()));
297 | }
298 |
299 | @Test
300 | public void setBoxedBooleanFieldValueAndVerify() {
301 | FieldAccess access = ClassAccessFactory.get(Foo.class);
302 | Foo obj = new Foo();
303 | Boolean boxedBoolean = true;
304 |
305 | access.setField(obj, access.fieldIndex("boxedBoolean"), boxedBoolean);
306 |
307 | assertThat(obj.getBoxedBoolean(), equalTo(boxedBoolean));
308 | }
309 |
310 | @Test
311 | public void setBoxedByteFieldValueAndVerify() {
312 | FieldAccess access = ClassAccessFactory.get(Foo.class);
313 | Foo obj = new Foo();
314 | Byte boxedByte = (byte)-55;
315 |
316 | access.setField(obj, access.fieldIndex("boxedByte"), boxedByte);
317 |
318 | assertThat(obj.getBoxedByte(), equalTo(boxedByte));
319 | }
320 |
321 | @Test
322 | public void setBoxedCharFieldValueAndVerify() {
323 | FieldAccess access = ClassAccessFactory.get(Foo.class);
324 | Foo obj = new Foo();
325 | Character boxedChar = '\n';
326 |
327 | access.setField(obj, access.fieldIndex("boxedChar"), boxedChar);
328 |
329 | assertThat(obj.getBoxedChar(), equalTo(boxedChar));
330 | }
331 |
332 | @Test
333 | public void setBoxedDoubleFieldValueAndVerify() {
334 | FieldAccess access = ClassAccessFactory.get(Foo.class);
335 | Foo obj = new Foo();
336 | Double boxedDouble = 0.9553896885798474d;
337 |
338 | access.setField(obj, access.fieldIndex("boxedDouble"), boxedDouble);
339 |
340 | assertThat(obj.getBoxedDouble(), equalTo(boxedDouble));
341 | }
342 |
343 | @Test
344 | public void setBoxedFloatFieldValueAndVerify() {
345 | FieldAccess access = ClassAccessFactory.get(Foo.class);
346 | Foo obj = new Foo();
347 | Float boxedFloat = 0.2747032f;
348 |
349 | access.setField(obj, access.fieldIndex("boxedFloat"), boxedFloat);
350 |
351 | assertThat(obj.getBoxedFloat(), equalTo(boxedFloat));
352 | }
353 |
354 | @Test
355 | public void setBoxedIntFieldValueAndVerify() {
356 | FieldAccess access = ClassAccessFactory.get(Foo.class);
357 | Foo obj = new Foo();
358 | Integer boxedInt = 274991538;
359 |
360 | access.setField(obj, access.fieldIndex("boxedInt"), boxedInt);
361 |
362 | assertThat(obj.getBoxedInt(), equalTo(boxedInt));
363 | }
364 |
365 | @Test
366 | public void setBoxedLongFieldValueAndVerify() {
367 | FieldAccess access = ClassAccessFactory.get(Foo.class);
368 | Foo obj = new Foo();
369 | Long boxedLong = 6774924159498401640L;
370 |
371 | access.setField(obj, access.fieldIndex("boxedLong"), boxedLong);
372 |
373 | assertThat(obj.getBoxedLong(), equalTo(boxedLong));
374 | }
375 |
376 | @Test
377 | public void setBoxedShortFieldValueAndVerify() {
378 | FieldAccess access = ClassAccessFactory.get(Foo.class);
379 | Foo obj = new Foo();
380 | Short boxedShort = (short)-31848;
381 |
382 | access.setField(obj, access.fieldIndex("boxedShort"), boxedShort);
383 |
384 | assertThat(obj.getBoxedShort(), equalTo(boxedShort));
385 | }
386 |
387 | /* END Primitive wrapper types */
388 |
389 | /* START Common reference types */
390 |
391 | @Test
392 | public void getBigDecimalFieldValueAndVerify() {
393 | FieldAccess access = ClassAccessFactory.get(Foo.class);
394 | Foo obj = new Foo();
395 | obj.setBigDecimal(new BigDecimal("123.456"));
396 |
397 | BigDecimal bigDecimal = (BigDecimal)access.getField(obj, access.fieldIndex("bigDecimal"));
398 |
399 | assertThat(bigDecimal, equalTo(obj.getBigDecimal()));
400 | }
401 |
402 | @Test
403 | public void setBigDecimalFieldValueAndVerify() {
404 | FieldAccess access = ClassAccessFactory.get(Foo.class);
405 | Foo obj = new Foo();
406 | BigDecimal bigDecimal = new BigDecimal("123.456");
407 |
408 | access.setField(obj, access.fieldIndex("bigDecimal"), bigDecimal);
409 |
410 | assertThat(obj.getBigDecimal(), equalTo(bigDecimal));
411 | }
412 |
413 | @Test
414 | public void getDateFieldValueAndVerify() {
415 | FieldAccess access = ClassAccessFactory.get(Foo.class);
416 | Foo obj = new Foo();
417 | obj.setDate(new Date());
418 |
419 | Date date = (Date)access.getField(obj, access.fieldIndex("date"));
420 |
421 | assertThat(date, equalTo(obj.getDate()));
422 | }
423 |
424 | @Test
425 | public void setDateFieldValueAndVerify() {
426 | FieldAccess access = ClassAccessFactory.get(Foo.class);
427 | Foo obj = new Foo();
428 | Date date = new Date();
429 |
430 | access.setField(obj, access.fieldIndex("date"), date);
431 |
432 | assertThat(obj.getDate(), equalTo(date));
433 | }
434 |
435 | @Test
436 | public void getLocalDateFieldValueAndVerify() {
437 | FieldAccess access = ClassAccessFactory.get(Foo.class);
438 | Foo obj = new Foo();
439 | obj.setLocalDate(LocalDate.now());
440 |
441 | LocalDate localDate = (LocalDate)access.getField(obj, access.fieldIndex("localDate"));
442 |
443 | assertThat(localDate, equalTo(obj.getLocalDate()));
444 | }
445 |
446 | @Test
447 | public void setLocalDateFieldValueAndVerify() {
448 | FieldAccess access = ClassAccessFactory.get(Foo.class);
449 | Foo obj = new Foo();
450 | LocalDate localDate = LocalDate.now();
451 |
452 | access.setField(obj, access.fieldIndex("localDate"), localDate);
453 |
454 | assertThat(obj.getLocalDate(), equalTo(localDate));
455 | }
456 |
457 | @Test
458 | public void getLocalDateTimeFieldValueAndVerify() {
459 | FieldAccess access = ClassAccessFactory.get(Foo.class);
460 | Foo obj = new Foo();
461 | obj.setLocalDateTime(LocalDateTime.now());
462 |
463 | LocalDateTime localDateTime = (LocalDateTime)access.getField(obj, access.fieldIndex("localDateTime"));
464 |
465 | assertThat(localDateTime, equalTo(obj.getLocalDateTime()));
466 | }
467 |
468 | @Test
469 | public void setLocalDateTimeFieldValueAndVerify() {
470 | FieldAccess access = ClassAccessFactory.get(Foo.class);
471 | Foo obj = new Foo();
472 | LocalDateTime localDateTime = LocalDateTime.now();
473 |
474 | access.setField(obj, access.fieldIndex("localDateTime"), localDateTime);
475 |
476 | assertThat(obj.getLocalDateTime(), equalTo(localDateTime));
477 | }
478 |
479 | @Test
480 | public void getStringFieldValueAndVerify() {
481 | FieldAccess access = ClassAccessFactory.get(Foo.class);
482 | Foo obj = new Foo();
483 | obj.setString("Pizza Hut");
484 |
485 | String string = (String)access.getField(obj, access.fieldIndex("string"));
486 |
487 | assertThat(string, equalTo(obj.getString()));
488 | }
489 |
490 | @Test
491 | public void setStringFieldValueAndVerify() {
492 | FieldAccess access = ClassAccessFactory.get(Foo.class);
493 | Foo obj = new Foo();
494 | String string = "Pizza Hut";
495 |
496 | access.setField(obj, access.fieldIndex("string"), string);
497 |
498 | assertThat(obj.getString(), equalTo(string));
499 | }
500 |
501 | /* END Common reference types */
502 | }
503 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/FieldAccessPrimitivesTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import org.junit.Test;
19 |
20 | import com.github.javalbert.reflection.ClassAccessFactory;
21 | import com.github.javalbert.reflection.FieldAccess;
22 |
23 | public class FieldAccessPrimitivesTest {
24 | @Test
25 | public void getBooleanFieldValueAndVerify() {
26 | FieldAccess access = ClassAccessFactory.get(Foo.class);
27 | Foo obj = new Foo();
28 | obj.setBooleanVal(true);
29 |
30 | boolean booleanVal = access.getBooleanField(obj, access.fieldIndex("booleanVal"));
31 |
32 | assertThat(booleanVal, equalTo(obj.getBooleanVal()));
33 | }
34 |
35 | @Test
36 | public void getByteFieldValueAndVerify() {
37 | FieldAccess access = ClassAccessFactory.get(Foo.class);
38 | Foo obj = new Foo();
39 | obj.setByteVal((byte)-21);
40 |
41 | byte byteVal = access.getByteField(obj, access.fieldIndex("byteVal"));
42 |
43 | assertThat(byteVal, equalTo(obj.getByteVal()));
44 | }
45 |
46 | @Test
47 | public void getCharFieldValueAndVerify() {
48 | FieldAccess access = ClassAccessFactory.get(Foo.class);
49 | Foo obj = new Foo();
50 | obj.setCharVal('.');
51 |
52 | char charVal = access.getCharField(obj, access.fieldIndex("charVal"));
53 |
54 | assertThat(charVal, equalTo(obj.getCharVal()));
55 | }
56 |
57 | @Test
58 | public void getDoubleFieldValueAndVerify() {
59 | FieldAccess access = ClassAccessFactory.get(Foo.class);
60 | Foo obj = new Foo();
61 | obj.setDoubleVal(0.14753383239666462d);
62 |
63 | double doubleVal = access.getDoubleField(obj, access.fieldIndex("doubleVal"));
64 |
65 | assertThat(doubleVal, equalTo(obj.getDoubleVal()));
66 | }
67 |
68 | @Test
69 | public void getFloatFieldValueAndVerify() {
70 | FieldAccess access = ClassAccessFactory.get(Foo.class);
71 | Foo obj = new Foo();
72 | obj.setFloatVal(0.27210158f);
73 |
74 | float floatVal = access.getFloatField(obj, access.fieldIndex("floatVal"));
75 |
76 | assertThat(floatVal, equalTo(obj.getFloatVal()));
77 | }
78 |
79 | @Test
80 | public void getIntFieldValueAndVerify() {
81 | FieldAccess access = ClassAccessFactory.get(Foo.class);
82 | Foo obj = new Foo();
83 | obj.setIntVal(1);
84 |
85 | int intVal = access.getIntField(obj, access.fieldIndex("intVal"));
86 |
87 | assertThat(intVal, equalTo(obj.getIntVal()));
88 | }
89 |
90 | @Test
91 | public void getLongFieldValueAndVerify() {
92 | FieldAccess access = ClassAccessFactory.get(Foo.class);
93 | Foo obj = new Foo();
94 | obj.setLongVal(3285927007071017350L);
95 |
96 | long longVal = access.getLongField(obj, access.fieldIndex("longVal"));
97 |
98 | assertThat(longVal, equalTo(obj.getLongVal()));
99 | }
100 |
101 | @Test
102 | public void getShortFieldValueAndVerify() {
103 | FieldAccess access = ClassAccessFactory.get(Foo.class);
104 | Foo obj = new Foo();
105 | obj.setShortVal((short)-8406);
106 |
107 | short shortVal = access.getShortField(obj, access.fieldIndex("shortVal"));
108 |
109 | assertThat(shortVal, equalTo(obj.getShortVal()));
110 | }
111 |
112 | @Test
113 | public void setBooleanFieldValueAndVerify() {
114 | FieldAccess access = ClassAccessFactory.get(Foo.class);
115 | Foo obj = new Foo();
116 | boolean booleanVal = true;
117 |
118 | access.setBooleanField(obj, access.fieldIndex("booleanVal"), booleanVal);
119 |
120 | assertThat(obj.getBooleanVal(), equalTo(booleanVal));
121 | }
122 |
123 | @Test
124 | public void setByteFieldValueAndVerify() {
125 | FieldAccess access = ClassAccessFactory.get(Foo.class);
126 | Foo obj = new Foo();
127 | byte byteVal = (byte)-21;
128 |
129 | access.setByteField(obj, access.fieldIndex("byteVal"), byteVal);
130 |
131 | assertThat(obj.getByteVal(), equalTo(byteVal));
132 | }
133 |
134 | @Test
135 | public void setCharFieldValueAndVerify() {
136 | FieldAccess access = ClassAccessFactory.get(Foo.class);
137 | Foo obj = new Foo();
138 | char charVal = '.';
139 |
140 | access.setCharField(obj, access.fieldIndex("charVal"), charVal);
141 |
142 | assertThat(obj.getCharVal(), equalTo(charVal));
143 | }
144 |
145 | @Test
146 | public void setDoubleFieldValueAndVerify() {
147 | FieldAccess access = ClassAccessFactory.get(Foo.class);
148 | Foo obj = new Foo();
149 | double doubleVal = 0.14753383239666462d;
150 |
151 | access.setDoubleField(obj, access.fieldIndex("doubleVal"), doubleVal);
152 |
153 | assertThat(obj.getDoubleVal(), equalTo(doubleVal));
154 | }
155 |
156 | @Test
157 | public void setFloatFieldValueAndVerify() {
158 | FieldAccess access = ClassAccessFactory.get(Foo.class);
159 | Foo obj = new Foo();
160 | float floatVal = 0.27210158f;
161 |
162 | access.setFloatField(obj, access.fieldIndex("floatVal"), floatVal);
163 |
164 | assertThat(obj.getFloatVal(), equalTo(floatVal));
165 | }
166 |
167 | @Test
168 | public void setIntFieldValueAndVerify() {
169 | FieldAccess access = ClassAccessFactory.get(Foo.class);
170 | Foo obj = new Foo();
171 | int intVal = 1;
172 |
173 | access.setIntField(obj, access.fieldIndex("intVal"), intVal);
174 |
175 | assertThat(obj.getIntVal(), equalTo(intVal));
176 | }
177 |
178 | @Test
179 | public void setLongFieldValueAndVerify() {
180 | FieldAccess access = ClassAccessFactory.get(Foo.class);
181 | Foo obj = new Foo();
182 | long longVal = 3285927007071017350L;
183 |
184 | access.setLongField(obj, access.fieldIndex("longVal"), longVal);
185 |
186 | assertThat(obj.getLongVal(), equalTo(longVal));
187 | }
188 |
189 | @Test
190 | public void setShortFieldValueAndVerify() {
191 | FieldAccess access = ClassAccessFactory.get(Foo.class);
192 | Foo obj = new Foo();
193 | short shortVal = (short)-8406;
194 |
195 | access.setShortField(obj, access.fieldIndex("shortVal"), shortVal);
196 |
197 | assertThat(obj.getShortVal(), equalTo(shortVal));
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/Foo.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import java.math.BigDecimal;
16 | import java.time.LocalDate;
17 | import java.time.LocalDateTime;
18 | import java.util.Date;
19 |
20 | public class Foo {
21 | // public static class FooAccess2 implements FieldAccess, PropertyAccess {
22 | // @Override
23 | // public int fieldIndex(String name) {
24 | // switch (name) {
25 | // case "booleanVal":
26 | // return 0;
27 | // default:
28 | // throw new IllegalArgumentException("No field with name: " + name);
29 | // }
30 | // }
31 | //
32 | // @Override
33 | // public boolean getBooleanField(Foo obj, int fieldIndex) {
34 | // switch (fieldIndex) {
35 | // case 0:
36 | // return obj.booleanVal;
37 | // default:
38 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
39 | // }
40 | // }
41 | //
42 | // @Override
43 | // public byte getByteField(Foo obj, int fieldIndex) {
44 | // return 0;
45 | // }
46 | //
47 | // @Override
48 | // public char getCharField(Foo obj, int fieldIndex) {
49 | // return 0;
50 | // }
51 | //
52 | // @Override
53 | // public double getDoubleField(Foo obj, int fieldIndex) {
54 | // switch (fieldIndex) {
55 | // case 3:
56 | // return obj.doubleVal;
57 | // default:
58 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
59 | // }
60 | // }
61 | //
62 | // @Override
63 | // public float getFloatField(Foo obj, int fieldIndex) {
64 | // return 0;
65 | // }
66 | //
67 | // @Override
68 | // public int getIntField(Foo obj, int fieldIndex) {
69 | // return 0;
70 | // }
71 | //
72 | // @Override
73 | // public long getLongField(Foo obj, int fieldIndex) {
74 | // return 0;
75 | // }
76 | //
77 | // @Override
78 | // public short getShortField(Foo obj, int fieldIndex) {
79 | // return 0;
80 | // }
81 | //
82 | // @Override
83 | // public Boolean getBoxedBooleanField(Foo obj, int fieldIndex) {
84 | // switch (fieldIndex) {
85 | // case 8:
86 | // return obj.boxedBoolean;
87 | // default:
88 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
89 | // }
90 | // }
91 | //
92 | // @Override
93 | // public Byte getBoxedByteField(Foo obj, int fieldIndex) {
94 | // return null;
95 | // }
96 | //
97 | // @Override
98 | // public Character getBoxedCharField(Foo obj, int fieldIndex) {
99 | // return null;
100 | // }
101 | //
102 | // @Override
103 | // public Double getBoxedDoubleField(Foo obj, int fieldIndex) {
104 | // switch (fieldIndex) {
105 | // case 11:
106 | // return obj.boxedDouble;
107 | // default:
108 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
109 | // }
110 | // }
111 | //
112 | // @Override
113 | // public Float getBoxedFloatField(Foo obj, int fieldIndex) {
114 | // return null;
115 | // }
116 | //
117 | // @Override
118 | // public Integer getBoxedIntField(Foo obj, int fieldIndex) {
119 | // return null;
120 | // }
121 | //
122 | // @Override
123 | // public Long getBoxedLongField(Foo obj, int fieldIndex) {
124 | // return null;
125 | // }
126 | //
127 | // @Override
128 | // public Short getBoxedShortField(Foo obj, int fieldIndex) {
129 | // return null;
130 | // }
131 | //
132 | // @Override
133 | // public void setBooleanField(Foo obj, int fieldIndex, boolean x) {
134 | // switch (fieldIndex) {
135 | // case 0:
136 | // obj.booleanVal = x;
137 | // default:
138 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
139 | // }
140 | // }
141 | //
142 | // @Override
143 | // public void setByteField(Foo obj, int fieldIndex, byte x) {
144 | // }
145 | //
146 | // @Override
147 | // public void setCharField(Foo obj, int fieldIndex, char x) {
148 | // }
149 | //
150 | // @Override
151 | // public void setDoubleField(Foo obj, int fieldIndex, double x) {
152 | // switch (fieldIndex) {
153 | // case 3:
154 | // obj.doubleVal = x;
155 | // default:
156 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
157 | // }
158 | // }
159 | //
160 | // @Override
161 | // public void setFloatField(Foo obj, int fieldIndex, float x) {
162 | // }
163 | //
164 | // @Override
165 | // public void setIntField(Foo obj, int fieldIndex, int x) {
166 | // }
167 | //
168 | // @Override
169 | // public void setLongField(Foo obj, int fieldIndex, long x) {
170 | // }
171 | //
172 | // @Override
173 | // public void setShortField(Foo obj, int fieldIndex, short x) {
174 | // }
175 | //
176 | // @Override
177 | // public void setBoxedBooleanField(Foo obj, int fieldIndex, Boolean x) {
178 | // switch (fieldIndex) {
179 | // case 8:
180 | // obj.boxedBoolean = x;
181 | // break;
182 | // default:
183 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
184 | // }
185 | // }
186 | //
187 | // @Override
188 | // public void setBoxedByteField(Foo obj, int fieldIndex, Byte x) {
189 | // }
190 | //
191 | // @Override
192 | // public void setBoxedCharField(Foo obj, int fieldIndex, Character x) {
193 | // }
194 | //
195 | // @Override
196 | // public void setBoxedDoubleField(Foo obj, int fieldIndex, Double x) {
197 | // switch (fieldIndex) {
198 | // case 11:
199 | // obj.boxedDouble = x;
200 | // default:
201 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
202 | // }
203 | // }
204 | //
205 | // @Override
206 | // public void setBoxedFloatField(Foo obj, int fieldIndex, Float x) {
207 | // }
208 | //
209 | // @Override
210 | // public void setBoxedIntField(Foo obj, int fieldIndex, Integer x) {
211 | // }
212 | //
213 | // @Override
214 | // public void setBoxedLongField(Foo obj, int fieldIndex, Long x) {
215 | // }
216 | //
217 | // @Override
218 | // public void setBoxedShortField(Foo obj, int fieldIndex, Short x) {
219 | // }
220 | //
221 | // @Override
222 | // public BigDecimal getBigDecimalField(Foo obj, int fieldIndex) {
223 | // switch (fieldIndex) {
224 | // case 16:
225 | // return obj.bigDecimal;
226 | // default:
227 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
228 | // }
229 | // }
230 | //
231 | // @Override
232 | // public void setBigDecimalField(Foo obj, int fieldIndex, BigDecimal x) {
233 | // switch (fieldIndex) {
234 | // case 16:
235 | // obj.bigDecimal = x;
236 | // break;
237 | // default:
238 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
239 | // }
240 | // }
241 | //
242 | // @Override
243 | // public Date getDateField(Foo obj, int fieldIndex) {
244 | // return null;
245 | // }
246 | //
247 | // @Override
248 | // public void setDateField(Foo obj, int fieldIndex, Date x) {
249 | // }
250 | //
251 | // @Override
252 | // public String getStringField(Foo obj, int fieldIndex) {
253 | // return null;
254 | // }
255 | //
256 | // @Override
257 | // public void setStringField(Foo obj, int fieldIndex, String x) {
258 | // }
259 | //
260 | // @Override
261 | // public LocalDate getLocalDateField(Foo obj, int fieldIndex) {
262 | // return null;
263 | // }
264 | //
265 | // @Override
266 | // public void setLocalDateField(Foo obj, int fieldIndex, LocalDate x) {
267 | // }
268 | //
269 | // @Override
270 | // public LocalDateTime getLocalDateTimeField(Foo obj, int fieldIndex) {
271 | // return null;
272 | // }
273 | //
274 | // @Override
275 | // public void setLocalDateTimeField(Foo obj, int fieldIndex, LocalDateTime x) {
276 | // }
277 | //
278 | // @Override
279 | // public Object getField(Foo obj, int fieldIndex) {
280 | // switch (fieldIndex) {
281 | // case 0:
282 | // return obj.booleanVal;
283 | // case 1:
284 | // return obj.byteVal;
285 | // case 2:
286 | // return obj.charVal;
287 | // case 3:
288 | // return obj.doubleVal;
289 | // case 4:
290 | // return obj.floatVal;
291 | // case 5:
292 | // return obj.intVal;
293 | // case 6:
294 | // return obj.longVal;
295 | // case 7:
296 | // return obj.shortVal;
297 | // case 8:
298 | // return obj.boxedBoolean;
299 | // default:
300 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
301 | // }
302 | // }
303 | //
304 | // @Override
305 | // public void setField(Foo obj, int fieldIndex, Object x) {
306 | // switch (fieldIndex) {
307 | // case 0:
308 | // obj.booleanVal = (boolean)x;
309 | // break;
310 | // case 1:
311 | // obj.byteVal = (byte)x;
312 | // break;
313 | // case 2:
314 | // obj.charVal = (char)x;
315 | // break;
316 | // case 3:
317 | // obj.doubleVal = (double)x;
318 | // break;
319 | // case 4:
320 | // obj.floatVal = (float)x;
321 | // break;
322 | // case 5:
323 | // obj.intVal = (int)x;
324 | // break;
325 | // case 6:
326 | // obj.longVal = (long)x;
327 | // break;
328 | // case 7:
329 | // obj.shortVal = (short)x;
330 | // break;
331 | // case 8:
332 | // obj.boxedBoolean = (Boolean)x;
333 | // break;
334 | // default:
335 | // throw new IllegalArgumentException("No field with index: " + fieldIndex);
336 | // }
337 | // }
338 | //
339 | // @Override
340 | // public int propertyIndex(String name) {
341 | // switch (name) {
342 | // case "BooleanVal":
343 | // return 0;
344 | // default:
345 | // throw new IllegalArgumentException("No property with name: " + name);
346 | // }
347 | // }
348 | //
349 | // @Override
350 | // public boolean getBooleanProperty(Foo obj, int propertyIndex) {
351 | // switch (propertyIndex) {
352 | // case 1:
353 | // return obj.getBooleanVal();
354 | // default:
355 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
356 | // }
357 | // }
358 | //
359 | // @Override
360 | // public byte getByteProperty(Foo obj, int propertyIndex) {
361 | // return 0;
362 | // }
363 | //
364 | // @Override
365 | // public char getCharProperty(Foo obj, int propertyIndex) {
366 | // return 0;
367 | // }
368 | //
369 | // @Override
370 | // public double getDoubleProperty(Foo obj, int propertyIndex) {
371 | // switch (propertyIndex) {
372 | // case 13:
373 | // return obj.getDoubleVal();
374 | // default:
375 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
376 | // }
377 | // }
378 | //
379 | // @Override
380 | // public float getFloatProperty(Foo obj, int propertyIndex) {
381 | // return 0;
382 | // }
383 | //
384 | // @Override
385 | // public int getIntProperty(Foo obj, int propertyIndex) {
386 | // return 0;
387 | // }
388 | //
389 | // @Override
390 | // public long getLongProperty(Foo obj, int propertyIndex) {
391 | // return 0;
392 | // }
393 | //
394 | // @Override
395 | // public short getShortProperty(Foo obj, int propertyIndex) {
396 | // return 0;
397 | // }
398 | //
399 | // @Override
400 | // public Boolean getBoxedBooleanProperty(Foo obj, int propertyIndex) {
401 | // switch (propertyIndex) {
402 | // case 2:
403 | // return obj.getBoxedBoolean();
404 | // default:
405 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
406 | // }
407 | // }
408 | //
409 | // @Override
410 | // public Byte getBoxedByteProperty(Foo obj, int propertyIndex) {
411 | // return null;
412 | // }
413 | //
414 | // @Override
415 | // public Character getBoxedCharProperty(Foo obj, int propertyIndex) {
416 | // return null;
417 | // }
418 | //
419 | // @Override
420 | // public Double getBoxedDoubleProperty(Foo obj, int propertyIndex) {
421 | // switch (propertyIndex) {
422 | // case 5:
423 | // return obj.getBoxedDouble();
424 | // default:
425 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
426 | // }
427 | // }
428 | //
429 | // @Override
430 | // public Float getBoxedFloatProperty(Foo obj, int propertyIndex) {
431 | // return null;
432 | // }
433 | //
434 | // @Override
435 | // public Integer getBoxedIntProperty(Foo obj, int propertyIndex) {
436 | // return null;
437 | // }
438 | //
439 | // @Override
440 | // public Long getBoxedLongProperty(Foo obj, int propertyIndex) {
441 | // return null;
442 | // }
443 | //
444 | // @Override
445 | // public Short getBoxedShortProperty(Foo obj, int propertyIndex) {
446 | // return null;
447 | // }
448 | //
449 | // @Override
450 | // public void setBooleanProperty(Foo obj, int propertyIndex, boolean x) {
451 | // switch (propertyIndex) {
452 | // case 1:
453 | // obj.setBooleanVal(x);
454 | // break;
455 | // default:
456 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
457 | // }
458 | // }
459 | //
460 | // @Override
461 | // public void setByteProperty(Foo obj, int propertyIndex, byte x) {
462 | // }
463 | //
464 | // @Override
465 | // public void setCharProperty(Foo obj, int propertyIndex, char x) {
466 | // }
467 | //
468 | // @Override
469 | // public void setDoubleProperty(Foo obj, int propertyIndex, double x) {
470 | // switch (propertyIndex) {
471 | // case 13:
472 | // obj.setDoubleVal(x);
473 | // break;
474 | // default:
475 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
476 | // }
477 | // }
478 | //
479 | // @Override
480 | // public void setFloatProperty(Foo obj, int propertyIndex, float x) {
481 | // }
482 | //
483 | // @Override
484 | // public void setIntProperty(Foo obj, int propertyIndex, int x) {
485 | // }
486 | //
487 | // @Override
488 | // public void setLongProperty(Foo obj, int propertyIndex, long x) {
489 | // }
490 | //
491 | // @Override
492 | // public void setShortProperty(Foo obj, int propertyIndex, short x) {
493 | // }
494 | //
495 | // @Override
496 | // public void setBoxedBooleanProperty(Foo obj, int propertyIndex, Boolean x) {
497 | // switch (propertyIndex) {
498 | // case 2:
499 | // obj.setBoxedBoolean(x);
500 | // break;
501 | // default:
502 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
503 | // }
504 | // }
505 | //
506 | // @Override
507 | // public void setBoxedByteProperty(Foo obj, int propertyIndex, Byte x) {
508 | // }
509 | //
510 | // @Override
511 | // public void setBoxedCharProperty(Foo obj, int propertyIndex, Character x) {
512 | // }
513 | //
514 | // @Override
515 | // public void setBoxedDoubleProperty(Foo obj, int propertyIndex, Double x) {
516 | // switch (propertyIndex) {
517 | // case 5:
518 | // obj.setBoxedDouble(x);
519 | // break;
520 | // default:
521 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
522 | // }
523 | // }
524 | //
525 | // @Override
526 | // public void setBoxedFloatProperty(Foo obj, int propertyIndex, Float x) {
527 | // }
528 | //
529 | // @Override
530 | // public void setBoxedIntProperty(Foo obj, int propertyIndex, Integer x) {
531 | // }
532 | //
533 | // @Override
534 | // public void setBoxedLongProperty(Foo obj, int propertyIndex, Long x) {
535 | // }
536 | //
537 | // @Override
538 | // public void setBoxedShortProperty(Foo obj, int propertyIndex, Short x) {
539 | // }
540 | //
541 | // @Override
542 | // public BigDecimal getBigDecimalProperty(Foo obj, int propertyIndex) {
543 | // switch (propertyIndex) {
544 | // case 0:
545 | // return obj.getBigDecimal();
546 | // default:
547 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
548 | // }
549 | // }
550 | //
551 | // @Override
552 | // public void setBigDecimalProperty(Foo obj, int propertyIndex, BigDecimal x) {
553 | // switch (propertyIndex) {
554 | // case 0:
555 | // obj.setBigDecimal(x);
556 | // break;
557 | // default:
558 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
559 | // }
560 | // }
561 | //
562 | // @Override
563 | // public Date getDateProperty(Foo obj, int propertyIndex) {
564 | // return null;
565 | // }
566 | //
567 | // @Override
568 | // public void setDateProperty(Foo obj, int propertyIndex, Date x) {
569 | // }
570 | //
571 | // @Override
572 | // public LocalDate getLocalDateProperty(Foo obj, int propertyIndex) {
573 | // return null;
574 | // }
575 | //
576 | // @Override
577 | // public void setLocalDateProperty(Foo obj, int propertyIndex, LocalDate x) {
578 | // }
579 | //
580 | // @Override
581 | // public LocalDateTime getLocalDateTimeProperty(Foo obj, int propertyIndex) {
582 | // return null;
583 | // }
584 | //
585 | // @Override
586 | // public void setLocalDateTimeProperty(Foo obj, int propertyIndex, LocalDateTime x) {
587 | // }
588 | //
589 | // @Override
590 | // public String getStringProperty(Foo obj, int propertyIndex) {
591 | // return null;
592 | // }
593 | //
594 | // @Override
595 | // public void setStringProperty(Foo obj, int propertyIndex, String x) {
596 | // }
597 | //
598 | //
599 | // @Override
600 | // public Object getProperty(Foo obj, int propertyIndex) {
601 | // switch (propertyIndex) {
602 | // case 1:
603 | // return obj.getBooleanVal();
604 | // case 2:
605 | // return obj.getBoxedBoolean();
606 | // case 10:
607 | // return obj.getByteVal();
608 | // case 11:
609 | // return obj.getCharVal();
610 | // case 13:
611 | // return obj.getDoubleVal();
612 | // case 14:
613 | // return obj.getFloatVal();
614 | // case 15:
615 | // return obj.getIntVal();
616 | // case 18:
617 | // return obj.getLongVal();
618 | // case 19:
619 | // return obj.getShortVal();
620 | // default:
621 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
622 | // }
623 | // }
624 | //
625 | // @Override
626 | // public void setProperty(Foo obj, int propertyIndex, Object x) {
627 | // switch (propertyIndex) {
628 | // case 1:
629 | // obj.setBooleanVal((boolean)x);
630 | // break;
631 | // case 2:
632 | // obj.setBoxedBoolean((Boolean)x);
633 | // break;
634 | // case 10:
635 | // obj.setByteVal((byte)x);
636 | // break;
637 | // case 11:
638 | // obj.setCharVal((char)x);
639 | // break;
640 | // case 13:
641 | // obj.setDoubleVal((double)x);
642 | // break;
643 | // case 14:
644 | // obj.setFloatVal((float)x);
645 | // break;
646 | // case 15:
647 | // obj.setIntVal((int)x);
648 | // break;
649 | // case 18:
650 | // obj.setLongVal((long)x);
651 | // break;
652 | // case 19:
653 | // obj.setShortVal((short)x);
654 | // break;
655 | // default:
656 | // throw new IllegalArgumentException("No property with index: " + propertyIndex);
657 | // }
658 | // }
659 | // }
660 |
661 | // Primitive types
662 | //
663 | private boolean booleanVal;
664 | private byte byteVal;
665 | private char charVal;
666 | private double doubleVal;
667 | private float floatVal;
668 | private int intVal;
669 | private long longVal;
670 | private short shortVal;
671 |
672 | // Primitive wrapper types
673 | //
674 | private Boolean boxedBoolean;
675 | private Byte boxedByte;
676 | private Character boxedChar;
677 | private Double boxedDouble;
678 | private Float boxedFloat;
679 | private Integer boxedInt;
680 | private Long boxedLong;
681 | private Short boxedShort;
682 |
683 | // Common reference types
684 | //
685 | private BigDecimal bigDecimal;
686 | private Date date;
687 | private LocalDate localDate;
688 | private LocalDateTime localDateTime;
689 | private String string;
690 |
691 | /* START Primitive type properties */
692 |
693 | public boolean getBooleanVal() {
694 | return booleanVal;
695 | }
696 | public void setBooleanVal(boolean booleanVal) {
697 | this.booleanVal = booleanVal;
698 | }
699 | public byte getByteVal() {
700 | return byteVal;
701 | }
702 | public void setByteVal(byte byteVal) {
703 | this.byteVal = byteVal;
704 | }
705 | public char getCharVal() {
706 | return charVal;
707 | }
708 | public void setCharVal(char charVal) {
709 | this.charVal = charVal;
710 | }
711 | public double getDoubleVal() {
712 | return doubleVal;
713 | }
714 | public void setDoubleVal(double doubleVal) {
715 | this.doubleVal = doubleVal;
716 | }
717 | public float getFloatVal() {
718 | return floatVal;
719 | }
720 | public void setFloatVal(float floatVal) {
721 | this.floatVal = floatVal;
722 | }
723 | public int getIntVal() {
724 | return intVal;
725 | }
726 | public void setIntVal(int intVal) {
727 | this.intVal = intVal;
728 | }
729 | public long getLongVal() {
730 | return longVal;
731 | }
732 | public void setLongVal(long longVal) {
733 | this.longVal = longVal;
734 | }
735 | public short getShortVal() {
736 | return shortVal;
737 | }
738 | public void setShortVal(short shortVal) {
739 | this.shortVal = shortVal;
740 | }
741 |
742 | /* END Primitive type properties */
743 |
744 | /* START Primitive wrapper type properties */
745 |
746 | public Boolean getBoxedBoolean() {
747 | return boxedBoolean;
748 | }
749 | public void setBoxedBoolean(Boolean boxedBoolean) {
750 | this.boxedBoolean = boxedBoolean;
751 | }
752 | public Byte getBoxedByte() {
753 | return boxedByte;
754 | }
755 | public void setBoxedByte(Byte boxedByte) {
756 | this.boxedByte = boxedByte;
757 | }
758 | public Character getBoxedChar() {
759 | return boxedChar;
760 | }
761 | public void setBoxedChar(Character boxedChar) {
762 | this.boxedChar = boxedChar;
763 | }
764 | public Double getBoxedDouble() {
765 | return boxedDouble;
766 | }
767 | public void setBoxedDouble(Double boxedDouble) {
768 | this.boxedDouble = boxedDouble;
769 | }
770 | public Float getBoxedFloat() {
771 | return boxedFloat;
772 | }
773 | public void setBoxedFloat(Float boxedFloat) {
774 | this.boxedFloat = boxedFloat;
775 | }
776 | public Integer getBoxedInt() {
777 | return boxedInt;
778 | }
779 | public void setBoxedInt(Integer boxedInt) {
780 | this.boxedInt = boxedInt;
781 | }
782 | public Long getBoxedLong() {
783 | return boxedLong;
784 | }
785 | public void setBoxedLong(Long boxedLong) {
786 | this.boxedLong = boxedLong;
787 | }
788 | public Short getBoxedShort() {
789 | return boxedShort;
790 | }
791 | public void setBoxedShort(Short boxedShort) {
792 | this.boxedShort = boxedShort;
793 | }
794 |
795 | /* END Primitive wrapper type properties */
796 |
797 | /* START Common reference types */
798 |
799 | public BigDecimal getBigDecimal() {
800 | return bigDecimal;
801 | }
802 | public void setBigDecimal(BigDecimal bigDecimal) {
803 | this.bigDecimal = bigDecimal;
804 | }
805 | public Date getDate() {
806 | return date;
807 | }
808 | public void setDate(Date date) {
809 | this.date = date;
810 | }
811 | public LocalDate getLocalDate() {
812 | return localDate;
813 | }
814 | public void setLocalDate(LocalDate localDate) {
815 | this.localDate = localDate;
816 | }
817 | public LocalDateTime getLocalDateTime() {
818 | return localDateTime;
819 | }
820 | public void setLocalDateTime(LocalDateTime localDateTime) {
821 | this.localDateTime = localDateTime;
822 | }
823 | public String getString() {
824 | return string;
825 | }
826 | public void setString(String string) {
827 | this.string = string;
828 | }
829 |
830 | /* END Common reference types */
831 |
832 | /* START Constructors */
833 |
834 | public Foo() {}
835 |
836 | public Foo(boolean booleanVal) {
837 | this(booleanVal, (byte)0);
838 | }
839 |
840 | public Foo(boolean booleanVal, byte byteVal) {
841 | this(booleanVal, byteVal, '\u0000');
842 | }
843 |
844 | public Foo(boolean booleanVal, byte byteVal, char charVal) {
845 | this(booleanVal, byteVal, charVal, 0.0d);
846 | }
847 |
848 | public Foo(boolean booleanVal, byte byteVal, char charVal, double doubleVal) {
849 | this(booleanVal, byteVal, charVal, doubleVal, 0.0f);
850 | }
851 |
852 | public Foo(boolean booleanVal, byte byteVal, char charVal, double doubleVal, float floatVal) {
853 | this(booleanVal, byteVal, charVal, doubleVal, floatVal, 0);
854 | }
855 |
856 | public Foo(
857 | boolean booleanVal,
858 | byte byteVal,
859 | char charVal,
860 | double doubleVal,
861 | float floatVal,
862 | int intVal) {
863 | this(booleanVal, byteVal, charVal, doubleVal, floatVal, intVal, 0L);
864 | }
865 |
866 | public Foo(
867 | boolean booleanVal,
868 | byte byteVal,
869 | char charVal,
870 | double doubleVal,
871 | float floatVal,
872 | int intVal,
873 | long longVal) {
874 | this(booleanVal, byteVal, charVal, doubleVal, floatVal, intVal, longVal, (short)0);
875 | }
876 |
877 | public Foo(
878 | boolean booleanVal,
879 | byte byteVal,
880 | char charVal,
881 | double doubleVal,
882 | float floatVal,
883 | int intVal,
884 | long longVal,
885 | short shortVal) {
886 | this(
887 | booleanVal,
888 | byteVal,
889 | charVal,
890 | doubleVal,
891 | floatVal,
892 | intVal,
893 | longVal,
894 | (short)0,
895 | null);
896 | }
897 |
898 | public Foo(
899 | boolean booleanVal,
900 | byte byteVal,
901 | char charVal,
902 | double doubleVal,
903 | float floatVal,
904 | int intVal,
905 | long longVal,
906 | short shortVal,
907 | Boolean boxedBoolean) {
908 | this(
909 | booleanVal,
910 | byteVal,
911 | charVal,
912 | doubleVal,
913 | floatVal,
914 | intVal,
915 | longVal,
916 | (short)0,
917 | boxedBoolean,
918 | null);
919 | }
920 |
921 | public Foo(
922 | boolean booleanVal,
923 | byte byteVal,
924 | char charVal,
925 | double doubleVal,
926 | float floatVal,
927 | int intVal,
928 | long longVal,
929 | short shortVal,
930 | Boolean boxedBoolean,
931 | Byte boxedByte) {
932 | this(
933 | booleanVal,
934 | byteVal,
935 | charVal,
936 | doubleVal,
937 | floatVal,
938 | intVal,
939 | longVal,
940 | (short)0,
941 | boxedBoolean,
942 | boxedByte,
943 | null);
944 | }
945 |
946 | public Foo(
947 | boolean booleanVal,
948 | byte byteVal,
949 | char charVal,
950 | double doubleVal,
951 | float floatVal,
952 | int intVal,
953 | long longVal,
954 | short shortVal,
955 | Boolean boxedBoolean,
956 | Byte boxedByte,
957 | Character boxedChar) {
958 | this(
959 | booleanVal,
960 | byteVal,
961 | charVal,
962 | doubleVal,
963 | floatVal,
964 | intVal,
965 | longVal,
966 | (short)0,
967 | boxedBoolean,
968 | boxedByte,
969 | boxedChar,
970 | null);
971 | }
972 |
973 | public Foo(
974 | boolean booleanVal,
975 | byte byteVal,
976 | char charVal,
977 | double doubleVal,
978 | float floatVal,
979 | int intVal,
980 | long longVal,
981 | short shortVal,
982 | Boolean boxedBoolean,
983 | Byte boxedByte,
984 | Character boxedChar,
985 | Double boxedDouble) {
986 | this(
987 | booleanVal,
988 | byteVal,
989 | charVal,
990 | doubleVal,
991 | floatVal,
992 | intVal,
993 | longVal,
994 | (short)0,
995 | boxedBoolean,
996 | boxedByte,
997 | boxedChar,
998 | boxedDouble,
999 | null);
1000 | }
1001 |
1002 | public Foo(
1003 | boolean booleanVal,
1004 | byte byteVal,
1005 | char charVal,
1006 | double doubleVal,
1007 | float floatVal,
1008 | int intVal,
1009 | long longVal,
1010 | short shortVal,
1011 | Boolean boxedBoolean,
1012 | Byte boxedByte,
1013 | Character boxedChar,
1014 | Double boxedDouble,
1015 | Float boxedFloat) {
1016 | this(
1017 | booleanVal,
1018 | byteVal,
1019 | charVal,
1020 | doubleVal,
1021 | floatVal,
1022 | intVal,
1023 | longVal,
1024 | (short)0,
1025 | boxedBoolean,
1026 | boxedByte,
1027 | boxedChar,
1028 | boxedDouble,
1029 | boxedFloat,
1030 | null);
1031 | }
1032 |
1033 | public Foo(
1034 | boolean booleanVal,
1035 | byte byteVal,
1036 | char charVal,
1037 | double doubleVal,
1038 | float floatVal,
1039 | int intVal,
1040 | long longVal,
1041 | short shortVal,
1042 | Boolean boxedBoolean,
1043 | Byte boxedByte,
1044 | Character boxedChar,
1045 | Double boxedDouble,
1046 | Float boxedFloat,
1047 | Integer boxedInt) {
1048 | this(
1049 | booleanVal,
1050 | byteVal,
1051 | charVal,
1052 | doubleVal,
1053 | floatVal,
1054 | intVal,
1055 | longVal,
1056 | (short)0,
1057 | boxedBoolean,
1058 | boxedByte,
1059 | boxedChar,
1060 | boxedDouble,
1061 | boxedFloat,
1062 | boxedInt,
1063 | null);
1064 | }
1065 |
1066 | public Foo(
1067 | boolean booleanVal,
1068 | byte byteVal,
1069 | char charVal,
1070 | double doubleVal,
1071 | float floatVal,
1072 | int intVal,
1073 | long longVal,
1074 | short shortVal,
1075 | Boolean boxedBoolean,
1076 | Byte boxedByte,
1077 | Character boxedChar,
1078 | Double boxedDouble,
1079 | Float boxedFloat,
1080 | Integer boxedInt,
1081 | Long boxedLong) {
1082 | this(
1083 | booleanVal,
1084 | byteVal,
1085 | charVal,
1086 | doubleVal,
1087 | floatVal,
1088 | intVal,
1089 | longVal,
1090 | (short)0,
1091 | boxedBoolean,
1092 | boxedByte,
1093 | boxedChar,
1094 | boxedDouble,
1095 | boxedFloat,
1096 | boxedInt,
1097 | boxedLong,
1098 | null);
1099 | }
1100 |
1101 | public Foo(
1102 | boolean booleanVal,
1103 | byte byteVal,
1104 | char charVal,
1105 | double doubleVal,
1106 | float floatVal,
1107 | int intVal,
1108 | long longVal,
1109 | short shortVal,
1110 | Boolean boxedBoolean,
1111 | Byte boxedByte,
1112 | Character boxedChar,
1113 | Double boxedDouble,
1114 | Float boxedFloat,
1115 | Integer boxedInt,
1116 | Long boxedLong,
1117 | Short boxedShort) {
1118 | this(
1119 | booleanVal,
1120 | byteVal,
1121 | charVal,
1122 | doubleVal,
1123 | floatVal,
1124 | intVal,
1125 | longVal,
1126 | (short)0,
1127 | boxedBoolean,
1128 | boxedByte,
1129 | boxedChar,
1130 | boxedDouble,
1131 | boxedFloat,
1132 | boxedInt,
1133 | boxedLong,
1134 | boxedShort,
1135 | null);
1136 | }
1137 |
1138 | public Foo(
1139 | boolean booleanVal,
1140 | byte byteVal,
1141 | char charVal,
1142 | double doubleVal,
1143 | float floatVal,
1144 | int intVal,
1145 | long longVal,
1146 | short shortVal,
1147 | Boolean boxedBoolean,
1148 | Byte boxedByte,
1149 | Character boxedChar,
1150 | Double boxedDouble,
1151 | Float boxedFloat,
1152 | Integer boxedInt,
1153 | Long boxedLong,
1154 | Short boxedShort,
1155 | BigDecimal bigDecimal) {
1156 | this(
1157 | booleanVal,
1158 | byteVal,
1159 | charVal,
1160 | doubleVal,
1161 | floatVal,
1162 | intVal,
1163 | longVal,
1164 | (short)0,
1165 | boxedBoolean,
1166 | boxedByte,
1167 | boxedChar,
1168 | boxedDouble,
1169 | boxedFloat,
1170 | boxedInt,
1171 | boxedLong,
1172 | boxedShort,
1173 | bigDecimal,
1174 | null);
1175 | }
1176 |
1177 | public Foo(
1178 | boolean booleanVal,
1179 | byte byteVal,
1180 | char charVal,
1181 | double doubleVal,
1182 | float floatVal,
1183 | int intVal,
1184 | long longVal,
1185 | short shortVal,
1186 | Boolean boxedBoolean,
1187 | Byte boxedByte,
1188 | Character boxedChar,
1189 | Double boxedDouble,
1190 | Float boxedFloat,
1191 | Integer boxedInt,
1192 | Long boxedLong,
1193 | Short boxedShort,
1194 | BigDecimal bigDecimal,
1195 | Date date) {
1196 | this(
1197 | booleanVal,
1198 | byteVal,
1199 | charVal,
1200 | doubleVal,
1201 | floatVal,
1202 | intVal,
1203 | longVal,
1204 | (short)0,
1205 | boxedBoolean,
1206 | boxedByte,
1207 | boxedChar,
1208 | boxedDouble,
1209 | boxedFloat,
1210 | boxedInt,
1211 | boxedLong,
1212 | boxedShort,
1213 | bigDecimal,
1214 | date,
1215 | null);
1216 | }
1217 |
1218 | public Foo(
1219 | boolean booleanVal,
1220 | byte byteVal,
1221 | char charVal,
1222 | double doubleVal,
1223 | float floatVal,
1224 | int intVal,
1225 | long longVal,
1226 | short shortVal,
1227 | Boolean boxedBoolean,
1228 | Byte boxedByte,
1229 | Character boxedChar,
1230 | Double boxedDouble,
1231 | Float boxedFloat,
1232 | Integer boxedInt,
1233 | Long boxedLong,
1234 | Short boxedShort,
1235 | BigDecimal bigDecimal,
1236 | Date date,
1237 | LocalDate localDate) {
1238 | this(
1239 | booleanVal,
1240 | byteVal,
1241 | charVal,
1242 | doubleVal,
1243 | floatVal,
1244 | intVal,
1245 | longVal,
1246 | (short)0,
1247 | boxedBoolean,
1248 | boxedByte,
1249 | boxedChar,
1250 | boxedDouble,
1251 | boxedFloat,
1252 | boxedInt,
1253 | boxedLong,
1254 | boxedShort,
1255 | bigDecimal,
1256 | date,
1257 | localDate,
1258 | null);
1259 | }
1260 |
1261 | public Foo(
1262 | boolean booleanVal,
1263 | byte byteVal,
1264 | char charVal,
1265 | double doubleVal,
1266 | float floatVal,
1267 | int intVal,
1268 | long longVal,
1269 | short shortVal,
1270 | Boolean boxedBoolean,
1271 | Byte boxedByte,
1272 | Character boxedChar,
1273 | Double boxedDouble,
1274 | Float boxedFloat,
1275 | Integer boxedInt,
1276 | Long boxedLong,
1277 | Short boxedShort,
1278 | BigDecimal bigDecimal,
1279 | Date date,
1280 | LocalDate localDate,
1281 | LocalDateTime localDateTime) {
1282 | this(
1283 | booleanVal,
1284 | byteVal,
1285 | charVal,
1286 | doubleVal,
1287 | floatVal,
1288 | intVal,
1289 | longVal,
1290 | (short)0,
1291 | boxedBoolean,
1292 | boxedByte,
1293 | boxedChar,
1294 | boxedDouble,
1295 | boxedFloat,
1296 | boxedInt,
1297 | boxedLong,
1298 | boxedShort,
1299 | bigDecimal,
1300 | date,
1301 | localDate,
1302 | localDateTime,
1303 | null);
1304 | }
1305 |
1306 | public Foo(
1307 | boolean booleanVal,
1308 | byte byteVal,
1309 | char charVal,
1310 | double doubleVal,
1311 | float floatVal,
1312 | int intVal,
1313 | long longVal,
1314 | short shortVal,
1315 | Boolean boxedBoolean,
1316 | Byte boxedByte,
1317 | Character boxedChar,
1318 | Double boxedDouble,
1319 | Float boxedFloat,
1320 | Integer boxedInt,
1321 | Long boxedLong,
1322 | Short boxedShort,
1323 | BigDecimal bigDecimal,
1324 | Date date,
1325 | LocalDate localDate,
1326 | LocalDateTime localDateTime,
1327 | String string) {
1328 | this.booleanVal = booleanVal;
1329 | this.byteVal = byteVal;
1330 | this.charVal = charVal;
1331 | this.doubleVal = doubleVal;
1332 | this.floatVal = floatVal;
1333 | this.intVal = intVal;
1334 | this.longVal = longVal;
1335 | this.shortVal = shortVal;
1336 | this.boxedBoolean = boxedBoolean;
1337 | this.boxedByte = boxedByte;
1338 | this.boxedChar = boxedChar;
1339 | this.boxedDouble = boxedDouble;
1340 | this.boxedFloat = boxedFloat;
1341 | this.boxedInt = boxedInt;
1342 | this.boxedLong = boxedLong;
1343 | this.boxedShort = boxedShort;
1344 | this.bigDecimal = bigDecimal;
1345 | this.date = date;
1346 | this.localDate = localDate;
1347 | this.localDateTime = localDateTime;
1348 | this.string = string;
1349 | }
1350 |
1351 | /* END Constructors */
1352 | }
1353 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/FooFactory.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import java.math.BigDecimal;
16 | import java.time.LocalDate;
17 | import java.time.LocalDateTime;
18 | import java.util.Date;
19 |
20 | public interface FooFactory {
21 | // public static class FooFactoryClassAccess2 implements MethodAccess {
22 | // @Override
23 | // public int methodIndex(String name, Class>... parameterTypes) {
24 | // switch (name) {
25 | // case "newInstance":
26 | // if (Arrays.equals(parameterTypes, new Class[] {})) {
27 | // return 0;
28 | // } else if (Arrays.equals(parameterTypes, new Class[] { boolean.class })) {
29 | // return 1;
30 | // } else if (Arrays.equals(parameterTypes, new Class[] { boolean.class, byte.class })) {
31 | // return 2;
32 | // }
33 | // break;
34 | // case "zzz":
35 | // if (Arrays.equals(parameterTypes, new Class[] {})) {
36 | // return 3;
37 | // }
38 | // break;
39 | // }
40 | // throw new IllegalArgumentException("No method called " + name
41 | // + " with parameters " + Arrays.stream(parameterTypes)
42 | // .map(Class::getName)
43 | // .collect(Collectors.toList()));
44 | // }
45 | //
46 | // @Override
47 | // public Object invoke(FooFactory obj, int methodIndex, Object... args) {
48 | // switch (methodIndex) {
49 | // case 0:
50 | // return obj.newInstance();
51 | // case 1:
52 | // return obj.newInstance((boolean)args[0]);
53 | // case 2:
54 | // return obj.newInstance((boolean)args[0], (byte)args[1]);
55 | // default:
56 | // throw new IllegalArgumentException("No method with index: " + methodIndex);
57 | // }
58 | // }
59 | //
60 | // @Override
61 | // public Object call(FooFactory obj, int methodIndex) {
62 | // switch (methodIndex) {
63 | // case 0:
64 | // return obj.newInstance();
65 | // case 3:
66 | // obj.zzz();
67 | // return null;
68 | // default:
69 | // throw new IllegalArgumentException("No method with index "
70 | // + methodIndex + " with 0 parameter(s)");
71 | // }
72 | // }
73 | //
74 | // @Override
75 | // public Object call(FooFactory obj, int methodIndex, Object arg0) {
76 | // switch (methodIndex) {
77 | // case 1:
78 | // return obj.newInstance((boolean)arg0);
79 | // default:
80 | // throw new IllegalArgumentException("No method with index "
81 | // + methodIndex + " with 1 parameter(s)");
82 | // }
83 | // }
84 | //
85 | // @Override
86 | // public Object call(FooFactory obj, int methodIndex, Object arg0, Object arg1) {
87 | // switch (methodIndex) {
88 | // case 2:
89 | // return obj.newInstance((boolean)arg0, (byte)arg1);
90 | // default:
91 | // throw new IllegalArgumentException("No method with index "
92 | // + methodIndex + " with 2 parameter(s)");
93 | // }
94 | // }
95 | // }
96 |
97 | Foo newInstance();
98 | Foo newInstance(boolean booleanVal);
99 | Foo newInstance(boolean booleanVal, byte byteVal);
100 | Foo newInstance(boolean booleanVal, byte byteVal, char charVal);
101 | Foo newInstance(boolean booleanVal, byte byteVal, char charVal, double doubleVal);
102 | Foo newInstance(
103 | boolean booleanVal,
104 | byte byteVal,
105 | char charVal,
106 | double doubleVal,
107 | float floatVal);
108 | Foo newInstance(
109 | boolean booleanVal,
110 | byte byteVal,
111 | char charVal,
112 | double doubleVal,
113 | float floatVal,
114 | int intVal);
115 | Foo newInstance(
116 | boolean booleanVal,
117 | byte byteVal,
118 | char charVal,
119 | double doubleVal,
120 | float floatVal,
121 | int intVal,
122 | long longVal);
123 | Foo newInstance(
124 | boolean booleanVal,
125 | byte byteVal,
126 | char charVal,
127 | double doubleVal,
128 | float floatVal,
129 | int intVal,
130 | long longVal,
131 | short shortVal);
132 | Foo newInstance(
133 | boolean booleanVal,
134 | byte byteVal,
135 | char charVal,
136 | double doubleVal,
137 | float floatVal,
138 | int intVal,
139 | long longVal,
140 | short shortVal,
141 | Boolean boxedBoolean);
142 | Foo newInstance(
143 | boolean booleanVal,
144 | byte byteVal,
145 | char charVal,
146 | double doubleVal,
147 | float floatVal,
148 | int intVal,
149 | long longVal,
150 | short shortVal,
151 | Boolean boxedBoolean,
152 | Byte boxedByte);
153 | Foo newInstance(
154 | boolean booleanVal,
155 | byte byteVal,
156 | char charVal,
157 | double doubleVal,
158 | float floatVal,
159 | int intVal,
160 | long longVal,
161 | short shortVal,
162 | Boolean boxedBoolean,
163 | Byte boxedByte,
164 | Character boxedChar);
165 | Foo newInstance(
166 | boolean booleanVal,
167 | byte byteVal,
168 | char charVal,
169 | double doubleVal,
170 | float floatVal,
171 | int intVal,
172 | long longVal,
173 | short shortVal,
174 | Boolean boxedBoolean,
175 | Byte boxedByte,
176 | Character boxedChar,
177 | Double boxedDouble);
178 | Foo newInstance(
179 | boolean booleanVal,
180 | byte byteVal,
181 | char charVal,
182 | double doubleVal,
183 | float floatVal,
184 | int intVal,
185 | long longVal,
186 | short shortVal,
187 | Boolean boxedBoolean,
188 | Byte boxedByte,
189 | Character boxedChar,
190 | Double boxedDouble,
191 | Float boxedFloat);
192 | Foo newInstance(
193 | boolean booleanVal,
194 | byte byteVal,
195 | char charVal,
196 | double doubleVal,
197 | float floatVal,
198 | int intVal,
199 | long longVal,
200 | short shortVal,
201 | Boolean boxedBoolean,
202 | Byte boxedByte,
203 | Character boxedChar,
204 | Double boxedDouble,
205 | Float boxedFloat,
206 | Integer boxedInt);
207 | Foo newInstance(
208 | boolean booleanVal,
209 | byte byteVal,
210 | char charVal,
211 | double doubleVal,
212 | float floatVal,
213 | int intVal,
214 | long longVal,
215 | short shortVal,
216 | Boolean boxedBoolean,
217 | Byte boxedByte,
218 | Character boxedChar,
219 | Double boxedDouble,
220 | Float boxedFloat,
221 | Integer boxedInt,
222 | Long boxedLong);
223 | Foo newInstance(
224 | boolean booleanVal,
225 | byte byteVal,
226 | char charVal,
227 | double doubleVal,
228 | float floatVal,
229 | int intVal,
230 | long longVal,
231 | short shortVal,
232 | Boolean boxedBoolean,
233 | Byte boxedByte,
234 | Character boxedChar,
235 | Double boxedDouble,
236 | Float boxedFloat,
237 | Integer boxedInt,
238 | Long boxedLong,
239 | Short boxedShort);
240 | Foo newInstance(
241 | boolean booleanVal,
242 | byte byteVal,
243 | char charVal,
244 | double doubleVal,
245 | float floatVal,
246 | int intVal,
247 | long longVal,
248 | short shortVal,
249 | Boolean boxedBoolean,
250 | Byte boxedByte,
251 | Character boxedChar,
252 | Double boxedDouble,
253 | Float boxedFloat,
254 | Integer boxedInt,
255 | Long boxedLong,
256 | Short boxedShort,
257 | BigDecimal bigDecimal);
258 | Foo newInstance(
259 | boolean booleanVal,
260 | byte byteVal,
261 | char charVal,
262 | double doubleVal,
263 | float floatVal,
264 | int intVal,
265 | long longVal,
266 | short shortVal,
267 | Boolean boxedBoolean,
268 | Byte boxedByte,
269 | Character boxedChar,
270 | Double boxedDouble,
271 | Float boxedFloat,
272 | Integer boxedInt,
273 | Long boxedLong,
274 | Short boxedShort,
275 | BigDecimal bigDecimal,
276 | Date date);
277 | Foo newInstance(
278 | boolean booleanVal,
279 | byte byteVal,
280 | char charVal,
281 | double doubleVal,
282 | float floatVal,
283 | int intVal,
284 | long longVal,
285 | short shortVal,
286 | Boolean boxedBoolean,
287 | Byte boxedByte,
288 | Character boxedChar,
289 | Double boxedDouble,
290 | Float boxedFloat,
291 | Integer boxedInt,
292 | Long boxedLong,
293 | Short boxedShort,
294 | BigDecimal bigDecimal,
295 | Date date,
296 | LocalDate localDate);
297 | Foo newInstance(
298 | boolean booleanVal,
299 | byte byteVal,
300 | char charVal,
301 | double doubleVal,
302 | float floatVal,
303 | int intVal,
304 | long longVal,
305 | short shortVal,
306 | Boolean boxedBoolean,
307 | Byte boxedByte,
308 | Character boxedChar,
309 | Double boxedDouble,
310 | Float boxedFloat,
311 | Integer boxedInt,
312 | Long boxedLong,
313 | Short boxedShort,
314 | BigDecimal bigDecimal,
315 | Date date,
316 | LocalDate localDate,
317 | LocalDateTime localDateTime);
318 | Foo newInstance(
319 | boolean booleanVal,
320 | byte byteVal,
321 | char charVal,
322 | double doubleVal,
323 | float floatVal,
324 | int intVal,
325 | long longVal,
326 | short shortVal,
327 | Boolean boxedBoolean,
328 | Byte boxedByte,
329 | Character boxedChar,
330 | Double boxedDouble,
331 | Float boxedFloat,
332 | Integer boxedInt,
333 | Long boxedLong,
334 | Short boxedShort,
335 | BigDecimal bigDecimal,
336 | Date date,
337 | LocalDate localDate,
338 | LocalDateTime localDateTime,
339 | String string);
340 | void zzz();
341 | }
342 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/Main.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static java.util.Comparator.*;
16 | import static java.util.stream.Collectors.*;
17 |
18 | import java.beans.BeanInfo;
19 | import java.beans.IntrospectionException;
20 | import java.beans.Introspector;
21 | import java.beans.PropertyDescriptor;
22 | import java.lang.reflect.Field;
23 | import java.lang.reflect.Method;
24 | import java.util.Arrays;
25 | import java.util.List;
26 | import java.util.stream.Collectors;
27 |
28 | import com.github.javalbert.reflection.ClassAccess;
29 | import com.github.javalbert.reflection.ClassAccessFactory;
30 |
31 | public class Main {
32 | public static void main(String[] args) throws IntrospectionException {
33 | Main main = new Main();
34 | main.printInfo(Foo.class);
35 | main.printInfo(FooFactory.class);
36 | }
37 |
38 | public void printInfo(Class clazz) throws IntrospectionException {
39 | ClassAccess access = ClassAccessFactory.get(clazz);
40 |
41 | BeanInfo fooInfo = Introspector.getBeanInfo(clazz);
42 |
43 | System.out.println("\n========== " + clazz + " ==========");
44 |
45 | System.out.println("\nFIELDS\n");
46 |
47 | List fields = Arrays.stream(clazz.getDeclaredFields())
48 | .sorted(comparing(Field::getName))
49 | .collect(toList());
50 | for (int i = 0; i < fields.size(); i++) {
51 | Field field = fields.get(i);
52 | System.out.println(field.getName() + " = "
53 | + access.fieldIndex(field.getName())
54 | );
55 | }
56 |
57 | System.out.println("\nPROPERTIES\n");
58 |
59 | List propertyDescriptors = Arrays.stream(fooInfo.getPropertyDescriptors())
60 | .filter(prop -> !prop.getName().equals("class"))
61 | .collect(Collectors.toList());
62 | for (int i = 0; i < propertyDescriptors.size(); i++) {
63 | PropertyDescriptor propertyDescriptor = propertyDescriptors.get(i);
64 | System.out.println(propertyDescriptor.getName() + " = "
65 | + access.propertyIndex(propertyDescriptor.getName())
66 | );
67 | }
68 |
69 | System.out.println("\nMETHODS\n");
70 |
71 | List methods = Arrays.stream(clazz.getDeclaredMethods())
72 | .sorted((a, b) -> {
73 | int compareMethodName = a.getName().compareTo(b.getName());
74 | if (compareMethodName != 0) {
75 | return compareMethodName;
76 | }
77 |
78 | Class>[] aparams = a.getParameterTypes();
79 | Class>[] bparams = b.getParameterTypes();
80 |
81 | int len = Math.min(aparams.length, bparams.length);
82 | for (int i = 0; i < len; i++) {
83 | int compareParamType = aparams[i].getName().compareTo(bparams[i].getName());
84 | if (compareParamType != 0) {
85 | return compareParamType;
86 | }
87 | }
88 | return Integer.compare(aparams.length, bparams.length);
89 | }).collect(toList());
90 | for (int i = 0; i < methods.size(); i++) {
91 | Method method = methods.get(i);
92 | System.out.println(method.getName() + Arrays.stream(method.getParameterTypes())
93 | .map(Class::getName)
94 | .collect(toList())
95 | + " = " + access.methodIndex(method.getName(), method.getParameterTypes())
96 | );
97 | }
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/MethodAccessInvokeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.javalbert.reflection.test;
2 |
3 | import static org.hamcrest.CoreMatchers.notNullValue;
4 | import static org.hamcrest.CoreMatchers.nullValue;
5 | import static org.junit.Assert.assertThat;
6 | import static org.mockito.Mockito.mock;
7 | import static org.mockito.Mockito.verify;
8 | import static org.mockito.Mockito.when;
9 |
10 | import java.math.BigDecimal;
11 | import java.time.LocalDate;
12 | import java.time.LocalDateTime;
13 | import java.util.Date;
14 |
15 | import org.junit.Test;
16 |
17 | import com.github.javalbert.reflection.ClassAccessFactory;
18 | import com.github.javalbert.reflection.MethodAccess;
19 |
20 | public class MethodAccessInvokeTest {
21 | @Test
22 | public void invokeMethodWithNoReturnValue() {
23 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
24 | FooFactory factory = mock(FooFactory.class);
25 |
26 | Object nullRetVal = access.invoke(factory, access.methodIndex("zzz"));
27 |
28 | verify(factory).zzz();
29 | assertThat(nullRetVal, nullValue());
30 | }
31 |
32 | @Test
33 | public void invokeMethodWith0Parameters() {
34 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
35 | FooFactory factory = mock(FooFactory.class);
36 | when(factory.newInstance()).thenReturn(new Foo());
37 |
38 | Object retVal = access.invoke(factory, access.methodIndex("newInstance"));
39 |
40 | verify(factory).newInstance();
41 | assertThat(retVal, notNullValue());
42 | }
43 |
44 | @Test
45 | public void invokeMethodWith1Parameter() {
46 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
47 | FooFactory factory = mock(FooFactory.class);
48 | when(factory.newInstance(
49 | true))
50 | .thenReturn(new Foo(
51 | true));
52 |
53 | int methodIndex = access.methodIndex(
54 | "newInstance",
55 | boolean.class);
56 | Object retVal = access.invoke(
57 | factory,
58 | methodIndex,
59 | true);
60 |
61 | verify(factory).newInstance(
62 | true);
63 | assertThat(retVal, notNullValue());
64 | }
65 |
66 | @Test
67 | public void invokeMethodWith2Parameters() {
68 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
69 | FooFactory factory = mock(FooFactory.class);
70 | when(factory.newInstance(
71 | true,
72 | (byte)-55))
73 | .thenReturn(new Foo(
74 | true,
75 | (byte)-55));
76 |
77 | int methodIndex = access.methodIndex(
78 | "newInstance",
79 | boolean.class,
80 | byte.class);
81 | Object retVal = access.invoke(
82 | factory,
83 | methodIndex,
84 | true,
85 | (byte)-55);
86 |
87 | verify(factory).newInstance(
88 | true,
89 | (byte)-55);
90 | assertThat(retVal, notNullValue());
91 | }
92 |
93 | @Test
94 | public void invokeMethodWith3Parameters() {
95 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
96 | FooFactory factory = mock(FooFactory.class);
97 | when(factory.newInstance(
98 | true,
99 | (byte)-55,
100 | '\n'))
101 | .thenReturn(new Foo(
102 | true,
103 | (byte)-55,
104 | '\n'));
105 |
106 | int methodIndex = access.methodIndex(
107 | "newInstance",
108 | boolean.class,
109 | byte.class,
110 | char.class);
111 | Object retVal = access.invoke(
112 | factory,
113 | methodIndex,
114 | true,
115 | (byte)-55,
116 | '\n');
117 |
118 | verify(factory).newInstance(
119 | true,
120 | (byte)-55,
121 | '\n');
122 | assertThat(retVal, notNullValue());
123 | }
124 |
125 | @Test
126 | public void invokeMethodWith4Parameters() {
127 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
128 | FooFactory factory = mock(FooFactory.class);
129 | when(factory.newInstance(
130 | true,
131 | (byte)-55,
132 | '\n',
133 | 0.9553896885798474d))
134 | .thenReturn(new Foo(
135 | true,
136 | (byte)-55,
137 | '\n',
138 | 0.9553896885798474d));
139 |
140 | int methodIndex = access.methodIndex(
141 | "newInstance",
142 | boolean.class,
143 | byte.class,
144 | char.class,
145 | double.class);
146 | Object retVal = access.invoke(
147 | factory,
148 | methodIndex,
149 | true,
150 | (byte)-55,
151 | '\n',
152 | 0.9553896885798474d);
153 |
154 | verify(factory).newInstance(
155 | true,
156 | (byte)-55,
157 | '\n',
158 | 0.9553896885798474d);
159 | assertThat(retVal, notNullValue());
160 | }
161 |
162 | @Test
163 | public void invokeMethodWith5Parameters() {
164 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
165 | FooFactory factory = mock(FooFactory.class);
166 | when(factory.newInstance(
167 | true,
168 | (byte)-55,
169 | '\n',
170 | 0.9553896885798474d,
171 | 0.2747032f))
172 | .thenReturn(new Foo(
173 | true,
174 | (byte)-55,
175 | '\n',
176 | 0.9553896885798474d,
177 | 0.2747032f));
178 |
179 | int methodIndex = access.methodIndex(
180 | "newInstance",
181 | boolean.class,
182 | byte.class,
183 | char.class,
184 | double.class,
185 | float.class);
186 | Object retVal = access.invoke(
187 | factory,
188 | methodIndex,
189 | true,
190 | (byte)-55,
191 | '\n',
192 | 0.9553896885798474d,
193 | 0.2747032f);
194 |
195 | verify(factory).newInstance(
196 | true,
197 | (byte)-55,
198 | '\n',
199 | 0.9553896885798474d,
200 | 0.2747032f);
201 | assertThat(retVal, notNullValue());
202 | }
203 |
204 | @Test
205 | public void invokeMethodWith6Parameters() {
206 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
207 | FooFactory factory = mock(FooFactory.class);
208 | when(factory.newInstance(
209 | true,
210 | (byte)-55,
211 | '\n',
212 | 0.9553896885798474d,
213 | 0.2747032f,
214 | 274991538))
215 | .thenReturn(new Foo(
216 | true,
217 | (byte)-55,
218 | '\n',
219 | 0.9553896885798474d,
220 | 0.2747032f,
221 | 274991538));
222 |
223 | int methodIndex = access.methodIndex(
224 | "newInstance",
225 | boolean.class,
226 | byte.class,
227 | char.class,
228 | double.class,
229 | float.class,
230 | int.class);
231 | Object retVal = access.invoke(
232 | factory,
233 | methodIndex,
234 | true,
235 | (byte)-55,
236 | '\n',
237 | 0.9553896885798474d,
238 | 0.2747032f,
239 | 274991538);
240 |
241 | verify(factory).newInstance(
242 | true,
243 | (byte)-55,
244 | '\n',
245 | 0.9553896885798474d,
246 | 0.2747032f,
247 | 274991538);
248 | assertThat(retVal, notNullValue());
249 | }
250 |
251 | @Test
252 | public void invokeMethodWith7Parameters() {
253 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
254 | FooFactory factory = mock(FooFactory.class);
255 | when(factory.newInstance(
256 | true,
257 | (byte)-55,
258 | '\n',
259 | 0.9553896885798474d,
260 | 0.2747032f,
261 | 274991538,
262 | 6774924159498401640L))
263 | .thenReturn(new Foo(
264 | true,
265 | (byte)-55,
266 | '\n',
267 | 0.9553896885798474d,
268 | 0.2747032f,
269 | 274991538,
270 | 6774924159498401640L));
271 |
272 | int methodIndex = access.methodIndex(
273 | "newInstance",
274 | boolean.class,
275 | byte.class,
276 | char.class,
277 | double.class,
278 | float.class,
279 | int.class,
280 | long.class);
281 | Object retVal = access.invoke(
282 | factory,
283 | methodIndex,
284 | true,
285 | (byte)-55,
286 | '\n',
287 | 0.9553896885798474d,
288 | 0.2747032f,
289 | 274991538,
290 | 6774924159498401640L);
291 |
292 | verify(factory).newInstance(
293 | true,
294 | (byte)-55,
295 | '\n',
296 | 0.9553896885798474d,
297 | 0.2747032f,
298 | 274991538,
299 | 6774924159498401640L);
300 | assertThat(retVal, notNullValue());
301 | }
302 |
303 | @Test
304 | public void invokeMethodWith8Parameters() {
305 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
306 | FooFactory factory = mock(FooFactory.class);
307 | when(factory.newInstance(
308 | true,
309 | (byte)-55,
310 | '\n',
311 | 0.9553896885798474d,
312 | 0.2747032f,
313 | 274991538,
314 | 6774924159498401640L,
315 | (short)-31848))
316 | .thenReturn(new Foo(
317 | true,
318 | (byte)-55,
319 | '\n',
320 | 0.9553896885798474d,
321 | 0.2747032f,
322 | 274991538,
323 | 6774924159498401640L,
324 | (short)-31848));
325 |
326 | int methodIndex = access.methodIndex(
327 | "newInstance",
328 | boolean.class,
329 | byte.class,
330 | char.class,
331 | double.class,
332 | float.class,
333 | int.class,
334 | long.class,
335 | short.class);
336 | Object retVal = access.invoke(
337 | factory,
338 | methodIndex,
339 | true,
340 | (byte)-55,
341 | '\n',
342 | 0.9553896885798474d,
343 | 0.2747032f,
344 | 274991538,
345 | 6774924159498401640L,
346 | (short)-31848);
347 |
348 | verify(factory).newInstance(
349 | true,
350 | (byte)-55,
351 | '\n',
352 | 0.9553896885798474d,
353 | 0.2747032f,
354 | 274991538,
355 | 6774924159498401640L,
356 | (short)-31848);
357 | assertThat(retVal, notNullValue());
358 | }
359 |
360 | @Test
361 | public void invokeMethodWith9Parameters() {
362 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
363 | FooFactory factory = mock(FooFactory.class);
364 | when(factory.newInstance(
365 | true,
366 | (byte)-55,
367 | '\n',
368 | 0.9553896885798474d,
369 | 0.2747032f,
370 | 274991538,
371 | 6774924159498401640L,
372 | (short)-31848,
373 | true))
374 | .thenReturn(new Foo(
375 | true,
376 | (byte)-55,
377 | '\n',
378 | 0.9553896885798474d,
379 | 0.2747032f,
380 | 274991538,
381 | 6774924159498401640L,
382 | (short)-31848,
383 | true));
384 |
385 | int methodIndex = access.methodIndex(
386 | "newInstance",
387 | boolean.class,
388 | byte.class,
389 | char.class,
390 | double.class,
391 | float.class,
392 | int.class,
393 | long.class,
394 | short.class,
395 | Boolean.class);
396 | Object retVal = access.invoke(
397 | factory,
398 | methodIndex,
399 | true,
400 | (byte)-55,
401 | '\n',
402 | 0.9553896885798474d,
403 | 0.2747032f,
404 | 274991538,
405 | 6774924159498401640L,
406 | (short)-31848,
407 | true);
408 |
409 | verify(factory).newInstance(
410 | true,
411 | (byte)-55,
412 | '\n',
413 | 0.9553896885798474d,
414 | 0.2747032f,
415 | 274991538,
416 | 6774924159498401640L,
417 | (short)-31848,
418 | true);
419 | assertThat(retVal, notNullValue());
420 | }
421 |
422 | @Test
423 | public void invokeMethodWith10Parameters() {
424 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
425 | FooFactory factory = mock(FooFactory.class);
426 | when(factory.newInstance(
427 | true,
428 | (byte)-55,
429 | '\n',
430 | 0.9553896885798474d,
431 | 0.2747032f,
432 | 274991538,
433 | 6774924159498401640L,
434 | (short)-31848,
435 | true,
436 | (byte)-55))
437 | .thenReturn(new Foo(
438 | true,
439 | (byte)-55,
440 | '\n',
441 | 0.9553896885798474d,
442 | 0.2747032f,
443 | 274991538,
444 | 6774924159498401640L,
445 | (short)-31848,
446 | true,
447 | (byte)-55));
448 |
449 | int methodIndex = access.methodIndex(
450 | "newInstance",
451 | boolean.class,
452 | byte.class,
453 | char.class,
454 | double.class,
455 | float.class,
456 | int.class,
457 | long.class,
458 | short.class,
459 | Boolean.class,
460 | Byte.class);
461 | Object retVal = access.invoke(
462 | factory,
463 | methodIndex,
464 | true,
465 | (byte)-55,
466 | '\n',
467 | 0.9553896885798474d,
468 | 0.2747032f,
469 | 274991538,
470 | 6774924159498401640L,
471 | (short)-31848,
472 | true,
473 | (byte)-55);
474 |
475 | verify(factory).newInstance(
476 | true,
477 | (byte)-55,
478 | '\n',
479 | 0.9553896885798474d,
480 | 0.2747032f,
481 | 274991538,
482 | 6774924159498401640L,
483 | (short)-31848,
484 | true,
485 | (byte)-55);
486 | assertThat(retVal, notNullValue());
487 | }
488 |
489 | @Test
490 | public void invokeMethodWith11Parameters() {
491 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
492 | FooFactory factory = mock(FooFactory.class);
493 | when(factory.newInstance(
494 | true,
495 | (byte)-55,
496 | '\n',
497 | 0.9553896885798474d,
498 | 0.2747032f,
499 | 274991538,
500 | 6774924159498401640L,
501 | (short)-31848,
502 | true,
503 | (byte)-55,
504 | '\n'))
505 | .thenReturn(new Foo(
506 | true,
507 | (byte)-55,
508 | '\n',
509 | 0.9553896885798474d,
510 | 0.2747032f,
511 | 274991538,
512 | 6774924159498401640L,
513 | (short)-31848,
514 | true,
515 | (byte)-55,
516 | '\n'));
517 |
518 | int methodIndex = access.methodIndex(
519 | "newInstance",
520 | boolean.class,
521 | byte.class,
522 | char.class,
523 | double.class,
524 | float.class,
525 | int.class,
526 | long.class,
527 | short.class,
528 | Boolean.class,
529 | Byte.class,
530 | Character.class);
531 | Object retVal = access.invoke(
532 | factory,
533 | methodIndex,
534 | true,
535 | (byte)-55,
536 | '\n',
537 | 0.9553896885798474d,
538 | 0.2747032f,
539 | 274991538,
540 | 6774924159498401640L,
541 | (short)-31848,
542 | true,
543 | (byte)-55,
544 | '\n');
545 |
546 | verify(factory).newInstance(
547 | true,
548 | (byte)-55,
549 | '\n',
550 | 0.9553896885798474d,
551 | 0.2747032f,
552 | 274991538,
553 | 6774924159498401640L,
554 | (short)-31848,
555 | true,
556 | (byte)-55,
557 | '\n');
558 | assertThat(retVal, notNullValue());
559 | }
560 |
561 | @Test
562 | public void invokeMethodWith12Parameters() {
563 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
564 | FooFactory factory = mock(FooFactory.class);
565 | when(factory.newInstance(
566 | true,
567 | (byte)-55,
568 | '\n',
569 | 0.9553896885798474d,
570 | 0.2747032f,
571 | 274991538,
572 | 6774924159498401640L,
573 | (short)-31848,
574 | true,
575 | (byte)-55,
576 | '\n',
577 | 0.9553896885798474d))
578 | .thenReturn(new Foo(
579 | true,
580 | (byte)-55,
581 | '\n',
582 | 0.9553896885798474d,
583 | 0.2747032f,
584 | 274991538,
585 | 6774924159498401640L,
586 | (short)-31848,
587 | true,
588 | (byte)-55,
589 | '\n',
590 | 0.9553896885798474d));
591 |
592 | int methodIndex = access.methodIndex(
593 | "newInstance",
594 | boolean.class,
595 | byte.class,
596 | char.class,
597 | double.class,
598 | float.class,
599 | int.class,
600 | long.class,
601 | short.class,
602 | Boolean.class,
603 | Byte.class,
604 | Character.class,
605 | Double.class);
606 | Object retVal = access.invoke(
607 | factory,
608 | methodIndex,
609 | true,
610 | (byte)-55,
611 | '\n',
612 | 0.9553896885798474d,
613 | 0.2747032f,
614 | 274991538,
615 | 6774924159498401640L,
616 | (short)-31848,
617 | true,
618 | (byte)-55,
619 | '\n',
620 | 0.9553896885798474d);
621 |
622 | verify(factory).newInstance(
623 | true,
624 | (byte)-55,
625 | '\n',
626 | 0.9553896885798474d,
627 | 0.2747032f,
628 | 274991538,
629 | 6774924159498401640L,
630 | (short)-31848,
631 | true,
632 | (byte)-55,
633 | '\n',
634 | 0.9553896885798474d);
635 | assertThat(retVal, notNullValue());
636 | }
637 |
638 | @Test
639 | public void invokeMethodWith13Parameters() {
640 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
641 | FooFactory factory = mock(FooFactory.class);
642 | when(factory.newInstance(
643 | true,
644 | (byte)-55,
645 | '\n',
646 | 0.9553896885798474d,
647 | 0.2747032f,
648 | 274991538,
649 | 6774924159498401640L,
650 | (short)-31848,
651 | true,
652 | (byte)-55,
653 | '\n',
654 | 0.9553896885798474d,
655 | 0.2747032f))
656 | .thenReturn(new Foo(
657 | true,
658 | (byte)-55,
659 | '\n',
660 | 0.9553896885798474d,
661 | 0.2747032f,
662 | 274991538,
663 | 6774924159498401640L,
664 | (short)-31848,
665 | true,
666 | (byte)-55,
667 | '\n',
668 | 0.9553896885798474d,
669 | 0.2747032f));
670 |
671 | int methodIndex = access.methodIndex(
672 | "newInstance",
673 | boolean.class,
674 | byte.class,
675 | char.class,
676 | double.class,
677 | float.class,
678 | int.class,
679 | long.class,
680 | short.class,
681 | Boolean.class,
682 | Byte.class,
683 | Character.class,
684 | Double.class,
685 | Float.class);
686 | Object retVal = access.invoke(
687 | factory,
688 | methodIndex,
689 | true,
690 | (byte)-55,
691 | '\n',
692 | 0.9553896885798474d,
693 | 0.2747032f,
694 | 274991538,
695 | 6774924159498401640L,
696 | (short)-31848,
697 | true,
698 | (byte)-55,
699 | '\n',
700 | 0.9553896885798474d,
701 | 0.2747032f);
702 |
703 | verify(factory).newInstance(
704 | true,
705 | (byte)-55,
706 | '\n',
707 | 0.9553896885798474d,
708 | 0.2747032f,
709 | 274991538,
710 | 6774924159498401640L,
711 | (short)-31848,
712 | true,
713 | (byte)-55,
714 | '\n',
715 | 0.9553896885798474d,
716 | 0.2747032f);
717 | assertThat(retVal, notNullValue());
718 | }
719 |
720 | @Test
721 | public void invokeMethodWith14Parameters() {
722 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
723 | FooFactory factory = mock(FooFactory.class);
724 | when(factory.newInstance(
725 | true,
726 | (byte)-55,
727 | '\n',
728 | 0.9553896885798474d,
729 | 0.2747032f,
730 | 274991538,
731 | 6774924159498401640L,
732 | (short)-31848,
733 | true,
734 | (byte)-55,
735 | '\n',
736 | 0.9553896885798474d,
737 | 0.2747032f,
738 | 274991538))
739 | .thenReturn(new Foo(
740 | true,
741 | (byte)-55,
742 | '\n',
743 | 0.9553896885798474d,
744 | 0.2747032f,
745 | 274991538,
746 | 6774924159498401640L,
747 | (short)-31848,
748 | true,
749 | (byte)-55,
750 | '\n',
751 | 0.9553896885798474d,
752 | 0.2747032f,
753 | 274991538));
754 |
755 | int methodIndex = access.methodIndex(
756 | "newInstance",
757 | boolean.class,
758 | byte.class,
759 | char.class,
760 | double.class,
761 | float.class,
762 | int.class,
763 | long.class,
764 | short.class,
765 | Boolean.class,
766 | Byte.class,
767 | Character.class,
768 | Double.class,
769 | Float.class,
770 | Integer.class);
771 | Object retVal = access.invoke(
772 | factory,
773 | methodIndex,
774 | true,
775 | (byte)-55,
776 | '\n',
777 | 0.9553896885798474d,
778 | 0.2747032f,
779 | 274991538,
780 | 6774924159498401640L,
781 | (short)-31848,
782 | true,
783 | (byte)-55,
784 | '\n',
785 | 0.9553896885798474d,
786 | 0.2747032f,
787 | 274991538);
788 |
789 | verify(factory).newInstance(
790 | true,
791 | (byte)-55,
792 | '\n',
793 | 0.9553896885798474d,
794 | 0.2747032f,
795 | 274991538,
796 | 6774924159498401640L,
797 | (short)-31848,
798 | true,
799 | (byte)-55,
800 | '\n',
801 | 0.9553896885798474d,
802 | 0.2747032f,
803 | 274991538);
804 | assertThat(retVal, notNullValue());
805 | }
806 |
807 | @Test
808 | public void invokeMethodWith15Parameters() {
809 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
810 | FooFactory factory = mock(FooFactory.class);
811 | when(factory.newInstance(
812 | true,
813 | (byte)-55,
814 | '\n',
815 | 0.9553896885798474d,
816 | 0.2747032f,
817 | 274991538,
818 | 6774924159498401640L,
819 | (short)-31848,
820 | true,
821 | (byte)-55,
822 | '\n',
823 | 0.9553896885798474d,
824 | 0.2747032f,
825 | 274991538,
826 | 6774924159498401640L))
827 | .thenReturn(new Foo(
828 | true,
829 | (byte)-55,
830 | '\n',
831 | 0.9553896885798474d,
832 | 0.2747032f,
833 | 274991538,
834 | 6774924159498401640L,
835 | (short)-31848,
836 | true,
837 | (byte)-55,
838 | '\n',
839 | 0.9553896885798474d,
840 | 0.2747032f,
841 | 274991538,
842 | 6774924159498401640L));
843 |
844 | int methodIndex = access.methodIndex(
845 | "newInstance",
846 | boolean.class,
847 | byte.class,
848 | char.class,
849 | double.class,
850 | float.class,
851 | int.class,
852 | long.class,
853 | short.class,
854 | Boolean.class,
855 | Byte.class,
856 | Character.class,
857 | Double.class,
858 | Float.class,
859 | Integer.class,
860 | Long.class);
861 | Object retVal = access.invoke(
862 | factory,
863 | methodIndex,
864 | true,
865 | (byte)-55,
866 | '\n',
867 | 0.9553896885798474d,
868 | 0.2747032f,
869 | 274991538,
870 | 6774924159498401640L,
871 | (short)-31848,
872 | true,
873 | (byte)-55,
874 | '\n',
875 | 0.9553896885798474d,
876 | 0.2747032f,
877 | 274991538,
878 | 6774924159498401640L);
879 |
880 | verify(factory).newInstance(
881 | true,
882 | (byte)-55,
883 | '\n',
884 | 0.9553896885798474d,
885 | 0.2747032f,
886 | 274991538,
887 | 6774924159498401640L,
888 | (short)-31848,
889 | true,
890 | (byte)-55,
891 | '\n',
892 | 0.9553896885798474d,
893 | 0.2747032f,
894 | 274991538,
895 | 6774924159498401640L);
896 | assertThat(retVal, notNullValue());
897 | }
898 |
899 | @Test
900 | public void invokeMethodWith16Parameters() {
901 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
902 | FooFactory factory = mock(FooFactory.class);
903 | when(factory.newInstance(
904 | true,
905 | (byte)-55,
906 | '\n',
907 | 0.9553896885798474d,
908 | 0.2747032f,
909 | 274991538,
910 | 6774924159498401640L,
911 | (short)-31848,
912 | true,
913 | (byte)-55,
914 | '\n',
915 | 0.9553896885798474d,
916 | 0.2747032f,
917 | 274991538,
918 | 6774924159498401640L,
919 | (short)-31848))
920 | .thenReturn(new Foo(
921 | true,
922 | (byte)-55,
923 | '\n',
924 | 0.9553896885798474d,
925 | 0.2747032f,
926 | 274991538,
927 | 6774924159498401640L,
928 | (short)-31848,
929 | true,
930 | (byte)-55,
931 | '\n',
932 | 0.9553896885798474d,
933 | 0.2747032f,
934 | 274991538,
935 | 6774924159498401640L,
936 | (short)-31848));
937 |
938 | int methodIndex = access.methodIndex(
939 | "newInstance",
940 | boolean.class,
941 | byte.class,
942 | char.class,
943 | double.class,
944 | float.class,
945 | int.class,
946 | long.class,
947 | short.class,
948 | Boolean.class,
949 | Byte.class,
950 | Character.class,
951 | Double.class,
952 | Float.class,
953 | Integer.class,
954 | Long.class,
955 | Short.class);
956 | Object retVal = access.invoke(
957 | factory,
958 | methodIndex,
959 | true,
960 | (byte)-55,
961 | '\n',
962 | 0.9553896885798474d,
963 | 0.2747032f,
964 | 274991538,
965 | 6774924159498401640L,
966 | (short)-31848,
967 | true,
968 | (byte)-55,
969 | '\n',
970 | 0.9553896885798474d,
971 | 0.2747032f,
972 | 274991538,
973 | 6774924159498401640L,
974 | (short)-31848);
975 |
976 | verify(factory).newInstance(
977 | true,
978 | (byte)-55,
979 | '\n',
980 | 0.9553896885798474d,
981 | 0.2747032f,
982 | 274991538,
983 | 6774924159498401640L,
984 | (short)-31848,
985 | true,
986 | (byte)-55,
987 | '\n',
988 | 0.9553896885798474d,
989 | 0.2747032f,
990 | 274991538,
991 | 6774924159498401640L,
992 | (short)-31848);
993 | assertThat(retVal, notNullValue());
994 | }
995 |
996 | @Test
997 | public void invokeMethodWith17Parameters() {
998 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
999 | FooFactory factory = mock(FooFactory.class);
1000 | when(factory.newInstance(
1001 | true,
1002 | (byte)-55,
1003 | '\n',
1004 | 0.9553896885798474d,
1005 | 0.2747032f,
1006 | 274991538,
1007 | 6774924159498401640L,
1008 | (short)-31848,
1009 | true,
1010 | (byte)-55,
1011 | '\n',
1012 | 0.9553896885798474d,
1013 | 0.2747032f,
1014 | 274991538,
1015 | 6774924159498401640L,
1016 | (short)-31848,
1017 | new BigDecimal("123.456")))
1018 | .thenReturn(new Foo(
1019 | true,
1020 | (byte)-55,
1021 | '\n',
1022 | 0.9553896885798474d,
1023 | 0.2747032f,
1024 | 274991538,
1025 | 6774924159498401640L,
1026 | (short)-31848,
1027 | true,
1028 | (byte)-55,
1029 | '\n',
1030 | 0.9553896885798474d,
1031 | 0.2747032f,
1032 | 274991538,
1033 | 6774924159498401640L,
1034 | (short)-31848,
1035 | new BigDecimal("123.456")));
1036 |
1037 | int methodIndex = access.methodIndex(
1038 | "newInstance",
1039 | boolean.class,
1040 | byte.class,
1041 | char.class,
1042 | double.class,
1043 | float.class,
1044 | int.class,
1045 | long.class,
1046 | short.class,
1047 | Boolean.class,
1048 | Byte.class,
1049 | Character.class,
1050 | Double.class,
1051 | Float.class,
1052 | Integer.class,
1053 | Long.class,
1054 | Short.class,
1055 | BigDecimal.class);
1056 | Object retVal = access.invoke(
1057 | factory,
1058 | methodIndex,
1059 | true,
1060 | (byte)-55,
1061 | '\n',
1062 | 0.9553896885798474d,
1063 | 0.2747032f,
1064 | 274991538,
1065 | 6774924159498401640L,
1066 | (short)-31848,
1067 | true,
1068 | (byte)-55,
1069 | '\n',
1070 | 0.9553896885798474d,
1071 | 0.2747032f,
1072 | 274991538,
1073 | 6774924159498401640L,
1074 | (short)-31848,
1075 | new BigDecimal("123.456"));
1076 |
1077 | verify(factory).newInstance(
1078 | true,
1079 | (byte)-55,
1080 | '\n',
1081 | 0.9553896885798474d,
1082 | 0.2747032f,
1083 | 274991538,
1084 | 6774924159498401640L,
1085 | (short)-31848,
1086 | true,
1087 | (byte)-55,
1088 | '\n',
1089 | 0.9553896885798474d,
1090 | 0.2747032f,
1091 | 274991538,
1092 | 6774924159498401640L,
1093 | (short)-31848,
1094 | new BigDecimal("123.456"));
1095 | assertThat(retVal, notNullValue());
1096 | }
1097 |
1098 | @Test
1099 | public void invokeMethodWith18Parameters() {
1100 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
1101 | FooFactory factory = mock(FooFactory.class);
1102 | Date date = new Date();
1103 | when(factory.newInstance(
1104 | true,
1105 | (byte)-55,
1106 | '\n',
1107 | 0.9553896885798474d,
1108 | 0.2747032f,
1109 | 274991538,
1110 | 6774924159498401640L,
1111 | (short)-31848,
1112 | true,
1113 | (byte)-55,
1114 | '\n',
1115 | 0.9553896885798474d,
1116 | 0.2747032f,
1117 | 274991538,
1118 | 6774924159498401640L,
1119 | (short)-31848,
1120 | new BigDecimal("123.456"),
1121 | date))
1122 | .thenReturn(new Foo(
1123 | true,
1124 | (byte)-55,
1125 | '\n',
1126 | 0.9553896885798474d,
1127 | 0.2747032f,
1128 | 274991538,
1129 | 6774924159498401640L,
1130 | (short)-31848,
1131 | true,
1132 | (byte)-55,
1133 | '\n',
1134 | 0.9553896885798474d,
1135 | 0.2747032f,
1136 | 274991538,
1137 | 6774924159498401640L,
1138 | (short)-31848,
1139 | new BigDecimal("123.456"),
1140 | date));
1141 |
1142 | int methodIndex = access.methodIndex(
1143 | "newInstance",
1144 | boolean.class,
1145 | byte.class,
1146 | char.class,
1147 | double.class,
1148 | float.class,
1149 | int.class,
1150 | long.class,
1151 | short.class,
1152 | Boolean.class,
1153 | Byte.class,
1154 | Character.class,
1155 | Double.class,
1156 | Float.class,
1157 | Integer.class,
1158 | Long.class,
1159 | Short.class,
1160 | BigDecimal.class,
1161 | Date.class);
1162 | Object retVal = access.invoke(
1163 | factory,
1164 | methodIndex,
1165 | true,
1166 | (byte)-55,
1167 | '\n',
1168 | 0.9553896885798474d,
1169 | 0.2747032f,
1170 | 274991538,
1171 | 6774924159498401640L,
1172 | (short)-31848,
1173 | true,
1174 | (byte)-55,
1175 | '\n',
1176 | 0.9553896885798474d,
1177 | 0.2747032f,
1178 | 274991538,
1179 | 6774924159498401640L,
1180 | (short)-31848,
1181 | new BigDecimal("123.456"),
1182 | date);
1183 |
1184 | verify(factory).newInstance(
1185 | true,
1186 | (byte)-55,
1187 | '\n',
1188 | 0.9553896885798474d,
1189 | 0.2747032f,
1190 | 274991538,
1191 | 6774924159498401640L,
1192 | (short)-31848,
1193 | true,
1194 | (byte)-55,
1195 | '\n',
1196 | 0.9553896885798474d,
1197 | 0.2747032f,
1198 | 274991538,
1199 | 6774924159498401640L,
1200 | (short)-31848,
1201 | new BigDecimal("123.456"),
1202 | date);
1203 | assertThat(retVal, notNullValue());
1204 | }
1205 |
1206 | @Test
1207 | public void invokeMethodWith19Parameters() {
1208 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
1209 | FooFactory factory = mock(FooFactory.class);
1210 | Date date = new Date();
1211 | LocalDate localDate = LocalDate.now();
1212 | when(factory.newInstance(
1213 | true,
1214 | (byte)-55,
1215 | '\n',
1216 | 0.9553896885798474d,
1217 | 0.2747032f,
1218 | 274991538,
1219 | 6774924159498401640L,
1220 | (short)-31848,
1221 | true,
1222 | (byte)-55,
1223 | '\n',
1224 | 0.9553896885798474d,
1225 | 0.2747032f,
1226 | 274991538,
1227 | 6774924159498401640L,
1228 | (short)-31848,
1229 | new BigDecimal("123.456"),
1230 | date,
1231 | localDate))
1232 | .thenReturn(new Foo(
1233 | true,
1234 | (byte)-55,
1235 | '\n',
1236 | 0.9553896885798474d,
1237 | 0.2747032f,
1238 | 274991538,
1239 | 6774924159498401640L,
1240 | (short)-31848,
1241 | true,
1242 | (byte)-55,
1243 | '\n',
1244 | 0.9553896885798474d,
1245 | 0.2747032f,
1246 | 274991538,
1247 | 6774924159498401640L,
1248 | (short)-31848,
1249 | new BigDecimal("123.456"),
1250 | date,
1251 | localDate));
1252 |
1253 | int methodIndex = access.methodIndex(
1254 | "newInstance",
1255 | boolean.class,
1256 | byte.class,
1257 | char.class,
1258 | double.class,
1259 | float.class,
1260 | int.class,
1261 | long.class,
1262 | short.class,
1263 | Boolean.class,
1264 | Byte.class,
1265 | Character.class,
1266 | Double.class,
1267 | Float.class,
1268 | Integer.class,
1269 | Long.class,
1270 | Short.class,
1271 | BigDecimal.class,
1272 | Date.class,
1273 | LocalDate.class);
1274 | Object retVal = access.invoke(
1275 | factory,
1276 | methodIndex,
1277 | true,
1278 | (byte)-55,
1279 | '\n',
1280 | 0.9553896885798474d,
1281 | 0.2747032f,
1282 | 274991538,
1283 | 6774924159498401640L,
1284 | (short)-31848,
1285 | true,
1286 | (byte)-55,
1287 | '\n',
1288 | 0.9553896885798474d,
1289 | 0.2747032f,
1290 | 274991538,
1291 | 6774924159498401640L,
1292 | (short)-31848,
1293 | new BigDecimal("123.456"),
1294 | date,
1295 | localDate);
1296 |
1297 | verify(factory).newInstance(
1298 | true,
1299 | (byte)-55,
1300 | '\n',
1301 | 0.9553896885798474d,
1302 | 0.2747032f,
1303 | 274991538,
1304 | 6774924159498401640L,
1305 | (short)-31848,
1306 | true,
1307 | (byte)-55,
1308 | '\n',
1309 | 0.9553896885798474d,
1310 | 0.2747032f,
1311 | 274991538,
1312 | 6774924159498401640L,
1313 | (short)-31848,
1314 | new BigDecimal("123.456"),
1315 | date,
1316 | localDate);
1317 | assertThat(retVal, notNullValue());
1318 | }
1319 |
1320 | @Test
1321 | public void invokeMethodWith20Parameters() {
1322 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
1323 | FooFactory factory = mock(FooFactory.class);
1324 | Date date = new Date();
1325 | LocalDate localDate = LocalDate.now();
1326 | LocalDateTime localDateTime = LocalDateTime.now();
1327 | when(factory.newInstance(
1328 | true,
1329 | (byte)-55,
1330 | '\n',
1331 | 0.9553896885798474d,
1332 | 0.2747032f,
1333 | 274991538,
1334 | 6774924159498401640L,
1335 | (short)-31848,
1336 | true,
1337 | (byte)-55,
1338 | '\n',
1339 | 0.9553896885798474d,
1340 | 0.2747032f,
1341 | 274991538,
1342 | 6774924159498401640L,
1343 | (short)-31848,
1344 | new BigDecimal("123.456"),
1345 | date,
1346 | localDate,
1347 | localDateTime))
1348 | .thenReturn(new Foo(
1349 | true,
1350 | (byte)-55,
1351 | '\n',
1352 | 0.9553896885798474d,
1353 | 0.2747032f,
1354 | 274991538,
1355 | 6774924159498401640L,
1356 | (short)-31848,
1357 | true,
1358 | (byte)-55,
1359 | '\n',
1360 | 0.9553896885798474d,
1361 | 0.2747032f,
1362 | 274991538,
1363 | 6774924159498401640L,
1364 | (short)-31848,
1365 | new BigDecimal("123.456"),
1366 | date,
1367 | localDate,
1368 | localDateTime));
1369 |
1370 | int methodIndex = access.methodIndex(
1371 | "newInstance",
1372 | boolean.class,
1373 | byte.class,
1374 | char.class,
1375 | double.class,
1376 | float.class,
1377 | int.class,
1378 | long.class,
1379 | short.class,
1380 | Boolean.class,
1381 | Byte.class,
1382 | Character.class,
1383 | Double.class,
1384 | Float.class,
1385 | Integer.class,
1386 | Long.class,
1387 | Short.class,
1388 | BigDecimal.class,
1389 | Date.class,
1390 | LocalDate.class,
1391 | LocalDateTime.class);
1392 | Object retVal = access.invoke(
1393 | factory,
1394 | methodIndex,
1395 | true,
1396 | (byte)-55,
1397 | '\n',
1398 | 0.9553896885798474d,
1399 | 0.2747032f,
1400 | 274991538,
1401 | 6774924159498401640L,
1402 | (short)-31848,
1403 | true,
1404 | (byte)-55,
1405 | '\n',
1406 | 0.9553896885798474d,
1407 | 0.2747032f,
1408 | 274991538,
1409 | 6774924159498401640L,
1410 | (short)-31848,
1411 | new BigDecimal("123.456"),
1412 | date,
1413 | localDate,
1414 | localDateTime);
1415 |
1416 | verify(factory).newInstance(
1417 | true,
1418 | (byte)-55,
1419 | '\n',
1420 | 0.9553896885798474d,
1421 | 0.2747032f,
1422 | 274991538,
1423 | 6774924159498401640L,
1424 | (short)-31848,
1425 | true,
1426 | (byte)-55,
1427 | '\n',
1428 | 0.9553896885798474d,
1429 | 0.2747032f,
1430 | 274991538,
1431 | 6774924159498401640L,
1432 | (short)-31848,
1433 | new BigDecimal("123.456"),
1434 | date,
1435 | localDate,
1436 | localDateTime);
1437 | assertThat(retVal, notNullValue());
1438 | }
1439 |
1440 | @Test
1441 | public void invokeMethodWith21Parameters() {
1442 | MethodAccess access = ClassAccessFactory.get(FooFactory.class);
1443 | FooFactory factory = mock(FooFactory.class);
1444 | Date date = new Date();
1445 | LocalDate localDate = LocalDate.now();
1446 | LocalDateTime localDateTime = LocalDateTime.now();
1447 | when(factory.newInstance(
1448 | true,
1449 | (byte)-55,
1450 | '\n',
1451 | 0.9553896885798474d,
1452 | 0.2747032f,
1453 | 274991538,
1454 | 6774924159498401640L,
1455 | (short)-31848,
1456 | true,
1457 | (byte)-55,
1458 | '\n',
1459 | 0.9553896885798474d,
1460 | 0.2747032f,
1461 | 274991538,
1462 | 6774924159498401640L,
1463 | (short)-31848,
1464 | new BigDecimal("123.456"),
1465 | date,
1466 | localDate,
1467 | localDateTime,
1468 | "Sushi-Ya Japan"))
1469 | .thenReturn(new Foo(
1470 | true,
1471 | (byte)-55,
1472 | '\n',
1473 | 0.9553896885798474d,
1474 | 0.2747032f,
1475 | 274991538,
1476 | 6774924159498401640L,
1477 | (short)-31848,
1478 | true,
1479 | (byte)-55,
1480 | '\n',
1481 | 0.9553896885798474d,
1482 | 0.2747032f,
1483 | 274991538,
1484 | 6774924159498401640L,
1485 | (short)-31848,
1486 | new BigDecimal("123.456"),
1487 | date,
1488 | localDate,
1489 | localDateTime,
1490 | "Sushi-Ya Japan"));
1491 |
1492 | int methodIndex = access.methodIndex(
1493 | "newInstance",
1494 | boolean.class,
1495 | byte.class,
1496 | char.class,
1497 | double.class,
1498 | float.class,
1499 | int.class,
1500 | long.class,
1501 | short.class,
1502 | Boolean.class,
1503 | Byte.class,
1504 | Character.class,
1505 | Double.class,
1506 | Float.class,
1507 | Integer.class,
1508 | Long.class,
1509 | Short.class,
1510 | BigDecimal.class,
1511 | Date.class,
1512 | LocalDate.class,
1513 | LocalDateTime.class,
1514 | String.class);
1515 | Object retVal = access.invoke(
1516 | factory,
1517 | methodIndex,
1518 | true,
1519 | (byte)-55,
1520 | '\n',
1521 | 0.9553896885798474d,
1522 | 0.2747032f,
1523 | 274991538,
1524 | 6774924159498401640L,
1525 | (short)-31848,
1526 | true,
1527 | (byte)-55,
1528 | '\n',
1529 | 0.9553896885798474d,
1530 | 0.2747032f,
1531 | 274991538,
1532 | 6774924159498401640L,
1533 | (short)-31848,
1534 | new BigDecimal("123.456"),
1535 | date,
1536 | localDate,
1537 | localDateTime,
1538 | "Sushi-Ya Japan");
1539 |
1540 | verify(factory).newInstance(
1541 | true,
1542 | (byte)-55,
1543 | '\n',
1544 | 0.9553896885798474d,
1545 | 0.2747032f,
1546 | 274991538,
1547 | 6774924159498401640L,
1548 | (short)-31848,
1549 | true,
1550 | (byte)-55,
1551 | '\n',
1552 | 0.9553896885798474d,
1553 | 0.2747032f,
1554 | 274991538,
1555 | 6774924159498401640L,
1556 | (short)-31848,
1557 | new BigDecimal("123.456"),
1558 | date,
1559 | localDate,
1560 | localDateTime,
1561 | "Sushi-Ya Japan");
1562 | assertThat(retVal, notNullValue());
1563 | }
1564 | }
1565 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/PropertyAccessBoxedPrimitivesTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import org.junit.Test;
19 |
20 | import com.github.javalbert.reflection.ClassAccessFactory;
21 | import com.github.javalbert.reflection.PropertyAccess;
22 |
23 | public class PropertyAccessBoxedPrimitivesTest {
24 | @Test
25 | public void getBoxedBooleanPropertyValueAndVerify() {
26 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
27 | Foo obj = new Foo();
28 | obj.setBoxedBoolean(true);
29 |
30 | Boolean boxedBoolean = access.getBoxedBooleanProperty(obj, access.propertyIndex("boxedBoolean"));
31 |
32 | assertThat(boxedBoolean, equalTo(obj.getBoxedBoolean()));
33 | }
34 |
35 | @Test
36 | public void getBoxedBytePropertyValueAndVerify() {
37 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
38 | Foo obj = new Foo();
39 | obj.setBoxedByte((byte)-55);
40 |
41 | Byte boxedByte = access.getBoxedByteProperty(obj, access.propertyIndex("boxedByte"));
42 |
43 | assertThat(boxedByte, equalTo(obj.getBoxedByte()));
44 | }
45 |
46 | @Test
47 | public void getBoxedCharPropertyValueAndVerify() {
48 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
49 | Foo obj = new Foo();
50 | obj.setBoxedChar('\n');
51 |
52 | Character boxedChar = access.getBoxedCharProperty(obj, access.propertyIndex("boxedChar"));
53 |
54 | assertThat(boxedChar, equalTo(obj.getBoxedChar()));
55 | }
56 |
57 | @Test
58 | public void getBoxedDoublePropertyValueAndVerify() {
59 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
60 | Foo obj = new Foo();
61 | obj.setBoxedDouble(0.9553896885798474d);
62 |
63 | Double boxedDouble = access.getBoxedDoubleProperty(obj, access.propertyIndex("boxedDouble"));
64 |
65 | assertThat(boxedDouble, equalTo(obj.getBoxedDouble()));
66 | }
67 |
68 | @Test
69 | public void getBoxedFloatPropertyValueAndVerify() {
70 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
71 | Foo obj = new Foo();
72 | obj.setBoxedFloat(0.2747032f);
73 |
74 | Float boxedFloat = access.getBoxedFloatProperty(obj, access.propertyIndex("boxedFloat"));
75 |
76 | assertThat(boxedFloat, equalTo(obj.getBoxedFloat()));
77 | }
78 |
79 | @Test
80 | public void getBoxedIntPropertyValueAndVerify() {
81 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
82 | Foo obj = new Foo();
83 | obj.setBoxedInt(274991538);
84 |
85 | Integer boxedInt = access.getBoxedIntProperty(obj, access.propertyIndex("boxedInt"));
86 |
87 | assertThat(boxedInt, equalTo(obj.getBoxedInt()));
88 | }
89 |
90 | @Test
91 | public void getBoxedLongPropertyValueAndVerify() {
92 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
93 | Foo obj = new Foo();
94 | obj.setBoxedLong(6774924159498401640L);
95 |
96 | Long boxedLong = access.getBoxedLongProperty(obj, access.propertyIndex("boxedLong"));
97 |
98 | assertThat(boxedLong, equalTo(obj.getBoxedLong()));
99 | }
100 |
101 | @Test
102 | public void getBoxedShortPropertyValueAndVerify() {
103 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
104 | Foo obj = new Foo();
105 | obj.setBoxedShort((short)-31848);
106 |
107 | Short boxedShort = access.getBoxedShortProperty(obj, access.propertyIndex("boxedShort"));
108 |
109 | assertThat(boxedShort, equalTo(obj.getBoxedShort()));
110 | }
111 |
112 | @Test
113 | public void setBoxedBooleanPropertyValueAndVerify() {
114 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
115 | Foo obj = new Foo();
116 | Boolean boxedBoolean = true;
117 |
118 | access.setBoxedBooleanProperty(obj, access.propertyIndex("boxedBoolean"), boxedBoolean);
119 |
120 | assertThat(obj.getBoxedBoolean(), equalTo(boxedBoolean));
121 | }
122 |
123 | @Test
124 | public void setBoxedBytePropertyValueAndVerify() {
125 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
126 | Foo obj = new Foo();
127 | Byte boxedByte = (byte)-55;
128 |
129 | access.setBoxedByteProperty(obj, access.propertyIndex("boxedByte"), boxedByte);
130 |
131 | assertThat(obj.getBoxedByte(), equalTo(boxedByte));
132 | }
133 |
134 | @Test
135 | public void setBoxedCharPropertyValueAndVerify() {
136 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
137 | Foo obj = new Foo();
138 | Character boxedChar = '\n';
139 |
140 | access.setBoxedCharProperty(obj, access.propertyIndex("boxedChar"), boxedChar);
141 |
142 | assertThat(obj.getBoxedChar(), equalTo(boxedChar));
143 | }
144 |
145 | @Test
146 | public void setBoxedDoublePropertyValueAndVerify() {
147 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
148 | Foo obj = new Foo();
149 | Double boxedDouble = 0.9553896885798474d;
150 |
151 | access.setBoxedDoubleProperty(obj, access.propertyIndex("boxedDouble"), boxedDouble);
152 |
153 | assertThat(obj.getBoxedDouble(), equalTo(boxedDouble));
154 | }
155 |
156 | @Test
157 | public void setBoxedFloatPropertyValueAndVerify() {
158 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
159 | Foo obj = new Foo();
160 | Float boxedFloat = 0.2747032f;
161 |
162 | access.setBoxedFloatProperty(obj, access.propertyIndex("boxedFloat"), boxedFloat);
163 |
164 | assertThat(obj.getBoxedFloat(), equalTo(boxedFloat));
165 | }
166 |
167 | @Test
168 | public void setBoxedIntPropertyValueAndVerify() {
169 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
170 | Foo obj = new Foo();
171 | Integer boxedInt = 274991538;
172 |
173 | access.setBoxedIntProperty(obj, access.propertyIndex("boxedInt"), boxedInt);
174 |
175 | assertThat(obj.getBoxedInt(), equalTo(boxedInt));
176 | }
177 |
178 | @Test
179 | public void setBoxedLongPropertyValueAndVerify() {
180 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
181 | Foo obj = new Foo();
182 | Long boxedLong = 6774924159498401640L;
183 |
184 | access.setBoxedLongProperty(obj, access.propertyIndex("boxedLong"), boxedLong);
185 |
186 | assertThat(obj.getBoxedLong(), equalTo(boxedLong));
187 | }
188 |
189 | @Test
190 | public void setBoxedShortPropertyValueAndVerify() {
191 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
192 | Foo obj = new Foo();
193 | Short boxedShort = (short)-31848;
194 |
195 | access.setBoxedShortProperty(obj, access.propertyIndex("boxedShort"), boxedShort);
196 |
197 | assertThat(obj.getBoxedShort(), equalTo(boxedShort));
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/PropertyAccessGeneralTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import java.math.BigDecimal;
19 | import java.time.LocalDate;
20 | import java.time.LocalDateTime;
21 | import java.util.Date;
22 |
23 | import org.junit.Test;
24 |
25 | import com.github.javalbert.reflection.ClassAccessFactory;
26 | import com.github.javalbert.reflection.PropertyAccess;
27 |
28 | public class PropertyAccessGeneralTest {
29 | /* START Primitive types */
30 |
31 | @Test
32 | public void getBooleanPropertyValueAndVerify() {
33 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
34 | Foo obj = new Foo();
35 | obj.setBooleanVal(true);
36 |
37 | boolean booleanVal = (boolean)access.getProperty(obj, access.propertyIndex("booleanVal"));
38 |
39 | assertThat(booleanVal, equalTo(obj.getBooleanVal()));
40 | }
41 |
42 | @Test
43 | public void getBytePropertyValueAndVerify() {
44 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
45 | Foo obj = new Foo();
46 | obj.setByteVal((byte)-21);
47 |
48 | byte byteVal = (byte)access.getProperty(obj, access.propertyIndex("byteVal"));
49 |
50 | assertThat(byteVal, equalTo(obj.getByteVal()));
51 | }
52 |
53 | @Test
54 | public void getCharPropertyValueAndVerify() {
55 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
56 | Foo obj = new Foo();
57 | obj.setCharVal('.');
58 |
59 | char charVal = (char)access.getProperty(obj, access.propertyIndex("charVal"));
60 |
61 | assertThat(charVal, equalTo(obj.getCharVal()));
62 | }
63 |
64 | @Test
65 | public void getDoublePropertyValueAndVerify() {
66 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
67 | Foo obj = new Foo();
68 | obj.setDoubleVal(0.14753383239666462d);
69 |
70 | double doubleVal = (double)access.getProperty(obj, access.propertyIndex("doubleVal"));
71 |
72 | assertThat(doubleVal, equalTo(obj.getDoubleVal()));
73 | }
74 |
75 | @Test
76 | public void getFloatPropertyValueAndVerify() {
77 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
78 | Foo obj = new Foo();
79 | obj.setFloatVal(0.27210158f);
80 |
81 | float floatVal = (float)access.getProperty(obj, access.propertyIndex("floatVal"));
82 |
83 | assertThat(floatVal, equalTo(obj.getFloatVal()));
84 | }
85 |
86 | @Test
87 | public void getIntPropertyValueAndVerify() {
88 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
89 | Foo obj = new Foo();
90 | obj.setIntVal(1);
91 |
92 | int intVal = (int)access.getProperty(obj, access.propertyIndex("intVal"));
93 |
94 | assertThat(intVal, equalTo(obj.getIntVal()));
95 | }
96 |
97 | @Test
98 | public void getLongPropertyValueAndVerify() {
99 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
100 | Foo obj = new Foo();
101 | obj.setLongVal(3285927007071017350L);
102 |
103 | long longVal = (long)access.getProperty(obj, access.propertyIndex("longVal"));
104 |
105 | assertThat(longVal, equalTo(obj.getLongVal()));
106 | }
107 |
108 | @Test
109 | public void getShortPropertyValueAndVerify() {
110 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
111 | Foo obj = new Foo();
112 | obj.setShortVal((short)-8406);
113 |
114 | short shortVal = (short)access.getProperty(obj, access.propertyIndex("shortVal"));
115 |
116 | assertThat(shortVal, equalTo(obj.getShortVal()));
117 | }
118 |
119 | @Test
120 | public void setBooleanPropertyValueAndVerify() {
121 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
122 | Foo obj = new Foo();
123 | boolean booleanVal = true;
124 |
125 | access.setProperty(obj, access.propertyIndex("booleanVal"), booleanVal);
126 |
127 | assertThat(obj.getBooleanVal(), equalTo(booleanVal));
128 | }
129 |
130 | @Test
131 | public void setBytePropertyValueAndVerify() {
132 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
133 | Foo obj = new Foo();
134 | byte byteVal = (byte)-21;
135 |
136 | access.setProperty(obj, access.propertyIndex("byteVal"), byteVal);
137 |
138 | assertThat(obj.getByteVal(), equalTo(byteVal));
139 | }
140 |
141 | @Test
142 | public void setCharPropertyValueAndVerify() {
143 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
144 | Foo obj = new Foo();
145 | char charVal = '.';
146 |
147 | access.setProperty(obj, access.propertyIndex("charVal"), charVal);
148 |
149 | assertThat(obj.getCharVal(), equalTo(charVal));
150 | }
151 |
152 | @Test
153 | public void setDoublePropertyValueAndVerify() {
154 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
155 | Foo obj = new Foo();
156 | double doubleVal = 0.14753383239666462d;
157 |
158 | access.setProperty(obj, access.propertyIndex("doubleVal"), doubleVal);
159 |
160 | assertThat(obj.getDoubleVal(), equalTo(doubleVal));
161 | }
162 |
163 | @Test
164 | public void setFloatPropertyValueAndVerify() {
165 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
166 | Foo obj = new Foo();
167 | float floatVal = 0.27210158f;
168 |
169 | access.setProperty(obj, access.propertyIndex("floatVal"), floatVal);
170 |
171 | assertThat(obj.getFloatVal(), equalTo(floatVal));
172 | }
173 |
174 | @Test
175 | public void setIntPropertyValueAndVerify() {
176 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
177 | Foo obj = new Foo();
178 | int intVal = 1;
179 |
180 | access.setProperty(obj, access.propertyIndex("intVal"), intVal);
181 |
182 | assertThat(obj.getIntVal(), equalTo(intVal));
183 | }
184 |
185 | @Test
186 | public void setLongPropertyValueAndVerify() {
187 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
188 | Foo obj = new Foo();
189 | long longVal = 3285927007071017350L;
190 |
191 | access.setProperty(obj, access.propertyIndex("longVal"), longVal);
192 |
193 | assertThat(obj.getLongVal(), equalTo(longVal));
194 | }
195 |
196 | @Test
197 | public void setShortPropertyValueAndVerify() {
198 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
199 | Foo obj = new Foo();
200 | short shortVal = (short)-8406;
201 |
202 | access.setShortProperty(obj, access.propertyIndex("shortVal"), shortVal);
203 |
204 | assertThat(obj.getShortVal(), equalTo(shortVal));
205 | }
206 |
207 | /* END Primitive types */
208 |
209 | /* START Primitive wrapper types */
210 |
211 | @Test
212 | public void getBoxedBooleanPropertyValueAndVerify() {
213 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
214 | Foo obj = new Foo();
215 | obj.setBoxedBoolean(true);
216 |
217 | Boolean boxedBoolean = (Boolean)access.getProperty(obj, access.propertyIndex("boxedBoolean"));
218 |
219 | assertThat(boxedBoolean, equalTo(obj.getBoxedBoolean()));
220 | }
221 |
222 | @Test
223 | public void getBoxedBytePropertyValueAndVerify() {
224 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
225 | Foo obj = new Foo();
226 | obj.setBoxedByte((byte)-55);
227 |
228 | Byte boxedByte = (Byte)access.getProperty(obj, access.propertyIndex("boxedByte"));
229 |
230 | assertThat(boxedByte, equalTo(obj.getBoxedByte()));
231 | }
232 |
233 | @Test
234 | public void getBoxedCharPropertyValueAndVerify() {
235 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
236 | Foo obj = new Foo();
237 | obj.setBoxedChar('\n');
238 |
239 | Character boxedChar = (Character)access.getProperty(obj, access.propertyIndex("boxedChar"));
240 |
241 | assertThat(boxedChar, equalTo(obj.getBoxedChar()));
242 | }
243 |
244 | @Test
245 | public void getBoxedDoublePropertyValueAndVerify() {
246 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
247 | Foo obj = new Foo();
248 | obj.setBoxedDouble(0.9553896885798474d);
249 |
250 | Double boxedDouble = (Double)access.getProperty(obj, access.propertyIndex("boxedDouble"));
251 |
252 | assertThat(boxedDouble, equalTo(obj.getBoxedDouble()));
253 | }
254 |
255 | @Test
256 | public void getBoxedFloatPropertyValueAndVerify() {
257 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
258 | Foo obj = new Foo();
259 | obj.setBoxedFloat(0.2747032f);
260 |
261 | Float boxedFloat = (Float)access.getProperty(obj, access.propertyIndex("boxedFloat"));
262 |
263 | assertThat(boxedFloat, equalTo(obj.getBoxedFloat()));
264 | }
265 |
266 | @Test
267 | public void getBoxedIntPropertyValueAndVerify() {
268 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
269 | Foo obj = new Foo();
270 | obj.setBoxedInt(274991538);
271 |
272 | Integer boxedInt = (Integer)access.getProperty(obj, access.propertyIndex("boxedInt"));
273 |
274 | assertThat(boxedInt, equalTo(obj.getBoxedInt()));
275 | }
276 |
277 | @Test
278 | public void getBoxedLongPropertyValueAndVerify() {
279 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
280 | Foo obj = new Foo();
281 | obj.setBoxedLong(6774924159498401640L);
282 |
283 | Long boxedLong = (Long)access.getProperty(obj, access.propertyIndex("boxedLong"));
284 |
285 | assertThat(boxedLong, equalTo(obj.getBoxedLong()));
286 | }
287 |
288 | @Test
289 | public void getBoxedShortPropertyValueAndVerify() {
290 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
291 | Foo obj = new Foo();
292 | obj.setBoxedShort((short)-31848);
293 |
294 | Short boxedShort = (Short)access.getProperty(obj, access.propertyIndex("boxedShort"));
295 |
296 | assertThat(boxedShort, equalTo(obj.getBoxedShort()));
297 | }
298 |
299 | @Test
300 | public void setBoxedBooleanPropertyValueAndVerify() {
301 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
302 | Foo obj = new Foo();
303 | Boolean boxedBoolean = true;
304 |
305 | access.setProperty(obj, access.propertyIndex("boxedBoolean"), boxedBoolean);
306 |
307 | assertThat(obj.getBoxedBoolean(), equalTo(boxedBoolean));
308 | }
309 |
310 | @Test
311 | public void setBoxedBytePropertyValueAndVerify() {
312 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
313 | Foo obj = new Foo();
314 | Byte boxedByte = (byte)-55;
315 |
316 | access.setProperty(obj, access.propertyIndex("boxedByte"), boxedByte);
317 |
318 | assertThat(obj.getBoxedByte(), equalTo(boxedByte));
319 | }
320 |
321 | @Test
322 | public void setBoxedCharPropertyValueAndVerify() {
323 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
324 | Foo obj = new Foo();
325 | Character boxedChar = '\n';
326 |
327 | access.setProperty(obj, access.propertyIndex("boxedChar"), boxedChar);
328 |
329 | assertThat(obj.getBoxedChar(), equalTo(boxedChar));
330 | }
331 |
332 | @Test
333 | public void setBoxedDoublePropertyValueAndVerify() {
334 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
335 | Foo obj = new Foo();
336 | Double boxedDouble = 0.9553896885798474d;
337 |
338 | access.setProperty(obj, access.propertyIndex("boxedDouble"), boxedDouble);
339 |
340 | assertThat(obj.getBoxedDouble(), equalTo(boxedDouble));
341 | }
342 |
343 | @Test
344 | public void setBoxedFloatPropertyValueAndVerify() {
345 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
346 | Foo obj = new Foo();
347 | Float boxedFloat = 0.2747032f;
348 |
349 | access.setProperty(obj, access.propertyIndex("boxedFloat"), boxedFloat);
350 |
351 | assertThat(obj.getBoxedFloat(), equalTo(boxedFloat));
352 | }
353 |
354 | @Test
355 | public void setBoxedIntPropertyValueAndVerify() {
356 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
357 | Foo obj = new Foo();
358 | Integer boxedInt = 274991538;
359 |
360 | access.setProperty(obj, access.propertyIndex("boxedInt"), boxedInt);
361 |
362 | assertThat(obj.getBoxedInt(), equalTo(boxedInt));
363 | }
364 |
365 | @Test
366 | public void setBoxedLongPropertyValueAndVerify() {
367 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
368 | Foo obj = new Foo();
369 | Long boxedLong = 6774924159498401640L;
370 |
371 | access.setProperty(obj, access.propertyIndex("boxedLong"), boxedLong);
372 |
373 | assertThat(obj.getBoxedLong(), equalTo(boxedLong));
374 | }
375 |
376 | @Test
377 | public void setBoxedShortPropertyValueAndVerify() {
378 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
379 | Foo obj = new Foo();
380 | Short boxedShort = (short)-31848;
381 |
382 | access.setProperty(obj, access.propertyIndex("boxedShort"), boxedShort);
383 |
384 | assertThat(obj.getBoxedShort(), equalTo(boxedShort));
385 | }
386 |
387 | /* END Primitive wrapper types */
388 |
389 | /* START Common reference types */
390 |
391 | @Test
392 | public void getBigDecimalPropertyValueAndVerify() {
393 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
394 | Foo obj = new Foo();
395 | obj.setBigDecimal(new BigDecimal("123.456"));
396 |
397 | BigDecimal bigDecimal = (BigDecimal)access.getProperty(obj, access.propertyIndex("bigDecimal"));
398 |
399 | assertThat(bigDecimal, equalTo(obj.getBigDecimal()));
400 | }
401 |
402 | @Test
403 | public void setBigDecimalPropertyValueAndVerify() {
404 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
405 | Foo obj = new Foo();
406 | BigDecimal bigDecimal = new BigDecimal("123.456");
407 |
408 | access.setProperty(obj, access.propertyIndex("bigDecimal"), bigDecimal);
409 |
410 | assertThat(obj.getBigDecimal(), equalTo(bigDecimal));
411 | }
412 |
413 | @Test
414 | public void getDatePropertyValueAndVerify() {
415 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
416 | Foo obj = new Foo();
417 | obj.setDate(new Date());
418 |
419 | Date date = (Date)access.getProperty(obj, access.propertyIndex("date"));
420 |
421 | assertThat(date, equalTo(obj.getDate()));
422 | }
423 |
424 | @Test
425 | public void setDatePropertyValueAndVerify() {
426 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
427 | Foo obj = new Foo();
428 | Date date = new Date();
429 |
430 | access.setProperty(obj, access.propertyIndex("date"), date);
431 |
432 | assertThat(obj.getDate(), equalTo(date));
433 | }
434 |
435 | @Test
436 | public void getLocalDatePropertyValueAndVerify() {
437 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
438 | Foo obj = new Foo();
439 | obj.setLocalDate(LocalDate.now());
440 |
441 | LocalDate localDate = (LocalDate)access.getProperty(obj, access.propertyIndex("localDate"));
442 |
443 | assertThat(localDate, equalTo(obj.getLocalDate()));
444 | }
445 |
446 | @Test
447 | public void setLocalDatePropertyValueAndVerify() {
448 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
449 | Foo obj = new Foo();
450 | LocalDate localDate = LocalDate.now();
451 |
452 | access.setProperty(obj, access.propertyIndex("localDate"), localDate);
453 |
454 | assertThat(obj.getLocalDate(), equalTo(localDate));
455 | }
456 |
457 | @Test
458 | public void getLocalDateTimePropertyValueAndVerify() {
459 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
460 | Foo obj = new Foo();
461 | obj.setLocalDateTime(LocalDateTime.now());
462 |
463 | LocalDateTime localDateTime = (LocalDateTime)access.getProperty(obj, access.propertyIndex("localDateTime"));
464 |
465 | assertThat(localDateTime, equalTo(obj.getLocalDateTime()));
466 | }
467 |
468 | @Test
469 | public void setLocalDateTimePropertyValueAndVerify() {
470 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
471 | Foo obj = new Foo();
472 | LocalDateTime localDateTime = LocalDateTime.now();
473 |
474 | access.setProperty(obj, access.propertyIndex("localDateTime"), localDateTime);
475 |
476 | assertThat(obj.getLocalDateTime(), equalTo(localDateTime));
477 | }
478 |
479 | @Test
480 | public void getStringPropertyValueAndVerify() {
481 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
482 | Foo obj = new Foo();
483 | obj.setString("Pizza Hut");
484 |
485 | String string = (String)access.getProperty(obj, access.propertyIndex("string"));
486 |
487 | assertThat(string, equalTo(obj.getString()));
488 | }
489 |
490 | @Test
491 | public void setStringPropertyValueAndVerify() {
492 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
493 | Foo obj = new Foo();
494 | String string = "Pizza Hut";
495 |
496 | access.setProperty(obj, access.propertyIndex("string"), string);
497 |
498 | assertThat(obj.getString(), equalTo(string));
499 | }
500 |
501 | /* END Common reference types */
502 | }
503 |
--------------------------------------------------------------------------------
/src/test/java/com/github/javalbert/reflection/test/PropertyAccessPrimitivesTest.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2017 Albert Shun-Dat Chan
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 | * compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 | * the License for the specific language governing permissions and limitations under the License.
12 | *******************************************************************************/
13 | package com.github.javalbert.reflection.test;
14 |
15 | import static org.hamcrest.CoreMatchers.*;
16 | import static org.junit.Assert.*;
17 |
18 | import org.junit.Test;
19 |
20 | import com.github.javalbert.reflection.ClassAccessFactory;
21 | import com.github.javalbert.reflection.PropertyAccess;
22 |
23 | public class PropertyAccessPrimitivesTest {
24 | @Test
25 | public void getBooleanPropertyValueAndVerify() {
26 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
27 | Foo obj = new Foo();
28 | obj.setBooleanVal(true);
29 |
30 | boolean booleanVal = access.getBooleanProperty(obj, access.propertyIndex("booleanVal"));
31 |
32 | assertThat(booleanVal, equalTo(obj.getBooleanVal()));
33 | }
34 |
35 | @Test
36 | public void getBytePropertyValueAndVerify() {
37 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
38 | Foo obj = new Foo();
39 | obj.setByteVal((byte)-21);
40 |
41 | byte byteVal = access.getByteProperty(obj, access.propertyIndex("byteVal"));
42 |
43 | assertThat(byteVal, equalTo(obj.getByteVal()));
44 | }
45 |
46 | @Test
47 | public void getCharPropertyValueAndVerify() {
48 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
49 | Foo obj = new Foo();
50 | obj.setCharVal('.');
51 |
52 | char charVal = access.getCharProperty(obj, access.propertyIndex("charVal"));
53 |
54 | assertThat(charVal, equalTo(obj.getCharVal()));
55 | }
56 |
57 | @Test
58 | public void getDoublePropertyValueAndVerify() {
59 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
60 | Foo obj = new Foo();
61 | obj.setDoubleVal(0.14753383239666462d);
62 |
63 | double doubleVal = access.getDoubleProperty(obj, access.propertyIndex("doubleVal"));
64 |
65 | assertThat(doubleVal, equalTo(obj.getDoubleVal()));
66 | }
67 |
68 | @Test
69 | public void getFloatPropertyValueAndVerify() {
70 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
71 | Foo obj = new Foo();
72 | obj.setFloatVal(0.27210158f);
73 |
74 | float floatVal = access.getFloatProperty(obj, access.propertyIndex("floatVal"));
75 |
76 | assertThat(floatVal, equalTo(obj.getFloatVal()));
77 | }
78 |
79 | @Test
80 | public void getIntPropertyValueAndVerify() {
81 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
82 | Foo obj = new Foo();
83 | obj.setIntVal(1);
84 |
85 | int intVal = access.getIntProperty(obj, access.propertyIndex("intVal"));
86 |
87 | assertThat(intVal, equalTo(obj.getIntVal()));
88 | }
89 |
90 | @Test
91 | public void getLongPropertyValueAndVerify() {
92 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
93 | Foo obj = new Foo();
94 | obj.setLongVal(3285927007071017350L);
95 |
96 | long longVal = access.getLongProperty(obj, access.propertyIndex("longVal"));
97 |
98 | assertThat(longVal, equalTo(obj.getLongVal()));
99 | }
100 |
101 | @Test
102 | public void getShortPropertyValueAndVerify() {
103 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
104 | Foo obj = new Foo();
105 | obj.setShortVal((short)-8406);
106 |
107 | short shortVal = access.getShortProperty(obj, access.propertyIndex("shortVal"));
108 |
109 | assertThat(shortVal, equalTo(obj.getShortVal()));
110 | }
111 |
112 | @Test
113 | public void setBooleanPropertyValueAndVerify() {
114 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
115 | Foo obj = new Foo();
116 | boolean booleanVal = true;
117 |
118 | access.setBooleanProperty(obj, access.propertyIndex("booleanVal"), booleanVal);
119 |
120 | assertThat(obj.getBooleanVal(), equalTo(booleanVal));
121 | }
122 |
123 | @Test
124 | public void setBytePropertyValueAndVerify() {
125 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
126 | Foo obj = new Foo();
127 | byte byteVal = (byte)-21;
128 |
129 | access.setByteProperty(obj, access.propertyIndex("byteVal"), byteVal);
130 |
131 | assertThat(obj.getByteVal(), equalTo(byteVal));
132 | }
133 |
134 | @Test
135 | public void setCharPropertyValueAndVerify() {
136 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
137 | Foo obj = new Foo();
138 | char charVal = '.';
139 |
140 | access.setCharProperty(obj, access.propertyIndex("charVal"), charVal);
141 |
142 | assertThat(obj.getCharVal(), equalTo(charVal));
143 | }
144 |
145 | @Test
146 | public void setDoublePropertyValueAndVerify() {
147 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
148 | Foo obj = new Foo();
149 | double doubleVal = 0.14753383239666462d;
150 |
151 | access.setDoubleProperty(obj, access.propertyIndex("doubleVal"), doubleVal);
152 |
153 | assertThat(obj.getDoubleVal(), equalTo(doubleVal));
154 | }
155 |
156 | @Test
157 | public void setFloatPropertyValueAndVerify() {
158 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
159 | Foo obj = new Foo();
160 | float floatVal = 0.27210158f;
161 |
162 | access.setFloatProperty(obj, access.propertyIndex("floatVal"), floatVal);
163 |
164 | assertThat(obj.getFloatVal(), equalTo(floatVal));
165 | }
166 |
167 | @Test
168 | public void setIntPropertyValueAndVerify() {
169 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
170 | Foo obj = new Foo();
171 | int intVal = 1;
172 |
173 | access.setIntProperty(obj, access.propertyIndex("intVal"), intVal);
174 |
175 | assertThat(obj.getIntVal(), equalTo(intVal));
176 | }
177 |
178 | @Test
179 | public void setLongPropertyValueAndVerify() {
180 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
181 | Foo obj = new Foo();
182 | long longVal = 3285927007071017350L;
183 |
184 | access.setLongProperty(obj, access.propertyIndex("longVal"), longVal);
185 |
186 | assertThat(obj.getLongVal(), equalTo(longVal));
187 | }
188 |
189 | @Test
190 | public void setShortPropertyValueAndVerify() {
191 | PropertyAccess access = ClassAccessFactory.get(Foo.class);
192 | Foo obj = new Foo();
193 | short shortVal = (short)-8406;
194 |
195 | access.setShortProperty(obj, access.propertyIndex("shortVal"), shortVal);
196 |
197 | assertThat(obj.getShortVal(), equalTo(shortVal));
198 | }
199 | }
200 |
--------------------------------------------------------------------------------