aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/tools/AbstractTrace.java
blob: 8b77c1a004ef03e00a32975fd30891babe8581b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *     Matthew Webster - initial implementation
 *******************************************************************************/
package org.aspectj.weaver.tools;

import java.io.File;
import java.lang.reflect.Array;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Pattern;

import org.aspectj.bridge.IMessage.Kind;

public abstract class AbstractTrace implements Trace {

	private static final Pattern packagePrefixPattern = Pattern.compile("([^.])[^.]*(\\.)");

	protected Class<?> tracedClass;

	private static SimpleDateFormat timeFormat;

	protected AbstractTrace (Class clazz) {
		this.tracedClass = clazz;
	}

	@Override
	public abstract void enter (String methodName, Object thiz, Object[] args);

	@Override
	public abstract void enter(String methodName, Object thiz);

	@Override
	public abstract void exit(String methodName, Object ret);

	@Override
	public abstract void exit(String methodName, Throwable th);

	/*
	 * Convenience methods
	 */
	public void enter (String methodName) {
		enter(methodName,null,null);
	}

	@Override
	public void enter (String methodName, Object thiz, Object arg) {
		enter(methodName,thiz,new Object[] { arg });
	}

	@Override
	public void enter (String methodName, Object thiz, boolean z) {
		enter(methodName,thiz,Boolean.valueOf(z));
	}

	@Override
	public void exit (String methodName, boolean b) {
		exit(methodName,Boolean.valueOf(b));
	}

	@Override
	public void exit (String methodName, int i) {
		exit(methodName, Integer.valueOf(i));
	}

	@Override
	public void event (String methodName, Object thiz, Object arg) {
		event(methodName,thiz,new Object[] { arg });
	}

	@Override
	public void warn(String message) {
		warn(message,null);
	}

	@Override
	public void error(String message) {
		error(message,null);
	}

	@Override
	public void fatal (String message) {
		fatal(message,null);
	}

	/*
	 * Formatting
	 */
	protected String formatMessage(String kind, String className, String methodName, Object thiz, Object[] args) {
		StringBuilder message = new StringBuilder();
		Date now = new Date();
		message.append(formatDate(now)).append(" ");
		message.append(Thread.currentThread().getName()).append(" ");
		message.append(kind).append(" ");
		message.append(formatClassName(className));
		message.append(".").append(methodName);
		if (thiz != null) message.append(" ").append(formatObj(thiz));
		if (args != null) message.append(" ").append(formatArgs(args));
		return message.toString();
	}

	/**
	 * @param className full dotted class name
	 * @return short version of class name with package collapse to initials
	 */
	private String formatClassName(String className) {
		return packagePrefixPattern.matcher(className).replaceAll("$1.");
	}

	protected String formatMessage(String kind, String text, Throwable th) {
		StringBuilder message = new StringBuilder();
		Date now = new Date();
		message.append(formatDate(now)).append(" ");
		message.append(Thread.currentThread().getName()).append(" ");
		message.append(kind).append(" ");
		message.append(text);
		if (th != null) message.append(" ").append(formatObj(th));
		return message.toString();
	}

	private static String formatDate (Date date) {
		if (timeFormat == null) {
			timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
		}

		return timeFormat.format(date);
	}

	/**
	 * Format objects safely avoiding toString which can cause recursion,
	 * NullPointerExceptions or highly verbose results.
	 *
	 * @param obj parameter to be formatted
	 * @return the formatted parameter
	 */
	protected Object formatObj(Object obj) {

		/* These classes have a safe implementation of toString() */
		if (obj == null
				|| obj instanceof String
				|| obj instanceof Number
				|| obj instanceof Boolean
				|| obj instanceof Exception
				|| obj instanceof Character
				|| obj instanceof Class
				|| obj instanceof File
				|| obj instanceof StringBuffer
				|| obj instanceof URL
				|| obj instanceof Kind
				) return obj;
		else if (obj.getClass().isArray()) {
			return formatArray(obj);
		}
		else if (obj instanceof Collection) {
			return formatCollection((Collection)obj);
		}
		else try {

			// Classes can provide an alternative implementation of toString()
			if (obj instanceof Traceable) {
				return ((Traceable)obj).toTraceString();
			}

			// classname@hashcode
			else return formatClassName(obj.getClass().getName()) + "@" + Integer.toHexString(System.identityHashCode(obj));

			/* Object.hashCode() can be override and may thow an exception */
		} catch (Exception ex) {
			return obj.getClass().getName() + "@FFFFFFFF";
		}
	}

	protected String formatArray(Object obj) {
		return obj.getClass().getComponentType().getName() + "[" + Array.getLength(obj) + "]";
	}

	protected String formatCollection(Collection<?> c) {
		return c.getClass().getName() + "(" + c.size() + ")";
	}

	/**
	 * Format arguments into a comma separated list
	 *
	 * @param args array of arguments
	 * @return the formated list
	 */
	protected String formatArgs(Object[] args) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < args.length; i++) {
			sb.append(formatObj(args[i]));
			if (i < args.length-1) sb.append(", ");
		}

		return sb.toString();
	}

	protected Object[] formatObjects(Object[] args) {
		for (int i = 0; i < args.length; i++) {
			args[i] = formatObj(args[i]);
		}

		return args;
	}
}