KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > PropsToStringGeneratorExtension


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * 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
24 package com.mchange.v2.codegen.bean;
25
26 import java.util.*;
27
28 import java.io.IOException JavaDoc;
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 JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
51     throws IOException JavaDoc
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