Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf98ea31a1cb1713a8e19812ee082b2c4212873b (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
/*******************************************************************************
 * Copyright (c) 2018 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.HashSet;
import java.util.List;
import java.util.Set;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;

import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding;
import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
import org.eclipse.jdt.internal.compiler.lookup.SplitPackageBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;

public class ModuleElementImpl extends ElementImpl implements ModuleElement {

	ModuleBinding binding;
	private List<Directive> directives;
	private static List<Directive> EMPTY_DIRECTIVES = Collections.emptyList();

	/**
	 * In general, clients should call
	 * {@link Factory#newDeclaredType(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)} or
	 * {@link Factory#newElement(org.eclipse.jdt.internal.compiler.lookup.Binding)}
	 * to create new instances.
	 */
	ModuleElementImpl(BaseProcessingEnvImpl env, ModuleBinding binding) {
		super(env, binding);
		this.binding = binding;
	}

	private PackageBinding getModulesPackageBinding(PackageBinding binding) {
		if (binding instanceof SplitPackageBinding) {
			return ((SplitPackageBinding) binding).getIncarnation(this.binding);
		}
		return binding;
	}

	@Override
	public ElementKind getKind() {
		return ElementKind.MODULE;
	}

	@Override
	public Set<Modifier> getModifiers() {
		int modifiers = this.binding.modifiers;
		return Factory.getModifiers(modifiers, getKind(), false);
	}

	@Override
	public Name getQualifiedName() {
		return new NameImpl(this.binding.moduleName);
	}

	@Override
	public Name getSimpleName() {
		return new NameImpl(this.binding.moduleName);
	}

	@Override
	public List<? extends Element> getEnclosedElements() {
		ModuleBinding module = this.binding;
		PackageBinding[] packs = module.declaredPackages.valueTable;
		Set<PackageBinding> unique = new HashSet<>();
		for (PackageBinding p : packs) {
			if (p == null)
				continue;
			if (!p.hasCompilationUnit(true))
				continue;
			unique.add(getModulesPackageBinding(p));
		}
		if (module.isUnnamed()) {
			PackageBinding def = module.environment.defaultPackage;
			// FIXME: Does it have any impact for unnamed modules - default package combo?
			if (def != null && def.hasCompilationUnit(true)) {
				unique.add(def);
			}
		} else {
			packs = this.binding.getExports();
			for (PackageBinding pBinding : packs) {
				unique.add(getModulesPackageBinding(pBinding));
			}
			packs = this.binding.getOpens();
			for (PackageBinding pBinding : packs) {
				unique.add(getModulesPackageBinding(pBinding));
			}
		}
		List<Element> enclosed = new ArrayList<>(unique.size());
		for (PackageBinding p : unique) {
			PackageElement pElement = (PackageElement) _env.getFactory().newElement(p);
			enclosed.add(pElement);
		}
		return Collections.unmodifiableList(enclosed);
	}

	@Override
	public boolean isOpen() {
		return (this.binding.modifiers & ClassFileConstants.ACC_OPEN) != 0;
	}

	@Override
	public boolean isUnnamed() {
		return this.binding.moduleName.length == 0;
	}

	@Override
	public Element getEnclosingElement() {
		// As of today, modules have no enclosing element
		return null;
	}

	@Override
	public List<? extends Directive> getDirectives() {
		if (isUnnamed()) {
			return EMPTY_DIRECTIVES;
		}
		if (this.directives == null)
			this.directives = new ArrayList<>();

		PackageBinding[] packs = this.binding.getExports();
		for (PackageBinding exp : packs) {
			exp = getModulesPackageBinding(exp);
			this.directives.add(new ExportsDirectiveImpl(exp));
		}
		Set<ModuleBinding> transitive = new HashSet<>();
		for (ModuleBinding mBinding : this.binding.getRequiresTransitive()) {
			transitive.add(mBinding);
		}
		ModuleBinding[] required = this.binding.getRequires();
		for (ModuleBinding mBinding : required) {
			if (transitive.contains(mBinding)) {
				this.directives.add(new RequiresDirectiveImpl(mBinding, true));
			} else {
				this.directives.add(new RequiresDirectiveImpl(mBinding, false));
			}
		}

		TypeBinding[] tBindings = this.binding.getUses();
		for (TypeBinding tBinding : tBindings) {
			this.directives.add(new UsesDirectiveImpl(tBinding));
		}
		tBindings = this.binding.getServices();
		for (TypeBinding tBinding : tBindings) {
			this.directives.add(new ProvidesDirectiveImpl(tBinding));
		}
		packs = this.binding.getOpens();
		for (PackageBinding exp : packs) {
			exp = getModulesPackageBinding(exp);
			this.directives.add(new OpensDirectiveImpl(exp));
		}
		return this.directives;
	}

	@Override
	public <R, P> R accept(ElementVisitor<R, P> visitor, P param) {
		return visitor.visitModule(this, param);
	}
	@Override
	protected AnnotationBinding[] getAnnotationBindings() {
		return ((ModuleBinding) _binding).getAnnotations();
	}

	abstract class PackageDirectiveImpl {
		PackageBinding binding;
		List<ModuleElement> targets;

		PackageDirectiveImpl(PackageBinding pBinding) {
			this.binding = pBinding;
		}

		public PackageElement getPackage() {
			return _env.getFactory().newPackageElement(binding);
		}

		public List<? extends ModuleElement> getTargetModules(String[] restrictions) {
			if(this.targets != null) {
				return targets;
			}
			if (restrictions.length == 0) {
				return (this.targets = null);
			}
			List<ModuleElement> targets = new ArrayList<>(restrictions.length);
			for (String string : restrictions) {
				ModuleBinding target = ModuleElementImpl.this.binding.environment.getModule(string.toCharArray());
				if (target != null) {
					ModuleElement element = ((ModuleElement) _env.getFactory().newElement(target));
					targets.add(element);
				}
			}
			return (this.targets = Collections.unmodifiableList(targets));
		}
	}

	class ExportsDirectiveImpl extends PackageDirectiveImpl implements ModuleElement.ExportsDirective {

		ExportsDirectiveImpl(PackageBinding pBinding) {
			super(pBinding);
		}

		@Override
		public <R, P> R accept(DirectiveVisitor<R, P> visitor, P param) {
			return visitor.visitExports(this, param);
		}

		@Override
		public javax.lang.model.element.ModuleElement.DirectiveKind getKind() {
			return DirectiveKind.EXPORTS;
		}

		@Override
		public PackageElement getPackage() {
			return _env.getFactory().newPackageElement(binding);
		}
		@Override
		public List<? extends ModuleElement> getTargetModules() {
			if(this.targets != null) {
				return targets;
			}
			return getTargetModules(ModuleElementImpl.this.binding.getExportRestrictions(this.binding));
		}

	}

	class RequiresDirectiveImpl implements ModuleElement.RequiresDirective {
		ModuleBinding dependency;
		boolean transitive;

		RequiresDirectiveImpl(ModuleBinding dependency, boolean transitive) {
			this.dependency = dependency;
			this.transitive = transitive;
		}

		@Override
		public <R, P> R accept(DirectiveVisitor<R, P> visitor, P param) {
			return visitor.visitRequires(this, param);
		}

		@Override
		public javax.lang.model.element.ModuleElement.DirectiveKind getKind() {
			return DirectiveKind.REQUIRES;
		}

		@Override
		public ModuleElement getDependency() {
			return (ModuleElement) _env.getFactory().newElement(dependency, ElementKind.MODULE);
		}

		@Override
		public boolean isStatic() {
			// TODO: Yet to see this in ModuleBinding. Check again.
			return false;
		}

		@Override
		public boolean isTransitive() {
			return this.transitive;
		}
	}

	class OpensDirectiveImpl extends PackageDirectiveImpl implements ModuleElement.OpensDirective {

		OpensDirectiveImpl(PackageBinding pBinding) {
			super(pBinding);
		}

		@Override
		public <R, P> R accept(DirectiveVisitor<R, P> visitor, P param) {
			return visitor.visitOpens(this, param);
		}

		@Override
		public javax.lang.model.element.ModuleElement.DirectiveKind getKind() {
			return DirectiveKind.OPENS;
		}
		@Override
		public List<? extends ModuleElement> getTargetModules() {
			if(this.targets != null) {
				return targets;
			}
			return getTargetModules(ModuleElementImpl.this.binding.getOpenRestrictions(this.binding));
		}
	}

	class UsesDirectiveImpl implements ModuleElement.UsesDirective {
		TypeBinding binding = null;

		UsesDirectiveImpl(TypeBinding binding) {
			this.binding = binding;
		}

		@Override
		public <R, P> R accept(DirectiveVisitor<R, P> visitor, P param) {
			return visitor.visitUses(this, param);
		}

		@Override
		public DirectiveKind getKind() {
			return DirectiveKind.USES;
		}

		@Override
		public TypeElement getService() {
			return (TypeElement) _env.getFactory().newElement(binding);
		}

	}

	class ProvidesDirectiveImpl implements ModuleElement.ProvidesDirective {

		TypeBinding service;
		public List<? extends TypeElement> implementations;

		ProvidesDirectiveImpl(TypeBinding service) {
			this.service = service;
		}

		@Override
		public <R, P> R accept(DirectiveVisitor<R, P> visitor, P param) {
			return visitor.visitProvides(this, param);
		}

		@Override
		public DirectiveKind getKind() {
			return DirectiveKind.PROVIDES;
		}

		@Override
		public List<? extends TypeElement> getImplementations() {
			if (this.implementations != null)
				return this.implementations;

			TypeBinding[] implementations2 = ModuleElementImpl.this.binding.getImplementations(this.service);
			if (implementations2.length == 0) {
				return (this.implementations = Collections.emptyList());
			}

			List<TypeElement> list = new ArrayList<>(implementations2.length);
			Factory factory = _env.getFactory();
			for (TypeBinding type: implementations2) {
				TypeElement element = (TypeElement) factory.newElement(type);
				list.add(element);
			}
			return Collections.unmodifiableList(list);
		}

		@Override
		public TypeElement getService() {
			return (TypeElement) _env.getFactory().newElement(this.service);
		}
	}
}

Back to the top