1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 import com.mchange.v2.log.*; 28 29 import java.lang.reflect.Modifier ; 30 import java.io.IOException ; 31 import com.mchange.v2.codegen.CodegenUtils; 32 import com.mchange.v2.codegen.IndentedWriter; 33 34 35 41 public class ExplicitPropsConstructorGeneratorExtension implements GeneratorExtension 42 { 43 final static MLogger logger = MLog.getLogger( ExplicitPropsConstructorGeneratorExtension.class ); 44 45 String [] propNames; 46 47 boolean skips_silently = false; 48 49 public ExplicitPropsConstructorGeneratorExtension() 50 {} 51 52 public ExplicitPropsConstructorGeneratorExtension(String [] propNames) 53 { this.propNames = propNames; } 54 55 public String [] getPropNames() 56 { return (String []) propNames.clone(); } 57 58 public void setPropNames(String [] propNames) 59 { this.propNames = (String []) propNames.clone(); } 60 61 public boolean isSkipsSilently() 62 { return skips_silently; } 63 64 public void setsSkipsSilently( boolean skips_silently ) 65 { this.skips_silently = skips_silently; } 66 67 int ctor_modifiers = Modifier.PUBLIC; 68 69 public Collection extraGeneralImports() 70 { return Collections.EMPTY_SET; } 71 72 public Collection extraSpecificImports() 73 { return Collections.EMPTY_SET; } 74 75 public Collection extraInterfaceNames() 76 { return Collections.EMPTY_SET; } 77 78 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 79 throws IOException 80 { 81 Map propNamesToProps = new HashMap(); 82 for (int i = 0, len = props.length; i < len; ++i) 83 propNamesToProps.put( props[i].getName(), props[i] ); 84 85 List subPropsList = new ArrayList( propNames.length ); 86 for (int i = 0, len = propNames.length; i < len; ++i) 87 { 88 Property p = (Property) propNamesToProps.get( propNames[i] ); 89 if ( p == null ) 90 logger.warning("Could not include property '" + propNames[i] +"' in explicit-props-constructor generated for bean class '" + 91 info.getClassName() +"' because the property is not defined for the bean. Skipping."); 92 else 93 subPropsList.add(p); 94 } 95 96 if (subPropsList.size() > 0) 97 { 98 Property[] subProps = (Property[]) subPropsList.toArray( new Property[ subPropsList.size() ] ); 99 100 iw.print( CodegenUtils.getModifierString( ctor_modifiers ) ); 101 iw.print( info.getClassName() + "( "); 102 BeangenUtils.writeArgList(subProps, true, iw); 103 iw.println(" )"); 104 iw.println("{"); 105 iw.upIndent(); 106 107 for (int i = 0, len = subProps.length; i < len; ++i) 108 { 109 iw.print("this." + subProps[i].getName() + " = "); 110 String setExp = subProps[i].getDefensiveCopyExpression(); 111 if (setExp == null) 112 setExp = subProps[i].getName(); 113 iw.println(setExp + ';'); 114 } 115 116 iw.downIndent(); 117 iw.println("}"); 118 } 119 } 120 } 121 | Popular Tags |