Skip to content

Package: PersistentPropertyMatcher

PersistentPropertyMatcher

nameinstructionbranchcomplexitylinemethod
PersistentPropertyMatcher(Class)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
doMatch(MethodDescription)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
equals(Object)
M: 26 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$doMatch$0(Field)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
tryFindingField(String)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model.metamodel.gen;
19:
20: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
21: import net.bytebuddy.description.method.MethodDescription;
22: import net.bytebuddy.matcher.ElementMatcher;
23:
24: import java.lang.reflect.Field;
25: import java.util.Objects;
26: import java.util.Optional;
27:
28: /**
29: * Matches only persistent attribute getters/setters.
30: * <p>
31: * This matcher checks that there exists a persistent field corresponding to the method name that appears to be a
32: * getter/setter.
33: *
34: * @param <T> MethodDescription
35: */
36: public abstract class PersistentPropertyMatcher<T extends MethodDescription> extends ElementMatcher.Junction.ForNonNullValues<T> {
37:
38: private final Class<?> parentType;
39:
40: public PersistentPropertyMatcher(Class<?> parentType) {this.parentType = Objects.requireNonNull(parentType);}
41:
42: @Override
43: protected boolean doMatch(T target) {
44: final String name = target.getName();
45: final Optional<String> fieldName = resolveFieldName(name, target);
46:• return fieldName.flatMap(this::tryFindingField).map(f -> !EntityPropertiesUtils.isFieldTransient(f))
47: .orElse(false);
48: }
49:
50: protected abstract Optional<String> resolveFieldName(String methodName, MethodDescription methodDesc);
51:
52: protected Optional<Field> tryFindingField(String fieldName) {
53: Class<?> type = parentType;
54: do {
55: try {
56: Field f = type.getDeclaredField(fieldName);
57: return Optional.of(f);
58: } catch (NoSuchFieldException e) {
59: type = type.getSuperclass();
60: }
61:• } while (type != null);
62: return Optional.empty();
63: }
64:
65: @Override
66: public boolean equals(Object o) {
67:• if (this == o) {
68: return true;
69: }
70:• if (!(o instanceof PersistentPropertyMatcher<?> that)) {
71: return false;
72: }
73:• if (!super.equals(o)) {
74: return false;
75: }
76: return Objects.equals(parentType, that.parentType);
77: }
78:
79: @Override
80: public int hashCode() {
81: return Objects.hash(super.hashCode(), parentType);
82: }
83: }