|
| 1 | +/* |
| 2 | + * Copyright 2011-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.neo4j.repository.support; |
| 17 | + |
| 18 | +import java.beans.PropertyDescriptor; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | + |
| 22 | +import org.aopalliance.intercept.MethodInterceptor; |
| 23 | +import org.aopalliance.intercept.MethodInvocation; |
| 24 | +import org.springframework.beans.BeanUtils; |
| 25 | +import org.springframework.beans.BeanWrapper; |
| 26 | +import org.springframework.beans.NotReadablePropertyException; |
| 27 | +import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext; |
| 28 | +import org.springframework.data.neo4j.core.mapping.Neo4jPersistentEntity; |
| 29 | +import org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty; |
| 30 | +import org.springframework.data.neo4j.core.schema.Property; |
| 31 | +import org.springframework.data.projection.MethodInterceptorFactory; |
| 32 | +import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.ReflectionUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Basically a lenient property accessing method interceptor, first trying the entity property (or attribute), than |
| 39 | + * a potentially renamed attribute via {@link Property}. |
| 40 | + * |
| 41 | + * @author Michael J. Simons |
| 42 | + */ |
| 43 | +final class EntityAndGraphPropertyAccessingMethodInterceptor implements MethodInterceptor { |
| 44 | + |
| 45 | + static MethodInterceptorFactory createMethodInterceptorFactory(Neo4jMappingContext mappingContext) { |
| 46 | + return new MethodInterceptorFactory() { |
| 47 | + @Override |
| 48 | + public MethodInterceptor createMethodInterceptor(Object source, Class<?> targetType) { |
| 49 | + return new EntityAndGraphPropertyAccessingMethodInterceptor(source, mappingContext); |
| 50 | + } |
| 51 | + |
| 52 | + @Override public boolean supports(Object source, Class<?> targetType) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + private final BeanWrapper target; |
| 59 | + |
| 60 | + private EntityAndGraphPropertyAccessingMethodInterceptor(Object target, Neo4jMappingContext ctx) { |
| 61 | + |
| 62 | + Assert.notNull(target, "Proxy target must not be null!"); |
| 63 | + this.target = new GraphPropertyAndDirectFieldAccessFallbackBeanWrapper(target, ctx); |
| 64 | + } |
| 65 | + |
| 66 | + @Nullable |
| 67 | + @Override |
| 68 | + public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable { |
| 69 | + |
| 70 | + Method method = invocation.getMethod(); |
| 71 | + |
| 72 | + if (ReflectionUtils.isObjectMethod(method)) { |
| 73 | + return invocation.proceed(); |
| 74 | + } |
| 75 | + |
| 76 | + PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); |
| 77 | + |
| 78 | + if (descriptor == null) { |
| 79 | + throw new IllegalStateException("Invoked method is not a property accessor!"); |
| 80 | + } |
| 81 | + |
| 82 | + if (!isSetterMethod(method, descriptor)) { |
| 83 | + return target.getPropertyValue(descriptor.getName()); |
| 84 | + } |
| 85 | + |
| 86 | + if (invocation.getArguments().length != 1) { |
| 87 | + throw new IllegalStateException("Invoked setter method requires exactly one argument!"); |
| 88 | + } |
| 89 | + |
| 90 | + target.setPropertyValue(descriptor.getName(), invocation.getArguments()[0]); |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean isSetterMethod(Method method, PropertyDescriptor descriptor) { |
| 95 | + return method.equals(descriptor.getWriteMethod()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * this version of the {@link DirectFieldAccessFallbackBeanWrapper} checks if there's an attribute on the entity |
| 100 | + * annotated with {@link Property} mapping it to a different graph property when it fails to access the original |
| 101 | + * attribute If so, that property is accessed. If not, the original exception is rethrown. |
| 102 | + * This helps in projections such as described here |
| 103 | + * https://stackoverflow.com/questions/68938823/sdn6-projection-interfaces-with-property-mapping |
| 104 | + * that could have been used as workaround prior to fixing 2371. |
| 105 | + */ |
| 106 | + static class GraphPropertyAndDirectFieldAccessFallbackBeanWrapper extends DirectFieldAccessFallbackBeanWrapper { |
| 107 | + |
| 108 | + private final Neo4jMappingContext ctx; |
| 109 | + |
| 110 | + GraphPropertyAndDirectFieldAccessFallbackBeanWrapper(Object target, Neo4jMappingContext ctx) { |
| 111 | + super(target); |
| 112 | + this.ctx = ctx; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Object getPropertyValue(String propertyName) { |
| 117 | + try { |
| 118 | + return super.getPropertyValue(propertyName); |
| 119 | + } catch (NotReadablePropertyException e) { |
| 120 | + Neo4jPersistentEntity<?> entity = ctx.getPersistentEntity(super.getRootClass()); |
| 121 | + |
| 122 | + AtomicReference<String> value = new AtomicReference<>(); |
| 123 | + if (entity != null) { |
| 124 | + entity.doWithProperties( |
| 125 | + (org.springframework.data.mapping.PropertyHandler<Neo4jPersistentProperty>) p -> { |
| 126 | + if (p.findAnnotation(Property.class) != null && p.getPropertyName() |
| 127 | + .equals(propertyName)) { |
| 128 | + value.compareAndSet(null, p.getFieldName()); |
| 129 | + } |
| 130 | + }); |
| 131 | + if (value.get() != null) { |
| 132 | + return super.getPropertyValue(value.get()); |
| 133 | + } |
| 134 | + } |
| 135 | + throw e; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments