Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f23f1fb1aab16deeec5731fd9773b05e8d117c33 (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
/*******************************************************************************
 * Copyright (c) 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
 * 
 * This is an implementation of an early-draft specification developed under the Java
 * Community Process (JCP) and is made available for testing and evaluation purposes
 * only. The code is not compatible with any specification of the JCP.
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.parser;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.ExportsStatement;
import org.eclipse.jdt.internal.compiler.ast.ModuleDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ModuleStatement;
import org.eclipse.jdt.internal.compiler.ast.OpensStatement;
import org.eclipse.jdt.internal.compiler.ast.ProvidesStatement;
import org.eclipse.jdt.internal.compiler.ast.RequiresStatement;
import org.eclipse.jdt.internal.compiler.ast.UsesStatement;

public class RecoveredModule extends RecoveredElement {
	
	public RecoveredExportsStatement[] exports;
	public int exportCount;
	public RecoveredOpensStatement[] opens;
	public int opensCount;
	public RecoveredRequiresStatement[] requires;
	public int requiresCount;
	public RecoveredUsesStatement[] uses;
	public int usesCount;
	public RecoveredProvidesStatement[] services;
	public int servicesCount;
	public ModuleDeclaration moduleDeclaration;

	public RecoveredModule(ModuleDeclaration moduleDeclaration, RecoveredElement parent, int bracketBalance) {
		super(parent, bracketBalance);
		this.moduleDeclaration = moduleDeclaration;
	}
	public RecoveredElement add(ModuleStatement moduleStatement, int bracketBalanceValue) {
		
		// TODO: can't we do away with all these additions except for ProvidesStatement - to check
		// if there are any corner cases that uses these.
		if (moduleStatement instanceof ExportsStatement) {
			return add((ExportsStatement) moduleStatement, bracketBalanceValue);
		}
		if (moduleStatement instanceof OpensStatement) {
			return add((OpensStatement) moduleStatement, bracketBalanceValue);
		}
		if (moduleStatement instanceof RequiresStatement) {
			return add((RequiresStatement) moduleStatement, bracketBalanceValue);
		}
		if (moduleStatement instanceof ProvidesStatement) {
			return add((ProvidesStatement) moduleStatement, bracketBalanceValue);
		}
		if (moduleStatement instanceof UsesStatement) {
			return add((UsesStatement) moduleStatement, bracketBalanceValue);
		}
		
		return this;
	}

	public RecoveredElement add(ExportsStatement exportsStatement, int bracketBalanceValue) {
		resetPendingModifiers();

		if (this.exports == null) {
			this.exports = new RecoveredExportsStatement[5];
			this.exportCount = 0;
		} else {
			if (this.exportCount == this.exports.length) {
				System.arraycopy(
					this.exports,
					0,
					(this.exports = new RecoveredExportsStatement[2 * this.exportCount]),
					0,
					this.exportCount);
			}
		}
		RecoveredExportsStatement element = new RecoveredExportsStatement(exportsStatement, this, bracketBalanceValue);
		this.exports[this.exportCount++] = element;

		return element;
	}
	public RecoveredElement add(OpensStatement opensStatement, int bracketBalanceValue) {
		resetPendingModifiers();

		if (this.opens == null) {
			this.opens = new RecoveredOpensStatement[5];
			this.opensCount = 0;
		} else {
			if (this.opensCount == this.opens.length) {
				System.arraycopy(
					this.opens,
					0,
					(this.opens = new RecoveredOpensStatement[2 * this.opensCount]),
					0,
					this.opensCount);
			}
		}
		RecoveredOpensStatement element = new RecoveredOpensStatement(opensStatement, this, bracketBalanceValue);
		this.opens[this.opensCount++] = element;

		return element;
	}
	public RecoveredElement add(RequiresStatement requiresStatement, int bracketBalanceValue) {
		if (this.requires == null) {
			this.requires = new RecoveredRequiresStatement[5];
			this.requiresCount = 0;
		} else {
			if (this.requiresCount == this.requires.length) {
				System.arraycopy(
					this.requires,
					0,
					(this.requires = new RecoveredRequiresStatement[2 * this.requiresCount]),
					0,
					this.requiresCount);
			}
		}
		RecoveredRequiresStatement element = new RecoveredRequiresStatement(requiresStatement, this, bracketBalanceValue);
		this.requires[this.requiresCount++] = element;
		return this;
	}
	public RecoveredElement add(ProvidesStatement providesStatement, int bracketBalanceValue) {
		if (this.services == null) {
			this.services = new RecoveredProvidesStatement[5];
			this.servicesCount = 0;
		} else {
			if (this.servicesCount == this.services.length) {
				System.arraycopy(
					this.services,
					0,
					(this.services = new RecoveredProvidesStatement[2 * this.servicesCount]),
					0,
					this.servicesCount);
			}
		}
		RecoveredProvidesStatement element = new RecoveredProvidesStatement(providesStatement, this, bracketBalanceValue);
		this.services[this.servicesCount++] = element;
		return element;
	}
	public RecoveredElement add(UsesStatement usesStatement, int bracketBalanceValue) {
		genAssign(usesStatement, bracketBalanceValue);
		return this;
	}
	private void genAssign(UsesStatement usesStatement, int bracketBalanceValue) {
		if (this.uses == null) {
			this.uses = new RecoveredUsesStatement[5];
			this.usesCount = 0;
		} else {
			if (this.usesCount == this.uses.length) {
				System.arraycopy(
					this.uses,
					0,
					(this.uses = new RecoveredUsesStatement[2 * this.usesCount]),
					0,
					this.usesCount);
			}
		}
		RecoveredUsesStatement element = new RecoveredUsesStatement(usesStatement, this, bracketBalanceValue);
		this.uses[this.usesCount++] = element;
	}
	public String toString(int tab) {
		StringBuffer result = new StringBuffer(tabString(tab));
		result.append("Recovered module:\n"); //$NON-NLS-1$
		result.append("module ");//$NON-NLS-1$
		result.append(CharOperation.charToString(this.moduleDeclaration.moduleName));
		result.append(" {");//$NON-NLS-1$
		if (this.exportCount > 0) {
			for (int i = 0; i < this.exportCount; ++i) {
				result.append("\n"); //$NON-NLS-1$
				result.append(this.exports[i].toString(tab + 1));
			}
		}
		if (this.requiresCount > 0) {
			for (int i = 0; i < this.requiresCount; ++i) {
				result.append("\n"); //$NON-NLS-1$
				result.append(this.requires[i].toString(tab + 1));
			}
		}
		if (this.usesCount > 0) {
			for (int i = 0; i < this.usesCount; ++i) {
				result.append("\n"); //$NON-NLS-1$
				result.append(this.uses[i].toString(tab + 1));
			}
		}
		if (this.servicesCount > 0) {
			for (int i = 0; i < this.servicesCount; ++i) {
				result.append("\n"); //$NON-NLS-1$
				result.append(this.services[i].toString(tab + 1));
			}
		}
		result.append("\n}");//$NON-NLS-1$
		return result.toString();
	}
	public ModuleDeclaration updatedModuleDeclaration() {

		updateExports(this.moduleDeclaration);
		updateOpens(this.moduleDeclaration);
		updateRequires(this.moduleDeclaration);
		updateUses(this.moduleDeclaration);
		updateServices(this.moduleDeclaration);
		return this.moduleDeclaration;
	}
	private void updateExports(ModuleDeclaration mod) {
		if (this.exportCount > 0) {
			int existingCount = mod.exportsCount, actualCount = 0;
			ExportsStatement[] exports1 = new ExportsStatement[existingCount + this.exportCount];
			if (existingCount > 0){
				System.arraycopy(mod.exports, 0, exports1, 0, existingCount);
				actualCount = existingCount;
			}
			for (int i = 0; i < this.exportCount; i++){
				exports1[actualCount++] = (ExportsStatement)this.exports[i].updatedPackageVisibilityStatement();
			}
			mod.exports = exports1;
			mod.exportsCount = actualCount;
		}
	}
	private void updateOpens(ModuleDeclaration mod) {
		if (this.opensCount > 0) {
			int existingCount = mod.opensCount, actualCount = 0;
			OpensStatement[] opens1 = new OpensStatement[existingCount + this.opensCount];
			if (existingCount > 0){
				System.arraycopy(mod.exports, 0, opens1, 0, existingCount);
				actualCount = existingCount;
			}
			for (int i = 0; i < this.opensCount; i++){
				opens1[actualCount++] = (OpensStatement)this.opens[i].updatedPackageVisibilityStatement();
			}
			mod.opens = opens1;
			mod.opensCount = actualCount;
		}
	}
	private void updateRequires(ModuleDeclaration mod) {
		if (this.requiresCount > 0) {
			int existingCount = mod.requiresCount, actualCount = 0;
			RequiresStatement[] requiresStmts = new RequiresStatement[existingCount + this.requiresCount];
			if (existingCount > 0){
				System.arraycopy(mod.requires, 0, requiresStmts, 0, existingCount);
				actualCount = existingCount;
			}
			for (int i = 0; i < this.requiresCount; i++){
				requiresStmts[actualCount++] = this.requires[i].updatedRequiresStatement();
			}
			mod.requires = requiresStmts;
			mod.requiresCount = actualCount;
		}
	}
	private void updateUses(ModuleDeclaration mod) {
		if (this.usesCount > 0) {
			int existingCount = mod.usesCount, actualCount = 0;
			UsesStatement[] usesStmts = new UsesStatement[existingCount + this.usesCount];
			if (existingCount > 0){
				System.arraycopy(mod.uses, 0, usesStmts, 0, existingCount);
				actualCount = existingCount;
			}
			for (int i = 0; i < this.usesCount; ++i) {
				usesStmts[actualCount++] = this.uses[i].updatedUsesStatement();
			}
			mod.uses = usesStmts;
			mod.usesCount = actualCount;
		}
	}
	private void updateServices(ModuleDeclaration mod) {
		if (this.servicesCount > 0) {
			int existingCount = mod.servicesCount, actualCount = 0;
			ProvidesStatement[] providesStmts = new ProvidesStatement[existingCount + this.servicesCount];
			if (existingCount > 0){
				System.arraycopy(mod.services, 0, providesStmts, 0, existingCount);
				actualCount = existingCount;
			}
			for (int i = 0; i < this.servicesCount; ++i) {
				providesStmts[actualCount++] = this.services[i].updatedProvidesStatement();
			}
			mod.services = providesStmts;
			mod.servicesCount = actualCount;  			
		}
	}
	public void updateParseTree(){
		updatedModuleDeclaration();
	}

}

Back to the top