Skip to content

Commit

Permalink
support array
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Aug 27, 2010
1 parent c811140 commit 0857f9a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 48 deletions.
32 changes: 32 additions & 0 deletions bason-example/src/main/java/info/sunng/bason/example/Flight.java
Expand Up @@ -22,6 +22,10 @@ public class Flight {
private int capacity;

private Set<String> drivers;

private String[] waiters;

private Integer[] commands;

/**
* @return the company
Expand Down Expand Up @@ -78,5 +82,33 @@ public void setDrivers(Set<String> drivers) {
public Set<String> getDrivers() {
return drivers;
}

/**
* @param waiters the waiters to set
*/
public void setWaiters(String[] waiters) {
this.waiters = waiters;
}

/**
* @return the waiters
*/
public String[] getWaiters() {
return waiters;
}

/**
* @param commands the commands to set
*/
public void setCommands(Integer[] commands) {
this.commands = commands;
}

/**
* @return the commands
*/
public Integer[] getCommands() {
return commands;
}

}
Expand Up @@ -38,6 +38,8 @@ public void testSerilization(){
f.setCompany("CA");
f.setFlightId("CA-2002");

f.setCommands(new Integer[]{28,43,54,55,1});

f.setDrivers(new HashSet<String>(Arrays.asList("Tom Hanks", "Nico Kiderman")));

p.setFlight(f);
Expand All @@ -63,6 +65,13 @@ public void testDeserilization(){
drivers.add("Hanks");
f.put("drivers", drivers);

BasicBSONList commands = new BasicBSONList();
commands.add(44);
commands.add(43);
commands.add(59);

f.put("commands", commands);

// f.put("capacity", 2);

s.put("flight", f);
Expand Down
Expand Up @@ -60,6 +60,10 @@ public String getType() {

return type;
}

public String getTypeRaw() {
return type;
}

/**
* @param type the type to set
Expand Down
Expand Up @@ -34,17 +34,16 @@ public SourceTemplate(Writer writer, String className,
}

public void writeSource() throws IOException {
doWriteHead(writer, className);
doWriteBody(writer, annotatedElements);
doWriteTail(writer);
doWriteHead(className);
doWriteBody(annotatedElements);
doWriteTail();

writer.flush();

writer.close();
}

protected void doWriteHead(Writer writer, String className)
throws IOException {
protected void doWriteHead(String className) throws IOException {
writer.write(StringUtils.asLine(0, "package ", StringUtils.splitAtLast(
className, ".")[0], ";"));
// import org.bson.*
Expand Down Expand Up @@ -76,13 +75,13 @@ protected void doWriteHead(Writer writer, String className)
* @param elements
* @throws IOException
*/
protected void doWriteBody(Writer writer,
List<BsonDocumentObjectElement> elements) throws IOException {
protected void doWriteBody(List<BsonDocumentObjectElement> elements)
throws IOException {

for (BsonDocumentObjectElement ele : annotatedElements) {
writeGetter(writer, ele);
writeGetter(ele);

writeSetter(writer, ele);
writeSetter(ele);
}

}
Expand All @@ -93,8 +92,7 @@ protected void doWriteBody(Writer writer,
* @param ele
* @throws IOException
*/
private void writeSetter(Writer writer, BsonDocumentObjectElement ele)
throws IOException {
private void writeSetter(BsonDocumentObjectElement ele) throws IOException {
writer.write(StringUtils.asLine(4, "public static final ", ele
.getClassName(), " fromBson(", ele.getClassName(),
" o, BSONObject bson){"));
Expand All @@ -116,9 +114,9 @@ private void writeSetter(Writer writer, BsonDocumentObjectElement ele)
.getType(), "(), (BSONObject)bson.get(", StringUtils
.quote(bsonAttrName), ")));"));
} else if (field.isArray()) /* array setter */{
writeArraySetter(writer, field, bsonAttrName);
writeArraySetter(field, bsonAttrName);
} else if (field.isCollection()) /* collection setter */{
writeCollectionSetter(writer, field, bsonAttrName);
writeCollectionSetter(field, bsonAttrName);
} else {
writer.write(StringUtils.asLine(12, "o.set", StringUtils
.capticalize(field.getName()), "((", field.getType(),
Expand All @@ -138,8 +136,8 @@ private void writeSetter(Writer writer, BsonDocumentObjectElement ele)
* @param writer2
* @param field
*/
protected void writeCollectionSetter(Writer writer2, FieldInfo field,
String bsonAttrName) throws IOException {
protected void writeCollectionSetter(FieldInfo field, String bsonAttrName)
throws IOException {
if (field.getType().startsWith("java.util.Collection")) {
writer.write(StringUtils.asLine(12, "o.set", StringUtils
.capticalize(field.getName()),
Expand Down Expand Up @@ -170,9 +168,12 @@ protected void writeCollectionSetter(Writer writer2, FieldInfo field,
* @param writer2
* @param field
*/
protected void writeArraySetter(Writer writer2, FieldInfo field,
String bsonAttrName) throws IOException {
// TODO
protected void writeArraySetter(FieldInfo field, String bsonAttrName)
throws IOException {
writer.write(StringUtils.asLine(12, "o.set", StringUtils
.capticalize(field.getName()), "(((BasicBSONList)bson.get(",
StringUtils.quote(bsonAttrName), ")).toArray(new ", StringUtils
.arrayTypeToZeroLengthArray(field.getType()), "));"));
}

/**
Expand All @@ -181,8 +182,7 @@ protected void writeArraySetter(Writer writer2, FieldInfo field,
* @param ele
* @throws IOException
*/
private void writeGetter(Writer writer, BsonDocumentObjectElement ele)
throws IOException {
private void writeGetter(BsonDocumentObjectElement ele) throws IOException {
writer.write(StringUtils.asLine(4,
"public static final BSONObject toBson(", ele.getClassName(),
" o){"));
Expand All @@ -206,9 +206,9 @@ private void writeGetter(Writer writer, BsonDocumentObjectElement ele)
.quote(bsonAttrName), ",", "toBson(", "o.get",
StringUtils.capticalize(field.getName()), "()", "));"));
} else if (field.isCollection()) /* collection getter */{
writeCollectionGetter(writer, field, bsonAttrName);
writeCollectionGetter(field, bsonAttrName);
} else if (field.isArray()) /* array getter */{
writeArrayGetter(writer, field, bsonAttrName);
writeArrayGetter(field, bsonAttrName);
} else {
writer.write(StringUtils.asLine(8, "bson.put(", StringUtils
.quote(bsonAttrName), ",", "o.get", StringUtils
Expand All @@ -227,10 +227,16 @@ private void writeGetter(Writer writer, BsonDocumentObjectElement ele)
* @param writer2
* @param field
*/
protected void writeArrayGetter(Writer writer2, FieldInfo field,
String bsonAttrName) throws IOException {
// TODO Auto-generated method stub
protected void writeArrayGetter(FieldInfo field, String bsonAttrName)
throws IOException {
writer.write(StringUtils.asLine(8, "if (o.get", StringUtils
.capticalize(field.getName()), "()!= null) {"));

writer.write(StringUtils.asLine(12, "bson.put(", StringUtils
.quote(bsonAttrName), ",", "Arrays.asList(o.get", StringUtils
.capticalize(field.getName()), "()));"));

writer.write(StringUtils.asLine(8, "}"));
}

/**
Expand All @@ -239,22 +245,22 @@ protected void writeArrayGetter(Writer writer2, FieldInfo field,
* @param writer2
* @param field
*/
protected void writeCollectionGetter(Writer writer2, FieldInfo field,
String bsonAttrName) throws IOException {
writer2.write(StringUtils.asLine(8, "if (o.get", StringUtils
protected void writeCollectionGetter(FieldInfo field, String bsonAttrName)
throws IOException {
writer.write(StringUtils.asLine(8, "if (o.get", StringUtils
.capticalize(field.getName()), "()!= null) {"));
writer2.write(StringUtils.asLine(12, "bson.put(", StringUtils
writer.write(StringUtils.asLine(12, "bson.put(", StringUtils
.quote(bsonAttrName), ",", "new ArrayList(o.get", StringUtils
.capticalize(field.getName()), "()));"));
writer2.write(StringUtils.asLine(8, "}"));
writer.write(StringUtils.asLine(8, "}"));
}

/**
*
* @param writer
* @throws IOException
*/
protected void doWriteTail(Writer writer) throws IOException {
protected void doWriteTail() throws IOException {

writer.write(StringUtils.asLine(0, "}"));

Expand Down
Expand Up @@ -12,60 +12,70 @@

/**
* @author SunNing
*
*
* @since Aug 18, 2010
*/
public class GetterElementVisitor6 extends ElementKindVisitor6<FieldInfo, Void> {

/* (non-Javadoc)
* @see javax.lang.model.util.ElementKindVisitor6#visitExecutableAsMethod(javax.lang.model.element.ExecutableElement, java.lang.Object)
/*
* (non-Javadoc)
*
* @see
* javax.lang.model.util.ElementKindVisitor6#visitExecutableAsMethod(javax
* .lang.model.element.ExecutableElement, java.lang.Object)
*/
@Override
public FieldInfo visitExecutableAsMethod(ExecutableElement e, Void p) {
// annotated with @BsonIgnore
if (e.getAnnotation(BsonIgnore.class) != null){
if (e.getAnnotation(BsonIgnore.class) != null) {
return null;
}
String name = e.getSimpleName().toString();
if(name.startsWith("get")) {
if (name.startsWith("get")) {

return createNameTypeTuple(e);

} else {
return null;
}
}

/**
*
* @param ele
* @return
*/
private FieldInfo createNameTypeTuple(ExecutableElement ele){
private FieldInfo createNameTypeTuple(ExecutableElement ele) {
String name = ele.getSimpleName().toString();
StringBuffer propertyName = new StringBuffer(name.substring(3));
char firstChar = propertyName.charAt(0);
propertyName.setCharAt(0, Character.toLowerCase(firstChar));
String fieldName = propertyName.toString();
String typeName = ele.getReturnType().toString();

FieldInfo ntt = new FieldInfo(fieldName, typeName);
if (ele.getReturnType().accept(new IsBsonDocumentVisitor(), null)){

if (ele.getReturnType().accept(new IsBsonDocumentVisitor(), null)) {
ntt.setDocument(true);
}

if (ele.getReturnType().accept(new IsArrayVisitor(), null)) {
ntt.setArray(true);
} else if (ele.getReturnType().accept(new IsCollectionVisitor(), null)){

if (typeName.indexOf('.') == -1) {
throw new IllegalStateException(
"Primitive array not supported currently, use wrapped ones");
}

} else if (ele.getReturnType().accept(new IsCollectionVisitor(), null)) {
ntt.setCollection(true);
}

BsonAlias alias = ele.getAnnotation(BsonAlias.class);
if (alias != null){
if (alias != null) {
ntt.setAlias(alias.value());
}

return ntt;
}

Expand Down
Expand Up @@ -95,4 +95,21 @@ public static String removeGenericQuote(String str) {

return str;
}

/**
*
* @param arrayType
* @return
*/
public static String arrayTypeToZeroLengthArray(String arrayType){
int o= arrayType.indexOf("[]");
if (o < 0){
throw new IllegalStateException("Not a array type");
}

StringBuffer sb = new StringBuffer();
sb.append(arrayType.substring(0, o)).append("[0]");

return sb.toString();
}
}
Expand Up @@ -29,6 +29,10 @@ public List<BsonDocumentObjectElement> getBSOE() {
FieldInfo f = new FieldInfo("data", "java.util.Collection");
f.setCollection(true);
ntts.add(f);

FieldInfo f2 = new FieldInfo("meta", "double[]");
f2.setArray(true);
ntts.add(f2);

bsoe.setFields(ntts);

Expand Down

0 comments on commit 0857f9a

Please sign in to comment.