001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import java.util.*;
026: import java.io.IOException;
027: import com.mchange.v2.codegen.IndentedWriter;
028:
029: public class CloneableExtension implements GeneratorExtension {
030: boolean export_public;
031: boolean exception_swallowing;
032:
033: String mLoggerName = null;
034:
035: public boolean isExportPublic() {
036: return export_public;
037: }
038:
039: public void setExportPublic(boolean export_public) {
040: this .export_public = export_public;
041: }
042:
043: public boolean isExceptionSwallowing() {
044: return exception_swallowing;
045: }
046:
047: public void setExceptionSwallowing(boolean exception_swallowing) {
048: this .exception_swallowing = exception_swallowing;
049: }
050:
051: public String getMLoggerName() {
052: return mLoggerName;
053: }
054:
055: public void setMLoggerName(String mLoggerName) {
056: this .mLoggerName = mLoggerName;
057: }
058:
059: public CloneableExtension(boolean export_public,
060: boolean exception_swallowing) {
061: this .export_public = export_public;
062: this .exception_swallowing = exception_swallowing;
063: }
064:
065: public CloneableExtension() {
066: this (true, false);
067: }
068:
069: public Collection extraGeneralImports() {
070: return (mLoggerName == null ? ((Collection) Collections.EMPTY_SET)
071: : ((Collection) Arrays
072: .asList(new String[] { "com.mchange.v2.log" })));
073: }
074:
075: public Collection extraSpecificImports() {
076: return Collections.EMPTY_SET;
077: }
078:
079: public Collection extraInterfaceNames() {
080: Set set = new HashSet();
081: set.add("Cloneable");
082: return set;
083: }
084:
085: public void generate(ClassInfo info, Class super classType,
086: Property[] props, Class[] propTypes, IndentedWriter iw)
087: throws IOException {
088: if (export_public) {
089: iw.print("public Object clone()");
090: if (!exception_swallowing)
091: iw.println(" throws CloneNotSupportedException");
092: else
093: iw.println();
094: iw.println("{");
095: iw.upIndent();
096: if (exception_swallowing) {
097: iw.println("try");
098: iw.println("{");
099: iw.upIndent();
100: }
101: iw.println("return super.clone();");
102: if (exception_swallowing) {
103: iw.downIndent();
104: iw.println("}");
105: iw.println("catch (CloneNotSupportedException e)");
106: iw.println("{");
107: iw.upIndent();
108: if (mLoggerName == null)
109: iw.println("e.printStackTrace();");
110: else {
111: iw.println("if ( " + mLoggerName
112: + ".isLoggable( MLevel.FINE ) )");
113: iw.upIndent();
114: iw
115: .println(mLoggerName
116: + ".log( MLevel.FINE, \"Inconsistent clone() definitions between subclass and superclass! \", e );");
117: iw.downIndent();
118: }
119: iw
120: .println("throw new RuntimeException(\"Inconsistent clone() definitions between subclass and superclass! \" + e);");
121: iw.downIndent();
122: iw.println("}");
123: }
124:
125: iw.downIndent();
126: iw.println("}");
127: }
128: //else, write nothing... just add Cloneable to interface definitions...
129: }
130: }
|