Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6dca9bf47a6e32255700ba9ea5ba5b63784a4af4 (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
/*******************************************************************************
 * 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.jpql.spi;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.persistence.jpa.jpql.spi.IConstructor;
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 IConstructor} that is wrapping the design-time
 * representation of a Java constructor.
 *
 * Provisional API: This interface is part of an interim API that is still under development and
 * expected to change significantly before reaching stability. It is available at this early stage
 * to solicit feedback from pioneering adopters on the understanding that any code that uses this
 * API will almost certainly be broken (repeatedly) as the API evolves.
 *
 * @version 3.1
 * @since 3.0
 * @author Pascal Filion
 */
public class JpaConstructor implements IConstructor {

	/**
	 * The design-time representation of a Java constructor.
	 */
	private IMethodBinding constructor;

	/**
	 * The cached {@link ITypeDeclaration ITypeDeclarations} representing each of the constructor's
	 * parameter types.
	 */
	private ITypeDeclaration[] typeDeclarations;

	/**
	 * The type owning this constructor.
	 */
	private JpaType type;

	/**
	 * Creates a new <code>JpaConstructor</code>.
	 *
	 * @param type The type owning the constructor
	 * @param constructor The design-time representation of a Java constructor
	 */
	public JpaConstructor(JpaType type, IMethodBinding constructor) {
		super();
		this.type        = type;
		this.constructor = constructor;
	}

	protected ITypeDeclaration buildTypeDeclaration(ITypeBinding parameterType) {

		boolean array = parameterType.isArray();
		String typeParameterName;

		if (array) {
			ITypeBinding componentType = parameterType.getComponentType();

			// <T>[] or <? extends <class_name>>[]
			if (componentType.isTypeVariable() ||
			    componentType.isParameterizedType()) {

				typeParameterName = componentType.getErasure().getQualifiedName();
			}
			else {
				typeParameterName = componentType.getQualifiedName();
			}

			// Now for the type arguments, we have to use the component type
			parameterType = componentType;
		}
		// <T> or <? extends <class_name>>
		else if (parameterType.isTypeVariable() ||
		         parameterType.isParameterizedType()) {

			typeParameterName = parameterType.getErasure().getQualifiedName();
		}
		else {
			typeParameterName = parameterType.getQualifiedName();
		}

		// Retrieve the fully qualified name of the type
		ITypeRepository typeRepository = type.getTypeRepository();
		ITypeBinding[] typeArguments = parameterType.getTypeArguments();
		ITypeDeclaration[] genericTypes = new ITypeDeclaration[typeArguments.length];
		int index = 0;

		for (ITypeBinding typeArgument : typeArguments) {
			String genericTypeName = typeArgument.getErasure().getQualifiedName();
			IType genericType = typeRepository.getType(genericTypeName);
			genericTypes[index++] = genericType.getTypeDeclaration();
		}

		return new JpaTypeDeclaration(
			typeRepository.getType(typeParameterName),
			genericTypes,
			parameterType.getDimensions()
		);
	}

	protected ITypeDeclaration[] buildTypeDeclarations() {

		ITypeBinding[] parameterTypes = constructor.getParameterTypes();
		ITypeDeclaration[] declarations = new ITypeDeclaration[parameterTypes.length];

		for (int index = declarations.length; --index >= 0; ) {
			declarations[index] = buildTypeDeclaration(parameterTypes[index]);
		}

		return declarations;
	}

	/**
	 * {@inheritDoc}
	 */
	public ITypeDeclaration[] getParameterTypes() {
		if (typeDeclarations == null) {
			typeDeclarations = buildTypeDeclarations();
		}
		return typeDeclarations;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return constructor.toString();
	}
}

Back to the top