Skip to content

Method: lambda$doMatch$1(Field)

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.proxy.lazy.gen;
19:
20: import cz.cvut.kbss.jopa.model.metamodel.AnnotatedAccessor;
21: import cz.cvut.kbss.jopa.model.metamodel.gen.PersistentPropertyMatcher;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23: import net.bytebuddy.description.method.MethodDescription;
24: import net.bytebuddy.description.type.TypeDescription;
25:
26: import java.beans.Introspector;
27: import java.util.Optional;
28:
29: /**
30: * Matches only persistent field getters.
31: * <p>
32: * This matcher checks that there exists a persistent field corresponding to the method name that appears to be a
33: * getter. {@code get, is, has} prefixes are supported.
34: *
35: * @param <T> MethodDescription
36: */
37: public class PersistentPropertyGetterMatcher<T extends MethodDescription> extends PersistentPropertyMatcher<T> {
38:
39: public PersistentPropertyGetterMatcher(Class<?> parentType) {
40: super(parentType);
41: }
42:
43: @Override
44: protected boolean doMatch(T target) {
45: final String name = target.getName();
46: final Optional<String> fieldName = resolveFieldName(name, target);
47: return fieldName.flatMap(this::tryFindingField)
48: // Field must not be transient, and it must not be an identifier (no need generating getter interceptor
49: // for identifier)
50:• .map(f -> !EntityPropertiesUtils.isFieldTransient(f))
51: .orElse(false);
52: }
53:
54: @Override
55: protected Optional<String> resolveFieldName(String methodName, MethodDescription methodDesc) {
56: assert methodDesc.getParameters().isEmpty();
57: final TypeDescription.Generic returnType = methodDesc.getReturnType();
58: if (methodName.startsWith(AnnotatedAccessor.GET_PREFIX)) {
59: return Optional.of(Introspector.decapitalize(methodName.substring(AnnotatedAccessor.GET_PREFIX.length())));
60: } else if (methodName.startsWith(AnnotatedAccessor.IS_PREFIX) && (returnType.represents(Boolean.class))) {
61: return Optional.of(Introspector.decapitalize(methodName.substring(AnnotatedAccessor.IS_PREFIX.length())));
62: } else if (methodName.startsWith(AnnotatedAccessor.HAS_PREFIX) && methodDesc.getReturnType()
63: .represents(Boolean.class)) {
64: return Optional.of(Introspector.decapitalize(methodName.substring(AnnotatedAccessor.HAS_PREFIX.length())));
65: } else {
66: return Optional.empty();
67: }
68: }
69: }
70: