Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 63ded692f6a0cb07520acd96ecb2935d15f9de05 (plain) (blame)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsp.ui.internal.contentassist;

import java.beans.Introspector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import com.ibm.icu.util.StringTokenizer;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.jsp.ui.internal.Logger;

/**
 * Navigates the IJavaProject classpath (incl. source) on a given resource and infers bean properties
 * given a fully qualified beanname. Bean properties can be retrieved using:
 * <code>getRuntimeProperties(IResource baseResource, String typeName)</code>
 * 
 * @plannedfor 1.0
 */
public class BeanInfoProvider implements IBeanInfoProvider {

	public class JavaPropertyDescriptor implements IJavaPropertyDescriptor {
		String fType = null;
		String fName = null;
		boolean fReadable = true;
		boolean fWritable = true;

		public JavaPropertyDescriptor(String name, String type, boolean readable, boolean writable) {
			fName = name;
			fType = type;
			fReadable = readable;
			fWritable = writable;
		}

		public String getDeclaredType() {
			return fType;
		}

		public String getDisplayName() {
			return fName;
		}

		public String getName() {
			return fName;
		}

		public boolean getReadable() {
			return fReadable;
		}

		public boolean getWriteable() {
			return fWritable;
		}
	}

	// looks up encoded type (see Class.getName), and gives you a displayable string
	private HashMap fEncodedTypeMap = null;
	// to avoid repeat properties from showing up
	private HashSet fRepeatMethods = null;

	public BeanInfoProvider() {
		fRepeatMethods = new HashSet();
	}

	/**
	 * Returns the inferred properties of a bean based on the project from the baseResource,
	 * and the fully qualified name of the bean.
	 * 
	 * @param baseResource the base resource where the bean is being used
	 * @param typeName the <i>fully qualified</i> type name (eg. javax.swing.JButton) of the bean
	 */
	public IJavaPropertyDescriptor[] getRuntimeProperties(IResource baseResource, String typeName) {
		IJavaProject javaProject = JavaCore.create(baseResource.getProject());
		QualifiedName typeQualifiedName = getTypeQualifiedName(typeName);
		List getMethodResults = new ArrayList();
		List isMethodResults = new ArrayList();
		List setMethodResults = new ArrayList();
		List descriptorResults = new ArrayList();
		try {
			IType type = javaProject.findType(typeQualifiedName.getQualifier() + "." + typeQualifiedName.getLocalName()); //$NON-NLS-1$
			// type must exist
			if(type != null) {
				ITypeHierarchy hierarchy = type.newTypeHierarchy(null);
				IType[] supers = hierarchy.getAllSuperclasses(type);
	
				IMethod[] methods = type.getMethods();
				// iterate the bean's methods
				for (int i = 0; i < methods.length; i++)
					acceptMethod(getMethodResults, isMethodResults, setMethodResults, methods[i]);
				// the bean hierarchy's methods
				for (int i = 0; i < supers.length; i++) {
					methods = supers[i].getMethods();
					for (int j = 0; j < methods.length; j++)
						acceptMethod(getMethodResults, isMethodResults, setMethodResults, methods[j]);
				}
				adaptMethodsToPropertyDescriptors(getMethodResults, isMethodResults, setMethodResults, descriptorResults);
			}
		}
		catch (JavaModelException jmex) {
			Logger.logException("Problem navigating JavaProject in BeanInfoProvider", jmex); //$NON-NLS-1$
		}

		IJavaPropertyDescriptor[] finalResults = new IJavaPropertyDescriptor[descriptorResults.size()];
		System.arraycopy(descriptorResults.toArray(), 0, finalResults, 0, descriptorResults.size());
		return finalResults;
	}

