Skip to main content
summaryrefslogtreecommitdiffstats
blob: d33231a442255e180aaccacaf41944fd77ba40fa (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
/*******************************************************************************
 * Copyright (c) 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.jpt.jpa.core.internal.jpql;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.persistence.jpa.jpql.spi.IType;
import org.eclipse.persistence.jpa.jpql.spi.ITypeDeclaration;
import org.eclipse.persistence.jpa.jpql.spi.ITypeRepository;

/**
 * The concrete implementation of {@link ITypeDeclaration} that is wrapping the representation
 * of the declaration description of a type.
 *
 * @see IMapping
 * @see IType
 *
 * @version 2.3
 * @since 2.3
 * @author Pascal Filion
 */
@SuppressWarnings("nls")
final class JavaTypeDeclaration implements ITypeDeclaration {

	/**
	 * Determines whether the type declaration represents an array.
	 */
	private boolean array;

	/**
	 * The actual type that contains the generics, if any is present.
	 */
	private Object genericType;

	/**
	 * The cached {@link ITypeDeclaration ITypeDeclarations} representing the generics of the {@link
	 * Type}.
	 */
	private ITypeDeclaration[] genericTypes;

	/**
	 * The external form of the Java type.
	 */
	private final IType type;

	/**
	 * The repository of {@link IType ITypes}.
	 */
	private ITypeRepository typeRepository;

	/**
	 * Creates a new <code>JavaTypeDeclaration</code>.
	 *
	 * @param typeRepository The repository of {@link IType ITypes}
	 * @param type The external form of the Java type
	 * @param genericType The actual type that contains the generics, if any is present
	 * @param array Determines whether the type declaration represents an array
	 */
	JavaTypeDeclaration(ITypeRepository typeRepository,
	                    IType type,
	                    Object genericType,
	                    boolean array) {

		super();
		this.type           = type;
		this.array          = array;
		this.genericType    = genericType;
		this.typeRepository = typeRepository;
	}

	private String buildArrayTypeName(String arrayTypeName) {

		StringBuilder sb = new StringBuilder();
		int index = arrayTypeName.indexOf('[');
		int dimensionality = (arrayTypeName.length() - index) / 2;
		String typeName = arrayTypeName.substring(0, index);

		while (--dimensionality >= 0) {
			sb.append("[");
		}

		String elementType = elementType(typeName);

		sb.append(elementType);
		sb.append(typeName);

		if (elementType.equals("L")) {
			sb.append(";");
		}

		return sb.toString();
	}

	private ITypeDeclaration[] buildParameterTypes() {

		List<ITypeDeclaration> parameterTypes = new ArrayList<ITypeDeclaration>();

		// Example: Class<T>
		if (genericType instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) genericType;
			for (Type type : parameterizedType.getActualTypeArguments()) {
				ITypeDeclaration typeParameter = buildTypeDeclaration(type);
				parameterTypes.add(typeParameter);
			}
		}
		// T[]
		else if (genericType instanceof GenericArrayType) {
			GenericArrayType genericArrayType = (GenericArrayType) genericType;
			parameterTypes.add(buildTypeDeclaration(genericArrayType.getGenericComponentType()));
		}
		// Example: Class
		else if (genericType.getClass() == Class.class) {
			ITypeDeclaration typeParameter = buildTypeDeclaration((Class<?>) genericType);
			parameterTypes.add(typeParameter);
		}
		// Example: <K, V>
		else if (genericType.getClass() == Class[].class) {
			for (Class<?> javaType : ((Class<?>[]) genericType)) {
				ITypeDeclaration typeParameter = buildTypeDeclaration(javaType);
				parameterTypes.add(typeParameter);
			}
		}
		// Example: <K, V>
		else if (genericType.getClass() == IType[].class) {
			for (IType type : ((IType[]) genericType)) {
				ITypeDeclaration typeParameter = new JavaTypeDeclaration(typeRepository, type, null, false);
				parameterTypes.add(typeParameter);
			}
		}

		return parameterTypes.toArray(new ITypeDeclaration[parameterTypes.size()]);
	}

	private JavaTypeDeclaration buildTypeDeclaration(Class<?> javaType) {
		return new JavaTypeDeclaration(
			typeRepository,
			getType(javaType),
			null,
			javaType.isArray()
		);
	}

	private JavaTypeDeclaration buildTypeDeclaration(Object genericType) {

		// <T1, ..., Tn>
		if (genericType instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) genericType;
			return buildTypeDeclaration(parameterizedType.getRawType());
		}

		// <T>
		if (genericType instanceof TypeVariable) {
			TypeVariable<?> typeVariable = (TypeVariable<?>) genericType;
			for (Type type : typeVariable.getBounds()) {
				return buildTypeDeclaration(type);
			}
			return buildTypeDeclaration(Object.class);
		}

		// ?
		if (genericType instanceof WildcardType) {
			WildcardType wildcardType = (WildcardType) genericType;
			for (Type type : wildcardType.getUpperBounds()) {
				return buildTypeDeclaration(type);
			}
			return buildTypeDeclaration(Object.class);
		}

		// T[]
		if (genericType instanceof GenericArrayType) {
			GenericArrayType genericArrayType = (GenericArrayType) genericType;
			String arrayTypeName = buildArrayTypeName(genericArrayType.toString());
			IType arrayType = typeRepository.getType(arrayTypeName);

			return new JavaTypeDeclaration(
				typeRepository,
				arrayType,
				genericArrayType.getGenericComponentType(),
				true
			);
		}

		return buildTypeDeclaration((Class<?>) genericType);
	}

	private String elementType(String typeName) {

		if (typeName.equals("boolean")) return "Z";
		if (typeName.equals("byte"))    return "B";
		if (typeName.equals("char"))    return "C";
		if (typeName.equals("double"))  return "D";
		if (typeName.equals("float"))   return "F";
		if (typeName.equals("int"))     return "I";
		if (typeName.equals("long"))    return "J";
		if (typeName.equals("short"))   return "S";

		return "L";
	}

	/**
	 * {@inheritDoc}
	 */
	public int getDimensionality() {
		if (array) {
			String name = type.getName();
			int index = 0;
			while (name.charAt(index) == '[') {
				index++;
			}
			return index;
		}
		return 0;
	}

	/**
	 * {@inheritDoc}
	 */
	public IType getType() {
		return type;
	}

	private IType getType(Class<?> type) {
		return typeRepository.getType(type);
	}

	/**
	 * {@inheritDoc}
	 */
	public ITypeDeclaration[] getTypeParameters() {
		if (genericTypes == null) {
			if (genericType == null) {
				genericTypes = new ITypeDeclaration[0];
			}
			else {
				genericTypes = buildParameterTypes();
			}
		}
		return genericTypes;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean isArray() {
		return array;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return (genericType != null) ? genericType.toString() : type.toString();
	}
}

Back to the top