Skip to content

Commit

Permalink
Convert ComplexExpressionExtractor to new type system.
Browse files Browse the repository at this point in the history
	Change on 2016/11/08 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=138551100
  • Loading branch information
kstanger authored and tomball committed Nov 8, 2016
1 parent c976cdc commit b1ed0ea
Showing 1 changed file with 9 additions and 13 deletions.
Expand Up @@ -34,16 +34,13 @@
import com.google.devtools.j2objc.ast.TreeVisitor; import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.VariableDeclarationStatement; import com.google.devtools.j2objc.ast.VariableDeclarationStatement;
import com.google.devtools.j2objc.ast.WhileStatement; import com.google.devtools.j2objc.ast.WhileStatement;
import com.google.devtools.j2objc.types.GeneratedVariableBinding; import com.google.devtools.j2objc.types.GeneratedVariableElement;
import com.google.devtools.j2objc.util.BindingUtil;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;

import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;


/** /**
* Detects deep expression trees and extracts them into separate statements. * Detects deep expression trees and extracts them into separate statements.
Expand All @@ -57,7 +54,7 @@ public class ComplexExpressionExtractor extends TreeVisitor {


private static int maxDepth = DEFAULT_MAX_DEPTH; private static int maxDepth = DEFAULT_MAX_DEPTH;
private Map<Expression, Integer> depths = Maps.newHashMap(); private Map<Expression, Integer> depths = Maps.newHashMap();
private IMethodBinding currentMethod; private ExecutableElement currentMethod;
private Statement currentStatement; private Statement currentStatement;
private int count = 1; private int count = 1;


Expand All @@ -81,9 +78,8 @@ private void handleNode(Expression node, Collection<Expression> children) {
depth = Math.max(depth, childDepth != null ? childDepth : 1); depth = Math.max(depth, childDepth != null ? childDepth : 1);
} }
if (depth >= maxDepth) { if (depth >= maxDepth) {
ITypeBinding type = node.getTypeBinding(); VariableElement newVar = GeneratedVariableElement.newLocalVar(
IVariableBinding newVar = new GeneratedVariableBinding( "complex$" + count++, node.getTypeMirror(), currentMethod);
"complex$" + count++, 0, type, false, false, null, currentMethod);
Statement newStmt = new VariableDeclarationStatement(newVar, node.copy()); Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
assert currentStatement != null; assert currentStatement != null;
TreeUtil.insertBefore(currentStatement, newStmt); TreeUtil.insertBefore(currentStatement, newStmt);
Expand All @@ -104,7 +100,7 @@ public boolean preVisit(TreeNode node) {


@Override @Override
public boolean visit(MethodDeclaration node) { public boolean visit(MethodDeclaration node) {
currentMethod = node.getMethodBinding(); currentMethod = node.getExecutableElement();
return true; return true;
} }


Expand Down Expand Up @@ -167,7 +163,7 @@ public void endVisit(PrefixExpression node) {


@Override @Override
public void endVisit(Assignment node) { public void endVisit(Assignment node) {
if (BindingUtil.isBoolean(node.getTypeBinding())) { if (node.getTypeMirror().getKind() == TypeKind.BOOLEAN) {
if (node.getRightHandSide() instanceof InfixExpression) { if (node.getRightHandSide() instanceof InfixExpression) {
// Avoid clang precedence warning by putting parentheses around expression. // Avoid clang precedence warning by putting parentheses around expression.
ParenthesizedExpression.parenthesizeAndReplace(node.getRightHandSide()); ParenthesizedExpression.parenthesizeAndReplace(node.getRightHandSide());
Expand Down

0 comments on commit b1ed0ea

Please sign in to comment.