Skip to main content
summaryrefslogtreecommitdiffstats
blob: cea82f657d9cc47d4499705aa52a1cac9a99e7bb (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
/*******************************************************************************
 * Copyright (c) 2004, 2015 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
 *     Technical University Berlin - extended API and implementation
 *     Stephan Herrmann - Contribution for
 *								Bug 466279 - [hovering] IAE on hover when annotation-based null analysis is enabled
 *******************************************************************************/
package org.eclipse.jdt.internal.core;

import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.compiler.CharOperation;

public class TypeParameter extends SourceRefElement implements ITypeParameter {

	static final ITypeParameter[] NO_TYPE_PARAMETERS = new ITypeParameter[0];

	protected String name;

//{ObjectTeams: value parameter / <B base R>:
	public boolean isValueParameter= false;
	public boolean hasBaseBound= false;
// SH}

	public TypeParameter(JavaElement parent, String name) {
		super(parent);
		this.name = name;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof TypeParameter)) return false;
		return super.equals(o);
	}

	@Override
	public String[] getBounds() throws JavaModelException {
		TypeParameterElementInfo info = (TypeParameterElementInfo) getElementInfo();
		return CharOperation.toStrings(info.bounds);
	}
	
	@Override
	public String[] getBoundsSignatures() throws JavaModelException {
		
		String[] boundSignatures = null;
		TypeParameterElementInfo info = (TypeParameterElementInfo) this.getElementInfo();
		
		// For a binary type or method, the signature is already available from the .class file.
		// No need to construct again
		if (this.parent instanceof BinaryMember) {
			char[][] boundsSignatures = info.boundsSignatures;
			if (boundsSignatures == null || boundsSignatures.length == 0) {
				return CharOperation.NO_STRINGS;	
			}
			return CharOperation.toStrings(info.boundsSignatures);
		}
		
		char[][] bounds = info.bounds;
		if (bounds == null || bounds.length == 0) {
			return CharOperation.NO_STRINGS;
		}
	
		int boundsLength = bounds.length;
		boundSignatures = new String[boundsLength];
		for (int i = 0; i < boundsLength; i++) {
			boundSignatures[i] = new String(Signature.createCharArrayTypeSignature(bounds[i], false));
		}
		return boundSignatures;
	}
	
	@Override
	public IMember getDeclaringMember() {
		return (IMember) getParent();
	}

	@Override
	public String getElementName() {
		return this.name;
	}

	@Override
	public int getElementType() {
		return TYPE_PARAMETER;
	}

	@Override
	protected char getHandleMementoDelimiter() {
		return JavaElement.JEM_TYPE_PARAMETER;
	}
	
	public String getKey(boolean forceOpen) throws JavaModelException {
		StringBuilder buf = new StringBuilder();
		if (this.parent instanceof IType) {
			if (this.parent instanceof BinaryType)
				buf.append(((BinaryType) this.parent).getKey(forceOpen));
			else 
				buf.append(((IType) this.parent).getKey());
		} else if (this.parent instanceof IMember) {
			if (this.parent instanceof BinaryMember)
				buf.append(((BinaryMember) this.parent).getKey(forceOpen));
			else 
				buf.append(((IMethod) this.parent).getKey());
		}
		buf.append(":T"); //$NON-NLS-1$
		buf.append(this.name);
		buf.append(';');
		return buf.toString();
	}

	@Override
	public ISourceRange getNameRange() throws JavaModelException {
		SourceMapper mapper= getSourceMapper();
		if (mapper != null) {
			// ensure the class file's buffer is open so that source ranges are computed
			IClassFile classFile = getClassFile();
			if (classFile != null) {
				classFile.getBuffer();
				return mapper.getNameRange(this);
			}
		}
		TypeParameterElementInfo info = (TypeParameterElementInfo) getElementInfo();
		return new SourceRange(info.nameStart, info.nameEnd - info.nameStart + 1);
	}

	/*
	 * @see ISourceReference
	 */
	@Override
	public ISourceRange getSourceRange() throws JavaModelException {
		SourceMapper mapper= getSourceMapper();
		if (mapper != null) {
			// ensure the class file's buffer is open so that source ranges are computed
			IClassFile classFile = getClassFile();
			if (classFile != null) {
				classFile.getBuffer();
				return mapper.getSourceRange(this);
			}
		}
		return super.getSourceRange();
	}

	@Override
	public IClassFile getClassFile() {
		return ((JavaElement)getParent()).getClassFile();
	}
	
	/**
	 * {@inheritDoc}
	 * @since 3.7
	 */
	@Override
	public ITypeRoot getTypeRoot() {
		return this.getDeclaringMember().getTypeRoot();
	}

	@Override
	protected void toStringName(StringBuffer buffer) {
		buffer.append('<');
//{ObjectTeams: value parameter?
		if (this.isValueParameter) {
			try {
				buffer.append(getBounds()[0]);
				buffer.append(' ');
			} catch (JavaModelException ex) {
				// no-op.
			}
		}
// SH}
		buffer.append(getElementName());
		buffer.append('>');
	}
}

Back to the top