Skip to content
Permalink
Browse files
8241798: Allow enums to have more constants
Reviewed-by: mcimadamore, briangoetz, forax
  • Loading branch information
cushon committed Mar 28, 2020
1 parent 3a0a3d2 commit 4895a19dd195ff01cc67411e1f113ce77ddfe418
Showing 4 changed files with 460 additions and 9 deletions.
@@ -2301,10 +2301,9 @@ private void visitEnumDef(JCClassDecl tree) {
}
}

// private static final T[] #VALUES = { a, b, c };
Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES");
while (tree.sym.members().findFirst(valuesName) != null) // avoid name clash
valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
// synthetic private static T[] $values() { return new T[] { a, b, c }; }
// synthetic private static final T[] $VALUES = $values();
Name valuesName = syntheticName(tree, "VALUES");
Type arrayType = new ArrayType(types.erasure(tree.type), syms.arrayClass);
VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC,
valuesName,
@@ -2314,7 +2313,15 @@ private void visitEnumDef(JCClassDecl tree) {
List.nil(),
values.toList());
newArray.type = arrayType;
enumDefs.append(make.VarDef(valuesVar, newArray));

MethodSymbol valuesMethod = new MethodSymbol(PRIVATE|STATIC|SYNTHETIC,
syntheticName(tree, "values"),
new MethodType(List.nil(), arrayType, List.nil(), tree.type.tsym),
tree.type.tsym);
enumDefs.append(make.MethodDef(valuesMethod, make.Block(0, List.of(make.Return(newArray)))));
tree.sym.members().enter(valuesMethod);

enumDefs.append(make.VarDef(valuesVar, make.App(make.QualIdent(valuesMethod))));
tree.sym.members().enter(valuesVar);

Symbol valuesSym = lookupMethod(tree.pos(), names.values,
@@ -2329,9 +2336,7 @@ private void visitEnumDef(JCClassDecl tree) {
valuesBody = List.of(make.Return(valuesResult));
} else {
// template: T[] $result = new T[$values.length];
Name resultName = names.fromString(target.syntheticNameChar() + "result");
while (tree.sym.members().findFirst(resultName) != null) // avoid name clash
resultName = names.fromString(resultName + "" + target.syntheticNameChar());
Name resultName = syntheticName(tree, "result");
VarSymbol resultVar = new VarSymbol(FINAL|SYNTHETIC,
resultName,
arrayType,
@@ -2418,6 +2423,13 @@ private boolean useClone() {
}
}

private Name syntheticName(JCClassDecl tree, String baseName) {
Name valuesName = names.fromString(target.syntheticNameChar() + baseName);
while (tree.sym.members().findFirst(valuesName) != null) // avoid name clash
valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
return valuesName;
}

/** Translate an enumeration constant and its initializer. */
private void visitEnumConstantDef(JCVariableDecl var, int ordinal) {
JCNewClass varDef = (JCNewClass)var.init;
@@ -2,6 +2,7 @@ enum EnumTest --
EnumTest.<init>($enum$name/*synthetic*/, $enum$ordinal/*synthetic*/)
EnumTest.<init>($enum$name/*synthetic*/, $enum$ordinal/*synthetic*/, a, ba)
EnumTest.ok(c, dc)
EnumTest.$values()/*synthetic*/
EnumTest.values()
EnumTest.valueOf(A, BA)
EnumTest.valueOf(name/*implicit*/)
EnumTest.valueOf(name/*implicit*/)
@@ -41,6 +41,7 @@
@ExpectedClass(className = "EnumTest",
expectedMethods = {"values()", "valueOf(java.lang.String)", "<clinit>()", "<init>(java.lang.String, int)"},
expectedFields = {"A", "B"},
expectedNumberOfSyntheticMethods = 1,
expectedNumberOfSyntheticFields = 1)
public enum EnumTest {
A, B

0 comments on commit 4895a19

Please sign in to comment.