	/**
	 * Retrieves the necessary information from method declaration lists, creates and fills a list of JavaPropertyDescriptors.
	 * @param getMethods
	 * @param isMethods
	 * @param setMethods
	 * @param descriptorResults
	 */
	private void adaptMethodsToPropertyDescriptors(List getMethods, List isMethods, List setMethods, List descriptors) throws JavaModelException {
		List readable = new ArrayList();
		HashMap types = new HashMap();

		// iterate through get* and is* methods, updating 'readable' list and 'types' map
		filterGetMethods(getMethods, readable, types);
		filterIsMethods(isMethods, readable, types);

		// iterate set* methods, checking overlap w/ readable
		Iterator it = setMethods.iterator();
		IMethod temp = null;
		String name = ""; //$NON-NLS-1$
		String type = ""; //$NON-NLS-1$
		String[] encodedParams = null;
		String returnType = ""; //$NON-NLS-1$
		String param0 = ""; //$NON-NLS-1$

		while (it.hasNext()) {
			temp = (IMethod) it.next();
			name = createPropertyNameFromMethod(temp);
			// invalid naming convention
			if (name == null)
				continue;

			returnType = getDecodedTypeName(temp.getReturnType());
			// setter should have no return type
			if (!returnType.equals("void")) //$NON-NLS-1$
				continue;

			// need to get type from parameter
			encodedParams = temp.getParameterTypes();
			if (encodedParams != null && encodedParams.length > 0) {
				if (encodedParams.length > 1) {
					// multiple params
					param0 = getDecodedTypeName(encodedParams[0]);
					if (!param0.equals("int")) //$NON-NLS-1$
						// not a valid indexed property
						continue;
					
					type = getDecodedTypeName(encodedParams[1]);
				}
				else {
					// one param, regular setter
					if (isArray(encodedParams[0]))
						type = getDecodedTypeName(encodedParams[0]);
				}
			}

			if (readable.contains(name)) {
				// writable and readable
				if (!fRepeatMethods.contains(name)) {
					descriptors.add(new JavaPropertyDescriptor(name, (String) types.get(name), true, true));
					readable.remove(name);
					fRepeatMethods.add(name);
				}
			}
			else {
				// wasn't readable, just writable
				String[] params = temp.getParameterTypes();
				// can't be setProperty if no parameters
				if (!(params.length > 0))
					continue;
				if (!fRepeatMethods.contains(name)) {
					type = getDecodedTypeName(params[0]);
					descriptors.add(new JavaPropertyDescriptor(name, type, false, true));
					fRepeatMethods.add(name);
				}
			}
		}
		// add leftover from readable, get* and is* methods (readable = true, writable = false)
		it = readable.iterator();
		while (it.hasNext()) {
			name = (String) it.next();
			if (!fRepeatMethods.contains(name)) {
				descriptors.add(new JavaPropertyDescriptor(name, (String) types.get(name), true, false));
				fRepeatMethods.add(name);
			}
		}
	}

	private void filterGetMethods(List getMethods, List readable, HashMap types) throws JavaModelException {
		IMethod temp;
		String name;
		String encodedReturnType;
		String returnType;
		Iterator it = getMethods.iterator();
		String[] encodedParams;
		String paramType;
		// iterate get* methods
		while (it.hasNext()) {
			temp = (IMethod) it.next();
			name = createPropertyNameFromMethod(temp);
			// invalid bean naming convention
			if (name == null)
				continue;

			encodedReturnType = temp.getReturnType();
			returnType = getDecodedTypeName(encodedReturnType);

			//  can't get be a getProperty if returns void
			if (returnType.equals("void")) //$NON-NLS-1$
				continue;

			// check params in case it's indexed propety
			encodedParams = temp.getParameterTypes();
			if (encodedParams != null && encodedParams.length == 1) {
				paramType = getDecodedTypeName(encodedParams[0]);
				// syntax is > Type getter(int);
				if (!paramType.equals("int")) { //$NON-NLS-1$
					//it's not an indexed property
					continue;
				}
				// it is indexed, prop type is an ARRAY
				returnType += "[]"; //$NON-NLS-1$
			}

			readable.add(name);
			types.put(name, returnType);
		}

	}

	private void filterIsMethods(List isMethodResults, List readable, HashMap types) throws JavaModelException {
		IMethod temp;
		String name;
		String encodedReturnType;
		String returnType;
		String[] encodedParams;
		String paramType;
		// iterate is* methods
		Iterator it = isMethodResults.iterator();
		while (it.hasNext()) {
			temp = (IMethod) it.next();
			name = createPropertyNameFromMethod(temp);
			// invalid bean naming convention
			if (name == null)
				continue;
			encodedReturnType = temp.getReturnType();
			returnType = getDecodedTypeName(encodedReturnType);

			// isProperty only valid for boolean
			if (!returnType.equals("boolean")) //$NON-NLS-1$
				continue;

			// check params in case it's indexed propety
			encodedParams = temp.getParameterTypes();
			if (encodedParams != null && encodedParams.length == 1) {
				paramType = getDecodedTypeName(encodedParams[0]);
				// syntax is > Type getter(int);
				if (!paramType.equals("int")) { //$NON-NLS-1$
					//it's not a valid indexed property
					continue;
				}
			}

			readable.add(name);
			types.put(name, returnType);
		}
	}

	/**
	 * Pass in a get*|set*|is* method and it will return an inferred property name using <code>Introspector.decapitalize(String)</code>
	 * @param temp
	 * @return an inferred property name based on the IMethod name, null if the name is not valid according to bean spec
	 */
	private String createPropertyNameFromMethod(IMethod temp) {
		String name = temp.getElementName();
		if (name.startsWith("is")) //$NON-NLS-1$
			name = Introspector.decapitalize(name.substring(2));
		else
			// must be get or set
			name = Introspector.decapitalize(name.substring(3));
		return name;
	}

