KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
28 import com.mchange.v2.codegen.IndentedWriter;
29
30 public class CloneableExtension implements GeneratorExtension
31 {
32     boolean export_public;
33     boolean exception_swallowing;
34
35     String JavaDoc mLoggerName = null;
36
37     public boolean isExportPublic()
38     { return export_public; }
39
40     public void setExportPublic(boolean export_public)
41     { this.export_public = export_public; }
42
43     public boolean isExceptionSwallowing()
44     { return exception_swallowing; }
45
46     public void setExceptionSwallowing(boolean exception_swallowing)
47     { this.exception_swallowing = exception_swallowing; }
48
49     public String JavaDoc getMLoggerName()
50     { return mLoggerName; }
51
52     public void setMLoggerName( String JavaDoc mLoggerName )
53     { this.mLoggerName = mLoggerName; }
54
55     public CloneableExtension(boolean export_public, boolean exception_swallowing)
56     {
57     this.export_public = export_public;
58     this.exception_swallowing = exception_swallowing;
59     }
60
61     public CloneableExtension()
62     { this ( true, false ); }
63
64     public Collection extraGeneralImports()
65     { return (mLoggerName == null ? ((Collection) Collections.EMPTY_SET) : ((Collection) Arrays.asList( new String JavaDoc[] {"com.mchange.v2.log"} )) ); }
66
67     public Collection extraSpecificImports()
68     { return Collections.EMPTY_SET; }
69
70     public Collection extraInterfaceNames()
71     {
72     Set set = new HashSet();
73     set.add( "Cloneable" );
74     return set;
75     }
76
77     public void generate(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
78     throws IOException JavaDoc
79     {
80     if (export_public)
81         {
82         iw.print("public Object clone()");
83         if ( !exception_swallowing )
84             iw.println(" throws CloneNotSupportedException");
85         else
86             iw.println();
87         iw.println("{");
88         iw.upIndent();
89         if ( exception_swallowing )
90             {
91             iw.println("try");
92             iw.println("{");
93             iw.upIndent();
94             }
95         iw.println( "return super.clone();" );
96         if ( exception_swallowing )
97             {
98             iw.downIndent();
99             iw.println("}");
100             iw.println("catch (CloneNotSupportedException e)");
101             iw.println("{");
102             iw.upIndent();
103             if (mLoggerName == null)
104                 iw.println("e.printStackTrace();");
105             else
106                 {
107                 iw.println("if ( " + mLoggerName + ".isLoggable( MLevel.FINE ) )" );
108                 iw.upIndent();
109                 iw.println( mLoggerName + ".log( MLevel.FINE, \"Inconsistent clone() definitions between subclass and superclass! \", e );");
110                 iw.downIndent();
111                 }
112             iw.println("throw new RuntimeException(\"Inconsistent clone() definitions between subclass and superclass! \" + e);" );
113             iw.downIndent();
114             iw.println("}");
115             }
116             
117         iw.downIndent();
118         iw.println("}");
119         }
120     //else, write nothing... just add Cloneable to interface definitions...
121
}
122 }
123
Popular Tags