From e7652ee3740c5d1237addd9fddf72c5f497f0ec6 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 20 May 2009 08:46:03 +0000 Subject: [PATCH] git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3319 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd --- src/main/java/org/xerial/lens/ObjectLens.java | 114 +++++++++++++------------- src/test/java/org/xerial/lens/LensTest.java | 32 +++++++- 2 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/main/java/org/xerial/lens/ObjectLens.java b/src/main/java/org/xerial/lens/ObjectLens.java index 29093d9..7d3cc8a 100644 --- a/src/main/java/org/xerial/lens/ObjectLens.java +++ b/src/main/java/org/xerial/lens/ObjectLens.java @@ -108,82 +108,78 @@ public class ObjectLens private void createBindRules(Class< ? > targetType) { // look for all super classes - for (Class< ? > eachClass = targetType; eachClass != null; eachClass = eachClass.getSuperclass()) + // scan public fields + for (Field eachField : targetType.getFields()) { - // scan public fields - for (Field eachField : eachClass.getFields()) + int fieldModifier = eachField.getModifiers(); + if (Modifier.isPublic(fieldModifier) && !Modifier.isTransient(fieldModifier)) { - int fieldModifier = eachField.getModifiers(); - if (Modifier.isPublic(fieldModifier) && !Modifier.isTransient(fieldModifier)) - { - Class< ? > fieldType = eachField.getType(); - String paramName = getCanonicalParameterName(eachField.getName()); - - if (TypeInfo.isArray(fieldType)) - { - // ignore the array field - continue; - } - else if (TypeInfo.isMap(fieldType)) - { + Class< ? > fieldType = eachField.getType(); + String paramName = getCanonicalParameterName(eachField.getName()); - // TODO map putter + if (TypeInfo.isArray(fieldType)) + { + // ignore the array field + continue; + } + else if (TypeInfo.isMap(fieldType)) + { - } - if (TypeInfo.isCollection(fieldType)) - { - Class< ? > elementType = ReflectionUtil.getGenericCollectionElementType(eachField); - setterContainer.add(ParameterSetter.newSetter(elementType, paramName, eachField)); - } - else - setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField)); + // TODO map putter } + if (TypeInfo.isCollection(fieldType)) + { + Class< ? > elementType = ReflectionUtil.getGenericCollectionElementType(eachField); + setterContainer.add(ParameterSetter.newSetter(elementType, paramName, eachField)); + } + else + setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField)); } - // scan methods - for (Method eachMethod : eachClass.getMethods()) - { - String methodName = eachMethod.getName(); - String paramPart = pickPropertyName(methodName); + } + + // scan methods + for (Method eachMethod : targetType.getMethods()) + { + String methodName = eachMethod.getName(); + String paramPart = pickPropertyName(methodName); - if (methodName.startsWith("add") || methodName.startsWith("set") || methodName.startsWith("put")) + if (methodName.startsWith("add") || methodName.startsWith("set") || methodName.startsWith("put")) + { + Class< ? >[] argTypes = eachMethod.getParameterTypes(); + switch (argTypes.length) { - Class< ? >[] argTypes = eachMethod.getParameterTypes(); - switch (argTypes.length) - { - case 1: - { - addNewSetter(setterContainer, paramPart, eachMethod); - break; - } - case 2: + case 1: + { + addNewSetter(setterContainer, paramPart, eachMethod); + break; + } + case 2: + { + // relation adder + Pair relName = pickRelationName(paramPart); + if (relName == null) { - // relation adder - Pair relName = pickRelationName(paramPart); - if (relName == null) - { - // infer relation node names - relName = new Pair(getCanonicalParameterName(argTypes[0].getSimpleName()), - getCanonicalParameterName(argTypes[1].getSimpleName())); - } - - relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(), relName - .getSecond(), eachMethod)); - break; - } + // infer relation node names + relName = new Pair(getCanonicalParameterName(argTypes[0].getSimpleName()), + getCanonicalParameterName(argTypes[1].getSimpleName())); } - continue; - + relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(), relName + .getSecond(), eachMethod)); + break; } - else if (methodName.startsWith("append")) - { - addNewSetter(setterContainer, paramPart, eachMethod); - continue; } + continue; + + } + else if (methodName.startsWith("append")) + { + addNewSetter(setterContainer, paramPart, eachMethod); + continue; } } diff --git a/src/test/java/org/xerial/lens/LensTest.java b/src/test/java/org/xerial/lens/LensTest.java index d102365..f53e71c 100644 --- a/src/test/java/org/xerial/lens/LensTest.java +++ b/src/test/java/org/xerial/lens/LensTest.java @@ -36,6 +36,7 @@ import org.xerial.core.XerialException; import org.xerial.silk.SilkUtilTest; import org.xerial.util.FileResource; import org.xerial.util.HashedArrayList; +import org.xerial.util.StringUtil; import org.xerial.util.log.Logger; public class LensTest @@ -103,26 +104,49 @@ public class LensTest @Test public void testBED() throws Exception { - BEDGene g = Lens.loadSilk(BEDGene.class, FileResource.find(LensTest.class, "sample.bed.silk")); + BEDQuery g = Lens.loadSilk(BEDQuery.class, FileResource.find(LensTest.class, "sample.bed.silk")); + _logger.info(StringUtil.join(g.gene, "\n")); } - public static class CDS + public static class Locus { public long start; public long end; + + @Override + public String toString() + { + return String.format("(%s, %s)", start, end); + } } - public static class Exon extends CDS + public static class CDS extends Locus { } - public static class BEDGene + public static class Exon extends Locus { + } + + public static class BEDGene extends Locus + { + public String coordinate; + public String name; public List cds; public List exon; + @Override + public String toString() + { + return String.format("%s %s (%s, %s), cds=%s, exon=%s", coordinate, name, start, end, cds, exon); + } + } + + public static class BEDQuery + { + public List gene; } } -- 2.11.0