	/**
	 * Initial filtering of methods.  Checks prefix if it's valid length.  If the prefix is "get" the  method name 
	 * is placed in the getMethodResults List.  If the prefix is "is", the name is added to the isMethodResults list.  If the
	 * prefix is "set", it's added to the setMethodResultsList.
	 * 
	 * @param getMethodResults
	 * @param isMethodResults
	 * @param setMethodResults
	 * @param method
	 */
	private void acceptMethod(List getMethodResults, List isMethodResults, List setMethodResults, IMethod method) throws JavaModelException {
		if (!fRepeatMethods.contains(method.getElementName())) {
			fRepeatMethods.add(method.getElementName());
			int flags = method.getFlags();
			String methodName = method.getElementName();
			if (Flags.isPublic(flags)) {
				if (methodName.length() > 3 && methodName.startsWith("get")) //$NON-NLS-1$
					getMethodResults.add(method);
				else if (methodName.length() > 2 && methodName.startsWith("is")) //$NON-NLS-1$
					isMethodResults.add(method);
				else if (methodName.length() > 3 && methodName.startsWith("set")) //$NON-NLS-1$
					setMethodResults.add(method);
			}
		}
	}

	/**
	 * @param typeName
	 * @return a Qualified name with the package as the qualifier, and class name as LocalName
	 */
	private QualifiedName getTypeQualifiedName(String typeName) {
		StringTokenizer st = new StringTokenizer(typeName, ".", false); //$NON-NLS-1$
		int length = st.countTokens();
		int count = 0;
		StringBuffer root = new StringBuffer();
		while (count++ < length - 1) {
			root.append(st.nextToken());
			if (count < length - 1)
				root.append('.');
		}
		return new QualifiedName(root.toString(), st.nextToken());
	}

	/**
	 * Checks if encodedTypeName is an array
	 * @param encodedTypeName
	 * @return true if encodedTypeName is an array, false otherwise.
	 */
	private boolean isArray(String encodedTypeName) {
		if (encodedTypeName != null && encodedTypeName.length() > 0) {
			if (encodedTypeName.charAt(0) == '[')
				return true;
		}
		return false;
	}

	/**
	 * Returns the decoded (displayable) name fo the type.
	 * Either a primitive type (int, long, float...) Object (String)
	 * @param type
	 * @return decoded name for the encoded string
	 */
	private String getDecodedTypeName(String encoded) {
		HashMap map = getEncodedTypeMap();

		StringBuffer decoded = new StringBuffer();
		char BRACKET = '[';
		String BRACKETS = "[]"; //$NON-NLS-1$
		char identifier = ' ';
		int last = 0;
		// count brackets
		while (encoded.indexOf(BRACKET, last) != -1) {
			last++;
		}
		identifier = encoded.charAt(last);
		Object primitiveType = map.get(String.valueOf(identifier));
		// L > binary type name, Q > source type name
		if (identifier == 'L' || identifier == 'Q') {
			// handle object
			String classname = encoded.substring(last + 1, encoded.length() - 1);
			decoded.append(classname);
		}
		else if (primitiveType != null) {
			// handle primitive type (from IField.getSignature())
			decoded.append((String) primitiveType);
		}
		else {
			// handle primitive type (from Class.getName())
			decoded.append(encoded);
		}
		// handle arrays
		if (last > 0) {
			for (int i = 0; i < last; i++) {
				decoded.append(BRACKETS);
			}
		}
		return decoded.toString();
	}

	/**
	 *	from Class.getName() javadoc
	 *	also see Signature in jdt.core api
	 *<pre>
	 *			B            byte
	 *			C            char
	 *			D            double
	 *			F            float
	 *			I            int
	 *			J            long
	 *			Lclassname;  class or interface
	 *			Qsourcename; source
	 *			S            short
	 *			Z            boolean
	 *			V	   		 void
	 *</pre>
	 *
	 * @return the "encoding letter" to "type" map.
	 */
	private HashMap getEncodedTypeMap() {
		if (fEncodedTypeMap == null) {
			fEncodedTypeMap = new HashMap();
			fEncodedTypeMap.put("B", "byte"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("C", "char"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("D", "double"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("F", "float"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("I", "int"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("J", "long"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("S", "short"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("Z", "boolean"); //$NON-NLS-1$ //$NON-NLS-2$
			fEncodedTypeMap.put("V", "void"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return fEncodedTypeMap;
	}
}

Back to the top