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:
parent
844ebd9155
commit
d3c24b70bf
8 changed files with 57 additions and 8 deletions
2
pom.xml
2
pom.xml
|
@ -5,7 +5,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.helenus</groupId>
|
<groupId>net.helenus</groupId>
|
||||||
<artifactId>helenus-core</artifactId>
|
<artifactId>helenus-core</artifactId>
|
||||||
<version>2.0.42-SNAPSHOT</version>
|
<version>2.0.43-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>helenus</name>
|
<name>helenus</name>
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package net.helenus.config;
|
package net.helenus.config;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import net.helenus.mapping.annotation.Transient;
|
import net.helenus.mapping.annotation.Transient;
|
||||||
|
|
||||||
|
@ -33,6 +34,10 @@ public enum GetterMethodDetector implements Function<Method, Boolean> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Modifier.isStatic(method.getModifiers())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Methods marked "Transient" are not mapped, skip them.
|
// Methods marked "Transient" are not mapped, skip them.
|
||||||
if (method.getDeclaredAnnotation(Transient.class) != null) {
|
if (method.getDeclaredAnnotation(Transient.class) != null) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -25,6 +25,7 @@ import net.helenus.core.Helenus;
|
||||||
import net.helenus.core.annotation.Cacheable;
|
import net.helenus.core.annotation.Cacheable;
|
||||||
import net.helenus.mapping.annotation.*;
|
import net.helenus.mapping.annotation.*;
|
||||||
import net.helenus.support.HelenusMappingException;
|
import net.helenus.support.HelenusMappingException;
|
||||||
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
|
|
||||||
public final class HelenusMappingEntity implements HelenusEntity {
|
public final class HelenusMappingEntity implements HelenusEntity {
|
||||||
|
|
||||||
|
@ -52,18 +53,32 @@ public final class HelenusMappingEntity implements HelenusEntity {
|
||||||
|
|
||||||
HelenusSettings settings = Helenus.settings();
|
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 : ClassUtils.getAllInterfaces(iface)) {
|
||||||
for (Class<?> c : iface.getInterfaces()) {
|
if (c.getDeclaredAnnotation(Table.class) != null || c.getDeclaredAnnotation(InheritedTable.class) != null) {
|
||||||
methods.addAll(Arrays.asList(c.getDeclaredMethods()));
|
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>();
|
List<HelenusProperty> propsLocal = new ArrayList<HelenusProperty>();
|
||||||
ImmutableMap.Builder<String, HelenusProperty> propsBuilder = ImmutableMap.builder();
|
ImmutableMap.Builder<String, HelenusProperty> propsBuilder = ImmutableMap.builder();
|
||||||
ImmutableMap.Builder<String, Method> methodsBuilder = ImmutableMap.builder();
|
ImmutableMap.Builder<String, Method> methodsBuilder = ImmutableMap.builder();
|
||||||
|
|
||||||
for (Method method : methods) {
|
for (Method method : methods.values()) {
|
||||||
|
|
||||||
if (settings.getGetterMethodDetector().apply(method)) {
|
if (settings.getGetterMethodDetector().apply(method)) {
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,9 @@ public interface Animal {
|
||||||
@Column(ordinal = 1)
|
@Column(ordinal = 1)
|
||||||
boolean eatable();
|
boolean eatable();
|
||||||
|
|
||||||
|
@Column
|
||||||
|
boolean warmBlodded();
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
default Animal me() {
|
default Animal me() {
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.Index;
|
||||||
import net.helenus.mapping.annotation.Table;
|
import net.helenus.mapping.annotation.Table;
|
||||||
|
|
||||||
@Table("cats")
|
@Table("cats")
|
||||||
public interface Cat extends Animal {
|
public interface Cat extends Mammal {
|
||||||
|
|
||||||
@Column(ordinal = 0)
|
@Column(ordinal = 0)
|
||||||
@Index(caseSensitive = false)
|
@Index(caseSensitive = false)
|
||||||
|
|
|
@ -53,6 +53,7 @@ public class HierarchyTest extends AbstractEmbeddedCassandraTest {
|
||||||
Optional<Cat> animal =
|
Optional<Cat> animal =
|
||||||
session.<Cat>select(Cat.class).where(cat::nickname, eq("garfield")).sync().findFirst();
|
session.<Cat>select(Cat.class).where(cat::nickname, eq("garfield")).sync().findFirst();
|
||||||
Assert.assertTrue(animal.isPresent());
|
Assert.assertTrue(animal.isPresent());
|
||||||
|
Assert.assertTrue(animal.get().warmBlodded());
|
||||||
Assert.assertFalse(animal.get().eatable());
|
Assert.assertFalse(animal.get().eatable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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; }
|
||||||
|
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ import net.helenus.mapping.annotation.Column;
|
||||||
import net.helenus.mapping.annotation.Table;
|
import net.helenus.mapping.annotation.Table;
|
||||||
|
|
||||||
@Table("pigs")
|
@Table("pigs")
|
||||||
public interface Pig extends Animal {
|
public interface Pig extends Mammal {
|
||||||
|
|
||||||
@Column(ordinal = 0)
|
@Column(ordinal = 0)
|
||||||
String nickname();
|
String nickname();
|
||||||
|
|
Loading…
Reference in a new issue