1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 import java.lang.reflect.Modifier ; 28 import java.io.IOException ; 29 import com.mchange.v2.codegen.CodegenUtils; 30 import com.mchange.v2.codegen.IndentedWriter; 31 32 public class BeanExtractingGeneratorExtension implements GeneratorExtension 33 { 34 int ctor_modifiers = Modifier.PUBLIC; 35 int method_modifiers = Modifier.PRIVATE; 36 37 public void setConstructorModifiers( int ctor_modifiers ) 38 { this.ctor_modifiers = ctor_modifiers; } 39 40 public int getConstructorModifiers() 41 { return ctor_modifiers; } 42 43 public void setExtractMethodModifiers( int ctor_modifiers ) 44 { this.method_modifiers = method_modifiers; } 45 46 public int getExtractMethodModifiers() 47 { return method_modifiers; } 48 49 public Collection extraGeneralImports() 50 { return Collections.EMPTY_SET; } 51 52 public Collection extraSpecificImports() 53 { 54 Set set = new HashSet(); 55 set.add("java.beans.BeanInfo"); 56 set.add("java.beans.PropertyDescriptor"); 57 set.add("java.beans.Introspector"); 58 set.add("java.beans.IntrospectionException"); 59 set.add("java.lang.reflect.InvocationTargetException"); 60 return set; 61 } 62 63 public Collection extraInterfaceNames() 64 { return Collections.EMPTY_SET; } 65 66 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 67 throws IOException 68 { 69 iw.println("private static Class[] NOARGS = new Class[0];"); 70 iw.println(); 71 iw.print( CodegenUtils.getModifierString( method_modifiers ) ); 72 iw.print(" void extractPropertiesFromBean( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException"); 73 iw.println("{"); 74 iw.upIndent(); 75 76 iw.println("BeanInfo bi = Introspector.getBeanInfo( bean.getClass() );"); 77 iw.println("PropertyDescriptor[] pds = bi.getPropertyDescriptors();"); 78 iw.println("for (int i = 0, len = pds.length; i < len; ++i)"); 79 iw.println("{"); 80 iw.upIndent(); 81 82 for (int i = 0, len = props.length; i < len; ++i) 83 { 84 iw.println("if (\"" + props[i].getName() + "\".equals( pds[i].getName() ) )"); 85 iw.upIndent(); 86 iw.println("this." + props[i].getName() + " = " + extractorExpr( props[i], propTypes[i] ) + ';'); 87 iw.downIndent(); 88 } 89 iw.println("}"); 91 92 iw.downIndent(); 93 iw.println("}"); iw.println(); 95 iw.print( CodegenUtils.getModifierString( ctor_modifiers ) ); 96 iw.println(' ' + info.getClassName() + "( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException"); 97 iw.println("{"); 98 iw.upIndent(); 99 iw.println("extractPropertiesFromBean( bean );"); 100 iw.downIndent(); 101 iw.println("}"); 102 } 103 104 private String extractorExpr( Property prop, Class propType ) 105 { 106 if ( propType.isPrimitive() ) 107 { 108 String castType = BeangenUtils.capitalize( prop.getSimpleTypeName() ); 109 String valueMethod = prop.getSimpleTypeName() + "Value()"; 110 111 if ( propType == char.class) 112 castType = "Character"; 113 else if ( propType == int.class) 114 castType = "Integer"; 115 116 return "((" + castType + ") pds[i].getReadMethod().invoke( bean, NOARGS ))." + valueMethod; 117 } 118 else 119 return "(" + prop.getSimpleTypeName() + ") pds[i].getReadMethod().invoke( bean, NOARGS )"; 120 } 121 } 122 | Popular Tags |