Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 26-36
Link Here
|
26 |
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; |
26 |
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; |
27 |
import org.eclipse.jdt.internal.compiler.lookup.MethodScope; |
27 |
import org.eclipse.jdt.internal.compiler.lookup.MethodScope; |
28 |
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; |
28 |
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; |
|
|
29 |
import org.eclipse.jdt.internal.compiler.lookup.SyntheticMethodBinding; |
29 |
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; |
30 |
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; |
30 |
import org.eclipse.jdt.internal.compiler.parser.Parser; |
31 |
import org.eclipse.jdt.internal.compiler.parser.Parser; |
31 |
import org.eclipse.jdt.internal.compiler.problem.AbortMethod; |
32 |
import org.eclipse.jdt.internal.compiler.problem.AbortMethod; |
32 |
|
33 |
|
33 |
public class Clinit extends AbstractMethodDeclaration { |
34 |
public class Clinit extends AbstractMethodDeclaration { |
|
|
35 |
private static int ENUM_CONSTANTS_THRESHOLD = 2000; |
34 |
|
36 |
|
35 |
private FieldBinding assertionSyntheticFieldBinding = null; |
37 |
private FieldBinding assertionSyntheticFieldBinding = null; |
36 |
private FieldBinding classLiteralSyntheticField = null; |
38 |
private FieldBinding classLiteralSyntheticField = null; |
Lines 191-206
Link Here
|
191 |
// generate static fields/initializers/enum constants |
193 |
// generate static fields/initializers/enum constants |
192 |
final FieldDeclaration[] fieldDeclarations = declaringType.fields; |
194 |
final FieldDeclaration[] fieldDeclarations = declaringType.fields; |
193 |
BlockScope lastInitializerScope = null; |
195 |
BlockScope lastInitializerScope = null; |
|
|
196 |
int remainingFieldCount = 0; |
194 |
if (TypeDeclaration.kind(declaringType.modifiers) == TypeDeclaration.ENUM_DECL) { |
197 |
if (TypeDeclaration.kind(declaringType.modifiers) == TypeDeclaration.ENUM_DECL) { |
195 |
int enumCount = 0; |
198 |
int enumCount = declaringType.enumConstantsCounter; |
196 |
int remainingFieldCount = 0; |
199 |
if (enumCount > ENUM_CONSTANTS_THRESHOLD) { |
197 |
if (fieldDeclarations != null) { |
200 |
// generate synthetic methods to initialize all the enum constants |
|
|
201 |
int begin = -1; |
202 |
int count = 0; |
203 |
if (fieldDeclarations != null) { |
204 |
int max = fieldDeclarations.length; |
205 |
for (int i = 0; i < max; i++) { |
206 |
FieldDeclaration fieldDecl = fieldDeclarations[i]; |
207 |
if (fieldDecl.isStatic()) { |
208 |
if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) { |
209 |
if (begin == -1) { |
210 |
begin = i; |
211 |
} |
212 |
count++; |
213 |
if (count > ENUM_CONSTANTS_THRESHOLD) { |
214 |
SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, i); |
215 |
codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */); |
216 |
begin = -1; |
217 |
count = 0; |
218 |
} |
219 |
} |
220 |
} |
221 |
} |
222 |
if (count != 0) { |
223 |
// add last synthetic method |
224 |
SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, max); |
225 |
codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */); |
226 |
} |
227 |
} |
228 |
} else if (fieldDeclarations != null) { |
198 |
for (int i = 0, max = fieldDeclarations.length; i < max; i++) { |
229 |
for (int i = 0, max = fieldDeclarations.length; i < max; i++) { |
199 |
FieldDeclaration fieldDecl = fieldDeclarations[i]; |
230 |
FieldDeclaration fieldDecl = fieldDeclarations[i]; |
200 |
if (fieldDecl.isStatic()) { |
231 |
if (fieldDecl.isStatic()) { |
201 |
if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) { |
232 |
if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) { |
202 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
233 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
203 |
enumCount++; |
|
|
204 |
} else { |
234 |
} else { |
205 |
remainingFieldCount++; |
235 |
remainingFieldCount++; |
206 |
} |
236 |
} |
Lines 228-247
Link Here
|
228 |
codeStream.fieldAccess(Opcodes.OPC_putstatic, declaringType.enumValuesSyntheticfield, null /* default declaringClass */); |
258 |
codeStream.fieldAccess(Opcodes.OPC_putstatic, declaringType.enumValuesSyntheticfield, null /* default declaringClass */); |
229 |
if (remainingFieldCount != 0) { |
259 |
if (remainingFieldCount != 0) { |
230 |
// if fields that are not enum constants need to be generated (static initializer/static field) |
260 |
// if fields that are not enum constants need to be generated (static initializer/static field) |
231 |
for (int i = 0, max = fieldDeclarations.length; i < max; i++) { |
261 |
for (int i = 0, max = fieldDeclarations.length; i < max && remainingFieldCount >= 0; i++) { |
232 |
FieldDeclaration fieldDecl = fieldDeclarations[i]; |
262 |
FieldDeclaration fieldDecl = fieldDeclarations[i]; |
233 |
switch (fieldDecl.getKind()) { |
263 |
switch (fieldDecl.getKind()) { |
234 |
case AbstractVariableDeclaration.ENUM_CONSTANT : |
264 |
case AbstractVariableDeclaration.ENUM_CONSTANT : |
235 |
break; |
265 |
break; |
236 |
case AbstractVariableDeclaration.INITIALIZER : |
266 |
case AbstractVariableDeclaration.INITIALIZER : |
237 |
if (!fieldDecl.isStatic()) |
267 |
if (!fieldDecl.isStatic()) { |
238 |
break; |
268 |
break; |
|
|
269 |
} |
270 |
remainingFieldCount--; |
239 |
lastInitializerScope = ((Initializer) fieldDecl).block.scope; |
271 |
lastInitializerScope = ((Initializer) fieldDecl).block.scope; |
240 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
272 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
241 |
break; |
273 |
break; |
242 |
case AbstractVariableDeclaration.FIELD : |
274 |
case AbstractVariableDeclaration.FIELD : |
243 |
if (!fieldDecl.binding.isStatic()) |
275 |
if (!fieldDecl.binding.isStatic()) { |
244 |
break; |
276 |
break; |
|
|
277 |
} |
278 |
remainingFieldCount--; |
245 |
lastInitializerScope = null; |
279 |
lastInitializerScope = null; |
246 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
280 |
fieldDecl.generateCode(staticInitializerScope, codeStream); |
247 |
break; |
281 |
break; |