Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/sboesebeck/morphium into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
sboesebeck committed Apr 20, 2015
2 parents 94bbef9 + 07fcc63 commit 5a41cbb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/de/caluga/morphium/AnnotationAndReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ public <T extends Annotation> T getAnnotationFromHierarchy(Class<?> cls, Class<?
z = z.getSuperclass();
if (z == null) break;
}

Queue<Class<?>> interfaces = new LinkedList<>();
for (Class<?> anInterface : cls.getInterfaces()) {
interfaces.add(anInterface);
}
while(!interfaces.isEmpty()) {
Class<?> iface = interfaces.poll();
if(iface.isAnnotationPresent(anCls))
return iface.getAnnotation(anCls);
for (Class<?> anInterface : iface.getInterfaces()) {
interfaces.add(anInterface);
}
}
return null;
}

Expand Down
81 changes: 81 additions & 0 deletions test/de/caluga/test/mongo/suite/InterfacePolymorphismTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.caluga.test.mongo.suite;

import de.caluga.morphium.MorphiumSingleton;
import de.caluga.morphium.annotations.Entity;
import de.caluga.morphium.annotations.Id;
import de.caluga.morphium.annotations.caching.NoCache;
import org.bson.types.ObjectId;
import org.junit.Test;

import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: bfsmith
* Date: 30.03.15
* Test interface polymorphism mechanism in Morphium
*/
public class InterfacePolymorphismTest extends MongoTest {
@Test
public void polymorphTest() throws Exception {
MorphiumSingleton.get().dropCollection(IfaceTestType.class);
IfaceTestType ifaceTestType = new IfaceTestType();
ifaceTestType.setName("A Complex Type");
ifaceTestType.setPolyTest(new SubClass(11));
MorphiumSingleton.get().store(ifaceTestType);

assert (MorphiumSingleton.get().createQueryFor(IfaceTestType.class).countAll() == 2);
List<IfaceTestType> lst = MorphiumSingleton.get().createQueryFor(IfaceTestType.class).asList();
for (IfaceTestType tst : lst) {
log.info("Class " + tst.getClass().toString());
}
}

@Entity
public class IfaceTestType {
@Id
private ObjectId id;
private String name;
private IPolyTest polyTest;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public IPolyTest getPolyTest() {
return polyTest;
}

public void setPolyTest(IPolyTest polyTest) {
this.polyTest = polyTest;
}
}

@Entity(polymorph = true)
@NoCache
public static interface IPolyTest {
int getNumber();
}

public static class SubClass implements IPolyTest {
private int number;

private SubClass() {}

public SubClass(int num) {
number = num;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}
}
}

0 comments on commit 5a41cbb

Please sign in to comment.