aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/SignatureUtils.java
blob: 4df6681aa352a4a3509123aef82896edb8915aa7 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* *******************************************************************
 * Copyright (c) 2008 Contributors
 *
 * 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:
 *   Andy Clement - refactored out of MemberImpl
 * ******************************************************************/
package org.aspectj.weaver;

import java.lang.reflect.Modifier;

public class SignatureUtils {

	public static String getSignatureString(Member m, World world) {
		MemberKind kind = m.getKind();
		if (kind == Member.METHOD) {
			return getMethodSignatureString(m, world);
		} else if (kind == Member.CONSTRUCTOR) {
			return getConstructorSignatureString(m, world);
		} else if (kind == Member.FIELD) {
			return getFieldSignatureString(m, world);
		} else if (kind == Member.HANDLER) {
			return getHandlerSignatureString(m, world);
		} else if (kind == Member.STATIC_INITIALIZATION) {
			return getStaticInitializationSignatureString(m, world);
		} else if (kind == Member.ADVICE) {
			return getAdviceSignatureString(m, world);
		} else if (kind == Member.MONITORENTER || kind == Member.MONITOREXIT) {
			return getMonitorSignatureString(m, world);
		} else {
			throw new BCException("Do not know the signature string for MemberKind " + kind);
		}
	}

	public static String getSignatureMakerName(Member m) {
		MemberKind kind = m.getKind();
		if (kind == Member.METHOD) {
			return "makeMethodSig";
		} else if (kind == Member.CONSTRUCTOR) {
			return "makeConstructorSig";
		} else if (kind == Member.FIELD) {
			return "makeFieldSig";
		} else if (kind == Member.HANDLER) {
			return "makeCatchClauseSig";
		} else if (kind == Member.STATIC_INITIALIZATION) {
			return "makeInitializerSig";
		} else if (kind == Member.ADVICE) {
			return "makeAdviceSig";
		} else if (kind == Member.MONITORENTER) {
			return "makeLockSig";
		} else if (kind == Member.MONITOREXIT) {
			return "makeUnlockSig";
		} else {
			throw new BCException("Do not know the signature maker name for MemberKind " + kind);
		}
	}

	public static String getSignatureType(Member m) {
		MemberKind kind = m.getKind();
		if (m.getName().equals("<clinit>") && kind != Member.STATIC_INITIALIZATION)
			throw new BCException();
		// if (m.getName().equals("<clinit>")) return "org.aspectj.lang.reflect.InitializerSignature";

		if (kind == Member.METHOD) {
			return "org.aspectj.lang.reflect.MethodSignature";
		} else if (kind == Member.CONSTRUCTOR) {
			return "org.aspectj.lang.reflect.ConstructorSignature";
		} else if (kind == Member.FIELD) {
			return "org.aspectj.lang.reflect.FieldSignature";
		} else if (kind == Member.HANDLER) {
			return "org.aspectj.lang.reflect.CatchClauseSignature";
		} else if (kind == Member.STATIC_INITIALIZATION) {
			return "org.aspectj.lang.reflect.InitializerSignature";
		} else if (kind == Member.ADVICE) {
			return "org.aspectj.lang.reflect.AdviceSignature";
		} else if (kind == Member.MONITORENTER) {
			return "org.aspectj.lang.reflect.LockSignature";
		} else if (kind == Member.MONITOREXIT) {
			return "org.aspectj.lang.reflect.UnlockSignature";
		} else {
			throw new BCException("Do not know the signature type for MemberKind " + kind);
		}
	}

	// ---

	private static String getHandlerSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(0));
		buf.append('-');
		// buf.append(getName());
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		buf.append(makeString(m.getParameterTypes()[0]));
		buf.append('-');
		String pName = "<missing>";
		String[] names = m.getParameterNames(world);
		if (names != null)
			pName = names[0];
		buf.append(pName);
		buf.append('-');
		return buf.toString();
	}

	private static String getStaticInitializationSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(m.getModifiers(world)));
		buf.append('-');
		// buf.append(getName());
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		return buf.toString();
	}

	protected static String getAdviceSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(m.getModifiers(world)));
		buf.append('-');
		buf.append(m.getName());
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		buf.append(makeString(m.getParameterTypes()));
		buf.append('-');
		buf.append(makeString(m.getParameterNames(world)));
		buf.append('-');
		buf.append(makeString(m.getExceptions(world)));
		buf.append('-');
		buf.append(makeString(m.getReturnType()));
		buf.append('-');
		return buf.toString();
	}

	protected static String getMethodSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(m.getModifiers(world)));
		buf.append('-');
		buf.append(m.getName());
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		buf.append(makeString(m.getParameterTypes()));
		buf.append('-');
		buf.append(makeString(m.getParameterNames(world)));
		buf.append('-');
		buf.append(makeString(m.getExceptions(world)));
		buf.append('-');
		buf.append(makeString(m.getReturnType()));
		buf.append('-');
		return buf.toString();
	}

	protected static String getMonitorSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(Modifier.STATIC)); // modifiers
		buf.append('-');
		buf.append(m.getName()); // name
		buf.append('-');
		buf.append(makeString(m.getDeclaringType())); // Declaring Type
		buf.append('-');
		buf.append(makeString(m.getParameterTypes()[0])); // Parameter Types
		buf.append('-');
		buf.append(""); // Parameter names
		buf.append('-');
		return buf.toString();
	}

	protected static String getConstructorSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(m.getModifiers(world)));
		buf.append('-');
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		buf.append(makeString(m.getParameterTypes()));
		buf.append('-');
		buf.append(makeString(m.getParameterNames(world)));
		buf.append('-');
		buf.append(makeString(m.getExceptions(world)));
		buf.append('-');
		return buf.toString();
	}

	protected static String getFieldSignatureString(Member m, World world) {
		StringBuffer buf = new StringBuffer();
		buf.append(makeString(m.getModifiers(world)));
		buf.append('-');
		buf.append(m.getName());
		buf.append('-');
		buf.append(makeString(m.getDeclaringType()));
		buf.append('-');
		buf.append(makeString(m.getReturnType()));
		buf.append('-');
		return buf.toString();
	}

	protected static String makeString(int i) {
		return Integer.toString(i, 16);
	}

	protected static String makeString(UnresolvedType t) {
		// this is the inverse of the odd behavior for Class.forName w/ arrays
		if (t.isArray()) {
			// this behavior matches the string used by the eclipse compiler for Foo.class literals
			return t.getSignature().replace('/', '.');
		} else {
			return t.getName();
		}
	}

	protected static String makeString(UnresolvedType[] types) {
		if (types == null)
			return "";
		StringBuffer buf = new StringBuffer();
		for (UnresolvedType type : types) {
			buf.append(makeString(type));
			buf.append(':');
		}
		return buf.toString();
	}

	protected static String makeString(String[] names) {
		if (names == null)
			return "";
		StringBuffer buf = new StringBuffer();
		for (String name : names) {
			buf.append(name);
			buf.append(':');
		}
		return buf.toString();
	}

}