Be sure to pick up all inherited interfaces and scan those with the @InheritedTable annotation for methods. When doing that, if we encounter the same method name twice prefer the least generic implementation.

This commit is contained in:
Greg Burd 2017-09-25 15:50:29 -04:00
parent 844ebd9155
commit d3c24b70bf
8 changed files with 57 additions and 8 deletions

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId>
<version>2.0.42-SNAPSHOT</version>
<version>2.0.43-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helenus</name>

View file

@ -16,6 +16,7 @@
package net.helenus.config;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Function;
import net.helenus.mapping.annotation.Transient;
@ -33,6 +34,10 @@ public enum GetterMethodDetector implements Function<Method, Boolean> {
return false;
}
if (Modifier.isStatic(method.getModifiers())) {
return false;
}
// Methods marked "Transient" are not mapped, skip them.
if (method.getDeclaredAnnotation(Transient.class) != null) {
return false;

View file

@ -25,6 +25,7 @@ import net.helenus.core.Helenus;
import net.helenus.core.annotation.Cacheable;
import net.helenus.mapping.annotation.*;
import net.helenus.support.HelenusMappingException;
import org.apache.commons.lang3.ClassUtils;
public final class HelenusMappingEntity implements HelenusEntity {
@ -52,18 +53,32 @@ public final class HelenusMappingEntity implements HelenusEntity {
HelenusSettings settings = Helenus.settings();
List<Method> methods = new ArrayList<Method>();
Map<String, Method> methods = new HashMap<String, Method>();
for (Method m : iface.getDeclaredMethods()) {
methods.put(m.getName(), m);
}
methods.addAll(Arrays.asList(iface.getDeclaredMethods()));
for (Class<?> c : iface.getInterfaces()) {
methods.addAll(Arrays.asList(c.getDeclaredMethods()));
for (Class<?> c : ClassUtils.getAllInterfaces(iface)) {
if (c.getDeclaredAnnotation(Table.class) != null || c.getDeclaredAnnotation(InheritedTable.class) != null) {
for (Method m : c.getDeclaredMethods()) {
Method o = methods.get(m.getName());
if (o != null) {
// Prefer overridden method implementation.
if (o.getDeclaringClass().isAssignableFrom(m.getDeclaringClass())) {
methods.put(m.getName(), m);
}
} else {
methods.put(m.getName(), m);
}
}
}
}
List<HelenusProperty> propsLocal = new ArrayList<HelenusProperty>();
ImmutableMap.Builder<String, HelenusProperty> propsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<String, Method> methodsBuilder = ImmutableMap.builder();
for (Method method : methods) {
for (Method method : methods.values()) {
if (settings.getGetterMethodDetector().apply(method)) {

View file

@ -29,6 +29,9 @@ public interface Animal {
@Column(ordinal = 1)
boolean eatable();
@Column
boolean warmBlodded();
@Transient
default Animal me() {
return this;

View file

@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.Index;
import net.helenus.mapping.annotation.Table;
@Table("cats")
public interface Cat extends Animal {
public interface Cat extends Mammal {
@Column(ordinal = 0)
@Index(caseSensitive = false)

View file

@ -53,6 +53,7 @@ public class HierarchyTest extends AbstractEmbeddedCassandraTest {
Optional<Cat> animal =
session.<Cat>select(Cat.class).where(cat::nickname, eq("garfield")).sync().findFirst();
Assert.assertTrue(animal.isPresent());
Assert.assertTrue(animal.get().warmBlodded());
Assert.assertFalse(animal.get().eatable());
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (C) 2015 The Helenus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.helenus.test.integration.core.hierarchy;
import net.helenus.mapping.annotation.InheritedTable;
@InheritedTable
public interface Mammal extends Animal {
default boolean warmBlodded() { return true; }
}

View file

@ -19,7 +19,7 @@ import net.helenus.mapping.annotation.Column;
import net.helenus.mapping.annotation.Table;
@Table("pigs")
public interface Pig extends Animal {
public interface Pig extends Mammal {
@Column(ordinal = 0)
String nickname();