1 30 package com.genimen.djeneric.tools.strongtyper; 31 32 public class ExceptionGenerator extends Generator 33 { 34 public ExceptionGenerator() 35 { 36 } 37 38 public String getPackageName() 39 { 40 return getItfPackageName(); 41 } 42 43 public String getClassName() 44 { 45 return getExceptionClassName() + (isAbstract() ? getGeneratedSuffix() : ""); 46 } 47 48 public String getCode() throws Exception 49 { 50 StringBuffer code = new StringBuffer (5000); 51 if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n"); 52 53 code.append("import java.io.*;\n" + "import com.genimen.djeneric.repository.exceptions.*;\n" + "\n"); 54 55 code.append(StrongTyper.getRegenerationTags(1)); 56 57 code 58 .append("public class " 59 + getClassName() 60 + " extends DjenericException implements Serializable\n" 61 + "{ private static final String CHAINED_MSG = \"Chained to: \";\n" 62 + "\n" 63 + " private String _exceptionId = \"ERRORTYPE_UNKNOWN\";\n" 64 + " private String _stackTrace = null;\n" 65 + " public static String OBJECT_NOT_FOUND = \"OBJECT_NOT_FOUND\";\n" 66 + " public static String MULTIPLE_OBJECTS_FOUND = \"MULTIPLE_OBJECTS_FOUND\";\n" 67 + " transient private boolean _ignoreMyOwnStack = false; // transient flag to recognize remote (serialized) exceptions\n" 68 + "\n" + " public " + getClassName() + "()\n" + " {\n" + " storeStack(this);\n" + " }\n" + "\n" 69 + " public " + getClassName() + "(Throwable chainThis)\n" + " {\n" 70 + " super(chainThis.getMessage());\n" + " storeStack(chainThis);\n" + " chain(chainThis);\n" 71 + " }\n" + "\n" + " public " + getClassName() + "(String message)\n" + " {\n" 72 + " super(message);\n" + " storeStack(this);\n" + " }\n" + "\n" + " public " + getClassName() 73 + "(String message, String exceptionId)\n" + " {\n" + " super(message);\n" 74 + " _exceptionId = exceptionId;\n" + " storeStack(this);\n" + " }\n" + "\n" + " public " 75 + getClassName() + "(String message, String exceptionId, Throwable chainThis)\n" + " {\n" 76 + " super(message);\n" + " _exceptionId = exceptionId;\n" + " storeStack(chainThis);\n" 77 + " chain(chainThis);\n" + " }\n" + "\n" + " private void storeStack(Throwable theEx)\n" + " {\n" 78 + " _stackTrace = stack2String(theEx);\n" 79 + " _ignoreMyOwnStack = true; // transient flag to recognize remote (serialized) exceptions\n" 80 + " }\n" + "\n" + " private void chain(Throwable chainThis)\n" + " {\n" 81 + " if(chainThis instanceof " + getClassName() + ")\n" + " { " + getClassName() + " bex = (" 82 + getClassName() + ") chainThis;\n" + " _exceptionId = bex._exceptionId;\n" + " }\n" 83 + " }\n" + "\n" + " public String getExceptionId()\n" + " {\n" + " return _exceptionId;\n" 84 + " }\n" + "\n" + " /**\n" + " * Prints the stack trace of the Thrown exception.\n" + " */\n" 85 + " public void printStackTrace()\n" + " {\n" + " if(_stackTrace != null)\n" 86 + " { System.err.println(_stackTrace);\n" 87 + " if(!_ignoreMyOwnStack) System.err.print(CHAINED_MSG);\n" + " }\n" 88 + " if(!_ignoreMyOwnStack) super.printStackTrace();\n" + " }\n" + "\n" + " /**\n" 89 + " * Prints the stack trace of the Thrown exception in a printStream.\n" + " *\n" 90 + " * @param printStream printStream which should be used to print the stacktrace.\n" + " */\n" 91 + " public void printStackTrace(PrintStream printStream)\n" + " {\n" 92 + " if(_stackTrace != null)\n" + " { printStream.println(_stackTrace);\n" 93 + " if(!_ignoreMyOwnStack) printStream.print(CHAINED_MSG);\n" + " }\n" 94 + " if(!_ignoreMyOwnStack) super.printStackTrace(printStream);\n" + " }\n" + " /**\n" 95 + " * Prints the stack trace of the thrown exception in a printWriter.\n" + " *\n" 96 + " * @param writer printWriter which should be used to print the stacktrace.\n" + " */\n" 97 + " public void printStackTrace(PrintWriter writer)\n" + " {\n" + " if(_stackTrace != null)\n" 98 + " { writer.println(_stackTrace);\n" 99 + " if(!_ignoreMyOwnStack) writer.print(CHAINED_MSG);\n" + " }\n" 100 + " if(!_ignoreMyOwnStack) super.printStackTrace(writer);\n" + " }\n" + "\n" 101 + " public void setExceptionID(String exceptionId)\n" + " {\n" + " _exceptionId = exceptionId;\n" 102 + " }\n" + "\n" + " public String toString()\n" + " {\n" 103 + " String exceptionMessage = getClass().getName()+\": \";\n" + "\n" 104 + " if(getMessage() != null) exceptionMessage += getMessage() + \"\\n\";\n" 105 + " exceptionMessage += \"Exception ID = \" + _exceptionId + \"\\n\";\n" 106 + " return exceptionMessage.trim();\n" + " }\n" + "\n" 107 + " private String stack2String(Throwable x)\n" + " {\n" + " if(x == null) return \"\";\n" + "\n" 108 + " StringWriter stringWriter = new StringWriter();\n" 109 + " java.io.PrintWriter stackTrace = new java.io.PrintWriter(stringWriter);\n" 110 + " x.printStackTrace(stackTrace);\n" + " String result = stringWriter.toString().trim();\n" 111 + " if(result.length() == 0) result = null;\n" + "\n" + " return result;\n" + " }\n"); 112 code.append(StrongTyper.getRegenerationTags(0)); 113 code.append("}\n"); 114 return code.toString(); 115 } 116 } | Popular Tags |