1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 import java.io.IOException ; 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 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 getMLoggerName() 50 { return mLoggerName; } 51 52 public void setMLoggerName( String 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 [] {"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 superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 78 throws IOException 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 } 122 } 123 | Popular Tags |