1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 28 import java.io.IOException ; 29 import com.mchange.v2.codegen.IndentedWriter; 30 31 public class PropsToStringGeneratorExtension implements GeneratorExtension 32 { 33 private Collection excludePropNames = null; 34 35 public void setExcludePropertyNames( Collection excludePropNames ) 36 { this.excludePropNames = excludePropNames; } 37 38 public Collection getExcludePropertyNames() 39 { return excludePropNames; } 40 41 public Collection extraGeneralImports() 42 { return Collections.EMPTY_SET; } 43 44 public Collection extraSpecificImports() 45 { return Collections.EMPTY_SET; } 46 47 public Collection extraInterfaceNames() 48 { return Collections.EMPTY_SET; } 49 50 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 51 throws IOException 52 { 53 iw.println("public String toString()"); 54 iw.println("{"); 55 iw.upIndent(); 56 57 iw.println("StringBuffer sb = new StringBuffer();"); 58 iw.println("sb.append( super.toString() );"); 59 iw.println("sb.append(\" [ \");"); 60 61 for (int i = 0, len = props.length; i < len; ++i) 62 { 63 Property prop = props[i]; 64 65 if ( excludePropNames != null && excludePropNames.contains( prop.getName() ) ) 66 continue; 67 68 iw.println("sb.append( \"" + prop.getName() + " -> \"" + " + " + prop.getName() + " );"); 69 if ( i != len - 1 ) 70 iw.println("sb.append( \", \");"); 71 } 72 73 iw.println(); 74 iw.println("String extraToStringInfo = this.extraToStringInfo();"); 75 iw.println("if (extraToStringInfo != null)"); 76 iw.upIndent(); 77 iw.println("sb.append( extraToStringInfo );"); 78 iw.downIndent(); 79 80 81 iw.println("sb.append(\" ]\");"); 82 iw.println("return sb.toString();"); 83 iw.downIndent(); 84 iw.println("}"); 85 iw.println(); 86 iw.println("protected String extraToStringInfo()"); 87 iw.println("{ return null; }"); 88 } 89 } 90 | Popular Tags |