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

leeonky / test-charm-java / 390

18 Oct 2025 04:40PM UTC coverage: 75.298% (+0.01%) from 75.288%
390

push

circleci

leeonky
refactor

8 of 8 new or added lines in 5 files covered. (100.0%)

19 existing lines in 8 files now uncovered.

8895 of 11813 relevant lines covered (75.3%)

0.75 hits per line

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

93.67
/bean-util/src/main/java/com/github/leeonky/util/BeanClass.java
1
package com.github.leeonky.util;
2

3
import java.lang.annotation.Annotation;
4
import java.lang.reflect.Array;
5
import java.lang.reflect.Type;
6
import java.util.*;
7
import java.util.concurrent.ConcurrentHashMap;
8

9
import static java.util.Arrays.asList;
10
import static java.util.Optional.ofNullable;
11
import static java.util.stream.Collectors.toList;
12

13
public class BeanClass<T> {
14
    private final static Map<Class<?>, BeanClass<?>> instanceCache = new ConcurrentHashMap<>();
1✔
15
    private static Converter converter = Converter.getInstance();
1✔
16
    private final TypeInfo<T> typeInfo;
17
    private final Class<T> type;
18

19
    @SuppressWarnings("unchecked")
20
    protected BeanClass(Class<T> type) {
1✔
21
        this.type = Objects.requireNonNull(type);
1✔
22
        typeInfo = TypeInfo.create(this, PropertyProxyFactory.NO_PROXY);
1✔
23
    }
1✔
24

25
    public BeanClass(Class<T> type, PropertyProxyFactory<T> proxyFactory) {
1✔
26
        this.type = Objects.requireNonNull(type);
1✔
27
        typeInfo = TypeInfo.create(this, proxyFactory);
1✔
28
    }
1✔
29

30
    @SuppressWarnings("unchecked")
31
    public static <T> BeanClass<T> create(Class<T> type) {
32
        return (BeanClass<T>) instanceCache.computeIfAbsent(type, BeanClass::new);
1✔
33
    }
34

35
    public static <T> Optional<T> cast(Object value, Class<T> type) {
36
        return ofNullable(value)
1✔
37
                .filter(type::isInstance)
1✔
38
                .map(type::cast);
1✔
39
    }
40

41
    public static BeanClass<?> create(GenericType type) {
42
        if (!type.hasTypeArguments())
1✔
43
            return create(type.getRawType());
1✔
44
        return GenericBeanClass.create(type);
1✔
45
    }
46

47
    @SuppressWarnings("unchecked")
48
    public static <T> Class<T> getClass(T instance) {
49
        return (Class<T>) Objects.requireNonNull(instance).getClass();
1✔
50
    }
51

52
    public static <T> BeanClass<T> createFrom(T instance) {
53
        return create(getClass(instance));
1✔
54
    }
55

56
    public static Converter getConverter() {
57
        return converter;
1✔
58
    }
59

60
    public static void setConverter(Converter converter) {
61
        BeanClass.converter = converter;
×
62
    }
×
63

64
    public Class<T> getType() {
65
        return type;
1✔
66
    }
67

68
    public String getName() {
69
        return type.getName();
1✔
70
    }
71

72
    public String getSimpleName() {
73
        return type.getSimpleName();
1✔
74
    }
75

76
    public Map<String, PropertyReader<T>> getPropertyReaders() {
77
        return typeInfo.getReaders();
1✔
78
    }
79

80
    public Map<String, PropertyWriter<T>> getPropertyWriters() {
81
        return typeInfo.getWriters();
1✔
82
    }
83

84
    public Object getPropertyValue(T bean, String property) {
85
        return getPropertyReader(property).getValue(bean);
1✔
86
    }
87

88
    public PropertyReader<T> getPropertyReader(String property) {
89
        return typeInfo.getReader(property);
1✔
90
    }
91

92
    public BeanClass<T> setPropertyValue(T bean, String property, Object value) {
93
        getPropertyWriter(property).setValue(bean, value);
1✔
94
        return this;
1✔
95
    }
96

97
    public PropertyWriter<T> getPropertyWriter(String property) {
98
        return typeInfo.getWriter(property);
1✔
99
    }
100

101
    public T newInstance(Object... args) {
102
        return Classes.newInstance(type, args);
1✔
103
    }
104

105
    public Object createCollection(Collection<?> elements) {
106
        return CollectionHelper.createCollection(elements, this);
1✔
107
    }
108

109
    public Object getPropertyChainValue(T object, String chain) {
110
        return getPropertyChainValue(object, Property.toChainNodes(chain));
1✔
111
    }
112

113
    public Object getPropertyChainValue(T object, List<Object> chain) {
114
        return getPropertyChainValueInner(chain, 0, object, new LinkedList<>(chain));
1✔
115
    }
116

117
    @SuppressWarnings("unchecked")
118
    private Object getPropertyChainValueInner(List<Object> originalChain, int level, T object, LinkedList<Object> chain) {
119
        if (chain.isEmpty())
1✔
120
            return object;
1✔
121
        if (object == null)
1✔
122
            throw new NullPointerInChainException(originalChain, level);
1✔
123
        Object p = chain.removeFirst();
1✔
124
        PropertyReader propertyReader = getPropertyReader(p.toString());
1✔
125
        return propertyReader.getType().getPropertyChainValueInner(originalChain, level + 1,
1✔
126
                propertyReader.getValue(object), chain);
1✔
127
    }
128

129
    public PropertyReader<?> getPropertyChainReader(String chain) {
130
        return getPropertyChainReader(Property.toChainNodes(chain));
1✔
131
    }
132

133
    public PropertyReader<?> getPropertyChainReader(List<Object> chain) {
134
        return getPropertyChainReaderInner(new LinkedList<>(chain));
1✔
135
    }
136

137
    private PropertyReader<?> getPropertyChainReaderInner(LinkedList<Object> chain) {
138
        return getPropertyReader((String) chain.removeFirst()).getPropertyChainReader(chain);
1✔
139
    }
140

141
    @SuppressWarnings("unchecked")
142
    public T createDefault() {
143
        return (T) Array.get(Array.newInstance(getType(), 1), 0);
1✔
144
    }
145

146
    public boolean hasTypeArguments() {
147
        return false;
1✔
148
    }
149

150
    public Optional<BeanClass<?>> getTypeArguments(int position) {
151
        return Optional.empty();
1✔
152
    }
153

154
    public BeanClass<?> getElementType() {
155
        if (type.isArray())
1✔
156
            return BeanClass.create(type.getComponentType());
1✔
157
        if (Iterable.class.isAssignableFrom(type))
1✔
158
            return getTypeArguments(0).orElseGet(() -> BeanClass.create(Object.class));
1✔
159
        return null;
1✔
160
    }
161

162
    public BeanClass<?> getElementOrPropertyType() {
163
        BeanClass<?> elementType = getElementType();
1✔
164
        return elementType == null ? this : elementType;
1✔
165
    }
166

167
    @Override
168
    public int hashCode() {
169
        return Objects.hash(BeanClass.class, type);
1✔
170
    }
171

172
    @Override
173
    public boolean equals(Object obj) {
174
        return obj != null && obj.getClass().equals(BeanClass.class) && Objects.equals(((BeanClass<?>) obj).getType(), type);
1✔
175
    }
176

177
    @SuppressWarnings("unchecked")
178
    public <S> BeanClass<S> getSuper(Class<S> target) {
179
        List<BeanClass<?>> superBeanClasses = supers();
1✔
180
        return (BeanClass<S>) superBeanClasses.stream().filter(beanClass -> beanClass.getType().equals(target))
1✔
181
                .findFirst().orElseGet(() -> superBeanClasses.stream()
1✔
182
                        .map(beanClass -> beanClass.getSuper(target))
1✔
183
                        .filter(Objects::nonNull).findFirst().orElse(null));
1✔
184
    }
185

186
    private List<BeanClass<?>> supers() {
187
        List<Type> suppers = new ArrayList<>(asList(type.getGenericInterfaces()));
1✔
188
        suppers.add(type.getGenericSuperclass());
1✔
189
        return suppers.stream().filter(Objects::nonNull)
1✔
190
                .map(t -> BeanClass.create(GenericType.createGenericType(t)))
1✔
191
                .collect(toList());
1✔
192
    }
193

194
    public boolean isCollection() {
195
        return getType().isArray() || Iterable.class.isAssignableFrom(getType());
1✔
196
    }
197

198
    public Map<String, Property<T>> getProperties() {
199
        return typeInfo.getProperties();
1✔
200
    }
201

202
    public Property<T> getProperty(String name) {
203
        return typeInfo.getProperty(name);
1✔
204
    }
205

206
    public boolean isInstance(Object instance) {
207
        return type.isInstance(instance);
1✔
208
    }
209

210
    public boolean isInheritedFrom(Class<?> type) {
211
        return type.isAssignableFrom(this.type);
1✔
212
    }
213

214
    public <A extends Annotation> Optional<A> annotation(Class<A> annotationClass) {
215
        return ofNullable(getAnnotation(annotationClass));
1✔
216
    }
217

218
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
219
        return type.getAnnotation(annotationClass);
1✔
220
    }
221

222
    public Type getGenericType() {
223
        return getType();
×
224
    }
225

226
    public boolean is(Class<?> clazz) {
227
        return getType().equals(clazz);
×
228
    }
229

230
    @Override
231
    public String toString() {
UNCOV
232
        return getType().toString();
×
233
    }
234
}
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

© 2026 Coveralls, Inc