Skip to content

Commit

Permalink
Convert javac enum and enum constant nodes.
Browse files Browse the repository at this point in the history
	Change on 2016/11/08 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=138537356
  • Loading branch information
tomball committed Nov 8, 2016
1 parent 24f3b98 commit d9e3e83
Showing 1 changed file with 50 additions and 7 deletions.
Expand Up @@ -37,6 +37,8 @@
import com.google.devtools.j2objc.ast.ContinueStatement;
import com.google.devtools.j2objc.ast.DoStatement;
import com.google.devtools.j2objc.ast.EnhancedForStatement;
import com.google.devtools.j2objc.ast.EnumConstantDeclaration;
import com.google.devtools.j2objc.ast.EnumDeclaration;
import com.google.devtools.j2objc.ast.Expression;
import com.google.devtools.j2objc.ast.ExpressionStatement;
import com.google.devtools.j2objc.ast.FieldAccess;
Expand Down Expand Up @@ -66,6 +68,7 @@
import com.google.devtools.j2objc.ast.SwitchStatement;
import com.google.devtools.j2objc.ast.ThisExpression;
import com.google.devtools.j2objc.ast.TreeNode;
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.Type;
import com.google.devtools.j2objc.ast.TypeDeclaration;
import com.google.devtools.j2objc.ast.VariableDeclarationExpression;
Expand Down Expand Up @@ -186,7 +189,7 @@ private SourcePosition getPosition(JCTree node) {
private TreeNode convertInner(JCTree javacNode) {
switch (javacNode.getKind()) {
default:
throw new AssertionError("Unknown node type: " + javacNode.getClass().getName());
throw new AssertionError("Unknown node type: " + javacNode.getKind());

case ANNOTATION_TYPE:
return convertAnnotationTypeDeclaration((JCTree.JCClassDecl) javacNode);
Expand Down Expand Up @@ -219,12 +222,16 @@ private TreeNode convertInner(JCTree javacNode) {
return convertDoStatement((JCTree.JCDoWhileLoop) javacNode);
case ENHANCED_FOR_LOOP:
return convertEnhancedForStatement((JCTree.JCEnhancedForLoop) javacNode);
case ENUM:
return convertEnum((JCTree.JCClassDecl) javacNode);
case EXPRESSION_STATEMENT:
return convertExpressionStatement((JCTree.JCExpressionStatement) javacNode);
case FOR_LOOP:
return convertForLoop((JCTree.JCForLoop) javacNode);
case IDENTIFIER:
return convertIdent((JCTree.JCIdent) javacNode);
case INTERFACE:
return convertClassDeclaration((JCTree.JCClassDecl) javacNode);
case MEMBER_SELECT:
return convertFieldAccess((JCTree.JCFieldAccess) javacNode);
case METHOD:
Expand Down Expand Up @@ -304,7 +311,10 @@ private TreeNode convertAbstractTypeDeclaration(
convertBodyDeclaration(node, newNode);
List<BodyDeclaration> bodyDeclarations = new ArrayList<>();
for (Object bodyDecl : node.getMembers()) {
bodyDeclarations.add((BodyDeclaration) convert(bodyDecl));
Object member = convert(bodyDecl);
if (member instanceof BodyDeclaration) { // Not true for enum constants.
bodyDeclarations.add((BodyDeclaration) member);
}
}
return newNode
.setName(convertName(node.sym))
Expand Down Expand Up @@ -413,9 +423,6 @@ private TreeNode convertClassDeclaration(JCTree.JCClassDecl node) {
if (node.sym.isAnonymous()) {
throw new AssertionError("Anonymous class declaration tree conversion not implemented");
}
if (node.sym.getKind() == ElementKind.ENUM) {
throw new AssertionError("Enum declaration tree conversion not implemented");
}
if (node.sym.getKind() == ElementKind.ANNOTATION_TYPE) {
throw new AssertionError("Annotation type declaration tree conversion not implemented");
}
Expand Down Expand Up @@ -458,6 +465,22 @@ private TreeNode convertEnhancedForStatement(JCTree.JCEnhancedForLoop node) {
.setBody((Statement) convert(node.getStatement()));
}

private TreeNode convertEnum(JCTree.JCClassDecl node) {
EnumDeclaration newNode =
(EnumDeclaration) convertAbstractTypeDeclaration(node, new EnumDeclaration());
for (Object superInterface : node.getImplementsClause()) {
newNode.addSuperInterfaceType((Type) convert(superInterface));
}
for (Object bodyDecl : node.getMembers()) {
Object member = convert(bodyDecl);
if (member instanceof EnumConstantDeclaration) {
// Other members were converted by convertAbstractTypeDeclaration().
newNode.addEnumConstant((EnumConstantDeclaration) member);
}
}
return newNode;
}

private TreeNode convertExpression(
JCTree.JCExpression node, Expression newNode) {
return newNode
Expand Down Expand Up @@ -618,11 +641,17 @@ private TreeNode convertNewClass(JCTree.JCNewClass node) {
// TODO(tball): Add the appropriate ExecutableType.
.setExecutablePair(new ExecutablePair((ExecutableElement) node.constructor, null))
.setExpression((Expression) convert(node.getEnclosingExpression()))
.setType(Type.newType(node.type))
.setAnonymousClassDeclaration((AnonymousClassDeclaration) convert(node.getClassBody()));
.setType(Type.newType(node.type));
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
Object classBody = convert(node.getClassBody());
if (classBody != null) {
EnumDeclaration anonymousEnum = (EnumDeclaration) classBody;
AnonymousClassDeclaration anonymousEnumType = new AnonymousClassDeclaration()
.setTypeElement(anonymousEnum.getTypeElement());
newNode.setAnonymousClassDeclaration(anonymousEnumType);
}
return newNode;
}

Expand Down Expand Up @@ -681,6 +710,20 @@ private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
}
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration()
.setName(convertName(var))
.setVariableElement(var);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
for (Expression arg : init.getArguments()) {
newNode.addArgument(arg);
}
if (init.getAnonymousClassDeclaration() != null) {
newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
}
return newNode
.setExecutablePair(init.getExecutablePair());
}
boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
Type newType;
if (isVarargs) {
Expand Down

0 comments on commit d9e3e83

Please sign in to comment.