Skip to content

Commit

Permalink
Started removing JDT references from AST classes.
Browse files Browse the repository at this point in the history
	Change on 2016/07/12 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127230396
  • Loading branch information
tomball authored and Keith Stanger committed Jul 18, 2016
1 parent 5e6dd7d commit 5b9e43e
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 24 deletions.
Expand Up @@ -25,10 +25,8 @@ public class ArrayAccess extends Expression {
private final ChildLink<Expression> array = ChildLink.create(Expression.class, this);
private final ChildLink<Expression> index = ChildLink.create(Expression.class, this);

public ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess jdtNode) {
super(jdtNode);
array.set((Expression) TreeConverter.convert(jdtNode.getArray()));
index.set((Expression) TreeConverter.convert(jdtNode.getIndex()));
ArrayAccess() {
super();
}

public ArrayAccess(ArrayAccess other) {
Expand All @@ -53,12 +51,18 @@ public Expression getArray() {
return array.get();
}

ArrayAccess setArray(Expression newArray) {
array.set(newArray);
return this;
}

public Expression getIndex() {
return index.get();
}

public void setIndex(Expression newIndex) {
public ArrayAccess setIndex(Expression newIndex) {
index.set(newIndex);
return this;
}

@Override
Expand Down
Expand Up @@ -37,13 +37,8 @@ public class ArrayCreation extends Expression {
private final ChildLink<ArrayInitializer> initializer =
ChildLink.create(ArrayInitializer.class, this);

public ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation jdtNode) {
super(jdtNode);
arrayType.set((ArrayType) TreeConverter.convert(jdtNode.getType()));
for (Object dimension : jdtNode.dimensions()) {
dimensions.add((Expression) TreeConverter.convert(dimension));
}
initializer.set((ArrayInitializer) TreeConverter.convert(jdtNode.getInitializer()));
ArrayCreation() {
super();
}

public ArrayCreation(ArrayCreation other) {
Expand Down Expand Up @@ -88,16 +83,27 @@ public ArrayType getType() {
return arrayType.get();
}

ArrayCreation setType(ArrayType newType) {
arrayType.set(newType);
return this;
}

public List<Expression> getDimensions() {
return dimensions;
}

ArrayCreation setDimensions(List<Expression> newDimensions) {
dimensions.replaceAll(newDimensions);
return this;
}

public ArrayInitializer getInitializer() {
return initializer.get();
}

public void setInitializer(ArrayInitializer newInitializer) {
public ArrayCreation setInitializer(ArrayInitializer newInitializer) {
initializer.set(newInitializer);
return this;
}

@Override
Expand Down
Expand Up @@ -22,10 +22,8 @@ public class AssertStatement extends Statement {
private ChildLink<Expression> expression = ChildLink.create(Expression.class, this);
private ChildLink<Expression> message = ChildLink.create(Expression.class, this);

public AssertStatement(org.eclipse.jdt.core.dom.AssertStatement jdtNode) {
super(jdtNode);
expression.set((Expression) TreeConverter.convert(jdtNode.getExpression()));
message.set((Expression) TreeConverter.convert(jdtNode.getMessage()));
AssertStatement() {
super();
}

public AssertStatement(AssertStatement other) {
Expand All @@ -43,16 +41,18 @@ public Expression getExpression() {
return expression.get();
}

public void setExpression(Expression newExpression) {
public AssertStatement setExpression(Expression newExpression) {
expression.set(newExpression);
return this;
}

public Expression getMessage() {
return message.get();
}

public void setMessage(Expression newMessage) {
public AssertStatement setMessage(Expression newMessage) {
message.set(newMessage);
return this;
}

@Override
Expand Down
Expand Up @@ -78,6 +78,11 @@ public void copyFrom(List<T> other) {
}
}

void replaceAll(List<T> other) {
clear();
copyFrom(other);
}

public void accept(TreeVisitor visitor) {
// Copy all the children into an array to avoid a
// ConcurrentModificationException if the visitor removes one of the nodes.
Expand Down
Expand Up @@ -25,8 +25,9 @@
*/
public abstract class Expression extends TreeNode {

private Object constantValue = null;
private Object constantValue;

// TODO(tball): remove when all subclasses are converted.
protected Expression(org.eclipse.jdt.core.dom.Expression jdtNode) {
super(jdtNode);
constantValue = jdtNode.resolveConstantExpressionValue();
Expand All @@ -37,7 +38,10 @@ protected Expression(Expression other) {
constantValue = other.getConstantValue();
}

protected Expression() {}
protected Expression() {
super();
constantValue = null;
}

public ITypeBinding getTypeBinding() {
return BindingConverter.unwrapTypeMirrorIntoTypeBinding(getTypeMirror());
Expand All @@ -49,6 +53,11 @@ public Object getConstantValue() {
return constantValue;
}

Expression setConstantValue(Object value) {
constantValue = value;
return this;
}

@Override
public abstract Expression copy();
}
@@ -0,0 +1,60 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.ast;

/**
* Source position information for an AST node.
*/
public class SourcePosition {
private int startPosition;
private int length;
private int lineNumber;

/**
* A pseudo-position used for nodes that are not derived from source code.
*/
public static final SourcePosition NO_POSITION = new SourcePosition(-1, 0, -1);

SourcePosition(int startPosition, int length) {
this(startPosition, length, -1);
}

SourcePosition(int startPosition, int length, int lineNumber) {
this.startPosition = startPosition;
this.length = length;
this.lineNumber = lineNumber;
}

public final int getStartPosition() {
return startPosition;
}

public final int getLength() {
return length;
}

public void setSourceRange(int newStartPosition, int newLength) {
startPosition = newStartPosition;
length = newLength;
}

public final int getLineNumber() {
return lineNumber;
}

public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
}
Expand Up @@ -21,6 +21,7 @@ public abstract class Statement extends TreeNode {

protected Statement() {}

// TODO(tball): remove when all subclasses are converted.
protected Statement(org.eclipse.jdt.core.dom.Statement jdtNode) {
super(jdtNode);
}
Expand Down
Expand Up @@ -18,6 +18,9 @@

import org.eclipse.jdt.core.dom.ASTNode;

import java.util.ArrayList;
import java.util.List;

/**
* Converts a Java AST from the JDT data structure to our J2ObjC data structure.
*/
Expand All @@ -43,6 +46,18 @@ public static TreeNode convert(Object obj) {
return node;
}

private static SourcePosition getPosition(ASTNode jdtNode) {
int startPosition = jdtNode.getStartPosition();
int length = jdtNode.getLength();
ASTNode root = jdtNode.getRoot();
if (root instanceof org.eclipse.jdt.core.dom.CompilationUnit) {
int line = ((org.eclipse.jdt.core.dom.CompilationUnit) root).getLineNumber(startPosition);
return new SourcePosition(startPosition, length, line);
} else {
return new SourcePosition(startPosition, length);
}
}

public static TreeNode convertInner(ASTNode jdtNode) {
switch (jdtNode.getNodeType()) {
case ASTNode.ANNOTATION_TYPE_DECLARATION:
Expand All @@ -55,15 +70,15 @@ public static TreeNode convertInner(ASTNode jdtNode) {
return new AnonymousClassDeclaration(
(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) jdtNode);
case ASTNode.ARRAY_ACCESS:
return new ArrayAccess((org.eclipse.jdt.core.dom.ArrayAccess) jdtNode);
return convertArrayAccess((org.eclipse.jdt.core.dom.ArrayAccess) jdtNode);
case ASTNode.ARRAY_CREATION:
return new ArrayCreation((org.eclipse.jdt.core.dom.ArrayCreation) jdtNode);
return convertArrayCreation((org.eclipse.jdt.core.dom.ArrayCreation) jdtNode);
case ASTNode.ARRAY_INITIALIZER:
return new ArrayInitializer((org.eclipse.jdt.core.dom.ArrayInitializer) jdtNode);
case ASTNode.ARRAY_TYPE:
return new ArrayType((org.eclipse.jdt.core.dom.ArrayType) jdtNode);
case ASTNode.ASSERT_STATEMENT:
return new AssertStatement((org.eclipse.jdt.core.dom.AssertStatement) jdtNode);
return convertAssertStatement((org.eclipse.jdt.core.dom.AssertStatement) jdtNode);
case ASTNode.ASSIGNMENT:
return new Assignment((org.eclipse.jdt.core.dom.Assignment) jdtNode);
case ASTNode.BLOCK:
Expand Down Expand Up @@ -240,4 +255,31 @@ public static TreeNode convertInner(ASTNode jdtNode) {
throw new AssertionError("Unknown node type: " + jdtNode.getClass().getName());
}
}

private static TreeNode convertArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess node) {
return new ArrayAccess()
.setArray((Expression) convert(node.getArray()))
.setIndex((Expression) convert(node.getIndex()))
.setPosition(getPosition(node));
}

private static TreeNode convertArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation node) {
List<Expression> dimensions = new ArrayList<>();
for (Object dimension : node.dimensions()) {
dimensions.add((Expression) TreeConverter.convert(dimension));
}
return new ArrayCreation()
.setType((ArrayType) convert(node.getType()))
.setDimensions(dimensions)
.setInitializer((ArrayInitializer) convert(node.getInitializer()))
.setConstantValue(node.resolveConstantExpressionValue())
.setPosition(getPosition(node));
}

private static TreeNode convertAssertStatement(org.eclipse.jdt.core.dom.AssertStatement node) {
return new AssertStatement()
.setExpression((Expression) convert(node.getExpression()))
.setMessage((Expression) convert(node.getMessage()))
.setPosition(getPosition(node));
}
}
Expand Up @@ -31,6 +31,7 @@ protected TreeNode() {
key = new Key();
}

// TODO(tball): remove when all subclasses are converted.
protected TreeNode(ASTNode jdtNode) {
this();
startPosition = jdtNode.getStartPosition();
Expand Down Expand Up @@ -135,6 +136,14 @@ public boolean preVisit(TreeNode node) {

public void validateInner() {}

TreeNode setPosition(SourcePosition position) {
this.startPosition = position.getStartPosition();
this.length = position.getLength();
this.lineNumber = position.getLineNumber();
return this;
}

@Override
public String toString() {
try {
return DebugASTPrinter.toString(this);
Expand Down

0 comments on commit 5b9e43e

Please sign in to comment.