You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTrace.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Matthew Webster - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.tools;
  12. import java.io.File;
  13. public abstract class AbstractTrace implements Trace {
  14. protected Class tracedClass;
  15. protected AbstractTrace (Class clazz) {
  16. this.tracedClass = clazz;
  17. }
  18. public abstract void enter (String methodName, Object thiz, Object[] args);
  19. public abstract void enter(String methodName, Object thiz);
  20. public abstract void exit(String methodName, Object ret);
  21. public abstract void exit(String methodName, Throwable th);
  22. public void error(String message) {
  23. // TODO Auto-generated method stub
  24. }
  25. public void error(String message, Throwable th) {
  26. // TODO Auto-generated method stub
  27. }
  28. public void event(String methodName) {
  29. // TODO Auto-generated method stub
  30. }
  31. public void event(String methodName, Object thiz, Object[] args) {
  32. // TODO Auto-generated method stub
  33. }
  34. public void exit(String methodName) {
  35. // TODO Auto-generated method stub
  36. }
  37. public void info(String message) {
  38. // TODO Auto-generated method stub
  39. }
  40. public void warn(String message) {
  41. // TODO Auto-generated method stub
  42. }
  43. public void warn(String message, Throwable th) {
  44. // TODO Auto-generated method stub
  45. }
  46. /*
  47. * Convenience methods
  48. */
  49. public void enter (String methodName, Object thiz, Object arg) {
  50. enter(methodName,thiz,new Object[] { arg });
  51. }
  52. public void enter (String methodName, Object thiz, boolean z) {
  53. enter(methodName,thiz,new Boolean(z));
  54. }
  55. public void exit (String methodName, boolean b) {
  56. exit(methodName,new Boolean(b));
  57. }
  58. public boolean isTraceEnabled () {
  59. return true;
  60. }
  61. protected String formatMessage(String className, String methodName, Object thiz, Object[] args) {
  62. StringBuffer message = new StringBuffer();
  63. message.append(className);
  64. message.append(".").append(methodName);
  65. if (thiz != null) message.append(" ").append(formatObj(thiz));
  66. if (args != null) message.append(" ").append(formatArgs(args));
  67. return message.toString();
  68. }
  69. /**
  70. * Format objects safely avoiding toString which can cause recursion,
  71. * NullPointerExceptions or highly verbose results.
  72. *
  73. * @param obj parameter to be formatted
  74. * @return the formated parameter
  75. */
  76. protected Object formatObj(Object obj) {
  77. /* These classes have a safe implementation of toString() */
  78. if (obj == null
  79. || obj instanceof String
  80. || obj instanceof Number
  81. || obj instanceof Boolean
  82. || obj instanceof Exception
  83. || obj instanceof Character
  84. || obj instanceof Class
  85. || obj instanceof File
  86. || obj instanceof StringBuffer
  87. ) return obj;
  88. else try {
  89. /* Classes can provide an alternative implementation of toString() */
  90. if (obj instanceof Traceable) {
  91. Traceable t = (Traceable)obj;
  92. return t.toTraceString();
  93. }
  94. /* Use classname@hashcode */
  95. else return obj.getClass().getName() + "@" + Integer.toString(obj.hashCode(),16);
  96. /* Object.hashCode() can be override and may thow an exception */
  97. } catch (Exception ex) {
  98. return obj.getClass().getName();
  99. }
  100. }
  101. /**
  102. * Format arguments into a comma separated list
  103. *
  104. * @param names array of argument names
  105. * @param args array of arguments
  106. * @return the formated list
  107. */
  108. protected String formatArgs(Object[] args) {
  109. StringBuffer sb = new StringBuffer();
  110. for (int i = 0; i < args.length; i++) {
  111. sb.append(formatObj(args[i]));
  112. if (i < args.length-1) sb.append(", ");
  113. }
  114. return sb.toString();
  115. }
  116. }