01: /*
02: * Distributed as part of c3p0 v.0.9.1.2
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v2.codegen.bean;
24:
25: import java.util.*;
26:
27: import java.io.IOException;
28: import com.mchange.v2.codegen.IndentedWriter;
29:
30: public class PropsToStringGeneratorExtension implements
31: GeneratorExtension {
32: private Collection excludePropNames = null;
33:
34: public void setExcludePropertyNames(Collection excludePropNames) {
35: this .excludePropNames = excludePropNames;
36: }
37:
38: public Collection getExcludePropertyNames() {
39: return excludePropNames;
40: }
41:
42: public Collection extraGeneralImports() {
43: return Collections.EMPTY_SET;
44: }
45:
46: public Collection extraSpecificImports() {
47: return Collections.EMPTY_SET;
48: }
49:
50: public Collection extraInterfaceNames() {
51: return Collections.EMPTY_SET;
52: }
53:
54: public void generate(ClassInfo info, Class super classType,
55: Property[] props, Class[] propTypes, IndentedWriter iw)
56: throws IOException {
57: iw.println("public String toString()");
58: iw.println("{");
59: iw.upIndent();
60:
61: iw.println("StringBuffer sb = new StringBuffer();");
62: iw.println("sb.append( super.toString() );");
63: iw.println("sb.append(\" [ \");");
64:
65: for (int i = 0, len = props.length; i < len; ++i) {
66: Property prop = props[i];
67:
68: if (excludePropNames != null
69: && excludePropNames.contains(prop.getName()))
70: continue;
71:
72: iw.println("sb.append( \"" + prop.getName() + " -> \""
73: + " + " + prop.getName() + " );");
74: if (i != len - 1)
75: iw.println("sb.append( \", \");");
76: }
77:
78: iw.println();
79: iw
80: .println("String extraToStringInfo = this.extraToStringInfo();");
81: iw.println("if (extraToStringInfo != null)");
82: iw.upIndent();
83: iw.println("sb.append( extraToStringInfo );");
84: iw.downIndent();
85:
86: iw.println("sb.append(\" ]\");");
87: iw.println("return sb.toString();");
88: iw.downIndent();
89: iw.println("}");
90: iw.println();
91: iw.println("protected String extraToStringInfo()");
92: iw.println("{ return null; }");
93: }
94: }
|