Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 569f02cfe50c62c87cf0e797fcb51c9aa4b53cea (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
/*******************************************************************************
 * Copyright (c) 2006, 2016 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.jdt.internal.compiler.apt.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.type.TypeVisitor;

import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;

/**
 * Implementation of the ExecutableType
 *
 */
public class ExecutableTypeImpl extends TypeMirrorImpl implements ExecutableType {
	
	ExecutableTypeImpl(BaseProcessingEnvImpl env, MethodBinding binding) {
		super(env, binding);
	}
	/* (non-Javadoc)
	 * @see javax.lang.model.type.ExecutableType#getParameterTypes()
	 */
	@Override
	public List<? extends TypeMirror> getParameterTypes() {
		MethodBinding binding = (MethodBinding) this._binding;
		TypeBinding[] parameters = binding.parameters;
		int length = parameters.length;
		boolean isEnumConstructor = binding.isConstructor()
				&& binding.declaringClass.isEnum()
				&& binding.declaringClass.isBinaryBinding()
				&& ((binding.modifiers & ExtraCompilerModifiers.AccGenericSignature) == 0);
		if (isEnumConstructor) {
			ArrayList<TypeMirror> list = new ArrayList<>();
			for (int i = 0; i < length; i++) {
				list.add(_env.getFactory().newTypeMirror(parameters[i]));
			}
			return Collections.unmodifiableList(list);
		}
		if (length != 0) {
			ArrayList<TypeMirror> list = new ArrayList<>();
			for (TypeBinding typeBinding : parameters) {
				list.add(_env.getFactory().newTypeMirror(typeBinding));
			}
			return Collections.unmodifiableList(list);
		}
		return Collections.emptyList();
	}

	/* (non-Javadoc)
	 * @see javax.lang.model.type.ExecutableType#getReturnType()
	 */
	@Override
	public TypeMirror getReturnType() {
		return _env.getFactory().newTypeMirror(((MethodBinding) this._binding).returnType);
	}

	protected AnnotationBinding[] getAnnotationBindings() {
		return ((MethodBinding) this._binding).returnType.getTypeAnnotations();
	}

	/* (non-Javadoc)
	 * @see javax.lang.model.type.ExecutableType#getThrownTypes()
	 */
	@Override
	public List<? extends TypeMirror> getThrownTypes() {
		ArrayList<TypeMirror> list = new ArrayList<>();
		ReferenceBinding[] thrownExceptions = ((MethodBinding) this._binding).thrownExceptions;
		if (thrownExceptions.length != 0) {
			for (ReferenceBinding referenceBinding : thrownExceptions) {
				list.add(_env.getFactory().newTypeMirror(referenceBinding));
			}
		}
		return Collections.unmodifiableList(list);
	}

	/* (non-Javadoc)
	 * @see javax.lang.model.type.ExecutableType#getTypeVariables()
	 */
	@Override
	public List<? extends TypeVariable> getTypeVariables() {
		ArrayList<TypeVariable> list = new ArrayList<>();
		TypeVariableBinding[] typeVariables = ((MethodBinding) this._binding).typeVariables();
		if (typeVariables.length != 0) {
			for (TypeVariableBinding typeVariableBinding : typeVariables) {
				list.add((TypeVariable) _env.getFactory().newTypeMirror(typeVariableBinding));
			}
		}
		return Collections.unmodifiableList(list);
	}

	/* (non-Javadoc)
	 * @see javax.lang.model.type.TypeMirror#accept(javax.lang.model.type.TypeVisitor, java.lang.Object)
	 */
	@Override
	public <R, P> R accept(TypeVisitor<R, P> v, P p) {
		return v.visitExecutable(this, p);
	}

	/* (non-Javadoc)
	 * @see javax.lang.model.type.TypeMirror#getKind()
	 */
	@Override
	public TypeKind getKind() {
		return TypeKind.EXECUTABLE;
	}

	public TypeMirror getReceiverType() {
		return _env.getFactory().getReceiverType((MethodBinding) _binding);
	}
	@Override
	public String toString() {
		return new String(((MethodBinding) this._binding).returnType.readableName());
	}
}

Back to the top