• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

future-architect / uroborosql / #743

28 Jun 2024 04:17PM UTC coverage: 90.988% (+0.5%) from 90.484%
#743

push

web-flow
add paramIfNotEmpty method (#318)

* v0.x to master merge

* fix nanoseconds diff (java8 to java11)

* eclipse cleanup and  optimize import

* eclipse format

* optimize Imports

* remove unused annotation

* library version up

* migrate v0.x to master
- update java version 8 to 11
- update junit4 to junit5
- library version update

* fix test failed

* remove unused annotation

* fixed bug

* use var

* fix java8 coding style

* Refactoring TransactionContextManager

* Refactoring SqlAgent

* 途中コミット

* fix testcase

* fix review

* fix javadoc comments

* merge v0.x PR

* cleanup code

* cleanup test code

* change build status badge

* - agent.query, update, proc, batch にそれぞれSupplierを引数にとるメソッドを追加
- SqlQuery.paramとSqlEntityUpdate.setにFunctionを受け取るメソッドを追加

* testcaseの整形

* - SqlFluent と ExtractionCondition の分離
- Arrays.asList -> List.of への変更
- テストの整形

* - v0.x系の不具合対応の追いつき
- ログ出力の整理

* - SqlKindの整理(ENTITY_XXXの追加)
- REPL_LOGの追加
- Deprecatedメソッドの削除(SqlAgent)
- SqlAgent, ExecutionContextでsetterをfluent APIに変更

* DB接続URLの修正

* add and fix testcases.

* add event testcases.

* fix typo

* add paramIfNotEmpty method and StringUtils rename ObjectUtils

* fix review comments.

* remove unused import

1695 of 1958 new or added lines in 97 files covered. (86.57%)

26 existing lines in 10 files now uncovered.

8249 of 9066 relevant lines covered (90.99%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
/src/main/java/jp/co/future/uroborosql/utils/BeanAccessor.java
1
/**
2
 * Copyright (c) 2017-present, Future Corporation
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
package jp.co.future.uroborosql.utils;
8

9
import java.lang.reflect.Field;
10
import java.lang.reflect.Modifier;
11
import java.util.AbstractMap;
12
import java.util.AbstractSet;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.Map;
18
import java.util.Set;
19

20
import jp.co.future.uroborosql.exception.UroborosqlRuntimeException;
21

22
/**
23
 * paramBeanメソッドで利用するアクセッサ
24
 *
25
 * @author hoshi-k
26
 *
27
 */
28
public final class BeanAccessor {
29
        private BeanAccessor() {
30
                // do nothing
31
        }
32

33
        /**
34
         * Class to convert Object to Map<br>
35
         * <br>
36
         * We created our own class to reduce the number of iterations when using SqlBatch#by.
37
         */
38
        private static class AsMap extends AbstractMap<String, Object> {
39
                private final Set<Entry<String, Object>> entrySet;
40

41
                public AsMap(final Object object) {
1✔
42
                        var fields = BeanAccessor.fields(object.getClass()).stream()
1✔
43
                                        .toArray(Field[]::new);
1✔
44
                        @SuppressWarnings("unchecked")
45
                        Entry<String, Object>[] entries = new Entry[fields.length];
1✔
46
                        this.entrySet = new AbstractSet<>() {
1✔
47

48
                                @Override
49
                                public Iterator<Entry<String, Object>> iterator() {
50
                                        return new Iterator<>() {
1✔
51
                                                private int index = 0;
1✔
52

53
                                                @Override
54
                                                public boolean hasNext() {
55
                                                        return fields.length > index;
1✔
56
                                                }
57

58
                                                @Override
59
                                                public Entry<String, Object> next() {
60
                                                        var next = entries[index];
1✔
61
                                                        if (next == null) {
1✔
62
                                                                var f = fields[index];
1✔
63
                                                                next = new AbstractMap.SimpleImmutableEntry<>(f.getName(),
1✔
64
                                                                                BeanAccessor.value(f, object));
1✔
65
                                                                entries[index] = next;
1✔
66
                                                        }
67
                                                        index++;
1✔
68
                                                        return next;
1✔
69
                                                }
70

71
                                        };
72
                                }
73

74
                                @Override
75
                                public int size() {
76
                                        return fields.length;
1✔
77
                                }
78
                        };
79
                }
1✔
80

81
                @Override
82
                public Set<Entry<String, Object>> entrySet() {
83
                        return this.entrySet;
1✔
84
                }
85

86
        }
87

88
        /**
89
         * 指定したクラスの持つ全てのフィールドを親クラスを含めて取得する
90
         *
91
         * @param cls 型
92
         * @return {@literal Set<Field>j}
93
         */
94
        public static Collection<Field> fields(final Class<?> cls) {
95
                var fieldMap = new HashMap<String, Field>();
1✔
96
                walkFields(cls, fieldMap);
1✔
97
                return fieldMap.values();
1✔
98
        }
99

100
        /**
101
         * 指定したクラスの持つ全てのフィールドを再帰的に探索して取得する
102
         *
103
         * @param cls 型
104
         * @param fieldMap {@literal Map<String, Field>j}
105
         */
106
        private static void walkFields(final Class<?> cls, final Map<String, Field> fieldMap) {
107
                if (cls.equals(Object.class)) {
1✔
108
                        return;
1✔
109
                }
110
                var superClass = cls.getSuperclass();
1✔
111
                walkFields(superClass, fieldMap);
1✔
112
                Arrays.stream(cls.getDeclaredFields())
1✔
113
                                .filter(f -> !Modifier.isStatic(f.getModifiers()))
1✔
114
                                .forEach(f -> fieldMap.put(f.getName(), f));
1✔
115
        }
1✔
116

117
        /**
118
         * 指定した<code>Field</code>の値を取得する
119
         *
120
         * @param f <code>Field</code>
121
         * @param bean 対象のオブジェクト
122
         * @return フィールドの値
123
         */
124
        public static Object value(final Field f, final Object bean) {
125
                try {
126
                        f.setAccessible(true);
1✔
127
                        return f.get(bean);
1✔
NEW
128
                } catch (IllegalArgumentException | IllegalAccessException ex) {
×
NEW
129
                        throw new UroborosqlRuntimeException(ex);
×
130
                }
131
        }
132

133
        /**
134
         * 与えられたオブジェクトをMap形式に変換します。
135
         *
136
         * @param object 対象のオブジェクト
137
         * @return {@literal Map<String, Object>}
138
         */
139
        public static Map<String, Object> asMap(final Object object) {
140
                return new AsMap(object);
1✔
141
        }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc